Skip to content

Commit c89a3d1

Browse files
feat(parametermanager): Added global and regional samples for parameter manager
1 parent 5eaff3c commit c89a3d1

35 files changed

Lines changed: 2961 additions & 0 deletions
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
# http://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+
name: parameter-manager
16+
on:
17+
push:
18+
branches:
19+
- main
20+
paths:
21+
- 'parameter-manager/**'
22+
- '.github/workflows/parameter-manager.yaml'
23+
pull_request:
24+
types:
25+
- opened
26+
- reopened
27+
- synchronize
28+
- labeled
29+
paths:
30+
- 'parameter-manager/**'
31+
- '.github/workflows/parameter-manager.yaml'
32+
schedule:
33+
- cron: '0 0 * * 0'
34+
jobs:
35+
test:
36+
# Ref: https://github.com/google-github-actions/auth#usage
37+
permissions:
38+
contents: 'read'
39+
id-token: 'write'
40+
if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run'
41+
uses: ./.github/workflows/test.yaml
42+
with:
43+
name: 'parameter-manager'
44+
path: 'parameter-manager'
45+
flakybot:
46+
# Ref: https://github.com/google-github-actions/auth#usage
47+
permissions:
48+
contents: 'read'
49+
id-token: 'write'
50+
if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail
51+
uses: ./.github/workflows/flakybot.yaml
52+
needs: [test]

.github/workflows/utils/workflows.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"mediatranslation",
8080
"monitoring/prometheus",
8181
"monitoring/snippets",
82+
"parameter-manager",
8283
"retail",
8384
"run/filesystem",
8485
"scheduler",

parameter-manager/createParam.js

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

0 commit comments

Comments
 (0)