Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parametermanager): add global and regional samples for kms_key field #4059

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions parametermanager/createParamWithKmsKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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 = 'my-project',
parameterId = 'my-parameter',
kmsKey = 'projects/my-project/locations/global/keyRings/my-key-ring/cryptoKeys/my-encryption-key'
) {
// [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}`
);
}

await createParamWithKmsKey();
// [END parametermanager_create_param_with_kms_key]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
29 changes: 29 additions & 0 deletions parametermanager/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "nodejs-parameter-manager-samples",
"private": true,
"license": "Apache-2.0",
"files": [
"*.js"
],
"author": "Google LLC",
"repository": "googleapis/nodejs-parameter-manager",
"engines": {
"node": ">=20"
},
"scripts": {
"test": "c8 mocha --recursive test/ --timeout=800000"
},
"directories": {
"test": "test"
},
"dependencies": {
"@google-cloud/parametermanager": "^0.3.0"
},
"devDependencies": {
"@google-cloud/kms": "^4.0.0",
"c8": "^10.1.3",
"chai": "^4.5.0",
"mocha": "^11.1.0",
"uuid": "^11.0.5"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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 = 'my-project',
locationId = 'us-central1',
parameterId = 'my-parameter',
kmsKey = 'projects/my-project/locations/us-central1/keyRings/my-key-ring/cryptoKeys/my-encryption-key'
) {
// [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}`
);
}

await createRegionalParamWithKmsKey();
// [END parametermanager_create_regional_param_with_kms_key]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
68 changes: 68 additions & 0 deletions parametermanager/regional_samples/removeRegionalParamKmsKey.js
Original file line number Diff line number Diff line change
@@ -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 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 = 'my-project',
locationId = 'us-central1',
parameterId = 'my-parameter'
) {
// [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,
},
Comment on lines +50 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The request object only contains the name in the parameter field. It might be clearer to directly pass the name to the client.updateParameter function, if the API allows it.

updateMask: {
paths: ['kms_key'],
},
};

const [parameter] = await client.updateParameter(request);
console.log(`Removed kms_key for regional parameter ${parameter.name}`);
}

await removeRegionalParamKmsKey();
// [END parametermanager_remove_regional_param_kms_key]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
74 changes: 74 additions & 0 deletions parametermanager/regional_samples/updateRegionalParamKmsKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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 = 'my-project',
locationId = 'us-central1',
parameterId = 'my-parameter',
kmsKey = 'projects/my-project/locations/us-central1/keyRings/my-key-ring/cryptoKeys/my-encryption-key'
) {
// [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,
},
Comment on lines +53 to +57
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The request object contains both the name and kmsKey in the parameter field. It might be clearer to separate these out if the API allows it, for better readability.

updateMask: {
paths: ['kms_key'],
},
};

const [parameter] = await client.updateParameter(request);
console.log(
`Updated regional parameter ${parameter.name} with kms_key ${parameter.kmsKey}`
);
}

await updateRegionalParamKmsKey();
// [END parametermanager_update_regional_param_kms_key]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
57 changes: 57 additions & 0 deletions parametermanager/removeParamKmsKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 = 'my-project', parameterId = 'my-parameter') {
// [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,
},
Comment on lines +39 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The request object only contains the name in the parameter field. It might be clearer to directly pass the name to the client.updateParameter function, if the API allows it.

updateMask: {
paths: ['kms_key'],
},
};

const [parameter] = await client.updateParameter(request);
console.log(`Removed kms_key for parameter ${parameter.name}`);
}

await removeParamKmsKey();
// [END parametermanager_remove_param_kms_key]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
17 changes: 17 additions & 0 deletions parametermanager/test/.eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading