Skip to content

Commit fa66587

Browse files
feat(parametermanager): Added samples for create, get, list and render regional parameter & parameter version (#4069)
* feat(parametermanager): Added samples for create, get, list and render regional parameter & parameter version * fix(parametermanager): update code samples and testcases for parameter manager * 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 0575171 commit fa66587

11 files changed

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

0 commit comments

Comments
 (0)