Skip to content

Commit 08aef70

Browse files
feat(parametermanager): add global and regional samples for kms_key field
1 parent dc572e0 commit 08aef70

File tree

9 files changed

+735
-0
lines changed

9 files changed

+735
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 global 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} parameterId - The ID of the parameter to create. This ID must be unique within the project.
22+
* @param {string} kmsKey - The ID of the KMS key to be used for encryption.
23+
*/
24+
async function main(
25+
projectId = 'my-project',
26+
parameterId = 'my-parameter',
27+
kmsKey = 'projects/my-project/locations/global/keyRings/my-key-ring/cryptoKeys/my-encryption-key'
28+
) {
29+
// [START parametermanager_create_param_with_kms_key]
30+
/**
31+
* TODO(developer): Uncomment these variables before running the sample.
32+
*/
33+
// const projectId = 'YOUR_PROJECT_ID';
34+
// const parameterId = 'YOUR_PARAMETER_ID';
35+
// const kmsKey = 'YOUR_KMS_KEY'
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 createParamWithKmsKey() {
44+
const parent = client.locationPath(projectId, 'global');
45+
const request = {
46+
parent: parent,
47+
parameterId: parameterId,
48+
parameter: {
49+
kmsKey: kmsKey,
50+
},
51+
};
52+
53+
const [parameter] = await client.createParameter(request);
54+
console.log(
55+
`Created parameter ${parameter.name} with kms_key ${parameter.kmsKey}`
56+
);
57+
}
58+
59+
await createParamWithKmsKey();
60+
// [END parametermanager_create_param_with_kms_key]
61+
}
62+
63+
const args = process.argv.slice(2);
64+
main(...args).catch(console.error);

