Skip to content

Commit a2a6bb3

Browse files
feat(parametermanager): Added samples for kms_key field in parameter manager (#4071)
* feat(parametermanager): Added samples for kms_key field in parameter manager * fix(parametermanager): update code samples and testcases * fix(parametermanager): update testcase and function arguments --------- Co-authored-by: Katie McLaughlin <[email protected]>
1 parent d513cc7 commit a2a6bb3

9 files changed

+724
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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(projectId, parameterId, kmsKey) {
25+
// [START parametermanager_create_param_with_kms_key]
26+
/**
27+
* TODO(developer): Uncomment these variables before running the sample.
28+
*/
29+
// const projectId = 'YOUR_PROJECT_ID';
30+
// const parameterId = 'YOUR_PARAMETER_ID';
31+
// const kmsKey = 'YOUR_KMS_KEY'
32+
33+
// Imports the Parameter Manager library
34+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
35+
36+
// Instantiates a client
37+
const client = new ParameterManagerClient();
38+
39+
async function createParamWithKmsKey() {
40+
const parent = client.locationPath(projectId, 'global');
41+
const request = {
42+
parent: parent,
43+
parameterId: parameterId,
44+
parameter: {
45+
kmsKey: kmsKey,
46+
},
47+
};
48+
49+
const [parameter] = await client.createParameter(request);
50+
console.log(
51+
`Created parameter ${parameter.name} with kms_key ${parameter.kmsKey}`
52+
);
53+
return parameter;
54+
}
55+
56+
return await createParamWithKmsKey();
57+
// [END parametermanager_create_param_with_kms_key]
58+
}
59+
module.exports.main = main;
60+
61+
/* c8 ignore next 10 */
62+
if (require.main === module) {
63+
main(...process.argv.slice(2)).catch(err => {
64+
console.error(err.message);
65+
process.exitCode = 1;
66+
});
67+
process.on('unhandledRejection', err => {
68+
console.error(err.message);
69+
process.exitCode = 1;
70+
});
71+
}

parametermanager/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
"test": "test"
1818
},
1919
"dependencies": {
20-
"@google-cloud/parametermanager": "^0.1.0"
20+
"@google-cloud/parametermanager": "^0.3.0"
2121
},
2222
"devDependencies": {
23+
"@google-cloud/kms": "^4.0.0",
2324
"@google-cloud/secret-manager": "^5.6.0",
2425
"c8": "^10.1.3",
2526
"chai": "^4.5.0",
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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(projectId, locationId, parameterId) {
25+
// [START parametermanager_remove_regional_param_kms_key]
26+
/**
27+
* TODO(developer): Uncomment these variables before running the sample.
28+
*/
29+
// const projectId = 'YOUR_PROJECT_ID';
30+
// const locationId = 'YOUR_LOCATION_ID';
31+
// const parameterId = 'YOUR_PARAMETER_ID';
32+
33+
// Imports the Parameter Manager library
34+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
35+
36+
// Adding the endpoint to call the regional parameter manager server
37+
const options = {
38+
apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`,
39+
};
40+
41+
// Instantiates a client with regional endpoint
42+
const client = new ParameterManagerClient(options);
43+
44+
async function removeRegionalParamKmsKey() {
45+
const name = client.parameterPath(projectId, locationId, parameterId);
46+
const request = {
47+
parameter: {
48+
name: name,
49+
},
50+
updateMask: {
51+
paths: ['kms_key'],
52+
},
53+
};
54+
55+
const [parameter] = await client.updateParameter(request);
56+
console.log(`Removed kms_key for regional parameter ${parameter.name}`);
57+
return parameter;
58+
}
59+
60+
return await removeRegionalParamKmsKey();
61+
// [END parametermanager_remove_regional_param_kms_key]
62+
}
63+
module.exports.main = main;
64+
65+
/* c8 ignore next 10 */
66+
if (require.main === module) {
67+
main(...process.argv.slice(2)).catch(err => {
68+
console.error(err.message);
69+
process.exitCode = 1;
70+
});
71+
process.on('unhandledRejection', err => {
72+
console.error(err.message);
73+
process.exitCode = 1;
74+
});
75+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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(projectId, locationId, parameterId, kmsKey) {
26+
// [START parametermanager_update_regional_param_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 updateRegionalParamKmsKey() {
47+
const name = client.parameterPath(projectId, locationId, parameterId);
48+
const request = {
49+
parameter: {
50+
name: name,
51+
kmsKey: kmsKey,
52+
},
53+
updateMask: {
54+
paths: ['kms_key'],
55+
},
56+
};
57+
58+
const [parameter] = await client.updateParameter(request);
59+
console.log(
60+
`Updated regional parameter ${parameter.name} with kms_key ${parameter.kmsKey}`
61+
);
62+
return parameter;
63+
}
64+
65+
return await updateRegionalParamKmsKey();
66+
// [END parametermanager_update_regional_param_kms_key]
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+
}

parametermanager/removeParamKmsKey.js

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 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, parameterId) {
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+
return parameter;
51+
}
52+
53+
return await removeParamKmsKey();
54+
// [END parametermanager_remove_param_kms_key]
55+
}
56+
module.exports.main = main;
57+
58+
/* c8 ignore next 10 */
59+
if (require.main === module) {
60+
main(...process.argv.slice(2)).catch(err => {
61+
console.error(err.message);
62+
process.exitCode = 1;
63+
});
64+
process.on('unhandledRejection', err => {
65+
console.error(err.message);
66+
process.exitCode = 1;
67+
});
68+
}

0 commit comments

Comments
 (0)