From 376e199c57da431b12de72cdb8645e12cfc6a81f Mon Sep 17 00:00:00 2001 From: durgesh-ninave-crest Date: Wed, 9 Apr 2025 12:26:50 +0530 Subject: [PATCH 1/5] feat(parametermanager): Added samples for create, get, list and render regional parameter & parameter version --- parametermanager/package.json | 29 ++ .../regional_samples/createRegionalParam.js | 67 +++++ .../createRegionalParamVersion.js | 85 ++++++ .../createRegionalParamVersionWithSecret.js | 93 +++++++ .../createStructuredRegionalParam.js | 77 +++++ .../createStructuredRegionalParamVersion.js | 85 ++++++ .../regional_samples/getRegionalParam.js | 71 +++++ .../getRegionalParamVersion.js | 84 ++++++ .../listRegionalParamVersions.js | 72 +++++ .../regional_samples/listRegionalParams.js | 66 +++++ .../renderRegionalParamVersion.js | 89 ++++++ parametermanager/test/.eslintrc.yml | 17 ++ .../test/parametermanager.test.js | 263 ++++++++++++++++++ 13 files changed, 1098 insertions(+) create mode 100644 parametermanager/package.json create mode 100644 parametermanager/regional_samples/createRegionalParam.js create mode 100644 parametermanager/regional_samples/createRegionalParamVersion.js create mode 100644 parametermanager/regional_samples/createRegionalParamVersionWithSecret.js create mode 100644 parametermanager/regional_samples/createStructuredRegionalParam.js create mode 100644 parametermanager/regional_samples/createStructuredRegionalParamVersion.js create mode 100644 parametermanager/regional_samples/getRegionalParam.js create mode 100644 parametermanager/regional_samples/getRegionalParamVersion.js create mode 100644 parametermanager/regional_samples/listRegionalParamVersions.js create mode 100644 parametermanager/regional_samples/listRegionalParams.js create mode 100644 parametermanager/regional_samples/renderRegionalParamVersion.js create mode 100644 parametermanager/test/.eslintrc.yml create mode 100644 parametermanager/test/parametermanager.test.js 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"}' + ); + }); +}); From cef77972ab25c10b836499d3315d058fc72b611f Mon Sep 17 00:00:00 2001 From: durgesh-ninave-crest Date: Fri, 25 Apr 2025 18:06:00 +0530 Subject: [PATCH 2/5] fix(parametermanager): update code samples and testcases for parameter manager --- .../regional_samples/createRegionalParam.js | 18 +- .../createRegionalParamVersion.js | 22 +- .../createRegionalParamVersionWithSecret.js | 22 +- .../createStructuredRegionalParam.js | 18 +- .../createStructuredRegionalParamVersion.js | 22 +- .../regional_samples/getRegionalParam.js | 18 +- .../getRegionalParamVersion.js | 18 +- .../listRegionalParamVersions.js | 21 +- .../regional_samples/listRegionalParams.js | 21 +- .../renderRegionalParamVersion.js | 24 ++- .../test/parametermanager.test.js | 191 ++++++++---------- 11 files changed, 241 insertions(+), 154 deletions(-) diff --git a/parametermanager/regional_samples/createRegionalParam.js b/parametermanager/regional_samples/createRegionalParam.js index e79c049891..a3815fae82 100644 --- a/parametermanager/regional_samples/createRegionalParam.js +++ b/parametermanager/regional_samples/createRegionalParam.js @@ -56,12 +56,22 @@ async function main( const [parameter] = await client.createParameter(request); console.log(`Created regional parameter: ${parameter.name}`); + return parameter; } - await createRegionalParam(); + return await createRegionalParam(); // [END parametermanager_create_regional_param] } +module.exports.main = main; -// This sample demonstrates how to create a regional parameter with unstructured data. -const args = process.argv.slice(2); -main(...args).catch(console.error); +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/regional_samples/createRegionalParamVersion.js b/parametermanager/regional_samples/createRegionalParamVersion.js index c6ce5fd113..a85d63a5f6 100644 --- a/parametermanager/regional_samples/createRegionalParamVersion.js +++ b/parametermanager/regional_samples/createRegionalParamVersion.js @@ -72,14 +72,24 @@ async function main( }; // Create the parameter version - const [response] = await client.createParameterVersion(request); - console.log(`Created regional parameter version: ${response.name}`); + const [paramVersion] = await client.createParameterVersion(request); + console.log(`Created regional parameter version: ${paramVersion.name}`); + return paramVersion; } - await createRegionalParamVersion(); + return await createRegionalParamVersion(); // [END parametermanager_create_regional_param_version] } +module.exports.main = main; -// Parse command line arguments -const args = process.argv.slice(2); -main(...args).catch(console.error); +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/regional_samples/createRegionalParamVersionWithSecret.js b/parametermanager/regional_samples/createRegionalParamVersionWithSecret.js index daaad1110a..70c7b1ebef 100644 --- a/parametermanager/regional_samples/createRegionalParamVersionWithSecret.js +++ b/parametermanager/regional_samples/createRegionalParamVersionWithSecret.js @@ -78,16 +78,26 @@ async function main( }; // Create the regional parameter version - const [response] = await client.createParameterVersion(request); + const [paramVersion] = await client.createParameterVersion(request); console.log( - `Created regional parameter version with secret: ${response.name}` + `Created regional parameter version with secret: ${paramVersion.name}` ); + return paramVersion; } - await createRegionalParamVersionWithSecret(); + return await createRegionalParamVersionWithSecret(); // [END parametermanager_create_regional_param_version_with_secret] } +module.exports.main = main; -// Parse command line arguments -const args = process.argv.slice(2); -main(...args).catch(console.error); +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/regional_samples/createStructuredRegionalParam.js b/parametermanager/regional_samples/createStructuredRegionalParam.js index 54cbeea02f..9c44dc4377 100644 --- a/parametermanager/regional_samples/createStructuredRegionalParam.js +++ b/parametermanager/regional_samples/createStructuredRegionalParam.js @@ -66,12 +66,22 @@ async function main( console.log( `Created regional parameter ${parameter.name} with format ${parameter.format}` ); + return parameter; } - await createStructuredRegionalParam(); + return await createStructuredRegionalParam(); // [END parametermanager_create_structured_regional_param] } +module.exports.main = main; -// This sample demonstrates how to create a regional parameter with structured (JSON) data. -const args = process.argv.slice(2); -main(...args).catch(console.error); +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/regional_samples/createStructuredRegionalParamVersion.js b/parametermanager/regional_samples/createStructuredRegionalParamVersion.js index 4889ab9a7b..5ef80ff793 100644 --- a/parametermanager/regional_samples/createStructuredRegionalParamVersion.js +++ b/parametermanager/regional_samples/createStructuredRegionalParamVersion.js @@ -72,14 +72,24 @@ async function main( }; // Create the regional parameter version - const [response] = await client.createParameterVersion(request); - console.log(`Created regional parameter version: ${response.name}`); + const [paramVersion] = await client.createParameterVersion(request); + console.log(`Created regional parameter version: ${paramVersion.name}`); + return paramVersion; } - await createStructuredRegionalParamVersion(); + return await createStructuredRegionalParamVersion(); // [END parametermanager_create_structured_regional_param_version] } +module.exports.main = main; -// Parse command line arguments -const args = process.argv.slice(2); -main(...args).catch(console.error); +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/regional_samples/getRegionalParam.js b/parametermanager/regional_samples/getRegionalParam.js index 93a5c0b227..e695a70746 100644 --- a/parametermanager/regional_samples/getRegionalParam.js +++ b/parametermanager/regional_samples/getRegionalParam.js @@ -60,12 +60,22 @@ async function main( console.log( `Found regional parameter ${parameter.name} with format ${parameter.format}` ); + return parameter; } - await getRegionalParam(); + return await getRegionalParam(); // [END parametermanager_get_regional_param] } +module.exports.main = main; -// The command-line arguments are passed as an array to main() -const args = process.argv.slice(2); -main(...args).catch(console.error); +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/regional_samples/getRegionalParamVersion.js b/parametermanager/regional_samples/getRegionalParamVersion.js index d59149694d..eb7dcb5e33 100644 --- a/parametermanager/regional_samples/getRegionalParamVersion.js +++ b/parametermanager/regional_samples/getRegionalParamVersion.js @@ -73,12 +73,22 @@ async function main( `Payload: ${parameterVersion.payload.data.toString('utf-8')}` ); } + return parameterVersion; } - await getRegionalParamVersion(); + return await getRegionalParamVersion(); // [END parametermanager_get_regional_param_version] } +module.exports.main = main; -// The command-line arguments are passed as an array to main() -const args = process.argv.slice(2); -main(...args).catch(console.error); +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/regional_samples/listRegionalParamVersions.js b/parametermanager/regional_samples/listRegionalParamVersions.js index 8615d3d94b..31f2e1034c 100644 --- a/parametermanager/regional_samples/listRegionalParamVersions.js +++ b/parametermanager/regional_samples/listRegionalParamVersions.js @@ -55,18 +55,29 @@ async function main( }; // Use listParameterVersionsAsync to handle pagination automatically - const iterable = await client.listParameterVersionsAsync(request); + const paramVersions = await client.listParameterVersionsAsync(request); - for await (const version of iterable) { + for await (const version of paramVersions) { console.log( `Found regional parameter version ${version.name} with state ${version.disabled ? 'disabled' : 'enabled'} ` ); } + return paramVersions; } - await listRegionalParamVersions(); + return await listRegionalParamVersions(); // [END parametermanager_list_regional_param_versions] } +module.exports.main = main; -const args = process.argv.slice(2); -main(...args).catch(console.error); +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/regional_samples/listRegionalParams.js b/parametermanager/regional_samples/listRegionalParams.js index 5d983788d5..7617cf31fd 100644 --- a/parametermanager/regional_samples/listRegionalParams.js +++ b/parametermanager/regional_samples/listRegionalParams.js @@ -49,18 +49,29 @@ async function main(projectId = 'my-project', locationId = 'us-central1') { }; // Use listParametersAsync to handle pagination automatically - const iterable = await client.listParametersAsync(request); + const parameters = await client.listParametersAsync(request); - for await (const parameter of iterable) { + for await (const parameter of parameters) { console.log( `Found regional parameter ${parameter.name} with format ${parameter.format}` ); } + return parameters; } - await listRegionalParams(); + return await listRegionalParams(); // [END parametermanager_list_regional_params] } +module.exports.main = main; -const args = process.argv.slice(2); -main(...args).catch(console.error); +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/regional_samples/renderRegionalParamVersion.js b/parametermanager/regional_samples/renderRegionalParamVersion.js index a52c48f306..836ad49cd8 100644 --- a/parametermanager/regional_samples/renderRegionalParamVersion.js +++ b/parametermanager/regional_samples/renderRegionalParamVersion.js @@ -65,10 +65,10 @@ async function main( }; // Render the parameter version - const [response] = await client.renderParameterVersion(request); + const [paramVersions] = await client.renderParameterVersion(request); console.log( - `Rendered regional parameter version: ${response.parameterVersion}` + `Rendered regional parameter version: ${paramVersions.parameterVersion}` ); // If the parameter contains secret references, they will be resolved @@ -76,14 +76,24 @@ async function main( // Be cautious with logging or displaying this information. console.log( 'Rendered payload: ', - response.renderedPayload.toString('utf-8') + paramVersions.renderedPayload.toString('utf-8') ); + return paramVersions; } - await renderRegionalParamVersion(); + return await renderRegionalParamVersion(); // [END parametermanager_render_regional_param_version] } +module.exports.main = main; -// Parse command line arguments -const args = process.argv.slice(2); -main(...args).catch(console.error); +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/parametermanager/test/parametermanager.test.js b/parametermanager/test/parametermanager.test.js index 36c6b8fa1b..685da834a3 100644 --- a/parametermanager/test/parametermanager.test.js +++ b/parametermanager/test/parametermanager.test.js @@ -15,7 +15,6 @@ 'use strict'; const {assert} = require('chai'); -const cp = require('child_process'); const {v4: uuidv4} = require('uuid'); const {ParameterManagerClient} = require('@google-cloud/parametermanager'); @@ -35,14 +34,11 @@ 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; @@ -75,17 +71,6 @@ describe('Parameter Manager samples', () => { }, }); 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 () => { @@ -107,122 +92,127 @@ describe('Parameter Manager samples', () => { }); 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` + const sample = require('../regional_samples/createRegionalParamVersionWithSecret'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId, + parameterVersionId + '2', + regionalSecretVersion.name + ); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `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` + const sample = require('../regional_samples/createStructuredRegionalParam'); + const parameter = await sample.main( + 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` + assert.exists(parameter); + assert.equal( + parameter.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-2` ); }); it('should create a regional unstructured parameter', async () => { - const output = execSync( - `node regional_samples/createRegionalParam.js ${projectId} ${locationId} ${regionalParameterId}-3` + const sample = require('../regional_samples/createRegionalParam'); + const parameter = await sample.main( + 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` + assert.exists(parameter); + assert.equal( + parameter.name, + `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}` + const sample = require('../regional_samples/createStructuredRegionalParamVersion'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId + '-2', + parameterVersionId + ); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `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}` + const sample = require('../regional_samples/createRegionalParamVersion'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId + '-3', + parameterVersionId + ); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `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` - ); + const sample = require('../regional_samples/listRegionalParams'); + const parameters = await sample.main(projectId, locationId); + assert.exists(parameters); }); it('should get a regional parameter', async () => { - const output = execSync( - `node regional_samples/getRegionalParam.js ${projectId} ${locationId} ${regionalParameterId}` + const sample = require('../regional_samples/getRegionalParam'); + const parameter = await sample.main( + projectId, + locationId, + regionalParameterId + '-2' ); - assert.include( - output, - `Found regional parameter ${regionalParameter.name} with format ${regionalParameter.format}` + assert.exists(parameter); + assert.equal( + parameter.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-2` ); }); 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` + const sample = require('../regional_samples/listRegionalParamVersions'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId ); + assert.exists(parameterVersion); }); 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}\\")"}` + const sample = require('../regional_samples/getRegionalParamVersion'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId, + parameterVersionId + '2' + ); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}/versions/${parameterVersionId}2` ); }); @@ -246,18 +236,13 @@ describe('Parameter Manager samples', () => { 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"}' + const sample = require('../regional_samples/renderRegionalParamVersion'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId, + parameterVersionId + '2' ); + assert.exists(parameterVersion); }); }); From 232099c5446854bab5c2e9c1a59037e9d07c7cee Mon Sep 17 00:00:00 2001 From: durgesh-ninave-crest Date: Fri, 2 May 2025 10:31:47 +0530 Subject: [PATCH 3/5] fix(parametermanager): update testcases and function arguments --- .../regional_samples/createRegionalParam.js | 6 +- .../createRegionalParamVersion.js | 10 +- .../createRegionalParamVersionWithSecret.js | 10 +- .../createStructuredRegionalParam.js | 9 +- .../createStructuredRegionalParamVersion.js | 10 +- .../regional_samples/getRegionalParam.js | 6 +- .../getRegionalParamVersion.js | 7 +- .../listRegionalParamVersions.js | 6 +- .../regional_samples/listRegionalParams.js | 2 +- .../renderRegionalParamVersion.js | 7 +- .../test/parametermanager.test.js | 107 +++++++++++------- 11 files changed, 90 insertions(+), 90 deletions(-) diff --git a/parametermanager/regional_samples/createRegionalParam.js b/parametermanager/regional_samples/createRegionalParam.js index a3815fae82..0e73e9e704 100644 --- a/parametermanager/regional_samples/createRegionalParam.js +++ b/parametermanager/regional_samples/createRegionalParam.js @@ -23,11 +23,7 @@ * @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' -) { +async function main(projectId, locationId, parameterId) { // [START parametermanager_create_regional_param] /** * TODO(developer): Uncomment these variables before running the sample. diff --git a/parametermanager/regional_samples/createRegionalParamVersion.js b/parametermanager/regional_samples/createRegionalParamVersion.js index a85d63a5f6..97fb0548dc 100644 --- a/parametermanager/regional_samples/createRegionalParamVersion.js +++ b/parametermanager/regional_samples/createRegionalParamVersion.js @@ -26,11 +26,11 @@ * @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' + projectId, + locationId, + parameterId, + parameterVersionId, + payload ) { // [START parametermanager_create_regional_param_version] /** diff --git a/parametermanager/regional_samples/createRegionalParamVersionWithSecret.js b/parametermanager/regional_samples/createRegionalParamVersionWithSecret.js index 70c7b1ebef..dc1da5c2d6 100644 --- a/parametermanager/regional_samples/createRegionalParamVersionWithSecret.js +++ b/parametermanager/regional_samples/createRegionalParamVersionWithSecret.js @@ -26,11 +26,11 @@ * @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' + projectId, + locationId, + parameterId, + parameterVersionId, + secretId ) { // [START parametermanager_create_regional_param_version_with_secret] /** diff --git a/parametermanager/regional_samples/createStructuredRegionalParam.js b/parametermanager/regional_samples/createStructuredRegionalParam.js index 9c44dc4377..6158a0a2d5 100644 --- a/parametermanager/regional_samples/createStructuredRegionalParam.js +++ b/parametermanager/regional_samples/createStructuredRegionalParam.js @@ -14,8 +14,6 @@ '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. @@ -25,12 +23,7 @@ const {protos} = require('@google-cloud/parametermanager'); * @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 -) { +async function main(projectId, locationId, parameterId, formatType) { // [START parametermanager_create_structured_regional_param] /** * TODO(developer): Uncomment these variables before running the sample. diff --git a/parametermanager/regional_samples/createStructuredRegionalParamVersion.js b/parametermanager/regional_samples/createStructuredRegionalParamVersion.js index 5ef80ff793..0a2bef49f2 100644 --- a/parametermanager/regional_samples/createStructuredRegionalParamVersion.js +++ b/parametermanager/regional_samples/createStructuredRegionalParamVersion.js @@ -26,11 +26,11 @@ * @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'} + projectId, + locationId, + parameterId, + parameterVersionId, + payload ) { // [START parametermanager_create_structured_regional_param_version] /** diff --git a/parametermanager/regional_samples/getRegionalParam.js b/parametermanager/regional_samples/getRegionalParam.js index e695a70746..cee52c5421 100644 --- a/parametermanager/regional_samples/getRegionalParam.js +++ b/parametermanager/regional_samples/getRegionalParam.js @@ -22,11 +22,7 @@ * @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' -) { +async function main(projectId, locationId, parameterId) { // [START parametermanager_get_regional_param] /** * TODO(developer): Uncomment these variables before running the sample. diff --git a/parametermanager/regional_samples/getRegionalParamVersion.js b/parametermanager/regional_samples/getRegionalParamVersion.js index eb7dcb5e33..9ecb24e19e 100644 --- a/parametermanager/regional_samples/getRegionalParamVersion.js +++ b/parametermanager/regional_samples/getRegionalParamVersion.js @@ -23,12 +23,7 @@ * @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' -) { +async function main(projectId, locationId, parameterId, versionId) { // [START parametermanager_get_regional_param_version] /** * TODO(developer): Uncomment these variables before running the sample. diff --git a/parametermanager/regional_samples/listRegionalParamVersions.js b/parametermanager/regional_samples/listRegionalParamVersions.js index 31f2e1034c..67dc4eb6e1 100644 --- a/parametermanager/regional_samples/listRegionalParamVersions.js +++ b/parametermanager/regional_samples/listRegionalParamVersions.js @@ -22,11 +22,7 @@ * @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' -) { +async function main(projectId, locationId, parameterId) { // [START parametermanager_list_regional_param_versions] /** * TODO(developer): Uncomment these variables before running the sample. diff --git a/parametermanager/regional_samples/listRegionalParams.js b/parametermanager/regional_samples/listRegionalParams.js index 7617cf31fd..cf78ac2e10 100644 --- a/parametermanager/regional_samples/listRegionalParams.js +++ b/parametermanager/regional_samples/listRegionalParams.js @@ -21,7 +21,7 @@ * @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') { +async function main(projectId, locationId) { // [START parametermanager_list_regional_params] /** * TODO(developer): Uncomment these variables before running the sample. diff --git a/parametermanager/regional_samples/renderRegionalParamVersion.js b/parametermanager/regional_samples/renderRegionalParamVersion.js index 836ad49cd8..8074b1ed61 100644 --- a/parametermanager/regional_samples/renderRegionalParamVersion.js +++ b/parametermanager/regional_samples/renderRegionalParamVersion.js @@ -24,12 +24,7 @@ * @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' -) { +async function main(projectId, locationId, parameterId, parameterVersionId) { // [START parametermanager_render_regional_param_version] /** * TODO(developer): Uncomment these variables before running the sample. diff --git a/parametermanager/test/parametermanager.test.js b/parametermanager/test/parametermanager.test.js index 685da834a3..734d5f584e 100644 --- a/parametermanager/test/parametermanager.test.js +++ b/parametermanager/test/parametermanager.test.js @@ -36,7 +36,10 @@ const regionalSecretClient = new SecretManagerServiceClient(secretOptions); const secretId = `test-secret-${uuidv4()}`; const regionalParameterId = `test-regional-${uuidv4()}`; -const parameterVersionId = 'v1'; +const parameterVersionId = `test-version-${uuidv4()}`; + +const jsonPayload = '{username: "test-user", host: "localhost"}'; +const payload = 'This is unstructured data'; let regionalParameter; let regionalSecret; @@ -44,6 +47,7 @@ let regionalSecretVersion; describe('Parameter Manager samples', () => { const regionalParametersToDelete = []; + const regionalParameterVersionsToDelete = []; before(async () => { projectId = await client.getProjectId(); @@ -74,21 +78,44 @@ describe('Parameter Manager samples', () => { }); after(async () => { - // Clean up - regionalParametersToDelete.forEach(async regionalParameterName => { - await regionalClient.deleteParameterVersion({ - name: `${regionalParameterName}/versions/v1`, + // Delete all parameter versions first + try { + await Promise.all( + regionalParameterVersionsToDelete.map( + async regionalParameterVersionName => { + await regionalClient.deleteParameterVersion({ + name: regionalParameterVersionName, + }); + } + ) + ); + } catch (err) { + if (!err.message.includes('NOT_FOUND')) { + throw err; + } + } + + // Delete all parameters + try { + await Promise.all( + regionalParametersToDelete.map(async regionalParameterName => { + await regionalClient.deleteParameter({name: regionalParameterName}); + }) + ); + } catch (err) { + if (!err.message.includes('NOT_FOUND')) { + throw err; + } + } + try { + await regionalSecretClient.deleteSecret({ + name: regionalSecret.name, }); - if (regionalParameterName === regionalParameter.name) { - await regionalClient.deleteParameterVersion({ - name: `${regionalParameterName}/versions/v12`, - }); + } catch (err) { + if (!err.message.includes('NOT_FOUND')) { + throw err; } - await regionalClient.deleteParameter({name: regionalParameterName}); - }); - await regionalSecretClient.deleteSecret({ - name: regionalSecret.name, - }); + } }); it('should create regional parameter version with secret references', async () => { @@ -97,13 +124,14 @@ describe('Parameter Manager samples', () => { projectId, locationId, regionalParameterId, - parameterVersionId + '2', + parameterVersionId + '-1', regionalSecretVersion.name ); + regionalParameterVersionsToDelete.push(parameterVersion.name); assert.exists(parameterVersion); assert.equal( parameterVersion.name, - `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}/versions/${parameterVersionId}2` + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}/versions/${parameterVersionId}-1` ); }); @@ -112,15 +140,13 @@ describe('Parameter Manager samples', () => { const parameter = await sample.main( projectId, locationId, - regionalParameterId + '-2' - ); - regionalParametersToDelete.push( - client.parameterPath(projectId, locationId, `${regionalParameterId}-2`) + regionalParameterId + '-1' ); + regionalParametersToDelete.push(parameter.name); assert.exists(parameter); assert.equal( parameter.name, - `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-2` + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-1` ); }); @@ -129,15 +155,13 @@ describe('Parameter Manager samples', () => { const parameter = await sample.main( projectId, locationId, - regionalParameterId + '-3' - ); - regionalParametersToDelete.push( - client.parameterPath(projectId, locationId, `${regionalParameterId}-3`) + regionalParameterId + '-2' ); + regionalParametersToDelete.push(parameter.name); assert.exists(parameter); assert.equal( parameter.name, - `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-3` + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-2` ); }); @@ -146,13 +170,15 @@ describe('Parameter Manager samples', () => { const parameterVersion = await sample.main( projectId, locationId, - regionalParameterId + '-2', - parameterVersionId + regionalParameterId + '-1', + parameterVersionId + '-2', + jsonPayload ); + regionalParameterVersionsToDelete.push(parameterVersion.name); assert.exists(parameterVersion); assert.equal( parameterVersion.name, - `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-2/versions/${parameterVersionId}` + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-1/versions/${parameterVersionId}-2` ); }); @@ -161,13 +187,15 @@ describe('Parameter Manager samples', () => { const parameterVersion = await sample.main( projectId, locationId, - regionalParameterId + '-3', - parameterVersionId + regionalParameterId + '-2', + parameterVersionId + '-3', + payload ); + regionalParameterVersionsToDelete.push(parameterVersion.name); assert.exists(parameterVersion); assert.equal( parameterVersion.name, - `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-3/versions/${parameterVersionId}` + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-2/versions/${parameterVersionId}-3` ); }); @@ -182,23 +210,23 @@ describe('Parameter Manager samples', () => { const parameter = await sample.main( projectId, locationId, - regionalParameterId + '-2' + regionalParameterId ); assert.exists(parameter); assert.equal( parameter.name, - `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-2` + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}` ); }); it('should list regional parameter versions', async () => { const sample = require('../regional_samples/listRegionalParamVersions'); - const parameterVersion = await sample.main( + const parameterVersions = await sample.main( projectId, locationId, regionalParameterId ); - assert.exists(parameterVersion); + assert.exists(parameterVersions); }); it('should get a regional parameter version', async () => { @@ -207,13 +235,14 @@ describe('Parameter Manager samples', () => { projectId, locationId, regionalParameterId, - parameterVersionId + '2' + parameterVersionId + '-1' ); assert.exists(parameterVersion); assert.equal( parameterVersion.name, - `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}/versions/${parameterVersionId}2` + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}/versions/${parameterVersionId}-1` ); + assert.equal(parameterVersion.disabled, false); }); it('should render regional parameter version', async () => { @@ -241,7 +270,7 @@ describe('Parameter Manager samples', () => { projectId, locationId, regionalParameterId, - parameterVersionId + '2' + parameterVersionId + '-1' ); assert.exists(parameterVersion); }); From 190fe0970d11e7e8cb60895d56810b718b8f2053 Mon Sep 17 00:00:00 2001 From: durgesh-ninave-crest Date: Fri, 2 May 2025 11:35:57 +0530 Subject: [PATCH 4/5] fix(parametermanager): fix lint issue --- parametermanager/test/.eslintrc.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parametermanager/test/.eslintrc.yml b/parametermanager/test/.eslintrc.yml index 9351c489b5..74add4846e 100644 --- a/parametermanager/test/.eslintrc.yml +++ b/parametermanager/test/.eslintrc.yml @@ -14,4 +14,4 @@ --- env: - mocha: true \ No newline at end of file + mocha: true From d35505a6399272f35e1ea6ddbd68db2a9719f8b5 Mon Sep 17 00:00:00 2001 From: durgesh-ninave-crest Date: Fri, 2 May 2025 15:48:43 +0530 Subject: [PATCH 5/5] fix(parametermanager): update testcase --- .../test/parametermanager.test.js | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/parametermanager/test/parametermanager.test.js b/parametermanager/test/parametermanager.test.js index 734d5f584e..90fb1614c1 100644 --- a/parametermanager/test/parametermanager.test.js +++ b/parametermanager/test/parametermanager.test.js @@ -79,34 +79,35 @@ describe('Parameter Manager samples', () => { after(async () => { // Delete all parameter versions first - try { - await Promise.all( - regionalParameterVersionsToDelete.map( - async regionalParameterVersionName => { + await Promise.all( + regionalParameterVersionsToDelete.map( + async regionalParameterVersionName => { + try { await regionalClient.deleteParameterVersion({ name: regionalParameterVersionName, }); + } catch (err) { + if (!err.message.includes('NOT_FOUND')) { + throw err; + } } - ) - ); - } catch (err) { - if (!err.message.includes('NOT_FOUND')) { - throw err; - } - } + } + ) + ); // Delete all parameters - try { - await Promise.all( - regionalParametersToDelete.map(async regionalParameterName => { + await Promise.all( + regionalParametersToDelete.map(async regionalParameterName => { + try { await regionalClient.deleteParameter({name: regionalParameterName}); - }) - ); - } catch (err) { - if (!err.message.includes('NOT_FOUND')) { - throw err; - } - } + } catch (err) { + if (!err.message.includes('NOT_FOUND')) { + throw err; + } + } + }) + ); + try { await regionalSecretClient.deleteSecret({ name: regionalSecret.name,