parametermanager/package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "nodejs-parameter-manager-samples",
3+
"private": true,
4+
"license": "Apache-2.0",
5+
"files": [
6+
"*.js"
7+
],
8+
"author": "Google LLC",
9+
"repository": "googleapis/nodejs-parameter-manager",
10+
"engines": {
11+
"node": ">=20"
12+
},
13+
"scripts": {
14+
"test": "c8 mocha --recursive test/ --timeout=800000"
15+
},
16+
"directories": {
17+
"test": "test"
18+
},
19+
"dependencies": {
20+
"@google-cloud/parametermanager": "^0.3.0"
21+
},
22+
"devDependencies": {
23+
"@google-cloud/kms": "^4.0.0",
24+
"c8": "^10.1.3",
25+
"chai": "^4.5.0",
26+
"mocha": "^11.1.0",
27+
"uuid": "^11.0.5"
28+
}
29+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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(
26+
projectId = 'my-project',
27+
locationId = 'us-central1',
28+
parameterId = 'my-parameter',
29+
kmsKey = 'projects/my-project/locations/us-central1/keyRings/my-key-ring/cryptoKeys/my-encryption-key'
30+
) {
31+
// [START parametermanager_create_regional_param_with_kms_key]
32+
/**
33+
* TODO(developer): Uncomment these variables before running the sample.
34+
*/
35+
// const projectId = 'YOUR_PROJECT_ID';
36+
// const locationId = 'YOUR_LOCATION_ID';
37+
// const parameterId = 'YOUR_PARAMETER_ID';
38+
// const kmsKey = 'YOUR_KMS_KEY'
39+
40+
// Imports the Parameter Manager library
41+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
42+
43+
// Adding the endpoint to call the regional parameter manager server
44+
const options = {
45+
apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`,
46+
};
47+
48+
// Instantiates a client with regional endpoint
49+
const client = new ParameterManagerClient(options);
50+
51+
async function createRegionalParamWithKmsKey() {
52+
const parent = client.locationPath(projectId, locationId);
53+
const request = {
54+
parent: parent,
55+
parameterId: parameterId,
56+
parameter: {
57+
kmsKey: kmsKey,
58+
},
59+
};
60+
61+
const [parameter] = await client.createParameter(request);
62+
console.log(
63+
`Created regional parameter ${parameter.name} with kms_key ${parameter.kmsKey}`
64+
);
65+
}
66+
67+
await createRegionalParamWithKmsKey();
68+
// [END parametermanager_create_regional_param_with_kms_key]
69+
}
70+
71+
const args = process.argv.slice(2);
72+
main(...args).catch(console.error);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
* Removes a kms_key for regional parameter using the Parameter Manager SDK.
19+
*
20+
* @param {string} projectId - The Google Cloud project ID where the parameter is to be updated.
21+
* @param {string} locationId - The ID of the region where parameter is to be updated.
22+
* @param {string} parameterId - The ID of the parameter to update. This ID must be unique within the project.
23+
*/
24+
async function main(
25+
projectId = 'my-project',
26+
locationId = 'us-central1',
27+
parameterId = 'my-parameter'
28+
) {
29+
// [START parametermanager_remove_regional_param_kms_key]
30+
/**
31+
* TODO(developer): Uncomment these variables before running the sample.
32+
*/
33+
// const projectId = 'YOUR_PROJECT_ID';
34+
// const locationId = 'YOUR_LOCATION_ID';
35+
// const parameterId = 'YOUR_PARAMETER_ID';
36+
37+
// Imports the Parameter Manager library
38+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
39+
40+
// Adding the endpoint to call the regional parameter manager server
41+
const options = {
42+
apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`,
43+
};
44+
45+
// Instantiates a client with regional endpoint
46+
const client = new ParameterManagerClient(options);
47+
48+
async function removeRegionalParamKmsKey() {
49+
const name = client.parameterPath(projectId, locationId, parameterId);
50+
const request = {
51+
parameter: {
52+
name: name,
53+
},
54+
updateMask: {
55+
paths: ['kms_key'],
56+
},
57+
};
58+
59+
const [parameter] = await client.updateParameter(request);
60+
console.log(`Removed kms_key for regional parameter ${parameter.name}`);
61+
}
62+
63+
await removeRegionalParamKmsKey();
64+
// [END parametermanager_remove_regional_param_kms_key]
65+
}
66+
67+
const args = process.argv.slice(2);
68+
main(...args).catch(console.error);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
* Updates 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 updated.
21+
* @param {string} locationId - The ID of the region where parameter is to be updated.
22+
* @param {string} parameterId - The ID of the parameter to update. 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(
26+
projectId = 'my-project',
27+
locationId = 'us-central1',
28+
parameterId = 'my-parameter',
29+
kmsKey = 'projects/my-project/locations/us-central1/keyRings/my-key-ring/cryptoKeys/my-encryption-key'
30+
) {
31+
// [START parametermanager_update_regional_param_kms_key]
32+
/**
33+
* TODO(developer): Uncomment these variables before running the sample.
34+
*/
35+
// const projectId = 'YOUR_PROJECT_ID';
36+
// const locationId = 'YOUR_LOCATION_ID';
37+
// const parameterId = 'YOUR_PARAMETER_ID';
38+
// const kmsKey = 'YOUR_KMS_KEY'
39+
40+
// Imports the Parameter Manager library
41+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
42+
43+
// Adding the endpoint to call the regional parameter manager server
44+
const options = {
45+
apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`,
46+
};
47+
48+
// Instantiates a client with regional endpoint
49+
const client = new ParameterManagerClient(options);
50+
51+
async function updateRegionalParamKmsKey() {
52+
const name = client.parameterPath(projectId, locationId, parameterId);
53+
const request = {
54+
parameter: {
55+
name: name,
56+
kmsKey: kmsKey,
57+
},
58+
updateMask: {
59+
paths: ['kms_key'],
60+
},
61+
};
62+
63+
const [parameter] = await client.updateParameter(request);
64+
console.log(
65+
`Updated regional parameter ${parameter.name} with kms_key ${parameter.kmsKey}`
66+
);
67+
}
68+
69+
await updateRegionalParamKmsKey();
70+
// [END parametermanager_update_regional_param_kms_key]
71+
}
72+
73+
const args = process.argv.slice(2);
74+
main(...args).catch(console.error);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
* Removes a kms_key for global parameter using the Parameter Manager SDK.
19+
*
20+
* @param {string} projectId - The Google Cloud project ID where the parameter is to be updated.
21+
* @param {string} parameterId - The ID of the parameter to update. This ID must be unique within the project.
22+
*/
23+
async function main(projectId = 'my-project', parameterId = 'my-parameter') {
24+
// [START parametermanager_remove_param_kms_key]
25+
/**
26+
* TODO(developer): Uncomment these variables before running the sample.
27+
*/
28+
// const projectId = 'YOUR_PROJECT_ID';
29+
// const parameterId = 'YOUR_PARAMETER_ID';
30+
31+
// Imports the Parameter Manager library
32+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
33+
34+
// Instantiates a client
35+
const client = new ParameterManagerClient();
36+
37+
async function removeParamKmsKey() {
38+
const name = client.parameterPath(projectId, 'global', parameterId);
39+
const request = {
40+
parameter: {
41+
name: name,
42+
},
43+
updateMask: {
44+
paths: ['kms_key'],
45+
},
46+
};
47+
48+
const [parameter] = await client.updateParameter(request);
49+
console.log(`Removed kms_key for parameter ${parameter.name}`);
50+
}
51+
52+
await removeParamKmsKey();
53+
// [END parametermanager_remove_param_kms_key]
54+
}
55+
56+
const args = process.argv.slice(2);
57+
main(...args).catch(console.error);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
---
16+
env:
17+
mocha: true

0 commit comments

Comments
 (0)