|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2025 Google LLC. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/* |
| 19 | + * For instructions on how to run the full sample: |
| 20 | + * |
| 21 | + * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/secretmanager/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +declare(strict_types=1); |
| 25 | + |
| 26 | +namespace Google\Cloud\Samples\SecretManager; |
| 27 | + |
| 28 | +// [START secretmanager_bind_tags_to_regional_secret] |
| 29 | +// Import the Secret Manager client library. |
| 30 | +use Google\Cloud\SecretManager\V1\CreateSecretRequest; |
| 31 | +use Google\Cloud\SecretManager\V1\Secret; |
| 32 | +use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; |
| 33 | +use Google\Cloud\ResourceManager\V3\Client\TagBindingsClient; |
| 34 | +use Google\Cloud\ResourceManager\V3\CreateTagBindingRequest; |
| 35 | +use Google\Cloud\ResourceManager\V3\TagBinding; |
| 36 | + |
| 37 | +/** |
| 38 | + * @param string $projectId Your Google Cloud Project ID (e.g. 'my-project') |
| 39 | + * @param string $locationId Your Google Cloud Location ID (e.g. 'us-central1') |
| 40 | + * @param string $secretId Your secret ID (e.g. 'my-secret') |
| 41 | + * @param string $tagValue Your tag value (e.g. 'tagValues/281476592621530') |
| 42 | + */ |
| 43 | +function bind_tags_to_regional_secret(string $projectId, string $locationId, string $secretId, string $tagValue): void |
| 44 | +{ |
| 45 | + // Specify regional endpoint. |
| 46 | + $options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"]; |
| 47 | + |
| 48 | + // Create the Secret Manager client. |
| 49 | + $client = new SecretManagerServiceClient($options); |
| 50 | + |
| 51 | + // Build the resource name of the parent project. |
| 52 | + $parent = $client->locationName($projectId, $locationId); |
| 53 | + |
| 54 | + $secret = new Secret(); |
| 55 | + |
| 56 | + // Build the request. |
| 57 | + $request = CreateSecretRequest::build($parent, $secretId, $secret); |
| 58 | + |
| 59 | + // Create the secret. |
| 60 | + $newSecret = $client->createSecret($request); |
| 61 | + |
| 62 | + // Print the new secret name. |
| 63 | + printf('Created secret %s' . PHP_EOL, $newSecret->getName()); |
| 64 | + |
| 65 | + // Specify regional endpoint. |
| 66 | + $tagBindOptions = ['apiEndpoint' => "$locationId-cloudresourcemanager.googleapis.com"]; |
| 67 | + $tagBindingsClient = new TagBindingsClient($tagBindOptions); |
| 68 | + $tagBinding = (new TagBinding()) |
| 69 | + ->setParent('//secretmanager.googleapis.com/' . $newSecret->getName()) |
| 70 | + ->setTagValue($tagValue); |
| 71 | + |
| 72 | + // Build the request. |
| 73 | + $request = (new CreateTagBindingRequest()) |
| 74 | + ->setTagBinding($tagBinding); |
| 75 | + |
| 76 | + // Create the tag binding. |
| 77 | + $operationResponse = $tagBindingsClient->createTagBinding($request); |
| 78 | + $operationResponse->pollUntilComplete(); |
| 79 | + |
| 80 | + // Check if the operation succeeded. |
| 81 | + if ($operationResponse->operationSucceeded()) { |
| 82 | + printf('Tag binding created for secret %s with tag value %s' . PHP_EOL, $newSecret->getName(), $tagValue); |
| 83 | + } else { |
| 84 | + $error = $operationResponse->getError(); |
| 85 | + printf('Error in creating tag binding: %s' . PHP_EOL, $error->getMessage()); |
| 86 | + } |
| 87 | +} |
| 88 | +// [END secretmanager_bind_tags_to_regional_secret] |
| 89 | + |
| 90 | +// The following 2 lines are only needed to execute the samples on the CLI |
| 91 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 92 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments