|
| 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 parameter version globally for unstructured data. |
| 19 | + * |
| 20 | + * @param {string} projectId - The Google Cloud project ID where the parameter is to be created |
| 21 | + * @param {string} parameterId - The ID of the parameter for which the version is to be created. |
| 22 | + * @param {string} parameterVersionId - The ID of the parameter version to be created. |
| 23 | + * @param {string} payload - The unformatted string payload to be stored in the new parameter version. |
| 24 | + */ |
| 25 | +async function main(projectId, parameterId, parameterVersionId, payload) { |
| 26 | + // [START parametermanager_create_param_version] |
| 27 | + /** |
| 28 | + * TODO(developer): Uncomment these variables before running the sample. |
| 29 | + */ |
| 30 | + // const projectId = 'YOUR_PROJECT_ID'; |
| 31 | + // const parameterId = 'YOUR_PARAMETER_ID'; |
| 32 | + // const parameterVersionId = 'YOUR_PARAMETER_VERSION_ID'; |
| 33 | + // const payload = 'This is unstructured data'; |
| 34 | + |
| 35 | + // Imports the Parameter Manager library |
| 36 | + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); |
| 37 | + |
| 38 | + // Instantiates a client |
| 39 | + const client = new ParameterManagerClient(); |
| 40 | + |
| 41 | + async function createParamVersion() { |
| 42 | + // Construct the parent resource name |
| 43 | + const parent = client.parameterPath(projectId, 'global', parameterId); |
| 44 | + |
| 45 | + // Construct the parameter version |
| 46 | + const parameterVersion = { |
| 47 | + payload: { |
| 48 | + data: Buffer.from(payload, 'utf8'), |
| 49 | + }, |
| 50 | + }; |
| 51 | + |
| 52 | + // Construct the request |
| 53 | + const request = { |
| 54 | + parent: parent, |
| 55 | + parameterVersionId: parameterVersionId, |
| 56 | + parameterVersion: parameterVersion, |
| 57 | + }; |
| 58 | + |
| 59 | + // Create the parameter version |
| 60 | + const [paramVersion] = await client.createParameterVersion(request); |
| 61 | + console.log(`Created parameter version: ${paramVersion.name}`); |
| 62 | + return paramVersion; |
| 63 | + } |
| 64 | + |
| 65 | + return await createParamVersion(); |
| 66 | + // [END parametermanager_create_param_version] |
| 67 | +} |
| 68 | +module.exports.main = main; |
| 69 | + |
| 70 | +/* c8 ignore next 10 */ |
| 71 | +if (require.main === module) { |
| 72 | + main(...process.argv.slice(2)).catch(err => { |
| 73 | + console.error(err.message); |
| 74 | + process.exitCode = 1; |
| 75 | + }); |
| 76 | + process.on('unhandledRejection', err => { |
| 77 | + console.error(err.message); |
| 78 | + process.exitCode = 1; |
| 79 | + }); |
| 80 | +} |
0 commit comments