diff --git a/storagecontrol/composer.json b/storagecontrol/composer.json index 01218016b..182f667f4 100644 --- a/storagecontrol/composer.json +++ b/storagecontrol/composer.json @@ -3,6 +3,6 @@ "google/cloud-storage-control": "1.3.0" }, "require-dev": { - "google/cloud-storage": "^1.41.3" + "google/cloud-storage": "^1.48.1" } } diff --git a/storagecontrol/src/create_anywhere_cache.php b/storagecontrol/src/create_anywhere_cache.php new file mode 100644 index 000000000..9e8ac273c --- /dev/null +++ b/storagecontrol/src/create_anywhere_cache.php @@ -0,0 +1,77 @@ +bucketName('_', $bucketName); + + $anywhereCache = new AnywhereCache([ + 'zone' => $zone, + ]); + + $request = new CreateAnywhereCacheRequest([ + 'parent' => $formattedName, + 'anywhere_cache' => $anywhereCache, + ]); + + // Start a create operation and block until it completes. Real applications + // may want to setup a callback, wait on a coroutine, or poll until it + // completes. + $operation = $storageControlClient->createAnywhereCache($request); + + printf('Waiting for operation %s to complete...' . PHP_EOL, $operation->getName()); + $operation->pollUntilComplete([ + 'totalPollTimeoutMillis' => 5400000, + 'initialPollDelayMillis' => 1000, // Start with 1 second delay + 'pollDelayMultiplier' => 2, // Double delay each time + 'maxPollDelayMillis' => 60000, // Max 60 seconds delay between polls + ]); + + /** @var AnywhereCache $anywhereCacheResult */ + $anywhereCacheResult = $operation->getResult(); + printf('Created anywhere cache: %s' . PHP_EOL, $anywhereCacheResult->getName()); +} +# [END storage_control_create_anywhere_cache] + +// The following 2 lines are only needed to run the samples +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/storagecontrol/src/disable_anywhere_cache.php b/storagecontrol/src/disable_anywhere_cache.php new file mode 100644 index 000000000..dbddf3c76 --- /dev/null +++ b/storagecontrol/src/disable_anywhere_cache.php @@ -0,0 +1,58 @@ +anywhereCacheName('_', $bucketName, $anywhereCacheId); + + $request = new DisableAnywhereCacheRequest([ + 'name' => $formattedName + ]); + + $response = $storageControlClient->disableAnywhereCache($request); + + printf('Disabled anywhere cache: %s', $response->getName()); +} +# [END storage_control_disable_anywhere_cache] + +// The following 2 lines are only needed to run the samples +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/storagecontrol/src/get_anywhere_cache.php b/storagecontrol/src/get_anywhere_cache.php new file mode 100644 index 000000000..7687cf6ca --- /dev/null +++ b/storagecontrol/src/get_anywhere_cache.php @@ -0,0 +1,58 @@ +anywhereCacheName('_', $bucketName, $anywhereCacheId); + + $request = new GetAnywhereCacheRequest([ + 'name' => $formattedName, + ]); + + $response = $storageControlClient->getAnywhereCache($request); + + printf('Got anywhere cache: %s', $response->getName()); +} +# [END storage_control_get_anywhere_cache] + +// The following 2 lines are only needed to run the samples +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/storagecontrol/src/list_anywhere_caches.php b/storagecontrol/src/list_anywhere_caches.php new file mode 100644 index 000000000..91ed6e29b --- /dev/null +++ b/storagecontrol/src/list_anywhere_caches.php @@ -0,0 +1,58 @@ +bucketName('_', $bucketName); + + $request = new ListAnywhereCachesRequest([ + 'parent' => $formattedName, + ]); + + $response = $storageControlClient->listAnywhereCaches($request); + + foreach ($response as $anywhereCache) { + printf('Anywhere cache name: %s' . PHP_EOL, $anywhereCache->getName()); + } +} +# [END storage_control_list_anywhere_caches] + +// The following 2 lines are only needed to run the samples +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/storagecontrol/src/pause_anywhere_cache.php b/storagecontrol/src/pause_anywhere_cache.php new file mode 100644 index 000000000..260e7bb37 --- /dev/null +++ b/storagecontrol/src/pause_anywhere_cache.php @@ -0,0 +1,58 @@ +anywhereCacheName('_', $bucketName, $anywhereCacheId); + + $request = new PauseAnywhereCacheRequest([ + 'name' => $formattedName, + ]); + + $response = $storageControlClient->pauseAnywhereCache($request); + + printf('Paused anywhere cache: %s', $response->getName()); +} +# [END storage_control_pause_anywhere_cache] + +// The following 2 lines are only needed to run the samples +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/storagecontrol/src/resume_anywhere_cache.php b/storagecontrol/src/resume_anywhere_cache.php new file mode 100644 index 000000000..a95dd6f92 --- /dev/null +++ b/storagecontrol/src/resume_anywhere_cache.php @@ -0,0 +1,58 @@ +anywhereCacheName('_', $bucketName, $anywhereCacheId); + + $request = new ResumeAnywhereCacheRequest([ + 'name' => $formattedName + ]); + + $response = $storageControlClient->resumeAnywhereCache($request); + + printf('Resumed anywhere cache: %s', $response->getName()); +} +# [END storage_control_resume_anywhere_cache] + +// The following 2 lines are only needed to run the samples +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/storagecontrol/src/update_anywhere_cache.php b/storagecontrol/src/update_anywhere_cache.php new file mode 100644 index 000000000..99b262e00 --- /dev/null +++ b/storagecontrol/src/update_anywhere_cache.php @@ -0,0 +1,82 @@ +anywhereCacheName('_', $bucketName, $anywhereCacheId); + + $anywhereCache = new AnywhereCache([ + 'name' => $formattedName, + 'admission_policy' => $admission_policy, + ]); + + $updateMask = new FieldMask([ + 'paths' => ['admission_policy'], + ]); + + $request = new UpdateAnywhereCacheRequest([ + 'anywhere_cache' => $anywhereCache, + 'update_mask' => $updateMask, + ]); + + // Start an update operation. This returns an Operation object which can be polled. + $operation = $storageControlClient->updateAnywhereCache($request); + + printf('Waiting for operation %s to complete...' . PHP_EOL, $operation->getName()); + $operation->pollUntilComplete([ + 'totalPollTimeoutMillis' => 5400000, + 'initialPollDelayMillis' => 1000, // Start with 1 second delay + 'pollDelayMultiplier' => 2, // Double delay each time + 'maxPollDelayMillis' => 60000, // Max 60 seconds delay between polls + ]); + + $anywhereCacheResult = $operation->getResult(); + printf('Updated anywhere cache: %s', $anywhereCacheResult->getName()); +} +# [END storage_control_update_anywhere_cache] + +// The following 2 lines are only needed to run the samples +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/storagecontrol/test/anywhereCacheTest.php b/storagecontrol/test/anywhereCacheTest.php new file mode 100644 index 000000000..d36f5b613 --- /dev/null +++ b/storagecontrol/test/anywhereCacheTest.php @@ -0,0 +1,182 @@ +createBucket( + sprintf('php-gcscontrol-sample-%s', $uniqueBucketId), + [ + 'location' => self::$location, + 'hierarchicalNamespace' => ['enabled' => true], + 'iamConfiguration' => ['uniformBucketLevelAccess' => ['enabled' => true]] + ] + ); + self::$anywhereCacheName = self::$storageControlClient->anywhereCacheName( + '_', // Set project to "_" to signify global bucket + self::$sourceBucket->name(), + self::$cacheId + ); + } + + public static function tearDownAfterClass(): void + { + foreach (self::$sourceBucket->objects(['versions' => true]) as $object) { + $object->delete(); + } + self::$sourceBucket->delete(); + } + + public function testCreateAnywhereCache() + { + $output = $this->runFunctionSnippet('create_anywhere_cache', [ + self::$sourceBucket->name(), + self::$zone, + ]); + + $this->assertStringContainsString( + sprintf('Created anywhere cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testCreateAnywhereCache + */ + public function testGetAnywhereCache() + { + $output = $this->runFunctionSnippet('get_anywhere_cache', [ + self::$sourceBucket->name(), + self::$cacheId, + ]); + + $this->assertStringContainsString( + sprintf('Got anywhere cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testGetAnywhereCache + */ + public function testListAnywhereCaches() + { + $output = $this->runFunctionSnippet('list_anywhere_caches', [ + self::$sourceBucket->name(), + ]); + + $this->assertStringContainsString( + sprintf('Anywhere cache name: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testListAnywhereCaches + */ + public function testPauseAnywhereCache() + { + $output = $this->runFunctionSnippet('pause_anywhere_cache', [ + self::$sourceBucket->name(), + self::$cacheId, + ]); + + $this->assertStringContainsString( + sprintf('Paused anywhere cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testPauseAnywhereCache + */ + public function testResumeAnywhereCache() + { + $output = $this->runFunctionSnippet('resume_anywhere_cache', [ + self::$sourceBucket->name(), + self::$cacheId, + ]); + + $this->assertStringContainsString( + sprintf('Resumed anywhere cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testResumeAnywhereCache + */ + public function testUpdateAnywhereCache() + { + $admission_policy = 'admit-on-second-miss'; + $output = $this->runFunctionSnippet('update_anywhere_cache', [ + self::$sourceBucket->name(), + self::$cacheId, + $admission_policy + ]); + + $this->assertStringContainsString( + sprintf('Updated anywhere cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testUpdateAnywhereCache + */ + public function testDisableAnywhereCache() + { + $output = $this->runFunctionSnippet('disable_anywhere_cache', [ + self::$sourceBucket->name(), + self::$cacheId, + ]); + + $this->assertStringContainsString( + sprintf('Disabled anywhere cache: %s', self::$anywhereCacheName), + $output + ); + } +}