diff --git a/parametermanager/createParamWithKmsKey.js b/parametermanager/createParamWithKmsKey.js new file mode 100644 index 0000000000..8075d11583 --- /dev/null +++ b/parametermanager/createParamWithKmsKey.js @@ -0,0 +1,71 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Creates a global parameter with kms_key using the Parameter Manager SDK. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is to be created. + * @param {string} parameterId - The ID of the parameter to create. This ID must be unique within the project. + * @param {string} kmsKey - The ID of the KMS key to be used for encryption. + */ +async function main(projectId, parameterId, kmsKey) { + // [START parametermanager_create_param_with_kms_key] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const parameterId = 'YOUR_PARAMETER_ID'; + // const kmsKey = 'YOUR_KMS_KEY' + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Instantiates a client + const client = new ParameterManagerClient(); + + async function createParamWithKmsKey() { + const parent = client.locationPath(projectId, 'global'); + const request = { + parent: parent, + parameterId: parameterId, + parameter: { + kmsKey: kmsKey, + }, + }; + + const [parameter] = await client.createParameter(request); + console.log( + `Created parameter ${parameter.name} with kms_key ${parameter.kmsKey}` + ); + return parameter; + } + + return await createParamWithKmsKey(); + // [END parametermanager_create_param_with_kms_key] +} +module.exports.main = main; + +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/package.json b/parametermanager/package.json index 62cca15a15..51ac77d454 100644 --- a/parametermanager/package.json +++ b/parametermanager/package.json @@ -17,9 +17,10 @@ "test": "test" }, "dependencies": { - "@google-cloud/parametermanager": "^0.1.0" + "@google-cloud/parametermanager": "^0.3.0" }, "devDependencies": { + "@google-cloud/kms": "^4.0.0", "@google-cloud/secret-manager": "^5.6.0", "c8": "^10.1.3", "chai": "^4.5.0", diff --git a/parametermanager/regional_samples/createRegionalParamWithKmsKey.js b/parametermanager/regional_samples/createRegionalParamWithKmsKey.js new file mode 100644 index 0000000000..4e2bdc39ad --- /dev/null +++ b/parametermanager/regional_samples/createRegionalParamWithKmsKey.js @@ -0,0 +1,78 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Creates a regional parameter with kms_key using the Parameter Manager SDK. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is to be created. + * @param {string} locationId - The ID of the region where parameter is to be created. + * @param {string} parameterId - The ID of the parameter to create. This ID must be unique within the project. + * @param {string} kmsKey - The ID of the KMS key to be used for encryption. + */ +async function main(projectId, locationId, parameterId, kmsKey) { + // [START parametermanager_create_regional_param_with_kms_key] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const locationId = 'YOUR_LOCATION_ID'; + // const parameterId = 'YOUR_PARAMETER_ID'; + // const kmsKey = 'YOUR_KMS_KEY' + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Adding the endpoint to call the regional parameter manager server + const options = { + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, + }; + + // Instantiates a client with regional endpoint + const client = new ParameterManagerClient(options); + + async function createRegionalParamWithKmsKey() { + const parent = client.locationPath(projectId, locationId); + const request = { + parent: parent, + parameterId: parameterId, + parameter: { + kmsKey: kmsKey, + }, + }; + + const [parameter] = await client.createParameter(request); + console.log( + `Created regional parameter ${parameter.name} with kms_key ${parameter.kmsKey}` + ); + return parameter; + } + + return await createRegionalParamWithKmsKey(); + // [END parametermanager_create_regional_param_with_kms_key] +} +module.exports.main = main; + +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/regional_samples/removeRegionalParamKmsKey.js b/parametermanager/regional_samples/removeRegionalParamKmsKey.js new file mode 100644 index 0000000000..b41d19ab0c --- /dev/null +++ b/parametermanager/regional_samples/removeRegionalParamKmsKey.js @@ -0,0 +1,75 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Removes a kms_key for regional parameter using the Parameter Manager SDK. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is to be updated. + * @param {string} locationId - The ID of the region where parameter is to be updated. + * @param {string} parameterId - The ID of the parameter to update. This ID must be unique within the project. + */ +async function main(projectId, locationId, parameterId) { + // [START parametermanager_remove_regional_param_kms_key] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const locationId = 'YOUR_LOCATION_ID'; + // const parameterId = 'YOUR_PARAMETER_ID'; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Adding the endpoint to call the regional parameter manager server + const options = { + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, + }; + + // Instantiates a client with regional endpoint + const client = new ParameterManagerClient(options); + + async function removeRegionalParamKmsKey() { + const name = client.parameterPath(projectId, locationId, parameterId); + const request = { + parameter: { + name: name, + }, + updateMask: { + paths: ['kms_key'], + }, + }; + + const [parameter] = await client.updateParameter(request); + console.log(`Removed kms_key for regional parameter ${parameter.name}`); + return parameter; + } + + return await removeRegionalParamKmsKey(); + // [END parametermanager_remove_regional_param_kms_key] +} +module.exports.main = main; + +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/regional_samples/updateRegionalParamKmsKey.js b/parametermanager/regional_samples/updateRegionalParamKmsKey.js new file mode 100644 index 0000000000..f55abe10c4 --- /dev/null +++ b/parametermanager/regional_samples/updateRegionalParamKmsKey.js @@ -0,0 +1,80 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Updates a regional parameter with kms_key using the Parameter Manager SDK. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is to be updated. + * @param {string} locationId - The ID of the region where parameter is to be updated. + * @param {string} parameterId - The ID of the parameter to update. This ID must be unique within the project. + * @param {string} kmsKey - The ID of the KMS key to be used for encryption. + */ +async function main(projectId, locationId, parameterId, kmsKey) { + // [START parametermanager_update_regional_param_kms_key] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const locationId = 'YOUR_LOCATION_ID'; + // const parameterId = 'YOUR_PARAMETER_ID'; + // const kmsKey = 'YOUR_KMS_KEY' + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Adding the endpoint to call the regional parameter manager server + const options = { + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, + }; + + // Instantiates a client with regional endpoint + const client = new ParameterManagerClient(options); + + async function updateRegionalParamKmsKey() { + const name = client.parameterPath(projectId, locationId, parameterId); + const request = { + parameter: { + name: name, + kmsKey: kmsKey, + }, + updateMask: { + paths: ['kms_key'], + }, + }; + + const [parameter] = await client.updateParameter(request); + console.log( + `Updated regional parameter ${parameter.name} with kms_key ${parameter.kmsKey}` + ); + return parameter; + } + + return await updateRegionalParamKmsKey(); + // [END parametermanager_update_regional_param_kms_key] +} +module.exports.main = main; + +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/removeParamKmsKey.js b/parametermanager/removeParamKmsKey.js new file mode 100644 index 0000000000..c8f811e7dc --- /dev/null +++ b/parametermanager/removeParamKmsKey.js @@ -0,0 +1,68 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Removes a kms_key for global parameter using the Parameter Manager SDK. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is to be updated. + * @param {string} parameterId - The ID of the parameter to update. This ID must be unique within the project. + */ +async function main(projectId, parameterId) { + // [START parametermanager_remove_param_kms_key] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const parameterId = 'YOUR_PARAMETER_ID'; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Instantiates a client + const client = new ParameterManagerClient(); + + async function removeParamKmsKey() { + const name = client.parameterPath(projectId, 'global', parameterId); + const request = { + parameter: { + name: name, + }, + updateMask: { + paths: ['kms_key'], + }, + }; + + const [parameter] = await client.updateParameter(request); + console.log(`Removed kms_key for parameter ${parameter.name}`); + return parameter; + } + + return await removeParamKmsKey(); + // [END parametermanager_remove_param_kms_key] +} +module.exports.main = main; + +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/test/.eslintrc.yaml b/parametermanager/test/.eslintrc.yaml new file mode 100644 index 0000000000..74add4846e --- /dev/null +++ b/parametermanager/test/.eslintrc.yaml @@ -0,0 +1,17 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +env: + mocha: true diff --git a/parametermanager/test/parametermanager.test.js b/parametermanager/test/parametermanager.test.js index 01d3661e98..713d700d23 100644 --- a/parametermanager/test/parametermanager.test.js +++ b/parametermanager/test/parametermanager.test.js @@ -23,31 +23,57 @@ const client = new ParameterManagerClient(); const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); const secretClient = new SecretManagerServiceClient(); +const {KeyManagementServiceClient} = require('@google-cloud/kms'); +const kmsClient = new KeyManagementServiceClient(); + let projectId; const locationId = process.env.GCLOUD_LOCATION || 'us-central1'; const options = {}; options.apiEndpoint = `parametermanager.${locationId}.rep.googleapis.com`; +const regionalClient = new ParameterManagerClient(options); + const secretOptions = {}; secretOptions.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`; const secretId = `test-secret-${uuidv4()}`; const parameterId = `test-parameter-${uuidv4()}`; +const regionalParameterId = `test-regional-${uuidv4()}`; const parameterVersionId = `test-version-${uuidv4()}`; +const keyRingId = 'node-test-kms-key'; +const keyId = `test-parameter-${uuidv4()}`; +const keyId1 = `test-parameter-${uuidv4()}`; + const jsonPayload = '{username: "test-user", host: "localhost"}'; const payload = 'This is unstructured data'; let parameter; +let regionalParameter; let secret; let secretVersion; +let keyRing; +let kmsKey; +let kmsKey1; + +let regionalKeyRing; +let regionalKmsKey; +let regionalKmsKey1; + describe('Parameter Manager samples', () => { const parametersToDelete = []; const parameterVersionsToDelete = []; + const regionalParametersToDelete = []; before(async () => { projectId = await client.getProjectId(); + keyRing = `projects/${projectId}/locations/global/keyRings/${keyRingId}`; + kmsKey = `projects/${projectId}/locations/global/keyRings/${keyRingId}/cryptoKeys/${keyId}`; + kmsKey1 = `projects/${projectId}/locations/global/keyRings/${keyRingId}/cryptoKeys/${keyId1}`; + regionalKeyRing = `projects/${projectId}/locations/${locationId}/keyRings/${keyRingId}`; + regionalKmsKey = `projects/${projectId}/locations/${locationId}/keyRings/${keyRingId}/cryptoKeys/${keyId}`; + regionalKmsKey1 = `projects/${projectId}/locations/${locationId}/keyRings/${keyRingId}/cryptoKeys/${keyId1}`; // Create a secret [secret] = await secretClient.createSecret({ @@ -77,6 +103,110 @@ describe('Parameter Manager samples', () => { }, }); parametersToDelete.push(parameter.name); + + // Create a test regional parameter + [regionalParameter] = await regionalClient.createParameter({ + parent: `projects/${projectId}/locations/${locationId}`, + parameterId: regionalParameterId, + parameter: { + format: 'JSON', + }, + }); + regionalParametersToDelete.push(regionalParameter.name); + + try { + await kmsClient.getKeyRing({name: keyRing}); + } catch (error) { + if (error.code === 5) { + await kmsClient.createKeyRing({ + parent: kmsClient.locationPath(projectId, 'global'), + keyRingId: keyRingId, + }); + } + } + + try { + await kmsClient.getKeyRing({name: regionalKeyRing}); + } catch (error) { + if (error.code === 5) { + await kmsClient.createKeyRing({ + parent: kmsClient.locationPath(projectId, locationId), + keyRingId: keyRingId, + }); + } + } + + try { + await kmsClient.getCryptoKey({name: kmsKey}); + } catch (error) { + if (error.code === 5) { + await kmsClient.createCryptoKey({ + parent: kmsClient.keyRingPath(projectId, 'global', keyRingId), + cryptoKeyId: keyId, + cryptoKey: { + purpose: 'ENCRYPT_DECRYPT', + versionTemplate: { + algorithm: 'GOOGLE_SYMMETRIC_ENCRYPTION', + protectionLevel: 'HSM', + }, + }, + }); + } + } + + try { + await kmsClient.getCryptoKey({name: regionalKmsKey}); + } catch (error) { + if (error.code === 5) { + await kmsClient.createCryptoKey({ + parent: kmsClient.keyRingPath(projectId, locationId, keyRingId), + cryptoKeyId: keyId, + cryptoKey: { + purpose: 'ENCRYPT_DECRYPT', + versionTemplate: { + algorithm: 'GOOGLE_SYMMETRIC_ENCRYPTION', + protectionLevel: 'HSM', + }, + }, + }); + } + } + + try { + await kmsClient.getCryptoKey({name: kmsKey1}); + } catch (error) { + if (error.code === 5) { + await kmsClient.createCryptoKey({ + parent: kmsClient.keyRingPath(projectId, 'global', keyRingId), + cryptoKeyId: keyId1, + cryptoKey: { + purpose: 'ENCRYPT_DECRYPT', + versionTemplate: { + algorithm: 'GOOGLE_SYMMETRIC_ENCRYPTION', + protectionLevel: 'HSM', + }, + }, + }); + } + } + + try { + await kmsClient.getCryptoKey({name: regionalKmsKey1}); + } catch (error) { + if (error.code === 5) { + await kmsClient.createCryptoKey({ + parent: kmsClient.keyRingPath(projectId, locationId, keyRingId), + cryptoKeyId: keyId1, + cryptoKey: { + purpose: 'ENCRYPT_DECRYPT', + versionTemplate: { + algorithm: 'GOOGLE_SYMMETRIC_ENCRYPTION', + protectionLevel: 'HSM', + }, + }, + }); + } + } }); after(async () => { @@ -119,6 +249,56 @@ describe('Parameter Manager samples', () => { throw err; } } + + regionalParametersToDelete.forEach(async regionalParameterName => { + try { + await regionalClient.deleteParameter({name: regionalParameterName}); + } catch (err) { + if (!err.message.includes('NOT_FOUND')) { + throw err; + } + } + }); + + try { + await kmsClient.destroyCryptoKeyVersion({ + name: `${kmsKey}/cryptoKeyVersions/1`, + }); + } catch (error) { + if (error.code === 5) { + // If the method is not found, skip it. + } + } + + try { + await kmsClient.destroyCryptoKeyVersion({ + name: `${kmsKey1}/cryptoKeyVersions/1`, + }); + } catch (error) { + if (error.code === 5) { + // If the method is not found, skip it. + } + } + + try { + await kmsClient.destroyCryptoKeyVersion({ + name: `${regionalKmsKey}/cryptoKeyVersions/1`, + }); + } catch (error) { + if (error.code === 5) { + // If the method is not found, skip it. + } + } + + try { + await kmsClient.destroyCryptoKeyVersion({ + name: `${regionalKmsKey1}/cryptoKeyVersions/1`, + }); + } catch (error) { + if (error.code === 5) { + // If the method is not found, skip it. + } + } }); it('should create parameter version with secret references', async () => { @@ -253,4 +433,84 @@ describe('Parameter Manager samples', () => { ); assert.exists(parameterVersion); }); + + it('should create a parameter with kms_key', async () => { + const sample = require('../createParamWithKmsKey'); + const parameter = await sample.main(projectId, parameterId + '-4', kmsKey); + parametersToDelete.push( + `projects/${projectId}/locations/global/parameters/${parameterId}-4` + ); + assert.exists(parameter); + assert.equal( + parameter.name, + `projects/${projectId}/locations/global/parameters/${parameterId}-4` + ); + }); + + it('should create a regional parameter with kms_key', async () => { + const sample = require('../regional_samples/createRegionalParamWithKmsKey'); + const parameter = await sample.main( + projectId, + locationId, + regionalParameterId + '-4', + regionalKmsKey + ); + regionalParametersToDelete.push( + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-4` + ); + assert.exists(parameter); + assert.equal( + parameter.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-4` + ); + }); + + it('should update a parameter with kms_key', async () => { + const sample = require('../updateParamKmsKey'); + const parameter = await sample.main(projectId, parameterId, kmsKey); + assert.exists(parameter); + assert.equal( + parameter.name, + `projects/${projectId}/locations/global/parameters/${parameterId}` + ); + }); + + it('should update a regional parameter with kms_key', async () => { + const sample = require('../regional_samples/updateRegionalParamKmsKey'); + const parameter = await sample.main( + projectId, + locationId, + regionalParameterId, + regionalKmsKey + ); + assert.exists(parameter); + assert.equal( + parameter.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}` + ); + }); + + it('should remove a kms_key for parameter', async () => { + const sample = require('../removeParamKmsKey'); + const parameter = await sample.main(projectId, parameterId); + assert.exists(parameter); + assert.equal( + parameter.name, + `projects/${projectId}/locations/global/parameters/${parameterId}` + ); + }); + + it('should remove a kms_key for regional parameter', async () => { + const sample = require('../regional_samples/removeRegionalParamKmsKey'); + const parameter = await sample.main( + projectId, + locationId, + regionalParameterId + ); + assert.exists(parameter); + assert.equal( + parameter.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}` + ); + }); }); diff --git a/parametermanager/updateParamKmsKey.js b/parametermanager/updateParamKmsKey.js new file mode 100644 index 0000000000..931be10c4c --- /dev/null +++ b/parametermanager/updateParamKmsKey.js @@ -0,0 +1,73 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Updates a global parameter with kms_key using the Parameter Manager SDK. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is to be updated. + * @param {string} parameterId - The ID of the parameter to update. This ID must be unique within the project. + * @param {string} kmsKey - The ID of the KMS key to be used for encryption. + */ +async function main(projectId, parameterId, kmsKey) { + // [START parametermanager_update_param_kms_key] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const parameterId = 'YOUR_PARAMETER_ID'; + // const kmsKey = 'YOUR_KMS_KEY' + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Instantiates a client + const client = new ParameterManagerClient(); + + async function updateParamKmsKey() { + const name = client.parameterPath(projectId, 'global', parameterId); + const request = { + parameter: { + name: name, + kmsKey: kmsKey, + }, + updateMask: { + paths: ['kms_key'], + }, + }; + + const [parameter] = await client.updateParameter(request); + console.log( + `Updated parameter ${parameter.name} with kms_key ${parameter.kmsKey}` + ); + return parameter; + } + + return await updateParamKmsKey(); + // [END parametermanager_update_param_kms_key] +} +module.exports.main = main; + +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +}