|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Creates a new version of an existing parameter in the global location |
| 19 | + * of the specified project using the Google Cloud Parameter Manager SDK. |
| 20 | + * The payload is specified as a JSON string and includes a reference to a secret. |
| 21 | + * |
| 22 | + * @param {string} projectId - The Google Cloud project ID where the parameter is located. |
| 23 | + * @param {string} parameterId - The ID of the parameter for which the version is to be created. |
| 24 | + * @param {string} parameterVersionId - The ID of the parameter version to be created. |
| 25 | + * @param {string} secretId - The ID of the secret to be referenced. |
| 26 | + */ |
| 27 | +async function main( |
| 28 | + projectId = 'my-project', |
| 29 | + parameterId = 'my-parameter', |
| 30 | + parameterVersionId = 'v1', |
| 31 | + secretId = 'projects/my-project/secrets/application-secret/version/latest' |
| 32 | +) { |
| 33 | + // [START parametermanager_create_param_version_with_secret] |
| 34 | + /** |
| 35 | + * TODO(developer): Uncomment these variables before running the sample. |
| 36 | + */ |
| 37 | + // const projectId = 'YOUR_PROJECT_ID'; |
| 38 | + // const parameterId = 'YOUR_PARAMETER_ID'; |
| 39 | + // const parameterVersionId = 'YOUR_PARAMETER_VERSION_ID'; |
| 40 | + // const secretId = 'YOUR_SECRET_ID'; // For example projects/my-project/secrets/application-secret/version/latest |
| 41 | + |
| 42 | + // Imports the Parameter Manager library |
| 43 | + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); |
| 44 | + |
| 45 | + // Instantiates a client |
| 46 | + const client = new ParameterManagerClient(); |
| 47 | + |
| 48 | + async function createParamVersionWithSecret() { |
| 49 | + // Construct the parent resource name |
| 50 | + const parent = client.parameterPath(projectId, 'global', parameterId); |
| 51 | + |
| 52 | + // Construct the JSON data with secret references |
| 53 | + const jsonData = { |
| 54 | + db_user: 'test_user', |
| 55 | + db_password: `__REF__(//secretmanager.googleapis.com/${secretId})`, |
| 56 | + }; |
| 57 | + |
| 58 | + // Construct the parameter version |
| 59 | + const parameterVersion = { |
| 60 | + payload: { |
| 61 | + data: Buffer.from(JSON.stringify(jsonData), 'utf8'), |
| 62 | + }, |
| 63 | + }; |
| 64 | + |
| 65 | + // Construct the request |
| 66 | + const request = { |
| 67 | + parent: parent, |
| 68 | + parameterVersionId: parameterVersionId, |
| 69 | + parameterVersion: parameterVersion, |
| 70 | + }; |
| 71 | + |
| 72 | + // Create the parameter version |
| 73 | + const [response] = await client.createParameterVersion(request); |
| 74 | + console.log( |
| 75 | + `Created parameter version with secret references: ${response.name}` |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + await createParamVersionWithSecret(); |
| 80 | + // [END parametermanager_create_param_version_with_secret] |
| 81 | +} |
| 82 | + |
| 83 | +// Parse command line arguments |
| 84 | +const args = process.argv.slice(2); |
| 85 | +main(...args).catch(console.error); |
0 commit comments