From d997da315f12b8463ff5ee507d0eb64ecd57847f Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Mon, 30 Jun 2025 12:05:03 +0000 Subject: [PATCH 01/10] Feat(StorageControl): Add sample for anywhereCache --- storagecontrol/composer.json | 2 +- storagecontrol/src/create_anywhere_cache.php | 67 +++++++++++++++++ storagecontrol/src/disable_anywhere_cache.php | 58 +++++++++++++++ storagecontrol/src/get_anywhere_cache.php | 58 +++++++++++++++ storagecontrol/src/list_anywhere_caches.php | 58 +++++++++++++++ storagecontrol/src/pause_anywhere_cache.php | 58 +++++++++++++++ storagecontrol/src/resume_anywhere_cache.php | 58 +++++++++++++++ storagecontrol/src/update_anywhere_cache.php | 74 +++++++++++++++++++ 8 files changed, 432 insertions(+), 1 deletion(-) create mode 100644 storagecontrol/src/create_anywhere_cache.php create mode 100644 storagecontrol/src/disable_anywhere_cache.php create mode 100644 storagecontrol/src/get_anywhere_cache.php create mode 100644 storagecontrol/src/list_anywhere_caches.php create mode 100644 storagecontrol/src/pause_anywhere_cache.php create mode 100644 storagecontrol/src/resume_anywhere_cache.php create mode 100644 storagecontrol/src/update_anywhere_cache.php diff --git a/storagecontrol/composer.json b/storagecontrol/composer.json index 01218016b5..182f667f4a 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 0000000000..e3a7495588 --- /dev/null +++ b/storagecontrol/src/create_anywhere_cache.php @@ -0,0 +1,67 @@ +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. + $response = $storageControlClient->createAnywhereCache($request); + + printf('Created anywhere cache: %s', $response->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 0000000000..dbddf3c765 --- /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 0000000000..7687cf6cac --- /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 0000000000..91ed6e29b0 --- /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 0000000000..260e7bb379 --- /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 0000000000..a95dd6f92d --- /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 0000000000..4d2fba02af --- /dev/null +++ b/storagecontrol/src/update_anywhere_cache.php @@ -0,0 +1,74 @@ +anywhereCacheName('_', $bucketName, $anywhereCacheId); + + $anywhereCache = new AnywhereCache([ + 'name' => $formattedName, + 'admission_policy' => $admission_policy, + ]); + + $updateMask = new FieldMask([ + 'paths' => ['admission_policy'], + ]); + + // Start an update operation and block until it completes. Real applications + // may want to setup a callback, wait on a coroutine, or poll until it + // completes. + $request = new UpdateAnywhereCacheRequest([ + 'anywhere_cache' => $anywhereCache, + 'update_mask' => $updateMask, + ]); + + $response = $storageControlClient->updateAnywhereCache($request); + + printf('Updated anywhere cache: %s', $response->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); From cf3c3c55ce8224617a58aa1b6c2ee54c528fb629 Mon Sep 17 00:00:00 2001 From: Durgesh Ninave Date: Wed, 9 Jul 2025 23:54:37 +0530 Subject: [PATCH 02/10] feat(secretmanager): Added samples for tags field (#2140) * feat(secretmanager): added samples for tags field * fix(secretmanager): Fix lint * fix(secretmanager): Added comments for sleep --- secretmanager/composer.json | 3 +- .../src/bind_tags_to_regional_secret.php | 92 ++++++++++++ secretmanager/src/bind_tags_to_secret.php | 92 ++++++++++++ .../src/create_regional_secret_with_tags.php | 71 +++++++++ secretmanager/src/create_secret_with_tags.php | 73 +++++++++ .../test/regionalsecretmanagerTest.php | 138 ++++++++++++++++++ secretmanager/test/secretmanagerTest.php | 136 +++++++++++++++++ 7 files changed, 604 insertions(+), 1 deletion(-) create mode 100644 secretmanager/src/bind_tags_to_regional_secret.php create mode 100644 secretmanager/src/bind_tags_to_secret.php create mode 100644 secretmanager/src/create_regional_secret_with_tags.php create mode 100644 secretmanager/src/create_secret_with_tags.php diff --git a/secretmanager/composer.json b/secretmanager/composer.json index f1840b1317..2a20b5355d 100644 --- a/secretmanager/composer.json +++ b/secretmanager/composer.json @@ -1,5 +1,6 @@ { "require": { - "google/cloud-secret-manager": "^2.0.0" + "google/cloud-secret-manager": "^2.1.0", + "google/cloud-resource-manager": "^1.0" } } diff --git a/secretmanager/src/bind_tags_to_regional_secret.php b/secretmanager/src/bind_tags_to_regional_secret.php new file mode 100644 index 0000000000..949a6f9aa8 --- /dev/null +++ b/secretmanager/src/bind_tags_to_regional_secret.php @@ -0,0 +1,92 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the parent project. + $parent = $client->locationName($projectId, $locationId); + + $secret = new Secret(); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s' . PHP_EOL, $newSecret->getName()); + + // Specify regional endpoint. + $tagBindOptions = ['apiEndpoint' => "$locationId-cloudresourcemanager.googleapis.com"]; + $tagBindingsClient = new TagBindingsClient($tagBindOptions); + $tagBinding = (new TagBinding()) + ->setParent('//secretmanager.googleapis.com/' . $newSecret->getName()) + ->setTagValue($tagValue); + + // Build the request. + $request = (new CreateTagBindingRequest()) + ->setTagBinding($tagBinding); + + // Create the tag binding. + $operationResponse = $tagBindingsClient->createTagBinding($request); + $operationResponse->pollUntilComplete(); + + // Check if the operation succeeded. + if ($operationResponse->operationSucceeded()) { + printf('Tag binding created for secret %s with tag value %s' . PHP_EOL, $newSecret->getName(), $tagValue); + } else { + $error = $operationResponse->getError(); + printf('Error in creating tag binding: %s' . PHP_EOL, $error->getMessage()); + } +} +// [END secretmanager_bind_tags_to_regional_secret] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/bind_tags_to_secret.php b/secretmanager/src/bind_tags_to_secret.php new file mode 100644 index 0000000000..60616353b7 --- /dev/null +++ b/secretmanager/src/bind_tags_to_secret.php @@ -0,0 +1,92 @@ +projectName($projectId); + + $secret = new Secret([ + 'replication' => new Replication([ + 'automatic' => new Automatic(), + ]), + ]); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s' . PHP_EOL, $newSecret->getName()); + + $tagBindingsClient = new TagBindingsClient(); + $tagBinding = (new TagBinding()) + ->setParent('//secretmanager.googleapis.com/' . $newSecret->getName()) + ->setTagValue($tagValue); + + // Build the binding request. + $request = (new CreateTagBindingRequest()) + ->setTagBinding($tagBinding); + + // Create the tag binding. + $operationResponse = $tagBindingsClient->createTagBinding($request); + $operationResponse->pollUntilComplete(); + + // Check if the operation succeeded. + if ($operationResponse->operationSucceeded()) { + printf('Tag binding created for secret %s with tag value %s' . PHP_EOL, $newSecret->getName(), $tagValue); + } else { + $error = $operationResponse->getError(); + printf('Error in creating tag binding: %s' . PHP_EOL, $error->getMessage()); + } +} +// [END secretmanager_bind_tags_to_secret] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/create_regional_secret_with_tags.php b/secretmanager/src/create_regional_secret_with_tags.php new file mode 100644 index 0000000000..519e8b84f3 --- /dev/null +++ b/secretmanager/src/create_regional_secret_with_tags.php @@ -0,0 +1,71 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the parent project. + $parent = $client->locationName($projectId, $locationId); + + $secret = new Secret(); + + // set the tags. + $tags = [$tagKey => $tagValue]; + $secret ->setTags($tags); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s with tag', $newSecret->getName()); +} +// [END secretmanager_regional_create_secret_with_tags] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/create_secret_with_tags.php b/secretmanager/src/create_secret_with_tags.php new file mode 100644 index 0000000000..33d7353c1f --- /dev/null +++ b/secretmanager/src/create_secret_with_tags.php @@ -0,0 +1,73 @@ +projectName($projectId); + + $secret = new Secret([ + 'replication' => new Replication([ + 'automatic' => new Automatic(), + ]), + ]); + + // set the tags. + $tags = [$tagKey => $tagValue]; + $secret->setTags($tags); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s with tag', $newSecret->getName()); +} +// [END secretmanager_create_secret_with_tags] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/test/regionalsecretmanagerTest.php b/secretmanager/test/regionalsecretmanagerTest.php index 80c6946200..078f9de5cf 100644 --- a/secretmanager/test/regionalsecretmanagerTest.php +++ b/secretmanager/test/regionalsecretmanagerTest.php @@ -20,6 +20,14 @@ namespace Google\Cloud\Samples\SecretManager; use Google\ApiCore\ApiException as GaxApiException; +use Google\Cloud\ResourceManager\V3\DeleteTagKeyRequest; +use Google\Cloud\ResourceManager\V3\DeleteTagValueRequest; +use Google\Cloud\ResourceManager\V3\Client\TagKeysClient; +use Google\Cloud\ResourceManager\V3\CreateTagKeyRequest; +use Google\Cloud\ResourceManager\V3\TagKey; +use Google\Cloud\ResourceManager\V3\Client\TagValuesClient; +use Google\Cloud\ResourceManager\V3\CreateTagValueRequest; +use Google\Cloud\ResourceManager\V3\TagValue; use Google\Cloud\SecretManager\V1\AddSecretVersionRequest; use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; use Google\Cloud\SecretManager\V1\CreateSecretRequest; @@ -36,6 +44,9 @@ class regionalsecretmanagerTest extends TestCase use TestTrait; private static $client; + private static $tagKeyClient; + private static $tagValuesClient; + private static $testSecret; private static $testSecretToDelete; private static $testSecretWithVersions; @@ -44,14 +55,21 @@ class regionalsecretmanagerTest extends TestCase private static $testSecretVersionToDestroy; private static $testSecretVersionToDisable; private static $testSecretVersionToEnable; + private static $testSecretWithTagToCreateName; + private static $testSecretBindTagToCreateName; private static $iamUser = 'user:kapishsingh@google.com'; private static $locationId = 'us-central1'; + private static $testTagKey; + private static $testTagValue; + public static function setUpBeforeClass(): void { $options = ['apiEndpoint' => 'secretmanager.' . self::$locationId . '.rep.googleapis.com' ]; self::$client = new SecretManagerServiceClient($options); + self::$tagKeyClient = new TagKeysClient(); + self::$tagValuesClient = new TagValuesClient(); self::$testSecret = self::createSecret(); self::$testSecretToDelete = self::createSecret(); @@ -61,7 +79,12 @@ public static function setUpBeforeClass(): void self::$testSecretVersionToDestroy = self::addSecretVersion(self::$testSecretWithVersions); self::$testSecretVersionToDisable = self::addSecretVersion(self::$testSecretWithVersions); self::$testSecretVersionToEnable = self::addSecretVersion(self::$testSecretWithVersions); + self::$testSecretWithTagToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); + self::$testSecretBindTagToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); self::disableSecretVersion(self::$testSecretVersionToEnable); + + self::$testTagKey = self::createTagKey(self::randomSecretId()); + self::$testTagValue = self::createTagValue(self::randomSecretId()); } public static function tearDownAfterClass(): void @@ -73,6 +96,11 @@ public static function tearDownAfterClass(): void self::deleteSecret(self::$testSecretToDelete->getName()); self::deleteSecret(self::$testSecretWithVersions->getName()); self::deleteSecret(self::$testSecretToCreateName); + self::deleteSecret(self::$testSecretWithTagToCreateName); + self::deleteSecret(self::$testSecretBindTagToCreateName); + sleep(15); // Added a sleep to wait for the tag unbinding + self::deleteTagValue(); + self::deleteTagKey(); } private static function randomSecretId(): string @@ -122,6 +150,86 @@ private static function deleteSecret(string $name) } } + private static function createTagKey(string $short_name): string + { + $parent = self::$client->projectName(self::$projectId); + $tagKey = (new TagKey()) + ->setParent($parent) + ->setShortName($short_name); + + $request = (new CreateTagKeyRequest()) + ->setTagKey($tagKey); + + $operation = self::$tagKeyClient->createTagKey($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + $createdTagKey = $operation->getResult(); + printf("Tag key created: %s\n", $createdTagKey->getName()); + return $createdTagKey->getName(); + } else { + $error = $operation->getError(); + printf("Error creating tag key: %s\n", $error->getMessage()); + return ''; + } + } + + private static function createTagValue(string $short_name): string + { + $tagValuesClient = new TagValuesClient(); + $tagValue = (new TagValue()) + ->setParent(self::$testTagKey) + ->setShortName($short_name); + + $request = (new CreateTagValueRequest()) + ->setTagValue($tagValue); + + $operation = self::$tagValuesClient->createTagValue($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + $createdTagValue = $operation->getResult(); + printf("Tag value created: %s\n", $createdTagValue->getName()); + return $createdTagValue->getName(); + } else { + $error = $operation->getError(); + printf("Error creating tag value: %s\n", $error->getMessage()); + return ''; + } + } + + private static function deleteTagKey() + { + $request = (new DeleteTagKeyRequest()) + ->setName(self::$testTagKey); + + $operation = self::$tagKeyClient->deleteTagKey($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + printf("Tag key deleted: %s\n", self::$testTagValue); + } else { + $error = $operation->getError(); + printf("Error deleting tag key: %s\n", $error->getMessage()); + } + } + + private static function deleteTagValue() + { + $request = (new DeleteTagValueRequest()) + ->setName(self::$testTagValue); + + $operation = self::$tagValuesClient->deleteTagValue($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + printf("Tag value deleted: %s\n", self::$testTagValue); + } else { + $error = $operation->getError(); + printf("Error deleting tag value: %s\n", $error->getMessage()); + } + } + public function testAccessSecretVersion() { $name = self::$client->parseName(self::$testSecretVersion->getName()); @@ -324,4 +432,34 @@ public function testUpdateSecretWithAlias() $this->assertStringContainsString('Updated secret', $output); } + + public function testCreateSecretWithTags() + { + $name = self::$client->parseName(self::$testSecretWithTagToCreateName); + + $output = $this->runFunctionSnippet('create_regional_secret_with_tags', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testTagKey, + self::$testTagValue + ]); + + $this->assertStringContainsString('Created secret', $output); + } + + public function testBindTagsToSecret() + { + $name = self::$client->parseName(self::$testSecretBindTagToCreateName); + + $output = $this->runFunctionSnippet('bind_tags_to_regional_secret', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testTagValue + ]); + + $this->assertStringContainsString('Created secret', $output); + $this->assertStringContainsString('Tag binding created for secret', $output); + } } diff --git a/secretmanager/test/secretmanagerTest.php b/secretmanager/test/secretmanagerTest.php index 48570fe253..2f63193e66 100644 --- a/secretmanager/test/secretmanagerTest.php +++ b/secretmanager/test/secretmanagerTest.php @@ -20,6 +20,14 @@ namespace Google\Cloud\Samples\SecretManager; use Google\ApiCore\ApiException as GaxApiException; +use Google\Cloud\ResourceManager\V3\DeleteTagKeyRequest; +use Google\Cloud\ResourceManager\V3\DeleteTagValueRequest; +use Google\Cloud\ResourceManager\V3\Client\TagKeysClient; +use Google\Cloud\ResourceManager\V3\CreateTagKeyRequest; +use Google\Cloud\ResourceManager\V3\TagKey; +use Google\Cloud\ResourceManager\V3\Client\TagValuesClient; +use Google\Cloud\ResourceManager\V3\CreateTagValueRequest; +use Google\Cloud\ResourceManager\V3\TagValue; use Google\Cloud\SecretManager\V1\AddSecretVersionRequest; use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; use Google\Cloud\SecretManager\V1\CreateSecretRequest; @@ -38,6 +46,9 @@ class secretmanagerTest extends TestCase use TestTrait; private static $client; + private static $tagKeyClient; + private static $tagValuesClient; + private static $testSecret; private static $testSecretToDelete; private static $testSecretWithVersions; @@ -47,24 +58,36 @@ class secretmanagerTest extends TestCase private static $testSecretVersionToDestroy; private static $testSecretVersionToDisable; private static $testSecretVersionToEnable; + private static $testSecretWithTagToCreateName; + private static $testSecretBindTagToCreateName; private static $iamUser = 'user:sethvargo@google.com'; + private static $testTagKey; + private static $testTagValue; + public static function setUpBeforeClass(): void { self::$client = new SecretManagerServiceClient(); + self::$tagKeyClient = new TagKeysClient(); + self::$tagValuesClient = new TagValuesClient(); self::$testSecret = self::createSecret(); self::$testSecretToDelete = self::createSecret(); self::$testSecretWithVersions = self::createSecret(); self::$testSecretToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); self::$testUmmrSecretToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); + self::$testSecretWithTagToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); + self::$testSecretBindTagToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); self::$testSecretVersion = self::addSecretVersion(self::$testSecretWithVersions); self::$testSecretVersionToDestroy = self::addSecretVersion(self::$testSecretWithVersions); self::$testSecretVersionToDisable = self::addSecretVersion(self::$testSecretWithVersions); self::$testSecretVersionToEnable = self::addSecretVersion(self::$testSecretWithVersions); self::disableSecretVersion(self::$testSecretVersionToEnable); + + self::$testTagKey = self::createTagKey(self::randomSecretId()); + self::$testTagValue = self::createTagValue(self::randomSecretId()); } public static function tearDownAfterClass(): void @@ -74,6 +97,11 @@ public static function tearDownAfterClass(): void self::deleteSecret(self::$testSecretWithVersions->getName()); self::deleteSecret(self::$testSecretToCreateName); self::deleteSecret(self::$testUmmrSecretToCreateName); + self::deleteSecret(self::$testSecretWithTagToCreateName); + self::deleteSecret(self::$testSecretBindTagToCreateName); + sleep(15); // Added a sleep to wait for the tag unbinding + self::deleteTagValue(); + self::deleteTagKey(); } private static function randomSecretId(): string @@ -127,6 +155,86 @@ private static function deleteSecret(string $name) } } + private static function createTagKey(string $short_name): string + { + $parent = self::$client->projectName(self::$projectId); + $tagKey = (new TagKey()) + ->setParent($parent) + ->setShortName($short_name); + + $request = (new CreateTagKeyRequest()) + ->setTagKey($tagKey); + + $operation = self::$tagKeyClient->createTagKey($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + $createdTagKey = $operation->getResult(); + printf("Tag key created: %s\n", $createdTagKey->getName()); + return $createdTagKey->getName(); + } else { + $error = $operation->getError(); + printf("Error creating tag key: %s\n", $error->getMessage()); + return ''; + } + } + + private static function createTagValue(string $short_name): string + { + $tagValuesClient = new TagValuesClient(); + $tagValue = (new TagValue()) + ->setParent(self::$testTagKey) + ->setShortName($short_name); + + $request = (new CreateTagValueRequest()) + ->setTagValue($tagValue); + + $operation = self::$tagValuesClient->createTagValue($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + $createdTagValue = $operation->getResult(); + printf("Tag value created: %s\n", $createdTagValue->getName()); + return $createdTagValue->getName(); + } else { + $error = $operation->getError(); + printf("Error creating tag value: %s\n", $error->getMessage()); + return ''; + } + } + + private static function deleteTagKey() + { + $request = (new DeleteTagKeyRequest()) + ->setName(self::$testTagKey); + + $operation = self::$tagKeyClient->deleteTagKey($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + printf("Tag key deleted: %s\n", self::$testTagValue); + } else { + $error = $operation->getError(); + printf("Error deleting tag key: %s\n", $error->getMessage()); + } + } + + private static function deleteTagValue() + { + $request = (new DeleteTagValueRequest()) + ->setName(self::$testTagValue); + + $operation = self::$tagValuesClient->deleteTagValue($request); + $operation->pollUntilComplete(); + + if ($operation->operationSucceeded()) { + printf("Tag value deleted: %s\n", self::$testTagValue); + } else { + $error = $operation->getError(); + printf("Error deleting tag value: %s\n", $error->getMessage()); + } + } + public function testAccessSecretVersion() { $name = self::$client->parseName(self::$testSecretVersion->getName()); @@ -328,4 +436,32 @@ public function testUpdateSecretWithAlias() $this->assertStringContainsString('Updated secret', $output); } + + public function testCreateSecretWithTags() + { + $name = self::$client->parseName(self::$testSecretWithTagToCreateName); + + $output = $this->runFunctionSnippet('create_secret_with_tags', [ + $name['project'], + $name['secret'], + self::$testTagKey, + self::$testTagValue + ]); + + $this->assertStringContainsString('Created secret', $output); + } + + public function testBindTagsToSecret() + { + $name = self::$client->parseName(self::$testSecretBindTagToCreateName); + + $output = $this->runFunctionSnippet('bind_tags_to_secret', [ + $name['project'], + $name['secret'], + self::$testTagValue + ]); + + $this->assertStringContainsString('Created secret', $output); + $this->assertStringContainsString('Tag binding created for secret', $output); + } } From d7022693e2ae0854dd4b921a078b85369a036afc Mon Sep 17 00:00:00 2001 From: Durgesh Ninave Date: Fri, 18 Jul 2025 21:15:40 +0530 Subject: [PATCH 03/10] feat(SecretManager): Add samples for labels and annotations in Secret Manager (#2146) --- ...reate_regional_secret_with_annotations.php | 71 ++++++++++ .../create_regional_secret_with_labels.php | 71 ++++++++++ .../src/create_secret_with_annotations.php | 73 ++++++++++ .../src/create_secret_with_labels.php | 73 ++++++++++ .../src/delete_regional_secret_annotation.php | 89 +++++++++++++ .../src/delete_regional_secret_label.php | 89 +++++++++++++ .../src/delete_secret_annotation.php | 85 ++++++++++++ secretmanager/src/delete_secret_label.php | 85 ++++++++++++ .../src/edit_regional_secret_annotations.php | 89 +++++++++++++ .../src/edit_regional_secret_labels.php | 89 +++++++++++++ secretmanager/src/edit_secret_annotations.php | 85 ++++++++++++ secretmanager/src/edit_secret_labels.php | 85 ++++++++++++ .../src/view_regional_secret_annotations.php | 69 ++++++++++ .../src/view_regional_secret_labels.php | 69 ++++++++++ secretmanager/src/view_secret_annotations.php | 65 +++++++++ secretmanager/src/view_secret_labels.php | 65 +++++++++ .../test/regionalsecretmanagerTest.php | 126 ++++++++++++++++++ secretmanager/test/secretmanagerTest.php | 118 ++++++++++++++++ 18 files changed, 1496 insertions(+) create mode 100644 secretmanager/src/create_regional_secret_with_annotations.php create mode 100644 secretmanager/src/create_regional_secret_with_labels.php create mode 100644 secretmanager/src/create_secret_with_annotations.php create mode 100644 secretmanager/src/create_secret_with_labels.php create mode 100644 secretmanager/src/delete_regional_secret_annotation.php create mode 100644 secretmanager/src/delete_regional_secret_label.php create mode 100644 secretmanager/src/delete_secret_annotation.php create mode 100644 secretmanager/src/delete_secret_label.php create mode 100644 secretmanager/src/edit_regional_secret_annotations.php create mode 100644 secretmanager/src/edit_regional_secret_labels.php create mode 100644 secretmanager/src/edit_secret_annotations.php create mode 100644 secretmanager/src/edit_secret_labels.php create mode 100644 secretmanager/src/view_regional_secret_annotations.php create mode 100644 secretmanager/src/view_regional_secret_labels.php create mode 100644 secretmanager/src/view_secret_annotations.php create mode 100644 secretmanager/src/view_secret_labels.php diff --git a/secretmanager/src/create_regional_secret_with_annotations.php b/secretmanager/src/create_regional_secret_with_annotations.php new file mode 100644 index 0000000000..03d78d0ebc --- /dev/null +++ b/secretmanager/src/create_regional_secret_with_annotations.php @@ -0,0 +1,71 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the parent project. + $parent = $client->locationName($projectId, $locationId); + + $secret = new Secret(); + + // set the annotations. + $annotations = [$annotationKey => $annotationValue]; + $secret->setAnnotations($annotations); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s with annotations', $newSecret->getName()); +} +// [END secretmanager_create_regional_secret_with_annotations] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/create_regional_secret_with_labels.php b/secretmanager/src/create_regional_secret_with_labels.php new file mode 100644 index 0000000000..0ebf77e31f --- /dev/null +++ b/secretmanager/src/create_regional_secret_with_labels.php @@ -0,0 +1,71 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the parent project. + $parent = $client->locationName($projectId, $locationId); + + $secret = new Secret(); + + // set the labels. + $labels = [$labelKey => $labelValue]; + $secret->setLabels($labels); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s with labels', $newSecret->getName()); +} +// [END secretmanager_create_regional_secret_with_labels] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/create_secret_with_annotations.php b/secretmanager/src/create_secret_with_annotations.php new file mode 100644 index 0000000000..40d8a44d04 --- /dev/null +++ b/secretmanager/src/create_secret_with_annotations.php @@ -0,0 +1,73 @@ +projectName($projectId); + + $secret = new Secret([ + 'replication' => new Replication([ + 'automatic' => new Automatic(), + ]), + ]); + + // set the annoation. + $annotation = [$annotationKey => $annotationValue]; + $secret->setAnnotations($annotation); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s with annotations', $newSecret->getName()); +} +// [END secretmanager_create_secret_with_annotations] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/create_secret_with_labels.php b/secretmanager/src/create_secret_with_labels.php new file mode 100644 index 0000000000..02edcdb988 --- /dev/null +++ b/secretmanager/src/create_secret_with_labels.php @@ -0,0 +1,73 @@ +projectName($projectId); + + $secret = new Secret([ + 'replication' => new Replication([ + 'automatic' => new Automatic(), + ]), + ]); + + // set the labels. + $labels = [$labelKey => $labelValue]; + $secret->setLabels($labels); + + // Build the request. + $request = CreateSecretRequest::build($parent, $secretId, $secret); + + // Create the secret. + $newSecret = $client->createSecret($request); + + // Print the new secret name. + printf('Created secret %s with labels', $newSecret->getName()); +} +// [END secretmanager_create_secret_with_labels] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/delete_regional_secret_annotation.php b/secretmanager/src/delete_regional_secret_annotation.php new file mode 100644 index 0000000000..c76fe6d1c4 --- /dev/null +++ b/secretmanager/src/delete_regional_secret_annotation.php @@ -0,0 +1,89 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the annotations + $annotations = $getSecret->getAnnotations(); + + // delete the annotation + unset($annotations[$annotationKey]); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['annotations']); + + // build the secret + $secret = new Secret(); + $secret->setAnnotations($annotations); + $secret->setName($getSecret->getName()); + + // build the request + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the secret name + printf('Updated secret %s' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_delete_regional_secret_annotation] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/delete_regional_secret_label.php b/secretmanager/src/delete_regional_secret_label.php new file mode 100644 index 0000000000..00c9c18bba --- /dev/null +++ b/secretmanager/src/delete_regional_secret_label.php @@ -0,0 +1,89 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the secret labels + $labels = $getSecret->getLabels(); + + // delete the label + unset($labels[$labelKey]); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['labels']); + + // build the secret + $secret = new Secret(); + $secret->setLabels($labels); + $secret->setName($getSecret->getName()); + + // build the request + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the secret name + printf('Updated secret %s' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_delete_regional_secret_label] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/delete_secret_annotation.php b/secretmanager/src/delete_secret_annotation.php new file mode 100644 index 0000000000..57286ac62e --- /dev/null +++ b/secretmanager/src/delete_secret_annotation.php @@ -0,0 +1,85 @@ +secretName($projectId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the annotations + $annotations = $getSecret->getAnnotations(); + + // delete the annotation + unset($annotations[$annotationKey]); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['annotations']); + + // build the secret + $secret = new Secret(); + $secret->setAnnotations($annotations); + $secret->setName($getSecret->getName()); + + // build the request + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the secret name + printf('Updated secret %s' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_delete_secret_annotation] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/delete_secret_label.php b/secretmanager/src/delete_secret_label.php new file mode 100644 index 0000000000..9e337f1dfb --- /dev/null +++ b/secretmanager/src/delete_secret_label.php @@ -0,0 +1,85 @@ +secretName($projectId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the secret labels + $labels = $getSecret->getLabels(); + + // delete the label + unset($labels[$labelKey]); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['labels']); + + // build the secret + $secret = new Secret(); + $secret->setLabels($labels); + $secret->setName($getSecret->getName()); + + // build the request + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the secret name + printf('Updated secret %s' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_delete_secret_label] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/edit_regional_secret_annotations.php b/secretmanager/src/edit_regional_secret_annotations.php new file mode 100644 index 0000000000..832022f751 --- /dev/null +++ b/secretmanager/src/edit_regional_secret_annotations.php @@ -0,0 +1,89 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the annotations + $annotations = $getSecret->getAnnotations(); + + // update the annotation - need to create a new annotations map with the updated values + $newAnnotations = []; + foreach ($annotations as $key => $value) { + $newAnnotations[$key] = $value; + } + $newAnnotations[$annotationKey] = $annotationValue; + $getSecret->setAnnotations($newAnnotations); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['annotations']); + + // build the secret + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the updated secret + printf('Updated secret %s annotations' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_edit_regional_secret_annotations] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/edit_regional_secret_labels.php b/secretmanager/src/edit_regional_secret_labels.php new file mode 100644 index 0000000000..fa2cfadefd --- /dev/null +++ b/secretmanager/src/edit_regional_secret_labels.php @@ -0,0 +1,89 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the secret labels + $labels = $getSecret->getLabels(); + + // update the label - need to create a new labels map with the updated values + $newLabels = []; + foreach ($labels as $key => $value) { + $newLabels[$key] = $value; + } + $newLabels[$labelKey] = $labelValue; + $getSecret->setLabels($newLabels); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['labels']); + + // build the secret + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the updated secret + printf('Updated secret %s labels' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_edit_regional_secret_labels] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/edit_secret_annotations.php b/secretmanager/src/edit_secret_annotations.php new file mode 100644 index 0000000000..1a58cf521b --- /dev/null +++ b/secretmanager/src/edit_secret_annotations.php @@ -0,0 +1,85 @@ +secretName($projectId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the annotations + $annotations = $getSecret->getAnnotations(); + + // update the annotation - need to create a new annotations map with the updated values + $newAnnotations = []; + foreach ($annotations as $key => $value) { + $newAnnotations[$key] = $value; + } + $newAnnotations[$annotationKey] = $annotationValue; + $getSecret->setAnnotations($newAnnotations); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['annotations']); + + // build the secret + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the updated secret + printf('Updated secret %s annotations' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_edit_secret_annotations] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/edit_secret_labels.php b/secretmanager/src/edit_secret_labels.php new file mode 100644 index 0000000000..cdbb79ce1f --- /dev/null +++ b/secretmanager/src/edit_secret_labels.php @@ -0,0 +1,85 @@ +secretName($projectId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the secret labels + $labels = $getSecret->getLabels(); + + // update the label - need to create a new labels map with the updated values + $newLabels = []; + foreach ($labels as $key => $value) { + $newLabels[$key] = $value; + } + $newLabels[$labelKey] = $labelValue; + $getSecret->setLabels($newLabels); + + // set the field mask + $fieldMask = new FieldMask(); + $fieldMask->setPaths(['labels']); + + // build the secret + $request = new UpdateSecretRequest(); + $request->setSecret($getSecret); + $request->setUpdateMask($fieldMask); + + // update the secret + $updateSecret = $client->updateSecret($request); + + // print the updated secret + printf('Updated secret %s labels' . PHP_EOL, $updateSecret->getName()); +} +// [END secretmanager_edit_secret_labels] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/view_regional_secret_annotations.php b/secretmanager/src/view_regional_secret_annotations.php new file mode 100644 index 0000000000..d58c9e9d7e --- /dev/null +++ b/secretmanager/src/view_regional_secret_annotations.php @@ -0,0 +1,69 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the annotations + $annotations = $getSecret->getAnnotations(); + + // print the secret name + printf('Get secret %s with annotation:' . PHP_EOL, $getSecret->getName()); + // we can even loop over all the annotations + foreach ($annotations as $key => $val) { + printf("\t$key: $val" . PHP_EOL); + } +} +// [END secretmanager_view_regional_secret_annotations] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/view_regional_secret_labels.php b/secretmanager/src/view_regional_secret_labels.php new file mode 100644 index 0000000000..af04dfe721 --- /dev/null +++ b/secretmanager/src/view_regional_secret_labels.php @@ -0,0 +1,69 @@ + "secretmanager.$locationId.rep.googleapis.com"]; + + // Create the Secret Manager client. + $client = new SecretManagerServiceClient($options); + + // Build the resource name of the secret. + $name = $client->projectLocationSecretName($projectId, $locationId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the secret labels + $labels = $getSecret->getLabels(); + + // print the secret name + printf('Get secret %s with labels:' . PHP_EOL, $getSecret->getName()); + // we can even loop over all the labels + foreach ($labels as $key => $val) { + printf("\t$key: $val" . PHP_EOL); + } +} +// [END secretmanager_view_regional_secret_labels] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/view_secret_annotations.php b/secretmanager/src/view_secret_annotations.php new file mode 100644 index 0000000000..e61aea65aa --- /dev/null +++ b/secretmanager/src/view_secret_annotations.php @@ -0,0 +1,65 @@ +secretName($projectId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the annotations + $annotations = $getSecret->getAnnotations(); + + // print the secret name + printf('Get secret %s with annotation:' . PHP_EOL, $getSecret->getName()); + // we can even loop over all the annotations + foreach ($annotations as $key => $val) { + printf("\t$key: $val" . PHP_EOL); + } +} +// [END secretmanager_view_secret_annotations] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/src/view_secret_labels.php b/secretmanager/src/view_secret_labels.php new file mode 100644 index 0000000000..d549ccf56c --- /dev/null +++ b/secretmanager/src/view_secret_labels.php @@ -0,0 +1,65 @@ +secretName($projectId, $secretId); + + // Build the request. + $request = GetSecretRequest::build($name); + + // get the secret. + $getSecret = $client->getSecret($request); + + // get the secret labels + $labels = $getSecret->getLabels(); + + // print the secret name + printf('Get secret %s with labels:' . PHP_EOL, $getSecret->getName()); + // we can even loop over all the labels + foreach ($labels as $key => $val) { + printf("\t$key: $val" . PHP_EOL); + } +} +// [END secretmanager_view_secret_labels] + +// The following 2 lines are only needed to execute the samples on the CLI +require_once __DIR__ . '/../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/secretmanager/test/regionalsecretmanagerTest.php b/secretmanager/test/regionalsecretmanagerTest.php index 078f9de5cf..01d5c7b48a 100644 --- a/secretmanager/test/regionalsecretmanagerTest.php +++ b/secretmanager/test/regionalsecretmanagerTest.php @@ -57,9 +57,17 @@ class regionalsecretmanagerTest extends TestCase private static $testSecretVersionToEnable; private static $testSecretWithTagToCreateName; private static $testSecretBindTagToCreateName; + private static $testSecretWithLabelsToCreateName; + private static $testSecretWithAnnotationsToCreateName; private static $iamUser = 'user:kapishsingh@google.com'; private static $locationId = 'us-central1'; + private static $testLabelKey = 'test-label-key'; + private static $testLabelValue = 'test-label-value'; + private static $testUpdatedLabelValue = 'test-label-new-value'; + private static $testAnnotationKey = 'test-annotation-key'; + private static $testAnnotationValue = 'test-annotation-value'; + private static $testUpdatedAnnotationValue = 'test-annotation-new-value'; private static $testTagKey; private static $testTagValue; @@ -81,6 +89,8 @@ public static function setUpBeforeClass(): void self::$testSecretVersionToEnable = self::addSecretVersion(self::$testSecretWithVersions); self::$testSecretWithTagToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); self::$testSecretBindTagToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); + self::$testSecretWithLabelsToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); + self::$testSecretWithAnnotationsToCreateName = self::$client->projectLocationSecretName(self::$projectId, self::$locationId, self::randomSecretId()); self::disableSecretVersion(self::$testSecretVersionToEnable); self::$testTagKey = self::createTagKey(self::randomSecretId()); @@ -98,6 +108,8 @@ public static function tearDownAfterClass(): void self::deleteSecret(self::$testSecretToCreateName); self::deleteSecret(self::$testSecretWithTagToCreateName); self::deleteSecret(self::$testSecretBindTagToCreateName); + self::deleteSecret(self::$testSecretWithLabelsToCreateName); + self::deleteSecret(self::$testSecretWithAnnotationsToCreateName); sleep(15); // Added a sleep to wait for the tag unbinding self::deleteTagValue(); self::deleteTagKey(); @@ -462,4 +474,118 @@ public function testBindTagsToSecret() $this->assertStringContainsString('Created secret', $output); $this->assertStringContainsString('Tag binding created for secret', $output); } + + public function testCreateSecretWithLabels() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('create_regional_secret_with_labels', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testLabelKey, + self::$testLabelValue + ]); + + $this->assertStringContainsString('Created secret', $output); + } + + public function testCreateSecretWithAnnotations() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('create_regional_secret_with_annotations', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testAnnotationKey, + self::$testAnnotationValue + ]); + + $this->assertStringContainsString('Created secret', $output); + } + + public function testViewSecretAnnotations() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('view_regional_secret_annotations', [ + $name['project'], + $name['location'], + $name['secret'] + ]); + + $this->assertStringContainsString('Get secret', $output); + } + + public function testViewSecretLabels() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('view_regional_secret_labels', [ + $name['project'], + $name['location'], + $name['secret'] + ]); + + $this->assertStringContainsString('Get secret', $output); + } + + public function testEditSecretLabels() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('edit_regional_secret_labels', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testLabelKey, + self::$testUpdatedLabelValue + ]); + + $this->assertStringContainsString('Updated secret', $output); + } + + public function testEditSecretAnnotations() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('edit_regional_secret_annotations', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testAnnotationKey, + self::$testUpdatedAnnotationValue + ]); + + $this->assertStringContainsString('Updated secret', $output); + } + + public function testDeleteSecretLabel() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('delete_regional_secret_label', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testLabelKey + ]); + + $this->assertStringContainsString('Updated secret', $output); + } + + public function testDeleteSecretAnnotation() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('delete_regional_secret_annotation', [ + $name['project'], + $name['location'], + $name['secret'], + self::$testAnnotationKey + ]); + + $this->assertStringContainsString('Updated secret', $output); + } } diff --git a/secretmanager/test/secretmanagerTest.php b/secretmanager/test/secretmanagerTest.php index 2f63193e66..a67d8cfa7e 100644 --- a/secretmanager/test/secretmanagerTest.php +++ b/secretmanager/test/secretmanagerTest.php @@ -60,8 +60,16 @@ class secretmanagerTest extends TestCase private static $testSecretVersionToEnable; private static $testSecretWithTagToCreateName; private static $testSecretBindTagToCreateName; + private static $testSecretWithLabelsToCreateName; + private static $testSecretWithAnnotationsToCreateName; private static $iamUser = 'user:sethvargo@google.com'; + private static $testLabelKey = 'test-label-key'; + private static $testLabelValue = 'test-label-value'; + private static $testUpdatedLabelValue = 'test-label-new-value'; + private static $testAnnotationKey = 'test-annotation-key'; + private static $testAnnotationValue = 'test-annotation-value'; + private static $testUpdatedAnnotationValue = 'test-annotation-new-value'; private static $testTagKey; private static $testTagValue; @@ -79,6 +87,8 @@ public static function setUpBeforeClass(): void self::$testUmmrSecretToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); self::$testSecretWithTagToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); self::$testSecretBindTagToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); + self::$testSecretWithLabelsToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); + self::$testSecretWithAnnotationsToCreateName = self::$client->secretName(self::$projectId, self::randomSecretId()); self::$testSecretVersion = self::addSecretVersion(self::$testSecretWithVersions); self::$testSecretVersionToDestroy = self::addSecretVersion(self::$testSecretWithVersions); @@ -99,6 +109,8 @@ public static function tearDownAfterClass(): void self::deleteSecret(self::$testUmmrSecretToCreateName); self::deleteSecret(self::$testSecretWithTagToCreateName); self::deleteSecret(self::$testSecretBindTagToCreateName); + self::deleteSecret(self::$testSecretWithLabelsToCreateName); + self::deleteSecret(self::$testSecretWithAnnotationsToCreateName); sleep(15); // Added a sleep to wait for the tag unbinding self::deleteTagValue(); self::deleteTagKey(); @@ -464,4 +476,110 @@ public function testBindTagsToSecret() $this->assertStringContainsString('Created secret', $output); $this->assertStringContainsString('Tag binding created for secret', $output); } + + public function testCreateSecretWithLabels() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('create_secret_with_labels', [ + $name['project'], + $name['secret'], + self::$testLabelKey, + self::$testLabelValue + ]); + + $this->assertStringContainsString('Created secret', $output); + } + + public function testCreateSecretWithAnnotations() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('create_secret_with_annotations', [ + $name['project'], + $name['secret'], + self::$testAnnotationKey, + self::$testAnnotationValue + ]); + + $this->assertStringContainsString('Created secret', $output); + } + + public function testViewSecretAnnotations() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('view_secret_annotations', [ + $name['project'], + $name['secret'] + ]); + + $this->assertStringContainsString('Get secret', $output); + } + + public function testViewSecretLabels() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('view_secret_labels', [ + $name['project'], + $name['secret'] + ]); + + $this->assertStringContainsString('Get secret', $output); + } + + public function testEditSecretLabels() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('edit_secret_labels', [ + $name['project'], + $name['secret'], + self::$testLabelKey, + self::$testUpdatedLabelValue + ]); + + $this->assertStringContainsString('Updated secret', $output); + } + + public function testEditSecretAnnotations() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('edit_secret_annotations', [ + $name['project'], + $name['secret'], + self::$testAnnotationKey, + self::$testUpdatedAnnotationValue + ]); + + $this->assertStringContainsString('Updated secret', $output); + } + + public function testDeleteSecretLabel() + { + $name = self::$client->parseName(self::$testSecretWithLabelsToCreateName); + + $output = $this->runFunctionSnippet('delete_secret_label', [ + $name['project'], + $name['secret'], + self::$testLabelKey + ]); + + $this->assertStringContainsString('Updated secret', $output); + } + + public function testDeleteSecretAnnotation() + { + $name = self::$client->parseName(self::$testSecretWithAnnotationsToCreateName); + + $output = $this->runFunctionSnippet('delete_secret_annotation', [ + $name['project'], + $name['secret'], + self::$testAnnotationKey + ]); + + $this->assertStringContainsString('Updated secret', $output); + } } From 8a7d6cc0fb91ce79a9c333088cdfce226b13069c Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 23 Jul 2025 16:25:23 -0700 Subject: [PATCH 04/10] fix(spanner): SQL syntax error --- spanner/src/pg_update_dml_returning.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spanner/src/pg_update_dml_returning.php b/spanner/src/pg_update_dml_returning.php index 2a975b2297..f5f11f57d6 100644 --- a/spanner/src/pg_update_dml_returning.php +++ b/spanner/src/pg_update_dml_returning.php @@ -48,7 +48,7 @@ function pg_update_dml_returning(string $instanceId, string $databaseId): void $result = $transaction->execute( 'UPDATE Albums ' . 'SET MarketingBudget = MarketingBudget * 2 ' - . 'WHERE SingerId = 1 and AlbumId = 1' + . 'WHERE SingerId = 1 and AlbumId = 1 ' . 'RETURNING MarketingBudget' ); foreach ($result->rows() as $row) { From b0fd0f130ab027cd0aaacf44888defe5bbe0bcd5 Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Wed, 30 Jul 2025 07:06:40 +0000 Subject: [PATCH 05/10] Docs: Update README file --- storagecontrol/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/storagecontrol/README.md b/storagecontrol/README.md index 7cabbfa193..769474eaa8 100644 --- a/storagecontrol/README.md +++ b/storagecontrol/README.md @@ -58,3 +58,7 @@ to [browse the source][google-cloud-php-source] and [report issues][google-clou ## Licensing * See [LICENSE](../../LICENSE) + +## Note + +Tests for Anywhere cache Sample are not included due to the long operation times typical for cache operations. From e4c8403cca3966207935ef3f97f9f22a89ca6235 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 31 Jul 2025 09:55:49 -0700 Subject: [PATCH 06/10] add test, wait for LROs --- storagecontrol/src/create_anywhere_cache.php | 11 +- storagecontrol/src/update_anywhere_cache.php | 11 +- storagecontrol/test/anywhereCacheTest.php | 180 +++++++++++++++++++ 3 files changed, 198 insertions(+), 4 deletions(-) create mode 100644 storagecontrol/test/anywhereCacheTest.php diff --git a/storagecontrol/src/create_anywhere_cache.php b/storagecontrol/src/create_anywhere_cache.php index e3a7495588..9c81bf1d52 100644 --- a/storagecontrol/src/create_anywhere_cache.php +++ b/storagecontrol/src/create_anywhere_cache.php @@ -27,6 +27,7 @@ # [START storage_control_create_anywhere_cache] use Google\Cloud\Storage\Control\V2\AnywhereCache; use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; +use Google\Cloud\Storage\Control\V2\CreateAnywhereCacheMetadata; use Google\Cloud\Storage\Control\V2\CreateAnywhereCacheRequest; /** @@ -56,9 +57,15 @@ function create_anywhere_cache(string $bucketName, string $zone): void // 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. - $response = $storageControlClient->createAnywhereCache($request); + $operation = $storageControlClient->createAnywhereCache($request); - printf('Created anywhere cache: %s', $response->getName()); + printf('Waiting for operation %s to complete...' . PHP_EOL, $operation->getName()); + $operation->pollUntilComplete(); + + // var_dump($operation);exit; + /** @var CreateAnywhereCacheMetadata */ + $metadata = $operation->getMetadata(); + printf('Created anywhere cache: %s' . PHP_EOL, $metadata->getAnywhereCacheId()); } # [END storage_control_create_anywhere_cache] diff --git a/storagecontrol/src/update_anywhere_cache.php b/storagecontrol/src/update_anywhere_cache.php index 4d2fba02af..00c88a5964 100644 --- a/storagecontrol/src/update_anywhere_cache.php +++ b/storagecontrol/src/update_anywhere_cache.php @@ -26,6 +26,7 @@ # [START storage_control_update_anywhere_cache] use Google\Cloud\Storage\Control\V2\AnywhereCache; use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; +use Google\Cloud\Storage\Control\V2\UpdateAnywhereCacheMetadata; use Google\Cloud\Storage\Control\V2\UpdateAnywhereCacheRequest; use Google\Protobuf\FieldMask; @@ -63,9 +64,15 @@ function update_anywhere_cache(string $bucketName, string $anywhereCacheId, stri 'update_mask' => $updateMask, ]); - $response = $storageControlClient->updateAnywhereCache($request); + $operation = $storageControlClient->updateAnywhereCache($request); - printf('Updated anywhere cache: %s', $response->getName()); + printf('Waiting for operation %s to complete...' . PHP_EOL, $operation->getName()); + $operation->pollUntilComplete(); + + // var_dump($operation);exit; + /** @var UpdateAnywhereCacheMetadata */ + $metadata = $operation->getMetadata(); + printf('Updated anywhere cache: %s', $metadata->getAnywhereCacheId()); } # [END storage_control_update_anywhere_cache] diff --git a/storagecontrol/test/anywhereCacheTest.php b/storagecontrol/test/anywhereCacheTest.php new file mode 100644 index 0000000000..44b0fe972b --- /dev/null +++ b/storagecontrol/test/anywhereCacheTest.php @@ -0,0 +1,180 @@ +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::$location, + 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::$location, + self::$cacheId, + ]); + + $this->assertStringContainsString( + sprintf('Got Anywhere Cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testGetAnywhereCache + */ + public function testListAnywhereCaches() + { + $output = $this->runFunctionSnippet('list_anywhere_caches', [ + self::$location, + ]); + + $this->assertStringContainsString( + sprintf('Listed Anywhere Cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testListAnywhereCaches + */ + public function testPauseAnywhereCache() + { + $output = $this->runFunctionSnippet('pause_anywhere_cache', [ + self::$location, + self::$cacheId, + ]); + + $this->assertStringContainsString( + sprintf('Paused Anywhere Cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testPauseAnywhereCache + */ + public function testResumeAnywhereCache() + { + $output = $this->runFunctionSnippet('resume_anywhere_cache', [ + self::$location, + self::$cacheId, + ]); + + $this->assertStringContainsString( + sprintf('Resumed Anywhere Cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testResumeAnywhereCache + */ + public function testUpdateAnywhereCache() + { + $output = $this->runFunctionSnippet('update_anywhere_cache', [ + self::$location, + self::$cacheId, + '7200s' + ]); + + $this->assertStringContainsString( + sprintf('Updated Anywhere Cache: %s', self::$anywhereCacheName), + $output + ); + } + + /** + * @depends testUpdateAnywhereCache + */ + public function testDisableAnywhereCache() + { + $output = $this->runFunctionSnippet('disable_anywhere_cache', [ + self::$location, + self::$cacheId, + ]); + + $this->assertStringContainsString( + sprintf('Disabled Anywhere Cache: %s', self::$anywhereCacheName), + $output + ); + } +} From e48785734cb66e69521dea2514fd6b34770aac53 Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Tue, 5 Aug 2025 11:16:01 +0000 Subject: [PATCH 07/10] Add test cases --- storagecontrol/src/create_anywhere_cache.php | 16 +++++---- storagecontrol/src/update_anywhere_cache.php | 20 ++++++------ storagecontrol/test/anywhereCacheTest.php | 34 +++++++++++--------- 3 files changed, 39 insertions(+), 31 deletions(-) diff --git a/storagecontrol/src/create_anywhere_cache.php b/storagecontrol/src/create_anywhere_cache.php index 9c81bf1d52..60aa5b541b 100644 --- a/storagecontrol/src/create_anywhere_cache.php +++ b/storagecontrol/src/create_anywhere_cache.php @@ -27,7 +27,6 @@ # [START storage_control_create_anywhere_cache] use Google\Cloud\Storage\Control\V2\AnywhereCache; use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; -use Google\Cloud\Storage\Control\V2\CreateAnywhereCacheMetadata; use Google\Cloud\Storage\Control\V2\CreateAnywhereCacheRequest; /** @@ -57,15 +56,20 @@ function create_anywhere_cache(string $bucketName, string $zone): void // 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. + /** @var Operation $operation */ $operation = $storageControlClient->createAnywhereCache($request); printf('Waiting for operation %s to complete...' . PHP_EOL, $operation->getName()); - $operation->pollUntilComplete(); + $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_dump($operation);exit; - /** @var CreateAnywhereCacheMetadata */ - $metadata = $operation->getMetadata(); - printf('Created anywhere cache: %s' . PHP_EOL, $metadata->getAnywhereCacheId()); + /** @var AnywhereCache $anywhereCacheResult */ + $anywhereCacheResult = $operation->getResult(); + printf('Created anywhere cache: %s' . PHP_EOL, $anywhereCacheResult->getName()); } # [END storage_control_create_anywhere_cache] diff --git a/storagecontrol/src/update_anywhere_cache.php b/storagecontrol/src/update_anywhere_cache.php index 00c88a5964..281ee04c16 100644 --- a/storagecontrol/src/update_anywhere_cache.php +++ b/storagecontrol/src/update_anywhere_cache.php @@ -1,4 +1,5 @@ ['admission_policy'], ]); - // Start an update operation and block until it completes. Real applications - // may want to setup a callback, wait on a coroutine, or poll until it - // completes. $request = new UpdateAnywhereCacheRequest([ 'anywhere_cache' => $anywhereCache, 'update_mask' => $updateMask, ]); + // Start an update operation. This returns an Operation object which can be polled. + /** @var Operation $operation */ $operation = $storageControlClient->updateAnywhereCache($request); printf('Waiting for operation %s to complete...' . PHP_EOL, $operation->getName()); - $operation->pollUntilComplete(); + $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_dump($operation);exit; - /** @var UpdateAnywhereCacheMetadata */ - $metadata = $operation->getMetadata(); - printf('Updated anywhere cache: %s', $metadata->getAnywhereCacheId()); + $anywhereCacheResult = $operation->getResult(); + printf('Updated anywhere cache: %s', $anywhereCacheResult->getName()); } # [END storage_control_update_anywhere_cache] diff --git a/storagecontrol/test/anywhereCacheTest.php b/storagecontrol/test/anywhereCacheTest.php index 44b0fe972b..d36f5b6134 100644 --- a/storagecontrol/test/anywhereCacheTest.php +++ b/storagecontrol/test/anywhereCacheTest.php @@ -1,4 +1,5 @@ createBucket( sprintf('php-gcscontrol-sample-%s', $uniqueBucketId), [ @@ -56,7 +57,7 @@ public static function setUpBeforeClass(): void ); self::$anywhereCacheName = self::$storageControlClient->anywhereCacheName( '_', // Set project to "_" to signify global bucket - self::$location, + self::$sourceBucket->name(), self::$cacheId ); } @@ -77,7 +78,7 @@ public function testCreateAnywhereCache() ]); $this->assertStringContainsString( - sprintf('Created Anywhere Cache: %s', self::$anywhereCacheName), + sprintf('Created anywhere cache: %s', self::$anywhereCacheName), $output ); } @@ -88,12 +89,12 @@ public function testCreateAnywhereCache() public function testGetAnywhereCache() { $output = $this->runFunctionSnippet('get_anywhere_cache', [ - self::$location, + self::$sourceBucket->name(), self::$cacheId, ]); $this->assertStringContainsString( - sprintf('Got Anywhere Cache: %s', self::$anywhereCacheName), + sprintf('Got anywhere cache: %s', self::$anywhereCacheName), $output ); } @@ -104,11 +105,11 @@ public function testGetAnywhereCache() public function testListAnywhereCaches() { $output = $this->runFunctionSnippet('list_anywhere_caches', [ - self::$location, + self::$sourceBucket->name(), ]); $this->assertStringContainsString( - sprintf('Listed Anywhere Cache: %s', self::$anywhereCacheName), + sprintf('Anywhere cache name: %s', self::$anywhereCacheName), $output ); } @@ -119,12 +120,12 @@ public function testListAnywhereCaches() public function testPauseAnywhereCache() { $output = $this->runFunctionSnippet('pause_anywhere_cache', [ - self::$location, + self::$sourceBucket->name(), self::$cacheId, ]); $this->assertStringContainsString( - sprintf('Paused Anywhere Cache: %s', self::$anywhereCacheName), + sprintf('Paused anywhere cache: %s', self::$anywhereCacheName), $output ); } @@ -135,12 +136,12 @@ public function testPauseAnywhereCache() public function testResumeAnywhereCache() { $output = $this->runFunctionSnippet('resume_anywhere_cache', [ - self::$location, + self::$sourceBucket->name(), self::$cacheId, ]); $this->assertStringContainsString( - sprintf('Resumed Anywhere Cache: %s', self::$anywhereCacheName), + sprintf('Resumed anywhere cache: %s', self::$anywhereCacheName), $output ); } @@ -150,14 +151,15 @@ public function testResumeAnywhereCache() */ public function testUpdateAnywhereCache() { + $admission_policy = 'admit-on-second-miss'; $output = $this->runFunctionSnippet('update_anywhere_cache', [ - self::$location, + self::$sourceBucket->name(), self::$cacheId, - '7200s' + $admission_policy ]); $this->assertStringContainsString( - sprintf('Updated Anywhere Cache: %s', self::$anywhereCacheName), + sprintf('Updated anywhere cache: %s', self::$anywhereCacheName), $output ); } @@ -168,12 +170,12 @@ public function testUpdateAnywhereCache() public function testDisableAnywhereCache() { $output = $this->runFunctionSnippet('disable_anywhere_cache', [ - self::$location, + self::$sourceBucket->name(), self::$cacheId, ]); $this->assertStringContainsString( - sprintf('Disabled Anywhere Cache: %s', self::$anywhereCacheName), + sprintf('Disabled anywhere cache: %s', self::$anywhereCacheName), $output ); } From 91efa59c0032d506bbb5ee6410b577605751348d Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Tue, 5 Aug 2025 11:29:08 +0000 Subject: [PATCH 08/10] Bug fix --- storagecontrol/src/create_anywhere_cache.php | 1 + storagecontrol/src/update_anywhere_cache.php | 1 + 2 files changed, 2 insertions(+) diff --git a/storagecontrol/src/create_anywhere_cache.php b/storagecontrol/src/create_anywhere_cache.php index 60aa5b541b..3abb21fbd6 100644 --- a/storagecontrol/src/create_anywhere_cache.php +++ b/storagecontrol/src/create_anywhere_cache.php @@ -28,6 +28,7 @@ use Google\Cloud\Storage\Control\V2\AnywhereCache; use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; use Google\Cloud\Storage\Control\V2\CreateAnywhereCacheRequest; +use Google\ApiCore\Operation; /** * Creates an Anywhere Cache instance. diff --git a/storagecontrol/src/update_anywhere_cache.php b/storagecontrol/src/update_anywhere_cache.php index 281ee04c16..167339d6d0 100644 --- a/storagecontrol/src/update_anywhere_cache.php +++ b/storagecontrol/src/update_anywhere_cache.php @@ -29,6 +29,7 @@ use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; use Google\Cloud\Storage\Control\V2\UpdateAnywhereCacheRequest; use Google\Protobuf\FieldMask; +use Google\ApiCore\Operation; /** * Updates an Anywhere Cache instance. From 1b103f08ca4e29eb03c5e3aff61099906d8e8059 Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Tue, 5 Aug 2025 12:01:49 +0000 Subject: [PATCH 09/10] Lint fix --- storagecontrol/src/create_anywhere_cache.php | 2 -- storagecontrol/src/update_anywhere_cache.php | 2 -- 2 files changed, 4 deletions(-) diff --git a/storagecontrol/src/create_anywhere_cache.php b/storagecontrol/src/create_anywhere_cache.php index 3abb21fbd6..9e8ac273c5 100644 --- a/storagecontrol/src/create_anywhere_cache.php +++ b/storagecontrol/src/create_anywhere_cache.php @@ -28,7 +28,6 @@ use Google\Cloud\Storage\Control\V2\AnywhereCache; use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; use Google\Cloud\Storage\Control\V2\CreateAnywhereCacheRequest; -use Google\ApiCore\Operation; /** * Creates an Anywhere Cache instance. @@ -57,7 +56,6 @@ function create_anywhere_cache(string $bucketName, string $zone): void // 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. - /** @var Operation $operation */ $operation = $storageControlClient->createAnywhereCache($request); printf('Waiting for operation %s to complete...' . PHP_EOL, $operation->getName()); diff --git a/storagecontrol/src/update_anywhere_cache.php b/storagecontrol/src/update_anywhere_cache.php index 167339d6d0..99b262e00c 100644 --- a/storagecontrol/src/update_anywhere_cache.php +++ b/storagecontrol/src/update_anywhere_cache.php @@ -29,7 +29,6 @@ use Google\Cloud\Storage\Control\V2\Client\StorageControlClient; use Google\Cloud\Storage\Control\V2\UpdateAnywhereCacheRequest; use Google\Protobuf\FieldMask; -use Google\ApiCore\Operation; /** * Updates an Anywhere Cache instance. @@ -63,7 +62,6 @@ function update_anywhere_cache(string $bucketName, string $anywhereCacheId, stri ]); // Start an update operation. This returns an Operation object which can be polled. - /** @var Operation $operation */ $operation = $storageControlClient->updateAnywhereCache($request); printf('Waiting for operation %s to complete...' . PHP_EOL, $operation->getName()); From 72cc20a90dbe6ae9c878f8178a0abfa24fdfa61d Mon Sep 17 00:00:00 2001 From: Thiyagu K Date: Tue, 5 Aug 2025 13:22:45 +0000 Subject: [PATCH 10/10] Docs: Update README file --- storagecontrol/README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/storagecontrol/README.md b/storagecontrol/README.md index 769474eaa8..7cabbfa193 100644 --- a/storagecontrol/README.md +++ b/storagecontrol/README.md @@ -58,7 +58,3 @@ to [browse the source][google-cloud-php-source] and [report issues][google-clou ## Licensing * See [LICENSE](../../LICENSE) - -## Note - -Tests for Anywhere cache Sample are not included due to the long operation times typical for cache operations.