|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2026 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_create_regional_secret_with_rotation] |
| 29 | +use Google\Cloud\SecretManager\V1\CreateSecretRequest; |
| 30 | +use Google\Cloud\SecretManager\V1\Secret; |
| 31 | +use Google\Cloud\SecretManager\V1\Rotation; |
| 32 | +use Google\Cloud\SecretManager\V1\Topic; |
| 33 | +use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient; |
| 34 | +use Google\Protobuf\Timestamp; |
| 35 | +use Google\Protobuf\Duration; |
| 36 | + |
| 37 | +/** |
| 38 | + * Create a regional secret with a rotation policy. |
| 39 | + * |
| 40 | + * @param string $projectId Your Google Cloud Project ID (e.g. 'my-project') |
| 41 | + * @param string $locationId Secret location (e.g. 'us-central1') |
| 42 | + * @param string $secretId Your secret ID (e.g. 'my-secret') |
| 43 | + * @param string $topicName Full Pub/Sub topic name (projects/{project}/topics/{topic}) |
| 44 | + */ |
| 45 | +function create_regional_secret_with_rotation(string $projectId, string $locationId, string $secretId, string $topicName): void |
| 46 | +{ |
| 47 | + $options = ['apiEndpoint' => "secretmanager.$locationId.rep.googleapis.com"]; |
| 48 | + $client = new SecretManagerServiceClient($options); |
| 49 | + |
| 50 | + $parent = $client->locationName($projectId, $locationId); |
| 51 | + |
| 52 | + $nextRotationTimeSeconds = time() + 7200; // 2 hours |
| 53 | + $rotationPeriodSeconds = 3600; // 1 hour |
| 54 | + |
| 55 | + $rotation = new Rotation([ |
| 56 | + 'next_rotation_time' => new Timestamp(['seconds' => $nextRotationTimeSeconds]), |
| 57 | + 'rotation_period' => new Duration(['seconds' => $rotationPeriodSeconds]), |
| 58 | + ]); |
| 59 | + |
| 60 | + $secret = new Secret([ |
| 61 | + 'rotation' => $rotation, |
| 62 | + 'topics' => [new Topic(['name' => $topicName])], |
| 63 | + ]); |
| 64 | + |
| 65 | + $request = CreateSecretRequest::build($parent, $secretId, $secret); |
| 66 | + |
| 67 | + $newSecret = $client->createSecret($request); |
| 68 | + |
| 69 | + printf('Created secret %s with rotation%s', $newSecret->getName(), PHP_EOL); |
| 70 | +} |
| 71 | +// [END secretmanager_create_regional_secret_with_rotation] |
| 72 | + |
| 73 | +// The following 2 lines are only needed to execute the samples on the CLI |
| 74 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 75 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments