|
| 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 regional parameter with kms_key using the Parameter Manager SDK. |
| 19 | + * |
| 20 | + * @param {string} projectId - The Google Cloud project ID where the parameter is to be created. |
| 21 | + * @param {string} locationId - The ID of the region where parameter is to be created. |
| 22 | + * @param {string} parameterId - The ID of the parameter to create. This ID must be unique within the project. |
| 23 | + * @param {string} kmsKey - The ID of the KMS key to be used for encryption. |
| 24 | + */ |
| 25 | +async function main(projectId, locationId, parameterId, kmsKey) { |
| 26 | + // [START parametermanager_create_regional_param_with_kms_key] |
| 27 | + /** |
| 28 | + * TODO(developer): Uncomment these variables before running the sample. |
| 29 | + */ |
| 30 | + // const projectId = 'YOUR_PROJECT_ID'; |
| 31 | + // const locationId = 'YOUR_LOCATION_ID'; |
| 32 | + // const parameterId = 'YOUR_PARAMETER_ID'; |
| 33 | + // const kmsKey = 'YOUR_KMS_KEY' |
| 34 | + |
| 35 | + // Imports the Parameter Manager library |
| 36 | + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); |
| 37 | + |
| 38 | + // Adding the endpoint to call the regional parameter manager server |
| 39 | + const options = { |
| 40 | + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, |
| 41 | + }; |
| 42 | + |
| 43 | + // Instantiates a client with regional endpoint |
| 44 | + const client = new ParameterManagerClient(options); |
| 45 | + |
| 46 | + async function createRegionalParamWithKmsKey() { |
| 47 | + const parent = client.locationPath(projectId, locationId); |
| 48 | + const request = { |
| 49 | + parent: parent, |
| 50 | + parameterId: parameterId, |
| 51 | + parameter: { |
| 52 | + kmsKey: kmsKey, |
| 53 | + }, |
| 54 | + }; |
| 55 | + |
| 56 | + const [parameter] = await client.createParameter(request); |
| 57 | + console.log( |
| 58 | + `Created regional parameter ${parameter.name} with kms_key ${parameter.kmsKey}` |
| 59 | + ); |
| 60 | + return parameter; |
| 61 | + } |
| 62 | + |
| 63 | + return await createRegionalParamWithKmsKey(); |
| 64 | + // [END parametermanager_create_regional_param_with_kms_key] |
| 65 | +} |
| 66 | +module.exports.main = main; |
| 67 | + |
| 68 | +/* c8 ignore next 10 */ |
| 69 | +if (require.main === module) { |
| 70 | + main(...process.argv.slice(2)).catch(err => { |
| 71 | + console.error(err.message); |
| 72 | + process.exitCode = 1; |
| 73 | + }); |
| 74 | + process.on('unhandledRejection', err => { |
| 75 | + console.error(err.message); |
| 76 | + process.exitCode = 1; |
| 77 | + }); |
| 78 | +} |
0 commit comments