diff --git a/.github/workflows/parametermanager.yaml b/.github/workflows/parametermanager.yaml new file mode 100644 index 0000000000..9f2c7b2ab9 --- /dev/null +++ b/.github/workflows/parametermanager.yaml @@ -0,0 +1,52 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: parametermanager +on: + push: + branches: + - main + paths: + - 'parametermanager/**' + - '.github/workflows/parametermanager.yaml' + pull_request: + types: + - opened + - reopened + - synchronize + - labeled + paths: + - 'parametermanager/**' + - '.github/workflows/parametermanager.yaml' + schedule: + - cron: '0 0 * * 0' +jobs: + test: + # Ref: https://github.com/google-github-actions/auth#usage + permissions: + contents: 'read' + id-token: 'write' + if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' + uses: ./.github/workflows/test.yaml + with: + name: 'parametermanager' + path: 'parametermanager' + flakybot: + # Ref: https://github.com/google-github-actions/auth#usage + permissions: + contents: 'read' + id-token: 'write' + if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail + uses: ./.github/workflows/flakybot.yaml + needs: [test] diff --git a/.github/workflows/utils/workflows.json b/.github/workflows/utils/workflows.json index ec662d7be7..002145312a 100644 --- a/.github/workflows/utils/workflows.json +++ b/.github/workflows/utils/workflows.json @@ -79,6 +79,7 @@ "mediatranslation", "monitoring/prometheus", "monitoring/snippets", + "parametermanager", "retail", "run/filesystem", "scheduler", diff --git a/parametermanager/package.json b/parametermanager/package.json new file mode 100644 index 0000000000..62cca15a15 --- /dev/null +++ b/parametermanager/package.json @@ -0,0 +1,29 @@ +{ + "name": "nodejs-parameter-manager-samples", + "private": true, + "license": "Apache-2.0", + "files": [ + "*.js" + ], + "author": "Google LLC", + "repository": "googleapis/nodejs-parameter-manager", + "engines": { + "node": ">=20" + }, + "scripts": { + "test": "c8 mocha --recursive test/ --timeout=800000" + }, + "directories": { + "test": "test" + }, + "dependencies": { + "@google-cloud/parametermanager": "^0.1.0" + }, + "devDependencies": { + "@google-cloud/secret-manager": "^5.6.0", + "c8": "^10.1.3", + "chai": "^4.5.0", + "mocha": "^11.1.0", + "uuid": "^11.0.5" + } +} diff --git a/parametermanager/regional_samples/createRegionalParam.js b/parametermanager/regional_samples/createRegionalParam.js new file mode 100644 index 0000000000..e79c049891 --- /dev/null +++ b/parametermanager/regional_samples/createRegionalParam.js @@ -0,0 +1,67 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Creates a new version of an existing parameter in the specified region + * of the specified project using the Google Cloud Parameter Manager SDK. + * The payload is specified as a JSON string and includes a reference to a secret. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is to be created. + * @param {string} locationId - The ID of the region where parameter is to be created. + * @param {string} parameterId - The ID of the parameter to create. This ID must be unique within the project location. + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-regional-parameter' +) { + // [START parametermanager_create_regional_param] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const locationId = 'YOUR_LOCATION_ID'; + // const parameterId = 'YOUR_PARAMETER_ID'; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Adding the endpoint to call the regional parameter manager server + const options = { + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, + }; + + // Instantiates a client with regional endpoint + const client = new ParameterManagerClient(options); + + async function createRegionalParam() { + const parent = client.locationPath(projectId, locationId); + const request = { + parent: parent, + parameterId: parameterId, + }; + + const [parameter] = await client.createParameter(request); + console.log(`Created regional parameter: ${parameter.name}`); + } + + await createRegionalParam(); + // [END parametermanager_create_regional_param] +} + +// This sample demonstrates how to create a regional parameter with unstructured data. +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/parametermanager/regional_samples/createRegionalParamVersion.js b/parametermanager/regional_samples/createRegionalParamVersion.js new file mode 100644 index 0000000000..c6ce5fd113 --- /dev/null +++ b/parametermanager/regional_samples/createRegionalParamVersion.js @@ -0,0 +1,85 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Creates a new version of an existing parameter in the specified region + * of the specified project using the Google Cloud Parameter Manager SDK. + * The payload is specified as an unformatted string. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is located. + * @param {string} locationId - The ID of the region where parameter is located. + * @param {string} parameterId - The ID of the parameter for which the version is to be created. + * @param {string} parameterVersionId - The ID of the parameter version to be created. + * @param {string} payload - The unformatted string payload to be stored in the parameter version. + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-parameter', + parameterVersionId = 'v1', + payload = 'This is unstructured data' +) { + // [START parametermanager_create_regional_param_version] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const locationId = 'us-central1'; + // const parameterId = 'YOUR_PARAMETER_ID'; + // const parameterVersionId = 'YOUR_PARAMETER_VERSION_ID'; + // const payload = 'This is unstructured data'; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Adding the endpoint to call the regional parameter manager server + const options = { + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, + }; + + // Instantiates a client with regional endpoint + const client = new ParameterManagerClient(options); + + async function createRegionalParamVersion() { + // Construct the parent resource name + const parent = client.parameterPath(projectId, locationId, parameterId); + + // Construct the parameter version + const parameterVersion = { + payload: { + data: Buffer.from(payload, 'utf8'), + }, + }; + + // Construct the request + const request = { + parent: parent, + parameterVersionId: parameterVersionId, + parameterVersion: parameterVersion, + }; + + // Create the parameter version + const [response] = await client.createParameterVersion(request); + console.log(`Created regional parameter version: ${response.name}`); + } + + await createRegionalParamVersion(); + // [END parametermanager_create_regional_param_version] +} + +// Parse command line arguments +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/parametermanager/regional_samples/createRegionalParamVersionWithSecret.js b/parametermanager/regional_samples/createRegionalParamVersionWithSecret.js new file mode 100644 index 0000000000..daaad1110a --- /dev/null +++ b/parametermanager/regional_samples/createRegionalParamVersionWithSecret.js @@ -0,0 +1,93 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Creates a new version of an existing parameter in the specified region + * of the specified project using the Google Cloud Parameter Manager SDK. + * The payload is specified as a JSON string and includes a reference to a secret. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is located. + * @param {string} locationId - The ID of the region where parameter is located. + * @param {string} parameterId - The ID of the parameter for which the version is to be created. + * @param {string} parameterVersionId - The ID of the parameter version to be created. + * @param {string} secretId - The ID of the secret to be referenced. + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-parameter', + parameterVersionId = 'v1', + secretId = 'projects/my-project/secrets/application-secret/version/latest' +) { + // [START parametermanager_create_regional_param_version_with_secret] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const locationId = 'us-central1'; + // const parameterId = 'YOUR_PARAMETER_ID'; + // const parameterVersionId = 'YOUR_PARAMETER_VERSION_ID'; + // const secretId = 'YOUR_SECRET_ID'; // For example projects/my-project/secrets/application-secret/version/latest + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Adding the endpoint to call the regional parameter manager server + const options = { + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, + }; + + // Instantiates a client with regional endpoint + const client = new ParameterManagerClient(options); + + async function createRegionalParamVersionWithSecret() { + // Construct the parent resource name + const parent = client.parameterPath(projectId, locationId, parameterId); + + // Construct the payload JSON data with secret references + const payloadData = { + db_user: 'test_user', + db_password: `__REF__("//secretmanager.googleapis.com/${secretId}")`, + }; + + // Construct the parameter version + const parameterVersion = { + payload: { + data: Buffer.from(JSON.stringify(payloadData), 'utf8'), + }, + }; + + // Construct the request + const request = { + parent: parent, + parameterVersionId: parameterVersionId, + parameterVersion: parameterVersion, + }; + + // Create the regional parameter version + const [response] = await client.createParameterVersion(request); + console.log( + `Created regional parameter version with secret: ${response.name}` + ); + } + + await createRegionalParamVersionWithSecret(); + // [END parametermanager_create_regional_param_version_with_secret] +} + +// Parse command line arguments +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/parametermanager/regional_samples/createStructuredRegionalParam.js b/parametermanager/regional_samples/createStructuredRegionalParam.js new file mode 100644 index 0000000000..54cbeea02f --- /dev/null +++ b/parametermanager/regional_samples/createStructuredRegionalParam.js @@ -0,0 +1,77 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {protos} = require('@google-cloud/parametermanager'); + +/** + * Creates a parameter in the specified region of the specified project using the Google Cloud Parameter Manager SDK. + * The parameter is created with the specified format type. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is to be created. + * @param {string} locationId - The ID of the region where parameter is to be created. + * @param {string} parameterId - The ID of the parameter to create. + * @param {string} formatType - The format type of the parameter (UNFORMATTED, YAML, JSON). + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-json-parameter', + formatType = protos.google.cloud.parametermanager.v1.ParameterFormat.JSON +) { + // [START parametermanager_create_structured_regional_param] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const {protos} = require('@google-cloud/parametermanager'); + // const projectId = 'YOUR_PROJECT_ID'; + // const locationId = 'YOUR_LOCATION_ID'; + // const parameterId = 'YOUR_PARAMETER_ID'; + // const formatType = protos.google.cloud.parametermanager.v1.ParameterFormat.JSON; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Adding the endpoint to call the regional parameter manager server + const options = { + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, + }; + + // Instantiates a client with regional endpoint + const client = new ParameterManagerClient(options); + + async function createStructuredRegionalParam() { + const parent = client.locationPath(projectId, locationId); + const request = { + parent: parent, + parameterId: parameterId, + parameter: { + format: formatType, + }, + }; + + const [parameter] = await client.createParameter(request); + console.log( + `Created regional parameter ${parameter.name} with format ${parameter.format}` + ); + } + + await createStructuredRegionalParam(); + // [END parametermanager_create_structured_regional_param] +} + +// This sample demonstrates how to create a regional parameter with structured (JSON) data. +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/parametermanager/regional_samples/createStructuredRegionalParamVersion.js b/parametermanager/regional_samples/createStructuredRegionalParamVersion.js new file mode 100644 index 0000000000..4889ab9a7b --- /dev/null +++ b/parametermanager/regional_samples/createStructuredRegionalParamVersion.js @@ -0,0 +1,85 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Creates a new version of an existing parameter in the specified region of the + * specified project using the Google Cloud Parameter Manager SDK. + * The payload is specified as a JSON format. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is located. + * @param {string} locationId - The ID of the region where parameter is located. + * @param {string} parameterId - The ID of the parameter for which the version is to be created. + * @param {string} parameterVersionId - The ID of the parameter version to be created. + * @param {Object} payload - The JSON data payload to be stored in the parameter version. + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-parameter', + parameterVersionId = 'v1', + payload = {username: 'test-user', host: 'localhost'} +) { + // [START parametermanager_create_structured_regional_param_version] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const locationId = 'us-central1'; + // const parameterId = 'YOUR_PARAMETER_ID'; + // const parameterVersionId = 'YOUR_PARAMETER_VERSION_ID'; + // const payload = {username: "test-user", host: "localhost"}; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Adding the endpoint to call the regional parameter manager server + const options = { + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, + }; + + // Instantiates a client with regional endpoint + const client = new ParameterManagerClient(options); + + async function createStructuredRegionalParamVersion() { + // Construct the parent resource name + const parent = client.parameterPath(projectId, locationId, parameterId); + + // Construct the parameter version + const parameterVersion = { + payload: { + data: Buffer.from(JSON.stringify(payload), 'utf8'), + }, + }; + + // Construct the request + const request = { + parent: parent, + parameterVersionId: parameterVersionId, + parameterVersion: parameterVersion, + }; + + // Create the regional parameter version + const [response] = await client.createParameterVersion(request); + console.log(`Created regional parameter version: ${response.name}`); + } + + await createStructuredRegionalParamVersion(); + // [END parametermanager_create_structured_regional_param_version] +} + +// Parse command line arguments +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/parametermanager/regional_samples/getRegionalParam.js b/parametermanager/regional_samples/getRegionalParam.js new file mode 100644 index 0000000000..93a5c0b227 --- /dev/null +++ b/parametermanager/regional_samples/getRegionalParam.js @@ -0,0 +1,71 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Retrieves a parameter from the specified region of the specified + * project using the Google Cloud Parameter Manager SDK. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is located. + * @param {string} locationId - The ID of the region where parameter is located. + * @param {string} parameterId - The ID of the parameter to retrieve. + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-parameter' +) { + // [START parametermanager_get_regional_param] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const locationId = 'us-central1'; + // const parameterId = 'my-parameter'; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Adding the endpoint to call the regional parameter manager server + const options = { + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, + }; + + // Instantiates a client with regional endpoint + const client = new ParameterManagerClient(options); + + async function getRegionalParam() { + // Construct the fully qualified parameter name + const name = client.parameterPath(projectId, locationId, parameterId); + + // Get the parameter + const [parameter] = await client.getParameter({ + name: name, + }); + + // Find more details for the Parameter object here: + // https://cloud.google.com/secret-manager/parameter-manager/docs/reference/rest/v1/projects.locations.parameters#Parameter + console.log( + `Found regional parameter ${parameter.name} with format ${parameter.format}` + ); + } + + await getRegionalParam(); + // [END parametermanager_get_regional_param] +} + +// The command-line arguments are passed as an array to main() +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/parametermanager/regional_samples/getRegionalParamVersion.js b/parametermanager/regional_samples/getRegionalParamVersion.js new file mode 100644 index 0000000000..d59149694d --- /dev/null +++ b/parametermanager/regional_samples/getRegionalParamVersion.js @@ -0,0 +1,84 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Retrieves the details of a specific version of an existing parameter in the specified region of the specified + * project using the Google Cloud Parameter Manager SDK. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is located. + * @param {string} locationId - The ID of the region where parameter is located. + * @param {string} parameterId - The ID of the parameter for which version details are to be retrieved. + * @param {string} versionId - The version ID of the parameter to retrieve. + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-parameter', + versionId = 'v1' +) { + // [START parametermanager_get_regional_param_version] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const locationId = 'us-central1'; + // const parameterId = 'my-parameter'; + // const versionId = 'v1'; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Adding the endpoint to call the regional parameter manager server + const options = { + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, + }; + + // Instantiates a client with regional endpoint + const client = new ParameterManagerClient(options); + + async function getRegionalParamVersion() { + // Construct the fully qualified parameter version name + const name = client.parameterVersionPath( + projectId, + locationId, + parameterId, + versionId + ); + + // Get the parameter version + const [parameterVersion] = await client.getParameterVersion({ + name: name, + }); + + // Find more details for the Parameter Version object here: + // https://cloud.google.com/secret-manager/parameter-manager/docs/reference/rest/v1/projects.locations.parameters.versions#ParameterVersion + console.log( + `Found regional parameter version ${parameterVersion.name} with state ${parameterVersion.disabled ? 'disabled' : 'enabled'}` + ); + if (!parameterVersion.disabled) { + console.log( + `Payload: ${parameterVersion.payload.data.toString('utf-8')}` + ); + } + } + + await getRegionalParamVersion(); + // [END parametermanager_get_regional_param_version] +} + +// The command-line arguments are passed as an array to main() +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/parametermanager/regional_samples/listRegionalParamVersions.js b/parametermanager/regional_samples/listRegionalParamVersions.js new file mode 100644 index 0000000000..8615d3d94b --- /dev/null +++ b/parametermanager/regional_samples/listRegionalParamVersions.js @@ -0,0 +1,72 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * List all versions of an existing parameter in the specific region for the specified + * project using the Google Cloud Parameter Manager SDK. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is located. + * @param {string} locationId - The ID of the region where the parameter is located. + * @param {string} parameterId - The parameter ID for which versions are to be listed. + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-parameter' +) { + // [START parametermanager_list_regional_param_versions] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const locationId = 'us-central1'; + // const parameterId = 'my-parameter'; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Adding the endpoint to call the regional parameter manager server + const options = { + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, + }; + + // Instantiates a client with regional endpoint + const client = new ParameterManagerClient(options); + + async function listRegionalParamVersions() { + // Construct the parent string for listing parameter versions in a specific region + const parent = client.parameterPath(projectId, locationId, parameterId); + + const request = { + parent: parent, + }; + + // Use listParameterVersionsAsync to handle pagination automatically + const iterable = await client.listParameterVersionsAsync(request); + + for await (const version of iterable) { + console.log( + `Found regional parameter version ${version.name} with state ${version.disabled ? 'disabled' : 'enabled'} ` + ); + } + } + + await listRegionalParamVersions(); + // [END parametermanager_list_regional_param_versions] +} + +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/parametermanager/regional_samples/listRegionalParams.js b/parametermanager/regional_samples/listRegionalParams.js new file mode 100644 index 0000000000..5d983788d5 --- /dev/null +++ b/parametermanager/regional_samples/listRegionalParams.js @@ -0,0 +1,66 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Lists all parameters in the specified region for the specified + * project using the Google Cloud Parameter Manager SDK. + * + * @param {string} projectId - The Google Cloud project ID where the parameters are located. + * @param {string} locationId - The ID of the region where parameters are located. + */ +async function main(projectId = 'my-project', locationId = 'us-central1') { + // [START parametermanager_list_regional_params] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const locationId = 'us-central1'; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Adding the endpoint to call the regional parameter manager server + const options = { + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, + }; + + // Instantiates a client with regional endpoint + const client = new ParameterManagerClient(options); + + async function listRegionalParams() { + // Construct the parent string for listing parameters in a specific region + const parent = client.locationPath(projectId, locationId); + + const request = { + parent: parent, + }; + + // Use listParametersAsync to handle pagination automatically + const iterable = await client.listParametersAsync(request); + + for await (const parameter of iterable) { + console.log( + `Found regional parameter ${parameter.name} with format ${parameter.format}` + ); + } + } + + await listRegionalParams(); + // [END parametermanager_list_regional_params] +} + +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/parametermanager/regional_samples/renderRegionalParamVersion.js b/parametermanager/regional_samples/renderRegionalParamVersion.js new file mode 100644 index 0000000000..a52c48f306 --- /dev/null +++ b/parametermanager/regional_samples/renderRegionalParamVersion.js @@ -0,0 +1,89 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +/** + * Retrieves and renders the details of a specific version of an + * existing parameter in the specified region of the specified project + * using the Google Cloud Parameter Manager SDK. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is located. + * @param {string} locationId - The ID of the region where parameter is located. + * @param {string} parameterId - The ID of the parameter for which version details are to be rendered. + * @param {string} parameterVersionId - The ID of the parameter version to be rendered. + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-parameter', + parameterVersionId = 'v1' +) { + // [START parametermanager_render_regional_param_version] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'YOUR_PROJECT_ID'; + // const locationId = 'us-central1'; + // const parameterId = 'YOUR_PARAMETER_ID'; + // const parameterVersionId = 'YOUR_PARAMETER_VERSION_ID'; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Adding the endpoint to call the regional parameter manager server + const options = { + apiEndpoint: `parametermanager.${locationId}.rep.googleapis.com`, + }; + + // Instantiates a client with regional endpoint + const client = new ParameterManagerClient(options); + + async function renderRegionalParamVersion() { + // Construct the parameter version name + const name = client.parameterVersionPath( + projectId, + locationId, + parameterId, + parameterVersionId + ); + + // Construct the request + const request = { + name: name, + }; + + // Render the parameter version + const [response] = await client.renderParameterVersion(request); + + console.log( + `Rendered regional parameter version: ${response.parameterVersion}` + ); + + // If the parameter contains secret references, they will be resolved + // and the actual secret values will be included in the rendered output. + // Be cautious with logging or displaying this information. + console.log( + 'Rendered payload: ', + response.renderedPayload.toString('utf-8') + ); + } + + await renderRegionalParamVersion(); + // [END parametermanager_render_regional_param_version] +} + +// Parse command line arguments +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/parametermanager/test/.eslintrc.yml b/parametermanager/test/.eslintrc.yml new file mode 100644 index 0000000000..9351c489b5 --- /dev/null +++ b/parametermanager/test/.eslintrc.yml @@ -0,0 +1,17 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +env: + mocha: true \ No newline at end of file diff --git a/parametermanager/test/parametermanager.test.js b/parametermanager/test/parametermanager.test.js new file mode 100644 index 0000000000..36c6b8fa1b --- /dev/null +++ b/parametermanager/test/parametermanager.test.js @@ -0,0 +1,263 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {assert} = require('chai'); +const cp = require('child_process'); +const {v4: uuidv4} = require('uuid'); + +const {ParameterManagerClient} = require('@google-cloud/parametermanager'); +const client = new ParameterManagerClient(); + +const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); + +let projectId; +const locationId = process.env.GCLOUD_LOCATION || 'us-central1'; +const options = {}; +options.apiEndpoint = `parametermanager.${locationId}.rep.googleapis.com`; + +const regionalClient = new ParameterManagerClient(options); + +const secretOptions = {}; +secretOptions.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`; + +const regionalSecretClient = new SecretManagerServiceClient(secretOptions); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const secretId = `test-secret-${uuidv4()}`; +const regionalParameterId = `test-regional-${uuidv4()}`; +const parameterVersionId = 'v1'; + +let regionalParameter; +let regionalParameterVersion; +let regionalSecret; +let regionalSecretVersion; + +describe('Parameter Manager samples', () => { + const regionalParametersToDelete = []; + + before(async () => { + projectId = await client.getProjectId(); + + // Create a regional secret + [regionalSecret] = await regionalSecretClient.createSecret({ + parent: `projects/${projectId}/locations/${locationId}`, + secretId: secretId, + }); + + // Create a regional secret version + [regionalSecretVersion] = await regionalSecretClient.addSecretVersion({ + parent: regionalSecret.name, + payload: { + data: Buffer.from('my super secret data', 'utf-8'), + }, + }); + + // Create a test regional parameter + [regionalParameter] = await regionalClient.createParameter({ + parent: `projects/${projectId}/locations/${locationId}`, + parameterId: regionalParameterId, + parameter: { + format: 'JSON', + }, + }); + regionalParametersToDelete.push(regionalParameter.name); + + // Create a version for the regional parameter + [regionalParameterVersion] = await regionalClient.createParameterVersion({ + parent: regionalParameter.name, + parameterVersionId: parameterVersionId, + parameterVersion: { + payload: { + data: Buffer.from(JSON.stringify({key: 'regional_value'}), 'utf-8'), + }, + }, + }); + }); + + after(async () => { + // Clean up + regionalParametersToDelete.forEach(async regionalParameterName => { + await regionalClient.deleteParameterVersion({ + name: `${regionalParameterName}/versions/v1`, + }); + if (regionalParameterName === regionalParameter.name) { + await regionalClient.deleteParameterVersion({ + name: `${regionalParameterName}/versions/v12`, + }); + } + await regionalClient.deleteParameter({name: regionalParameterName}); + }); + await regionalSecretClient.deleteSecret({ + name: regionalSecret.name, + }); + }); + + it('should create regional parameter version with secret references', async () => { + const output = execSync( + `node regional_samples/createRegionalParamVersionWithSecret.js ${projectId} ${locationId} ${regionalParameterId} ${parameterVersionId}2 ${regionalSecretVersion.name}` + ); + assert.include( + output, + `Created regional parameter version with secret: projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}/versions/${parameterVersionId}2` + ); + }); + + it('should create a regional structured parameter', async () => { + const output = execSync( + `node regional_samples/createStructuredRegionalParam.js ${projectId} ${locationId} ${regionalParameterId}-2` + ); + regionalParametersToDelete.push( + client.parameterPath(projectId, locationId, `${regionalParameterId}-2`) + ); + assert.include( + output, + `Created regional parameter projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-2 with format JSON` + ); + }); + + it('should create a regional unstructured parameter', async () => { + const output = execSync( + `node regional_samples/createRegionalParam.js ${projectId} ${locationId} ${regionalParameterId}-3` + ); + regionalParametersToDelete.push( + client.parameterPath(projectId, locationId, `${regionalParameterId}-3`) + ); + assert.include( + output, + `Created regional parameter: projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-3` + ); + }); + + it('should create a regional structured parameter version', async () => { + const output = execSync( + `node regional_samples/createStructuredRegionalParamVersion.js ${projectId} ${locationId} ${regionalParameterId}-2 ${parameterVersionId}` + ); + assert.include( + output, + `Created regional parameter version: projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-2/versions/${parameterVersionId}` + ); + }); + + it('should create a regional unstructured parameter version', async () => { + const output = execSync( + `node regional_samples/createRegionalParamVersion.js ${projectId} ${locationId} ${regionalParameterId}-3 ${parameterVersionId}` + ); + assert.include( + output, + `Created regional parameter version: projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-3/versions/${parameterVersionId}` + ); + }); + + it('should list regional parameters', async () => { + const output = execSync( + `node regional_samples/listRegionalParams.js ${projectId} ${locationId}` + ); + assert.include( + output, + `Found regional parameter ${regionalParameter.name} with format ${regionalParameter.format}` + ); + assert.include( + output, + `Found regional parameter projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-2 with format JSON` + ); + assert.include( + output, + `Found regional parameter projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-3 with format UNFORMATTED` + ); + }); + + it('should get a regional parameter', async () => { + const output = execSync( + `node regional_samples/getRegionalParam.js ${projectId} ${locationId} ${regionalParameterId}` + ); + assert.include( + output, + `Found regional parameter ${regionalParameter.name} with format ${regionalParameter.format}` + ); + }); + + it('should list regional parameter versions', async () => { + const output = execSync( + `node regional_samples/listRegionalParamVersions.js ${projectId} ${locationId} ${regionalParameterId}` + ); + assert.include( + output, + `Found regional parameter version ${regionalParameterVersion.name} with state enabled` + ); + assert.include( + output, + `Found regional parameter version ${regionalParameterVersion.name}2 with state enabled` + ); + }); + + it('should get a regional parameter version', async () => { + let output = execSync( + `node regional_samples/getRegionalParamVersion.js ${projectId} ${locationId} ${regionalParameterId} ${parameterVersionId}` + ); + assert.include( + output, + `Found regional parameter version ${regionalParameterVersion.name} with state enabled` + ); + + output = execSync( + `node regional_samples/getRegionalParamVersion.js ${projectId} ${locationId} ${regionalParameterId} ${parameterVersionId}2` + ); + assert.include( + output, + `Found regional parameter version ${regionalParameterVersion.name}2 with state enabled` + ); + assert.include( + output, + `Payload: {"db_user":"test_user","db_password":"__REF__(\\"//secretmanager.googleapis.com/${regionalSecretVersion.name}\\")"}` + ); + }); + + it('should render regional parameter version', async () => { + // Get the current IAM policy. + const [policy] = await regionalSecretClient.getIamPolicy({ + resource: regionalSecret.name, + }); + + // Add the user with accessor permissions to the bindings list. + policy.bindings.push({ + role: 'roles/secretmanager.secretAccessor', + members: [regionalParameter.policyMember.iamPolicyUidPrincipal], + }); + + // Save the updated IAM policy. + await regionalSecretClient.setIamPolicy({ + resource: regionalSecret.name, + policy: policy, + }); + + await new Promise(resolve => setTimeout(resolve, 120000)); + + const output = execSync( + `node regional_samples/renderRegionalParamVersion.js ${projectId} ${locationId} ${regionalParameterId} ${parameterVersionId}2` + ); + assert.include(output, 'Rendered regional parameter version:'); + assert.include( + output, + `/parameters/${regionalParameterId}/versions/${parameterVersionId}2` + ); + assert.include(output, 'Rendered payload:'); + assert.include( + output, + '{"db_user":"test_user","db_password":"my super secret data"}' + ); + }); +});