Skip to content

Commit 47ab9d6

Browse files
feat(parametermanager): Added samples for create, get, list and render global parameter & parameter version (#4068)
* feat(parametermanager): Added samples for create, get, list and render global parameter & parameter version * fix(parametermanager): update samples and testcases * fix(parametermanager): fix linting issue * fix(parametermanager): update test cases * fix(parametermanager): update testcases and function arguments * fix(parametermanager): fix lint issue * fix(parametermanager): update testcase --------- Co-authored-by: Katie McLaughlin <[email protected]>
1 parent 2e46a66 commit 47ab9d6

13 files changed

+1071
-0
lines changed

parametermanager/createParam.js

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 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+
*/
23+
async function main(projectId, parameterId) {
24+
// [START parametermanager_create_param]
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 createParam() {
38+
const parent = client.locationPath(projectId, 'global');
39+
const request = {
40+
parent: parent,
41+
parameterId: parameterId,
42+
};
43+
44+
const [parameter] = await client.createParameter(request);
45+
console.log(`Created parameter: ${parameter.name}`);
46+
return parameter;
47+
}
48+
49+
return await createParam();
50+
// [END parametermanager_create_param]
51+
}
52+
module.exports.main = main;
53+
54+
/* c8 ignore next 10 */
55+
if (require.main === module) {
56+
main(...process.argv.slice(2)).catch(err => {
57+
console.error(err.message);
58+
process.exitCode = 1;
59+
});
60+
process.on('unhandledRejection', err => {
61+
console.error(err.message);
62+
process.exitCode = 1;
63+
});
64+
}
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+
* Creates a parameter version globally for unstructured data.
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 for which the version is to be created.
22+
* @param {string} parameterVersionId - The ID of the parameter version to be created.
23+
* @param {string} payload - The unformatted string payload to be stored in the new parameter version.
24+
*/
25+
async function main(projectId, parameterId, parameterVersionId, payload) {
26+
// [START parametermanager_create_param_version]
27+
/**
28+
* TODO(developer): Uncomment these variables before running the sample.
29+
*/
30+
// const projectId = 'YOUR_PROJECT_ID';
31+
// const parameterId = 'YOUR_PARAMETER_ID';
32+
// const parameterVersionId = 'YOUR_PARAMETER_VERSION_ID';
33+
// const payload = 'This is unstructured data';
34+
35+
// Imports the Parameter Manager library
36+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
37+
38+
// Instantiates a client
39+
const client = new ParameterManagerClient();
40+
41+
async function createParamVersion() {
42+
// Construct the parent resource name
43+
const parent = client.parameterPath(projectId, 'global', parameterId);
44+
45+
// Construct the parameter version
46+
const parameterVersion = {
47+
payload: {
48+
data: Buffer.from(payload, 'utf8'),
49+
},
50+
};
51+
52+
// Construct the request
53+
const request = {
54+
parent: parent,
55+
parameterVersionId: parameterVersionId,
56+
parameterVersion: parameterVersion,
57+
};
58+
59+
// Create the parameter version
60+
const [paramVersion] = await client.createParameterVersion(request);
61+
console.log(`Created parameter version: ${paramVersion.name}`);
62+
return paramVersion;
63+
}
64+
65+
return await createParamVersion();
66+
// [END parametermanager_create_param_version]
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+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 new version of an existing parameter in the global location
19+
* of the specified project using the Google Cloud Parameter Manager SDK.
20+
* The payload is specified as a JSON string and includes a reference to a secret.
21+
*
22+
* @param {string} projectId - The Google Cloud project ID where the parameter is located.
23+
* @param {string} parameterId - The ID of the parameter for which the version is to be created.
24+
* @param {string} parameterVersionId - The ID of the parameter version to be created.
25+
* @param {string} secretId - The ID of the secret to be referenced.
26+
*/
27+
async function main(projectId, parameterId, parameterVersionId, secretId) {
28+
// [START parametermanager_create_param_version_with_secret]
29+
/**
30+
* TODO(developer): Uncomment these variables before running the sample.
31+
*/
32+
// const projectId = 'YOUR_PROJECT_ID';
33+
// const parameterId = 'YOUR_PARAMETER_ID';
34+
// const parameterVersionId = 'YOUR_PARAMETER_VERSION_ID';
35+
// const secretId = 'YOUR_SECRET_ID'; // For example projects/my-project/secrets/application-secret/version/latest
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 createParamVersionWithSecret() {
44+
// Construct the parent resource name
45+
const parent = client.parameterPath(projectId, 'global', parameterId);
46+
47+
// Construct the JSON data with secret references
48+
const jsonData = {
49+
db_user: 'test_user',
50+
db_password: `__REF__(//secretmanager.googleapis.com/${secretId})`,
51+
};
52+
53+
// Construct the parameter version
54+
const parameterVersion = {
55+
payload: {
56+
data: Buffer.from(JSON.stringify(jsonData), 'utf8'),
57+
},
58+
};
59+
60+
// Construct the request
61+
const request = {
62+
parent: parent,
63+
parameterVersionId: parameterVersionId,
64+
parameterVersion: parameterVersion,
65+
};
66+
67+
// Create the parameter version
68+
const [paramVersion] = await client.createParameterVersion(request);
69+
console.log(
70+
`Created parameter version with secret references: ${paramVersion.name}`
71+
);
72+
return paramVersion;
73+
}
74+
75+
return await createParamVersionWithSecret();
76+
// [END parametermanager_create_param_version_with_secret]
77+
}
78+
module.exports.main = main;
79+
80+
/* c8 ignore next 10 */
81+
if (require.main === module) {
82+
main(...process.argv.slice(2)).catch(err => {
83+
console.error(err.message);
84+
process.exitCode = 1;
85+
});
86+
process.on('unhandledRejection', err => {
87+
console.error(err.message);
88+
process.exitCode = 1;
89+
});
90+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 parameter in the global location of the specified
19+
* project with specified format using the Google Cloud Parameter Manager SDK.
20+
*
21+
* @param {string} projectId - The Google Cloud project ID where the 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} formatType - The format type of the parameter (UNFORMATTED, YAML, JSON).
24+
*/
25+
async function main(projectId, parameterId, formatType) {
26+
// [START parametermanager_create_structured_param]
27+
/**
28+
* TODO(developer): Uncomment these variables before running the sample.
29+
*/
30+
// const {protos} = require('@google-cloud/parametermanager');
31+
// const projectId = 'YOUR_PROJECT_ID';
32+
// const parameterId = 'YOUR_PARAMETER_ID';
33+
// const formatType = protos.google.cloud.parametermanager.v1.ParameterFormat.JSON;
34+
35+
// Imports the Parameter Manager library
36+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
37+
38+
// Instantiates a client
39+
const client = new ParameterManagerClient();
40+
41+
async function createStructuredParam() {
42+
const parent = client.locationPath(projectId, 'global');
43+
const request = {
44+
parent: parent,
45+
parameterId: parameterId,
46+
parameter: {
47+
format: formatType,
48+
},
49+
};
50+
51+
const [parameter] = await client.createParameter(request);
52+
console.log(
53+
`Created parameter ${parameter.name} with format ${parameter.format}`
54+
);
55+
return parameter;
56+
}
57+
58+
return await createStructuredParam();
59+
// [END parametermanager_create_structured_param]
60+
}
61+
module.exports.main = main;
62+
63+
/* c8 ignore next 10 */
64+
if (require.main === module) {
65+
main(...process.argv.slice(2)).catch(err => {
66+
console.error(err.message);
67+
process.exitCode = 1;
68+
});
69+
process.on('unhandledRejection', err => {
70+
console.error(err.message);
71+
process.exitCode = 1;
72+
});
73+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 new version of an existing parameter in the global location
19+
* of the specified project using the Google Cloud Parameter Manager SDK.
20+
* The payload is specified as a JSON format.
21+
*
22+
* @param {string} projectId - The Google Cloud project ID where the parameter is to be created.
23+
* @param {string} parameterId - The ID of the parameter to create. This ID must be unique within the project.
24+
* @param {string} parameterVersionId - The ID of the parameter version to be created.
25+
* @param {Object} payload - The JSON payload data to be stored in the parameter version.
26+
*/
27+
async function main(projectId, parameterId, parameterVersionId, payload) {
28+
// [START parametermanager_create_structured_param_version]
29+
/**
30+
* TODO(developer): Uncomment these variables before running the sample.
31+
*/
32+
// const projectId = 'YOUR_PROJECT_ID';
33+
// const parameterId = 'YOUR_PARAMETER_ID';
34+
// const parameterVersionId = 'YOUR_PARAMETER_VERSION_ID';
35+
// const jsonData = {username: "test-user", host: "localhost"};
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 createStructuredParamVersion() {
44+
// Construct the parent resource name
45+
const parent = client.parameterPath(projectId, 'global', parameterId);
46+
47+
// Construct the parameter version
48+
const parameterVersion = {
49+
payload: {
50+
data: Buffer.from(JSON.stringify(payload), 'utf8'),
51+
},
52+
};
53+
54+
// Construct the request
55+
const request = {
56+
parent: parent,
57+
parameterVersionId: parameterVersionId,
58+
parameterVersion: parameterVersion,
59+
};
60+
61+
// Create the parameter version
62+
const [paramVersion] = await client.createParameterVersion(request);
63+
console.log(`Created parameter version: ${paramVersion.name}`);
64+
return paramVersion;
65+
}
66+
67+
return await createStructuredParamVersion();
68+
// [END parametermanager_create_structured_param_version]
69+
}
70+
module.exports.main = main;
71+
72+
/* c8 ignore next 10 */
73+
if (require.main === module) {
74+
main(...process.argv.slice(2)).catch(err => {
75+
console.error(err.message);
76+
process.exitCode = 1;
77+
});
78+
process.on('unhandledRejection', err => {
79+
console.error(err.message);
80+
process.exitCode = 1;
81+
});
82+
}

0 commit comments

Comments
 (0)