Skip to content

Commit 376e199

Browse files
feat(parametermanager): Added samples for create, get, list and render regional parameter & parameter version
1 parent 7de4517 commit 376e199

13 files changed

Lines changed: 1098 additions & 0 deletions

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.1.0"
21+
},
22+
"devDependencies": {
23+
"@google-cloud/secret-manager": "^5.6.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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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(
27+
projectId = 'my-project',
28+
locationId = 'us-central1',
29+
parameterId = 'my-regional-parameter'
30+
) {
31+
// [START parametermanager_create_regional_param]
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+
39+
// Imports the Parameter Manager library
40+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
41+
42+
// Adding the endpoint to call the regional parameter manager server
43+
const options = {
44+
apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`,
45+
};
46+
47+
// Instantiates a client with regional endpoint
48+
const client = new ParameterManagerClient(options);
49+
50+
async function createRegionalParam() {
51+
const parent = client.locationPath(projectId, locationId);
52+
const request = {
53+
parent: parent,
54+
parameterId: parameterId,
55+
};
56+
57+
const [parameter] = await client.createParameter(request);
58+
console.log(`Created regional parameter: ${parameter.name}`);
59+
}
60+
61+
await createRegionalParam();
62+
// [END parametermanager_create_regional_param]
63+
}
64+
65+
// This sample demonstrates how to create a regional parameter with unstructured data.
66+
const args = process.argv.slice(2);
67+
main(...args).catch(console.error);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 = 'my-project',
30+
locationId = 'us-central1',
31+
parameterId = 'my-parameter',
32+
parameterVersionId = 'v1',
33+
payload = 'This is unstructured data'
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 [response] = await client.createParameterVersion(request);
76+
console.log(`Created regional parameter version: ${response.name}`);
77+
}
78+
79+
await createRegionalParamVersion();
80+
// [END parametermanager_create_regional_param_version]
81+
}
82+
83+
// Parse command line arguments
84+
const args = process.argv.slice(2);
85+
main(...args).catch(console.error);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 = 'my-project',
30+
locationId = 'us-central1',
31+
parameterId = 'my-parameter',
32+
parameterVersionId = 'v1',
33+
secretId = 'projects/my-project/secrets/application-secret/version/latest'
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 [response] = await client.createParameterVersion(request);
82+
console.log(
83+
`Created regional parameter version with secret: ${response.name}`
84+
);
85+
}
86+
87+
await createRegionalParamVersionWithSecret();
88+
// [END parametermanager_create_regional_param_version_with_secret]
89+
}
90+
91+
// Parse command line arguments
92+
const args = process.argv.slice(2);
93+
main(...args).catch(console.error);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
const {protos} = require('@google-cloud/parametermanager');
18+
19+
/**
20+
* Creates a parameter in the specified region of the specified project using the Google Cloud Parameter Manager SDK.
21+
* The parameter is created with the specified format type.
22+
*
23+
* @param {string} projectId - The Google Cloud project ID where the parameter is to be created.
24+
* @param {string} locationId - The ID of the region where parameter is to be created.
25+
* @param {string} parameterId - The ID of the parameter to create.
26+
* @param {string} formatType - The format type of the parameter (UNFORMATTED, YAML, JSON).
27+
*/
28+
async function main(
29+
projectId = 'my-project',
30+
locationId = 'us-central1',
31+
parameterId = 'my-json-parameter',
32+
formatType = protos.google.cloud.parametermanager.v1.ParameterFormat.JSON
33+
) {
34+
// [START parametermanager_create_structured_regional_param]
35+
/**
36+
* TODO(developer): Uncomment these variables before running the sample.
37+
*/
38+
// const {protos} = require('@google-cloud/parametermanager');
39+
// const projectId = 'YOUR_PROJECT_ID';
40+
// const locationId = 'YOUR_LOCATION_ID';
41+
// const parameterId = 'YOUR_PARAMETER_ID';
42+
// const formatType = protos.google.cloud.parametermanager.v1.ParameterFormat.JSON;
43+
44+
// Imports the Parameter Manager library
45+
const {ParameterManagerClient} = require('@google-cloud/parametermanager');
46+
47+
// Adding the endpoint to call the regional parameter manager server
48+
const options = {
49+
apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`,
50+
};
51+
52+
// Instantiates a client with regional endpoint
53+
const client = new ParameterManagerClient(options);
54+
55+
async function createStructuredRegionalParam() {
56+
const parent = client.locationPath(projectId, locationId);
57+
const request = {
58+
parent: parent,
59+
parameterId: parameterId,
60+
parameter: {
61+
format: formatType,
62+
},
63+
};
64+
65+
const [parameter] = await client.createParameter(request);
66+
console.log(
67+
`Created regional parameter ${parameter.name} with format ${parameter.format}`
68+
);
69+
}
70+
71+
await createStructuredRegionalParam();
72+
// [END parametermanager_create_structured_regional_param]
73+
}
74+
75+
// This sample demonstrates how to create a regional parameter with structured (JSON) data.
76+
const args = process.argv.slice(2);
77+
main(...args).catch(console.error);

0 commit comments

Comments
 (0)