diff --git a/parametermanager/regional_samples/createRegionalParam.js b/parametermanager/regional_samples/createRegionalParam.js new file mode 100644 index 0000000000..0e73e9e704 --- /dev/null +++ b/parametermanager/regional_samples/createRegionalParam.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'; + +/** + * 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, locationId, parameterId) { + // [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}`); + return parameter; + } + + return await createRegionalParam(); + // [END parametermanager_create_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/createRegionalParamVersion.js b/parametermanager/regional_samples/createRegionalParamVersion.js new file mode 100644 index 0000000000..97fb0548dc --- /dev/null +++ b/parametermanager/regional_samples/createRegionalParamVersion.js @@ -0,0 +1,95 @@ +// 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, + locationId, + parameterId, + parameterVersionId, + payload +) { + // [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 [paramVersion] = await client.createParameterVersion(request); + console.log(`Created regional parameter version: ${paramVersion.name}`); + return paramVersion; + } + + return await createRegionalParamVersion(); + // [END parametermanager_create_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/createRegionalParamVersionWithSecret.js b/parametermanager/regional_samples/createRegionalParamVersionWithSecret.js new file mode 100644 index 0000000000..dc1da5c2d6 --- /dev/null +++ b/parametermanager/regional_samples/createRegionalParamVersionWithSecret.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. + +'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, + locationId, + parameterId, + parameterVersionId, + secretId +) { + // [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 [paramVersion] = await client.createParameterVersion(request); + console.log( + `Created regional parameter version with secret: ${paramVersion.name}` + ); + return paramVersion; + } + + return await createRegionalParamVersionWithSecret(); + // [END parametermanager_create_regional_param_version_with_secret] +} +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/createStructuredRegionalParam.js b/parametermanager/regional_samples/createStructuredRegionalParam.js new file mode 100644 index 0000000000..6158a0a2d5 --- /dev/null +++ b/parametermanager/regional_samples/createStructuredRegionalParam.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'; + +/** + * 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, locationId, parameterId, formatType) { + // [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}` + ); + return parameter; + } + + return await createStructuredRegionalParam(); + // [END parametermanager_create_structured_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/createStructuredRegionalParamVersion.js b/parametermanager/regional_samples/createStructuredRegionalParamVersion.js new file mode 100644 index 0000000000..0a2bef49f2 --- /dev/null +++ b/parametermanager/regional_samples/createStructuredRegionalParamVersion.js @@ -0,0 +1,95 @@ +// 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, + locationId, + parameterId, + parameterVersionId, + payload +) { + // [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 [paramVersion] = await client.createParameterVersion(request); + console.log(`Created regional parameter version: ${paramVersion.name}`); + return paramVersion; + } + + return await createStructuredRegionalParamVersion(); + // [END parametermanager_create_structured_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/getRegionalParam.js b/parametermanager/regional_samples/getRegionalParam.js new file mode 100644 index 0000000000..cee52c5421 --- /dev/null +++ b/parametermanager/regional_samples/getRegionalParam.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'; + +/** + * 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, locationId, parameterId) { + // [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}` + ); + return parameter; + } + + return await getRegionalParam(); + // [END parametermanager_get_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/getRegionalParamVersion.js b/parametermanager/regional_samples/getRegionalParamVersion.js new file mode 100644 index 0000000000..9ecb24e19e --- /dev/null +++ b/parametermanager/regional_samples/getRegionalParamVersion.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 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, locationId, parameterId, versionId) { + // [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')}` + ); + } + return parameterVersion; + } + + return await getRegionalParamVersion(); + // [END parametermanager_get_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/listRegionalParamVersions.js b/parametermanager/regional_samples/listRegionalParamVersions.js new file mode 100644 index 0000000000..67dc4eb6e1 --- /dev/null +++ b/parametermanager/regional_samples/listRegionalParamVersions.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'; + +/** + * 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, locationId, parameterId) { + // [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 paramVersions = await client.listParameterVersionsAsync(request); + + for await (const version of paramVersions) { + console.log( + `Found regional parameter version ${version.name} with state ${version.disabled ? 'disabled' : 'enabled'} ` + ); + } + return paramVersions; + } + + return await listRegionalParamVersions(); + // [END parametermanager_list_regional_param_versions] +} +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/listRegionalParams.js b/parametermanager/regional_samples/listRegionalParams.js new file mode 100644 index 0000000000..cf78ac2e10 --- /dev/null +++ b/parametermanager/regional_samples/listRegionalParams.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'; + +/** + * 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, locationId) { + // [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 parameters = await client.listParametersAsync(request); + + for await (const parameter of parameters) { + console.log( + `Found regional parameter ${parameter.name} with format ${parameter.format}` + ); + } + return parameters; + } + + return await listRegionalParams(); + // [END parametermanager_list_regional_params] +} +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/renderRegionalParamVersion.js b/parametermanager/regional_samples/renderRegionalParamVersion.js new file mode 100644 index 0000000000..8074b1ed61 --- /dev/null +++ b/parametermanager/regional_samples/renderRegionalParamVersion.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'; + +/** + * 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, locationId, parameterId, parameterVersionId) { + // [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 [paramVersions] = await client.renderParameterVersion(request); + + console.log( + `Rendered regional parameter version: ${paramVersions.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: ', + paramVersions.renderedPayload.toString('utf-8') + ); + return paramVersions; + } + + return await renderRegionalParamVersion(); + // [END parametermanager_render_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/test/parametermanager.test.js b/parametermanager/test/parametermanager.test.js index e19bb2d87a..c56b399d39 100644 --- a/parametermanager/test/parametermanager.test.js +++ b/parametermanager/test/parametermanager.test.js @@ -36,6 +36,8 @@ const regionalClient = new ParameterManagerClient(options); const secretOptions = {}; secretOptions.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`; +const regionalSecretClient = new SecretManagerServiceClient(secretOptions); + const secretId = `test-secret-${uuidv4()}`; const parameterId = `test-parameter-${uuidv4()}`; const regionalParameterId = `test-regional-${uuidv4()}`; @@ -56,6 +58,8 @@ let parameterToDelete; let regionalParameterToDelete; let parameterVersion; let regionalParameterVersion; +let regionalSecret; +let regionalSecretVersion; let keyRing; let kmsKey; @@ -163,6 +167,20 @@ describe('Parameter Manager samples', () => { }); regionalParameterVersionsToDelete.push(regionalParameterVersion.name); + // 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'), + }, + }); + try { await kmsClient.getKeyRing({name: keyRing}); } catch (error) { @@ -329,6 +347,16 @@ describe('Parameter Manager samples', () => { }) ); + try { + await regionalSecretClient.deleteSecret({ + name: regionalSecret.name, + }); + } catch (err) { + if (!err.message.includes('NOT_FOUND')) { + throw err; + } + } + try { await kmsClient.destroyCryptoKeyVersion({ name: `${kmsKey}/cryptoKeyVersions/1`, @@ -730,4 +758,161 @@ describe('Parameter Manager samples', () => { `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-3` ); }); + + it('should create regional parameter version with secret references', async () => { + const sample = require('../regional_samples/createRegionalParamVersionWithSecret'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId, + parameterVersionId + '-1', + regionalSecretVersion.name + ); + regionalParameterVersionsToDelete.push(parameterVersion.name); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}/versions/${parameterVersionId}-1` + ); + }); + + it('should create a regional structured parameter', async () => { + const sample = require('../regional_samples/createStructuredRegionalParam'); + const parameter = await sample.main( + projectId, + locationId, + regionalParameterId + '-1' + ); + regionalParametersToDelete.push(parameter.name); + assert.exists(parameter); + assert.equal( + parameter.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-1` + ); + }); + + it('should create a regional unstructured parameter', async () => { + const sample = require('../regional_samples/createRegionalParam'); + const parameter = await sample.main( + projectId, + locationId, + regionalParameterId + '-2' + ); + regionalParametersToDelete.push(parameter.name); + assert.exists(parameter); + assert.equal( + parameter.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-2` + ); + }); + + it('should create a regional structured parameter version', async () => { + const sample = require('../regional_samples/createStructuredRegionalParamVersion'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId + '-1', + parameterVersionId + '-2', + jsonPayload + ); + regionalParameterVersionsToDelete.push(parameterVersion.name); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-1/versions/${parameterVersionId}-2` + ); + }); + + it('should create a regional unstructured parameter version', async () => { + const sample = require('../regional_samples/createRegionalParamVersion'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId + '-2', + parameterVersionId + '-3', + payload + ); + regionalParameterVersionsToDelete.push(parameterVersion.name); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}-2/versions/${parameterVersionId}-3` + ); + }); + + it('should list regional parameters', async () => { + const sample = require('../regional_samples/listRegionalParams'); + const parameters = await sample.main(projectId, locationId); + assert.exists(parameters); + }); + + it('should get a regional parameter', async () => { + const sample = require('../regional_samples/getRegionalParam'); + const parameter = await sample.main( + projectId, + locationId, + regionalParameterId + ); + assert.exists(parameter); + assert.equal( + parameter.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}` + ); + }); + + it('should list regional parameter versions', async () => { + const sample = require('../regional_samples/listRegionalParamVersions'); + const parameterVersions = await sample.main( + projectId, + locationId, + regionalParameterId + ); + assert.exists(parameterVersions); + }); + + it('should get a regional parameter version', async () => { + const sample = require('../regional_samples/getRegionalParamVersion'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId, + parameterVersionId + '-1' + ); + assert.exists(parameterVersion); + assert.equal( + parameterVersion.name, + `projects/${projectId}/locations/${locationId}/parameters/${regionalParameterId}/versions/${parameterVersionId}-1` + ); + assert.equal(parameterVersion.disabled, false); + }); + + 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 sample = require('../regional_samples/renderRegionalParamVersion'); + const parameterVersion = await sample.main( + projectId, + locationId, + regionalParameterId, + parameterVersionId + '-1' + ); + assert.exists(parameterVersion); + }); });