From f30c448d5c494b84841726a49ae5f2378ebbe119 Mon Sep 17 00:00:00 2001 From: Yash Sahu <54198301+yash30201@users.noreply.github.com> Date: Tue, 31 Oct 2023 12:36:10 +0530 Subject: [PATCH] feat(Storage): Update samples to showcase Storage Autoclass V2 usage (#1928) --- storage/src/get_bucket_autoclass.php | 6 ++++++ storage/src/set_bucket_autoclass.php | 22 +++++++++++++++------- storage/test/storageTest.php | 27 ++++++++++++++++++--------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/storage/src/get_bucket_autoclass.php b/storage/src/get_bucket_autoclass.php index 277653d537..89a869615f 100644 --- a/storage/src/get_bucket_autoclass.php +++ b/storage/src/get_bucket_autoclass.php @@ -47,6 +47,12 @@ function get_bucket_autoclass(string $bucketName): void $bucketName, $info['autoclass']['toggleTime'] ); + printf( + 'Autoclass terminal storage class is set to %s for %s at %s.' . PHP_EOL, + $info['autoclass']['terminalStorageClass'], + $info['name'], + $info['autoclass']['terminalStorageClassUpdateTime'], + ); } } # [END storage_get_autoclass] diff --git a/storage/src/set_bucket_autoclass.php b/storage/src/set_bucket_autoclass.php index 0a57c86c89..912539b055 100644 --- a/storage/src/set_bucket_autoclass.php +++ b/storage/src/set_bucket_autoclass.php @@ -27,30 +27,38 @@ use Google\Cloud\Storage\StorageClient; /** - * Updates an existing bucket with provided autoclass toggle. - * - * Note: Only patch requests that disable autoclass are currently supported. - * To enable autoclass, it must be set at bucket creation time. + * Updates an existing bucket with provided autoclass config. * * @param string $bucketName The name of your Cloud Storage bucket (e.g. 'my-bucket'). * @param bool $autoclassStatus If true, enables Autoclass. Disables otherwise. + * @param string $terminalStorageClass This field is optional and defaults to `NEARLINE`. + * Valid values are `NEARLINE` and `ARCHIVE`. */ -function set_bucket_autoclass(string $bucketName, bool $autoclassStatus): void -{ +function set_bucket_autoclass( + string $bucketName, + bool $autoclassStatus, + string $terminalStorageClass +): void { $storage = new StorageClient(); $bucket = $storage->bucket($bucketName); $bucket->update([ 'autoclass' => [ 'enabled' => $autoclassStatus, + 'terminalStorageClass' => $terminalStorageClass ], ]); + $info = $bucket->info(); printf( 'Updated bucket %s with autoclass set to %s.' . PHP_EOL, - $bucketName, + $info['name'], $autoclassStatus ? 'true' : 'false' ); + printf( + 'Autoclass terminal storage class is %s.' . PHP_EOL, + $info['autoclass']['terminalStorageClass'] + ); } # [END storage_set_autoclass] diff --git a/storage/test/storageTest.php b/storage/test/storageTest.php index 1b5a87f6c1..bbf0df0d33 100644 --- a/storage/test/storageTest.php +++ b/storage/test/storageTest.php @@ -836,6 +836,7 @@ public function testGetBucketWithAutoclass() $bucket = self::$storage->createBucket($bucketName, [ 'autoclass' => [ 'enabled' => true, + 'terminalStorageClass' => 'ARCHIVE', ], 'location' => 'US', ]); @@ -849,30 +850,38 @@ public function testGetBucketWithAutoclass() sprintf('Bucket %s has autoclass enabled: %s', $bucketName, true), $output ); + $this->assertStringContainsString( + sprintf('Autoclass terminal storage class is set to %s', 'ARCHIVE'), + $output + ); } public function testSetBucketWithAutoclass() { $bucket = self::$storage->createBucket(uniqid('samples-set-autoclass-'), [ - 'autoclass' => [ - 'enabled' => true, - ], 'location' => 'US', ]); - $info = $bucket->reload(); - $this->assertArrayHasKey('autoclass', $info); - $this->assertTrue($info['autoclass']['enabled']); + $terminalStorageClass = 'ARCHIVE'; $output = self::runFunctionSnippet('set_bucket_autoclass', [ $bucket->name(), - false + true, + $terminalStorageClass ]); $bucket->delete(); $this->assertStringContainsString( sprintf( - 'Updated bucket %s with autoclass set to false.', - $bucket->name(), + 'Updated bucket %s with autoclass set to true.', + $bucket->name() + ), + $output + ); + + $this->assertStringContainsString( + sprintf( + 'Autoclass terminal storage class is %s.' . PHP_EOL, + $terminalStorageClass ), $output );