|
| 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/parametermanager/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +declare(strict_types=1); |
| 25 | + |
| 26 | +namespace Google\Cloud\Samples\ParameterManager; |
| 27 | + |
| 28 | +// [START parametermanager_create_param_version_with_secret] |
| 29 | +// Import necessary classes for creating a parameter version. |
| 30 | +use Google\Cloud\ParameterManager\V1\Client\ParameterManagerClient; |
| 31 | +use Google\Cloud\ParameterManager\V1\CreateParameterVersionRequest; |
| 32 | +use Google\Cloud\ParameterManager\V1\ParameterVersion; |
| 33 | +use Google\Cloud\ParameterManager\V1\ParameterVersionPayload; |
| 34 | + |
| 35 | +/** |
| 36 | + * Creates a parameter version with an secret reference. |
| 37 | + * |
| 38 | + * @param string $projectId The Google Cloud Project ID (e.g. 'my-project') |
| 39 | + * @param string $parameterId The Parameter ID (e.g. 'my-param') |
| 40 | + * @param string $versionId The Version ID (e.g. 'my-param-version') |
| 41 | + * @param string $secretId The ID of the secret to be referenced (e.g. 'projects/my-project/secrets/my-secret/versions/latest') |
| 42 | + */ |
| 43 | +function create_param_version_with_secret(string $projectId, string $parameterId, string $versionId, string $secretId): void |
| 44 | +{ |
| 45 | + // Create a client for the Parameter Manager service. |
| 46 | + $client = new ParameterManagerClient(); |
| 47 | + |
| 48 | + // Build the resource name of the parent object. |
| 49 | + $parent = $client->parameterName($projectId, 'global', $parameterId); |
| 50 | + |
| 51 | + // Build payload. |
| 52 | + $payload = json_encode([ |
| 53 | + 'username' => 'test-user', |
| 54 | + 'password' => sprintf('__REF__(//secretmanager.googleapis.com/%s)', $secretId) |
| 55 | + ], JSON_UNESCAPED_SLASHES); |
| 56 | + |
| 57 | + // Create a new ParameterVersionPayload object and set the payload with secret reference. |
| 58 | + $parameterVersionPayload = new ParameterVersionPayload(); |
| 59 | + $parameterVersionPayload->setData($payload); |
| 60 | + |
| 61 | + // Create a new ParameterVersion object and set the payload. |
| 62 | + $parameterVersion = new ParameterVersion(); |
| 63 | + $parameterVersion->setPayload($parameterVersionPayload); |
| 64 | + |
| 65 | + // Prepare the request with the parent and parameter version object. |
| 66 | + $request = (new CreateParameterVersionRequest()) |
| 67 | + ->setParent($parent) |
| 68 | + ->setParameterVersionId($versionId) |
| 69 | + ->setParameterVersion($parameterVersion); |
| 70 | + |
| 71 | + // Call the API to create the parameter version. |
| 72 | + $newParameterVersion = $client->createParameterVersion($request); |
| 73 | + printf('Created parameter version: %s' . PHP_EOL, $newParameterVersion->getName()); |
| 74 | +} |
| 75 | +// [END parametermanager_create_param_version_with_secret] |
| 76 | + |
| 77 | +// The following 2 lines are only needed to execute the samples on the CLI |
| 78 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 79 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments