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/deleteParam.js b/parametermanager/deleteParam.js new file mode 100644 index 0000000000..6753e7c269 --- /dev/null +++ b/parametermanager/deleteParam.js @@ -0,0 +1,55 @@ +// 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'; + +/** + * Deletes a parameter from the global location 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} parameterId - The ID of the parameter to delete. + */ +async function main(projectId = 'my-project', parameterId = 'my-parameter') { + // [START parametermanager_delete_param] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const parameterId = 'my-parameter'; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Instantiates a client + const client = new ParameterManagerClient(); + + async function deleteParam() { + // Construct the fully qualified parameter name + const name = client.parameterPath(projectId, 'global', parameterId); + + // Delete the parameter + await client.deleteParameter({ + name: name, + }); + + console.log(`Deleted parameter: ${name}`); + } + + await deleteParam(); + // [END parametermanager_delete_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/deleteParamVersion.js b/parametermanager/deleteParamVersion.js new file mode 100644 index 0000000000..d5e0da3743 --- /dev/null +++ b/parametermanager/deleteParamVersion.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'; + +/** + * Deletes a specific version of an existing parameter in the global location + * 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} parameterId - The ID of the parameter for which version is to be deleted. + * @param {string} versionId - The version ID of the parameter to delete. + */ +async function main( + projectId = 'my-project', + parameterId = 'my-parameter', + versionId = 'v1' +) { + // [START parametermanager_delete_param_version] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const parameterId = 'my-parameter'; + // const versionId = 'v1'; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Instantiates a client + const client = new ParameterManagerClient(); + + async function deleteParamVersion() { + // Construct the fully qualified parameter version name + const name = client.parameterVersionPath( + projectId, + 'global', + parameterId, + versionId + ); + + // Delete the parameter version + await client.deleteParameterVersion({ + name: name, + }); + + console.log(`Deleted parameter version: ${name}`); + } + + await deleteParamVersion(); + // [END parametermanager_delete_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/disableParamVersion.js b/parametermanager/disableParamVersion.js new file mode 100644 index 0000000000..237767910f --- /dev/null +++ b/parametermanager/disableParamVersion.js @@ -0,0 +1,80 @@ +// 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'; + +/** + * Disables a specific version of a global parameter in Google Cloud Parameter Manager. + * This function demonstrates how to disable a global parameter version by setting + * its 'disabled' field to true using the Parameter Manager client library. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is located. + * @param {string} parameterId - The ID of the parameter for which version is to be disabled. + * @param {string} versionId - The version ID of the parameter to be disabled. + */ +async function main( + projectId = 'my-project', + parameterId = 'my-parameter', + versionId = 'v1' +) { + // [START parametermanager_disable_param_version] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const parameterId = 'my-parameter'; + // const versionId = 'v1'; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Instantiates a client + const client = new ParameterManagerClient(); + + async function disableParamVersion() { + // Construct the full resource name + const name = client.parameterVersionPath( + projectId, + 'global', + parameterId, + versionId + ); + + // Construct the request + const request = { + parameterVersion: { + name: name, + disabled: true, + }, + updateMask: { + paths: ['disabled'], + }, + }; + + // Make the API call to update the parameter version + const [response] = await client.updateParameterVersion(request); + + console.log( + `Disabled parameter version ${response.name} for parameter ${parameterId}` + ); + return response; + } + + await disableParamVersion(); + // [END parametermanager_disable_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/enableParamVersion.js b/parametermanager/enableParamVersion.js new file mode 100644 index 0000000000..aa05e17932 --- /dev/null +++ b/parametermanager/enableParamVersion.js @@ -0,0 +1,80 @@ +// 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'; + +/** + * Enables a specific version of a parameter in Google Cloud Parameter Manager. + * This function demonstrates how to enable a parameter version by setting + * its 'disabled' field to false using the Parameter Manager client library. + * + * @param {string} projectId - The Google Cloud project ID where the parameter is located. + * @param {string} parameterId - The ID of the parameter for which version is to be enabled. + * @param {string} versionId - The version ID of the parameter to be enabled. + */ +async function main( + projectId = 'my-project', + parameterId = 'my-parameter', + versionId = 'v1' +) { + // [START parametermanager_enable_param_version] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const parameterId = 'my-parameter'; + // const versionId = 'v1'; + + // Imports the Parameter Manager library + const {ParameterManagerClient} = require('@google-cloud/parametermanager'); + + // Instantiates a client + const client = new ParameterManagerClient(); + + async function enableParamVersion() { + // Construct the full resource name + const name = client.parameterVersionPath( + projectId, + 'global', + parameterId, + versionId + ); + + // Construct the request + const request = { + parameterVersion: { + name: name, + disabled: false, + }, + updateMask: { + paths: ['disabled'], + }, + }; + + // Make the API call to update the parameter version + const [response] = await client.updateParameterVersion(request); + + console.log( + `Enabled parameter version ${response.name} for parameter ${parameterId}` + ); + return response; + } + + await enableParamVersion(); + // [END parametermanager_enable_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/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/quickstart.js b/parametermanager/quickstart.js new file mode 100644 index 0000000000..ad4d71e7ae --- /dev/null +++ b/parametermanager/quickstart.js @@ -0,0 +1,97 @@ +// 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. + +/** + * This is a quickstart sample for the Google Cloud Parameter Manager. + * It demonstrates how to create a parameter, create a parameter version, + * view the parameter version, and render its payload. + */ + +'use strict'; + +/** + * Quickstart example for using Google Cloud Parameter Manager to + * create a global parameter, add a version with a JSON payload, + * and fetch the parameter version details. + * + * @param {string} projectId - The Google Cloud project ID where parameter is created. + * @param {string} parameterId - The ID of the new parameter. + * @param {string} parameterVersionId - The ID of the parameter version. + */ +async function main( + projectId = 'my-project', + parameterId = 'my-parameter', + parameterVersionId = 'v1' +) { + // [START parametermanager_quickstart] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const parameterId = 'my-parameter'; + // const parameterVersionId = 'v1'; + + // Imports the Google Cloud Parameter Manager library + const { + ParameterManagerClient, + protos, + } = require('@google-cloud/parametermanager'); + + // Instantiates a client + const client = new ParameterManagerClient(); + + async function quickstart() { + const parent = client.locationPath(projectId, 'global'); + const parameterRequest = { + parent: parent, + parameterId: parameterId, + parameter: { + format: protos.google.cloud.parametermanager.v1.ParameterFormat.JSON, + }, + }; + + // Create a new parameter + const [parameter] = await client.createParameter(parameterRequest); + console.log( + `Created parameter ${parameter.name} with format ${parameter.format}` + ); + + const payload = {username: 'test-user', host: 'localhost'}; + // Create a new parameter version + const [parameterVersion] = await client.createParameterVersion({ + parent: parameter.name, + parameterVersionId: parameterVersionId, + parameterVersion: { + payload: { + data: Buffer.from(JSON.stringify(payload), 'utf8'), + }, + }, + }); + console.log(`Created parameter version: ${parameterVersion.name}`); + + // Get the parameter version + const [response] = await client.getParameterVersion({ + name: parameterVersion.name, + }); + console.log(`Retrieved parameter version: ${response.name}`); + console.log('Payload:', response.payload.data.toString('utf8')); + } + + await quickstart(); + // [END parametermanager_quickstart] +} + +// 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/deleteRegionalParam.js b/parametermanager/regional_samples/deleteRegionalParam.js new file mode 100644 index 0000000000..9f2e183a06 --- /dev/null +++ b/parametermanager/regional_samples/deleteRegionalParam.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'; + +/** + * Deletes 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 delete. + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-parameter' +) { + // [START parametermanager_delete_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 deleteRegionalParam() { + // Construct the fully qualified parameter name + const name = client.parameterPath(projectId, locationId, parameterId); + + // Delete the parameter + await client.deleteParameter({ + name: name, + }); + + console.log(`Deleted regional parameter: ${name}`); + } + + await deleteRegionalParam(); + // [END parametermanager_delete_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/deleteRegionalParamVersion.js b/parametermanager/regional_samples/deleteRegionalParamVersion.js new file mode 100644 index 0000000000..bab32ec8b3 --- /dev/null +++ b/parametermanager/regional_samples/deleteRegionalParamVersion.js @@ -0,0 +1,75 @@ +// 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'; + +/** + * Deletes 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 is to be deleted. + * @param {string} versionId - The version ID of the parameter to delete. + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-parameter', + versionId = 'v1' +) { + // [START parametermanager_delete_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 deleteRegionalParamVersion() { + // Construct the fully qualified parameter version name + const name = client.parameterVersionPath( + projectId, + locationId, + parameterId, + versionId + ); + + // Delete the parameter version + await client.deleteParameterVersion({ + name: name, + }); + + console.log(`Deleted regional parameter version: ${name}`); + } + + await deleteRegionalParamVersion(); + // [END parametermanager_delete_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/disableRegionalParamVersion.js b/parametermanager/regional_samples/disableRegionalParamVersion.js new file mode 100644 index 0000000000..8992c6c2a6 --- /dev/null +++ b/parametermanager/regional_samples/disableRegionalParamVersion.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'; + +/** + * Disables a specific version of an existing parameter in the specified region + * of the specified project using the Google Cloud Parameter Manager SDK. + * + * This function demonstrates how to disable a global parameter version by setting + * its 'disabled' field to true using the Parameter Manager client library. + * + * @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 is to be disabled. + * @param {string} versionId - The version ID of the parameter to be disabled. + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-parameter', + versionId = 'v1' +) { + // [START parametermanager_disable_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 disableRegionalParamVersion() { + // Construct the full resource name + const name = client.parameterVersionPath( + projectId, + locationId, + parameterId, + versionId + ); + + // Construct the request + const request = { + parameterVersion: { + name: name, + disabled: true, + }, + updateMask: { + paths: ['disabled'], + }, + }; + + // Make the API call to update the parameter version + const [response] = await client.updateParameterVersion(request); + + console.log( + `Disabled regional parameter version ${response.name} for parameter ${parameterId}` + ); + return response; + } + + await disableRegionalParamVersion(); + // [END parametermanager_disable_regional_param_version] +} + +// The command-line arguments are passed as an array to main(). +main(...process.argv.slice(2)).catch(console.error); diff --git a/parametermanager/regional_samples/enableRegionalParamVersion.js b/parametermanager/regional_samples/enableRegionalParamVersion.js new file mode 100644 index 0000000000..2351d472e9 --- /dev/null +++ b/parametermanager/regional_samples/enableRegionalParamVersion.js @@ -0,0 +1,87 @@ +// 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'; + +/** + * Enables a specific version of a regional parameter in Google Cloud Parameter Manager. + * This function demonstrates how to enable a regional parameter version by setting + * its 'disabled' field to false using the Parameter Manager client library. + * + * @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 is to be enabled. + * @param {string} versionId - The version ID of the parameter to be enabled. + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-parameter', + versionId = 'v1' +) { + // [START parametermanager_enable_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 enableRegionalParamVersion() { + // Construct the full resource name + const name = client.parameterVersionPath( + projectId, + locationId, + parameterId, + versionId + ); + + // Construct the request + const request = { + parameterVersion: { + name: name, + disabled: false, + }, + updateMask: { + paths: ['disabled'], + }, + }; + + // Make the API call to update the parameter version + const [response] = await client.updateParameterVersion(request); + + console.log( + `Enabled regional parameter version ${response.name} for parameter ${parameterId}` + ); + return response; + } + + await enableRegionalParamVersion(); + // [END parametermanager_enable_regional_param_version] +} + +// The command-line arguments are passed as an array to main(). +main(...process.argv.slice(2)).catch(console.error); diff --git a/parametermanager/regional_samples/regionalQuickstart.js b/parametermanager/regional_samples/regionalQuickstart.js new file mode 100644 index 0000000000..1a2b8f24ea --- /dev/null +++ b/parametermanager/regional_samples/regionalQuickstart.js @@ -0,0 +1,99 @@ +// 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'; + +/** + * Quickstart example for using Google Cloud Parameter Manager to + * create a regional parameter, add a version with a JSON payload, + * fetch the parameter version details and render its payload. + * + * @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} parameterVersionId - The ID of the parameter version to create. + */ +async function main( + projectId = 'my-project', + locationId = 'us-central1', + parameterId = 'my-parameter', + parameterVersionId = 'v1' +) { + // [START parametermanager_regional_quickstart] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project'; + // const locationId = 'us-central1'; + // const parameterId = 'my-parameter'; + // const parameterVersionId = 'v1'; + + // Imports the Google Cloud Parameter Manager library + const { + ParameterManagerClient, + protos, + } = 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 regionalQuickstart() { + const parent = client.locationPath(projectId, locationId); + const parameterRequest = { + parent: parent, + parameterId: parameterId, + parameter: { + format: protos.google.cloud.parametermanager.v1.ParameterFormat.JSON, + }, + }; + + // Create a new parameter + const [parameter] = await client.createParameter(parameterRequest); + console.log( + `Created regional parameter ${parameter.name} with format ${parameter.format}` + ); + + const payload = {username: 'test-user', host: 'localhost'}; + // Create a new parameter version + const [parameterVersion] = await client.createParameterVersion({ + parent: parameter.name, + parameterVersionId: parameterVersionId, + parameterVersion: { + payload: { + data: Buffer.from(JSON.stringify(payload), 'utf8'), + }, + }, + }); + console.log(`Created regional parameter version: ${parameterVersion.name}`); + + // Get the parameter version + const [response] = await client.getParameterVersion({ + name: parameterVersion.name, + }); + console.log(`Retrieved regional parameter version: ${response.name}`); + console.log('Payload:', response.payload.data.toString('utf8')); + } + + await regionalQuickstart(); + // [END parametermanager_regional_quickstart] +} + +// 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/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..8323853801 --- /dev/null +++ b/parametermanager/test/parametermanager.test.js @@ -0,0 +1,226 @@ +// 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(); + +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 execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +const parameterId = `test-parameter-${uuidv4()}`; +const regionalParameterId = `test-regional-${uuidv4()}`; +const parameterVersionId = 'v1'; + +let parameter; +let regionalParameter; +let parameterVersion; +let regionalParameterVersion; + +describe('Parameter Manager samples', () => { + const parametersToDelete = []; + const regionalParametersToDelete = []; + + before(async () => { + projectId = await client.getProjectId(); + + // Create a test global parameter + [parameter] = await client.createParameter({ + parent: `projects/${projectId}/locations/global`, + parameterId: parameterId, + parameter: { + format: 'JSON', + }, + }); + + // Create a test regional parameter + [regionalParameter] = await regionalClient.createParameter({ + parent: `projects/${projectId}/locations/${locationId}`, + parameterId: regionalParameterId, + parameter: { + format: 'JSON', + }, + }); + + // Create a version for the global parameter + [parameterVersion] = await client.createParameterVersion({ + parent: parameter.name, + parameterVersionId: parameterVersionId, + parameterVersion: { + payload: { + data: Buffer.from(JSON.stringify({key: 'global_value'}), 'utf-8'), + }, + }, + }); + + // 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 + parametersToDelete.forEach(async parameterName => { + await client.deleteParameterVersion({ + name: `${parameterName}/versions/v1`, + }); + await client.deleteParameter({name: parameterName}); + }); + regionalParametersToDelete.forEach(async regionalParameterName => { + await regionalClient.deleteParameterVersion({ + name: `${regionalParameterName}/versions/v1`, + }); + await regionalClient.deleteParameter({name: regionalParameterName}); + }); + }); + + it('should runs the quickstart', async () => { + const output = execSync( + `node quickstart.js ${projectId} ${parameterId}-quickstart ${parameterVersionId}` + ); + parametersToDelete.push(`${parameterId}-quickstart`); + assert.include( + output, + `Created parameter projects/${projectId}/locations/global/parameters/${parameterId}-quickstart with format JSON` + ); + assert.include( + output, + `Created parameter version: projects/${projectId}/locations/global/parameters/${parameterId}-quickstart/versions/v1` + ); + assert.include( + output, + `Retrieved parameter version: projects/${projectId}/locations/global/parameters/${parameterId}-quickstart/versions/v1` + ); + assert.include( + output, + 'Payload: {"username":"test-user","host":"localhost"}' + ); + }); + + it('should runs the regional quickstart', async () => { + const output = execSync( + `node regional_samples/regionalQuickstart.js ${projectId} ${locationId} ${regionalParameterId}-quickstart ${parameterVersionId}` + ); + regionalParametersToDelete.push(`${regionalParameterId}-quickstart`); + assert.include( + output, + `Created regional parameter projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-quickstart with format JSON` + ); + assert.include( + output, + `Created regional parameter version: projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-quickstart/versions/v1` + ); + assert.include( + output, + `Retrieved regional parameter version: projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-quickstart/versions/v1` + ); + assert.include( + output, + 'Payload: {"username":"test-user","host":"localhost"}' + ); + }); + + it('should disable a parameter version', async () => { + const output = execSync( + `node disableParamVersion.js ${projectId} ${parameterId} ${parameterVersionId}` + ); + assert.include( + output, + `Disabled parameter version ${parameterVersion.name} for parameter ${parameterId}` + ); + }); + + it('should disable a regional parameter version', async () => { + const output = execSync( + `node regional_samples/disableRegionalParamVersion.js ${projectId} ${locationId} ${regionalParameterId} ${parameterVersionId}` + ); + assert.include( + output, + `Disabled regional parameter version ${regionalParameterVersion.name} for parameter ${regionalParameterId}` + ); + }); + + it('should enable a parameter version', async () => { + const output = execSync( + `node enableParamVersion.js ${projectId} ${parameterId} ${parameterVersionId}` + ); + assert.include( + output, + `Enabled parameter version ${parameterVersion.name} for parameter ${parameterId}` + ); + }); + + it('should enable a regional parameter version', async () => { + const output = execSync( + `node regional_samples/enableRegionalParamVersion.js ${projectId} ${locationId} ${regionalParameterId} ${parameterVersionId}` + ); + assert.include( + output, + `Enabled regional parameter version ${regionalParameterVersion.name} for parameter ${regionalParameterId}` + ); + }); + + it('should delete a parameter version', async () => { + const output = execSync( + `node deleteParamVersion.js ${projectId} ${parameterId} ${parameterVersionId}` + ); + assert.include( + output, + `Deleted parameter version: ${parameterVersion.name}` + ); + }); + + it('should delete a regional parameter version', async () => { + const output = execSync( + `node regional_samples/deleteRegionalParamVersion.js ${projectId} ${locationId} ${regionalParameterId} ${parameterVersionId}` + ); + assert.include( + output, + `Deleted regional parameter version: ${regionalParameterVersion.name}` + ); + }); + + it('should delete a parameter', async () => { + const output = execSync(`node deleteParam.js ${projectId} ${parameterId}`); + assert.include(output, `Deleted parameter: ${parameter.name}`); + }); + + it('should delete a regional parameter', async () => { + const output = execSync( + `node regional_samples/deleteRegionalParam.js ${projectId} ${locationId} ${regionalParameterId}` + ); + assert.include( + output, + `Deleted regional parameter: ${regionalParameter.name}` + ); + }); +});