diff --git a/parametermanager/deleteParam.js b/parametermanager/deleteParam.js new file mode 100644 index 0000000000..f801578b75 --- /dev/null +++ b/parametermanager/deleteParam.js @@ -0,0 +1,65 @@ +// 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, parameterId) { + // [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}`); + return name; + } + + return await deleteParam(); + // [END parametermanager_delete_param] +} +module.exports.main = main; + +/* 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/deleteParamVersion.js b/parametermanager/deleteParamVersion.js new file mode 100644 index 0000000000..518fee0f20 --- /dev/null +++ b/parametermanager/deleteParamVersion.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'; + +/** + * 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, parameterId, versionId) { + // [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}`); + return name; + } + + return await deleteParamVersion(); + // [END parametermanager_delete_param_version] +} +module.exports.main = main; + +/* 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/disableParamVersion.js b/parametermanager/disableParamVersion.js new file mode 100644 index 0000000000..890ee7b549 --- /dev/null +++ b/parametermanager/disableParamVersion.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'; + +/** + * 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, parameterId, versionId) { + // [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 [parameterVersion] = await client.updateParameterVersion(request); + + console.log( + `Disabled parameter version ${parameterVersion.name} for parameter ${parameterId}` + ); + return parameterVersion; + } + + return await disableParamVersion(); + // [END parametermanager_disable_param_version] +} +module.exports.main = main; + +/* 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/enableParamVersion.js b/parametermanager/enableParamVersion.js new file mode 100644 index 0000000000..6ae5c68205 --- /dev/null +++ b/parametermanager/enableParamVersion.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'; + +/** + * 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, parameterId, versionId) { + // [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 [parameterVersion] = await client.updateParameterVersion(request); + + console.log( + `Enabled parameter version ${parameterVersion.name} for parameter ${parameterId}` + ); + return parameterVersion; + } + + return await enableParamVersion(); + // [END parametermanager_enable_param_version] +} +module.exports.main = main; + +/* 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/quickstart.js b/parametermanager/quickstart.js new file mode 100644 index 0000000000..67197845d6 --- /dev/null +++ b/parametermanager/quickstart.js @@ -0,0 +1,103 @@ +// 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, parameterId, parameterVersionId) { + // [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 [paramVersion] = await client.getParameterVersion({ + name: parameterVersion.name, + }); + console.log(`Retrieved parameter version: ${paramVersion.name}`); + console.log('Payload:', paramVersion.payload.data.toString('utf8')); + return paramVersion; + } + + return await quickstart(); + // [END parametermanager_quickstart] +} +module.exports.main = main; + +/* 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/deleteRegionalParam.js b/parametermanager/regional_samples/deleteRegionalParam.js new file mode 100644 index 0000000000..8a9b9700b0 --- /dev/null +++ b/parametermanager/regional_samples/deleteRegionalParam.js @@ -0,0 +1,73 @@ +// 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, locationId, parameterId) { + // [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}`); + return name; + } + + return await deleteRegionalParam(); + // [END parametermanager_delete_regional_param] +} +module.exports.main = main; + +/* 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/deleteRegionalParamVersion.js b/parametermanager/regional_samples/deleteRegionalParamVersion.js new file mode 100644 index 0000000000..877a910c5a --- /dev/null +++ b/parametermanager/regional_samples/deleteRegionalParamVersion.js @@ -0,0 +1,79 @@ +// 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, locationId, parameterId, versionId) { + // [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}`); + return name; + } + + return await deleteRegionalParamVersion(); + // [END parametermanager_delete_regional_param_version] +} +module.exports.main = main; + +/* 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/disableRegionalParamVersion.js b/parametermanager/regional_samples/disableRegionalParamVersion.js new file mode 100644 index 0000000000..cbdf4ca459 --- /dev/null +++ b/parametermanager/regional_samples/disableRegionalParamVersion.js @@ -0,0 +1,94 @@ +// 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, locationId, parameterId, versionId) { + // [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 [paramVersion] = await client.updateParameterVersion(request); + + console.log( + `Disabled regional parameter version ${paramVersion.name} for parameter ${parameterId}` + ); + return paramVersion; + } + + return await disableRegionalParamVersion(); + // [END parametermanager_disable_regional_param_version] +} +module.exports.main = main; + +/* 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/enableRegionalParamVersion.js b/parametermanager/regional_samples/enableRegionalParamVersion.js new file mode 100644 index 0000000000..2c686eaa29 --- /dev/null +++ b/parametermanager/regional_samples/enableRegionalParamVersion.js @@ -0,0 +1,92 @@ +// 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, locationId, parameterId, versionId) { + // [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 [paramVersion] = await client.updateParameterVersion(request); + + console.log( + `Enabled regional parameter version ${paramVersion.name} for parameter ${parameterId}` + ); + return paramVersion; + } + + return await enableRegionalParamVersion(); + // [END parametermanager_enable_regional_param_version] +} +module.exports.main = main; + +/* 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/regionalQuickstart.js b/parametermanager/regional_samples/regionalQuickstart.js new file mode 100644 index 0000000000..eca72585b9 --- /dev/null +++ b/parametermanager/regional_samples/regionalQuickstart.js @@ -0,0 +1,104 @@ +// 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, locationId, parameterId, parameterVersionId) { + // [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 [paramVersion] = await client.getParameterVersion({ + name: parameterVersion.name, + }); + console.log(`Retrieved regional parameter version: ${paramVersion.name}`); + console.log('Payload:', paramVersion.payload.data.toString('utf8')); + return paramVersion; + } + + return await regionalQuickstart(); + // [END parametermanager_regional_quickstart] +} +module.exports.main = main; + +/* 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 713d700d23..e19bb2d87a 100644 --- a/parametermanager/test/parametermanager.test.js +++ b/parametermanager/test/parametermanager.test.js @@ -52,6 +52,10 @@ let parameter; let regionalParameter; let secret; let secretVersion; +let parameterToDelete; +let regionalParameterToDelete; +let parameterVersion; +let regionalParameterVersion; let keyRing; let kmsKey; @@ -65,6 +69,7 @@ describe('Parameter Manager samples', () => { const parametersToDelete = []; const parameterVersionsToDelete = []; const regionalParametersToDelete = []; + const regionalParameterVersionsToDelete = []; before(async () => { projectId = await client.getProjectId(); @@ -114,6 +119,50 @@ describe('Parameter Manager samples', () => { }); regionalParametersToDelete.push(regionalParameter.name); + // Create a test global parameter for delete use case + [parameterToDelete] = await client.createParameter({ + parent: `projects/${projectId}/locations/global`, + parameterId: parameterId + '-3', + parameter: { + format: 'JSON', + }, + }); + parametersToDelete.push(parameterToDelete.name); + + // 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'), + }, + }, + }); + parameterVersionsToDelete.push(parameterVersion.name); + + // Create a test regional parameter for delete use case + [regionalParameterToDelete] = await regionalClient.createParameter({ + parent: `projects/${projectId}/locations/${locationId}`, + parameterId: regionalParameterId + '-3', + parameter: { + format: 'JSON', + }, + }); + regionalParametersToDelete.push(regionalParameterToDelete.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'), + }, + }, + }); + regionalParameterVersionsToDelete.push(regionalParameterVersion.name); + try { await kmsClient.getKeyRing({name: keyRing}); } catch (error) { @@ -250,15 +299,35 @@ describe('Parameter Manager samples', () => { } } - regionalParametersToDelete.forEach(async regionalParameterName => { - try { - await regionalClient.deleteParameter({name: regionalParameterName}); - } catch (err) { - if (!err.message.includes('NOT_FOUND')) { - throw err; + // Delete all regional parameter versions first + await Promise.all( + regionalParameterVersionsToDelete.map( + async regionalParameterVersionName => { + try { + await regionalClient.deleteParameterVersion({ + name: regionalParameterVersionName, + }); + } catch (err) { + if (!err.message.includes('NOT_FOUND')) { + throw err; + } + } } - } - }); + ) + ); + + // Delete all regional parameters + await Promise.all( + regionalParametersToDelete.map(async regionalParameterName => { + try { + await regionalClient.deleteParameter({name: regionalParameterName}); + } catch (err) { + if (!err.message.includes('NOT_FOUND')) { + throw err; + } + } + }) + ); try { await kmsClient.destroyCryptoKeyVersion({ @@ -513,4 +582,152 @@ describe('Parameter Manager samples', () => { `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}` ); }); + + it('should runs the quickstart', async () => { + const sample = require('../quickstart'); + const parameterVersion = await sample.main( + projectId, + parameterId + '-quickstart', + parameterVersionId + ); + parametersToDelete.push( + `projects/${projectId}/locations/global/parameters/${parameterId}-quickstart` + ); + parameterVersionsToDelete.push(parameterVersion.name); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `projects/${projectId}/locations/global/parameters/${parameterId}-quickstart/versions/${parameterVersionId}` + ); + }); + + it('should runs the regional quickstart', async () => { + const sample = require('../regional_samples/regionalQuickstart'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId + '-quickstart', + parameterVersionId + ); + regionalParametersToDelete.push( + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-quickstart` + ); + regionalParameterVersionsToDelete.push(parameterVersion.name); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-quickstart/versions/${parameterVersionId}` + ); + }); + + it('should disable a parameter version', async () => { + const sample = require('../disableParamVersion'); + const parameterVersion = await sample.main( + projectId, + parameterId, + parameterVersionId + ); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `projects/${projectId}/locations/global/parameters/${parameterId}/versions/${parameterVersionId}` + ); + }); + + it('should disable a regional parameter version', async () => { + const sample = require('../regional_samples/disableRegionalParamVersion'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId, + parameterVersionId + ); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}/versions/${parameterVersionId}` + ); + }); + + it('should enable a parameter version', async () => { + const sample = require('../enableParamVersion'); + const parameterVersion = await sample.main( + projectId, + parameterId, + parameterVersionId + ); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `projects/${projectId}/locations/global/parameters/${parameterId}/versions/${parameterVersionId}` + ); + }); + + it('should enable a regional parameter version', async () => { + const sample = require('../regional_samples/enableRegionalParamVersion'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId, + parameterVersionId + ); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}/versions/${parameterVersionId}` + ); + }); + + it('should delete a parameter version', async () => { + const sample = require('../deleteParamVersion'); + const parameterVersion = await sample.main( + projectId, + parameterId, + parameterVersionId + ); + assert.exists(parameterVersion); + assert.equal( + parameterVersion, + `projects/${projectId}/locations/global/parameters/${parameterId}/versions/${parameterVersionId}` + ); + }); + + it('should delete a regional parameter version', async () => { + const sample = require('../regional_samples/deleteRegionalParamVersion'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId, + parameterVersionId + ); + assert.exists(parameterVersion); + assert.equal( + parameterVersion, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}/versions/${parameterVersionId}` + ); + }); + + it('should delete a parameter', async () => { + const sample = require('../deleteParam'); + const parameterVersion = await sample.main(projectId, parameterId + '-3'); + assert.exists(parameterVersion); + assert.equal( + parameterVersion, + `projects/${projectId}/locations/global/parameters/${parameterId}-3` + ); + }); + + it('should delete a regional parameter', async () => { + const sample = require('../regional_samples/deleteRegionalParam'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId + '-3' + ); + assert.exists(parameterVersion); + assert.equal( + parameterVersion, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-3` + ); + }); });