|
| 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(projectId, parameterId, parameterVersionId, secretId) { |
| 28 | + // [START parametermanager_create_param_version_with_secret] |
| 29 | + /** |
| 30 | + * TODO(developer): Uncomment these variables before running the sample. |
| 31 | + */ |
| 32 | + // const projectId = 'YOUR_PROJECT_ID'; |
| 33 | + // const parameterId = 'YOUR_PARAMETER_ID'; |
| 34 | + // const parameterVersionId = 'YOUR_PARAMETER_VERSION_ID'; |
| 35 | + // const secretId = 'YOUR_SECRET_ID'; // For example projects/my-project/secrets/application-secret/version/latest |
| 36 | + |
| 37 | + // Imports the Parameter Manager library |
| 38 | + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); |
| 39 | + |
| 40 | + // Instantiates a client |
| 41 | + const client = new ParameterManagerClient(); |
| 42 | + |
| 43 | + async function createParamVersionWithSecret() { |
| 44 | + // Construct the parent resource name |
| 45 | + const parent = client.parameterPath(projectId, 'global', parameterId); |
| 46 | + |
| 47 | + // Construct the JSON data with secret references |
| 48 | + const jsonData = { |
| 49 | + db_user: 'test_user', |
| 50 | + db_password: `__REF__(//secretmanager.googleapis.com/${secretId})`, |
| 51 | + }; |
| 52 | + |
| 53 | + // Construct the parameter version |
| 54 | + const parameterVersion = { |
| 55 | + payload: { |
| 56 | + data: Buffer.from(JSON.stringify(jsonData), 'utf8'), |
| 57 | + }, |
| 58 | + }; |
| 59 | + |
| 60 | + // Construct the request |
| 61 | + const request = { |
| 62 | + parent: parent, |
| 63 | + parameterVersionId: parameterVersionId, |
| 64 | + parameterVersion: parameterVersion, |
| 65 | + }; |
| 66 | + |
| 67 | + // Create the parameter version |
| 68 | + const [paramVersion] = await client.createParameterVersion(request); |
| 69 | + console.log( |
| 70 | + `Created parameter version with secret references: ${paramVersion.name}` |
| 71 | + ); |
| 72 | + return paramVersion; |
| 73 | + } |
| 74 | + |
| 75 | + return await createParamVersionWithSecret(); |
| 76 | + // [END parametermanager_create_param_version_with_secret] |
| 77 | +} |
| 78 | +module.exports.main = main; |
| 79 | + |
| 80 | +/* c8 ignore next 10 */ |
| 81 | +if (require.main === module) { |
| 82 | + main(...process.argv.slice(2)).catch(err => { |
| 83 | + console.error(err.message); |
| 84 | + process.exitCode = 1; |
| 85 | + }); |
| 86 | + process.on('unhandledRejection', err => { |
| 87 | + console.error(err.message); |
| 88 | + process.exitCode = 1; |
| 89 | + }); |
| 90 | +} |
0 commit comments