Skip to content

feat(parametermanager): Added samples for delete, disable, enable parameter version in global & regional #4070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions parametermanager/deleteParam.js
Original file line number Diff line number Diff line change
@@ -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,
});
Comment on lines +42 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Consider adding error handling to the deleteParameter call to provide more informative error messages to the user. A simple try...catch block would suffice.

    try {
      await client.deleteParameter({
        name: name,
      });
    } catch (err) {
      console.error(`Error deleting parameter: ${err}`);
      throw err; // Re-throw the error to prevent further execution
    }


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;
});
}
72 changes: 72 additions & 0 deletions parametermanager/deleteParamVersion.js
Original file line number Diff line number Diff line change
@@ -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,
});
Comment on lines +50 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Consider adding error handling to the deleteParameterVersion call to provide more informative error messages to the user. A simple try...catch block would suffice.

    try {
      await client.deleteParameterVersion({
        name: name,
      });
    } catch (err) {
      console.error(`Error deleting parameter version: ${err}`);
      throw err; // Re-throw the error to prevent further execution
    }

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;
});
}
85 changes: 85 additions & 0 deletions parametermanager/disableParamVersion.js
Original file line number Diff line number Diff line change
@@ -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;
});
}
85 changes: 85 additions & 0 deletions parametermanager/enableParamVersion.js
Original file line number Diff line number Diff line change
@@ -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;
});
}
29 changes: 29 additions & 0 deletions parametermanager/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading