Skip to content

Commit

Permalink
feat(Storage): Update samples to showcase Storage Autoclass V2 usage (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
yash30201 authored Oct 31, 2023
1 parent 461c088 commit f30c448
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
6 changes: 6 additions & 0 deletions storage/src/get_bucket_autoclass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
22 changes: 15 additions & 7 deletions storage/src/set_bucket_autoclass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
27 changes: 18 additions & 9 deletions storage/test/storageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ public function testGetBucketWithAutoclass()
$bucket = self::$storage->createBucket($bucketName, [
'autoclass' => [
'enabled' => true,
'terminalStorageClass' => 'ARCHIVE',
],
'location' => 'US',
]);
Expand All @@ -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,

This comment has been minimized.

Copy link
@AuQaE

AuQaE Dec 4, 2023

pendiente

],
'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
);
Expand Down

0 comments on commit f30c448

Please sign in to comment.