Skip to content
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

feat(parametermanager): Added samples to delete, enable and disable parameter versions in both region and global #4014

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
52 changes: 52 additions & 0 deletions .github/workflows/parametermanager.yaml
Original file line number Diff line number Diff line change
@@ -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]
1 change: 1 addition & 0 deletions .github/workflows/utils/workflows.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"mediatranslation",
"monitoring/prometheus",
"monitoring/snippets",
"parametermanager",
"retail",
"run/filesystem",
"scheduler",
Expand Down
55 changes: 55 additions & 0 deletions parametermanager/deleteParam.js
Original file line number Diff line number Diff line change
@@ -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,
});
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.

medium

Consider adding error handling to the API call to improve the robustness of the sample.

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


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

Choose a reason for hiding this comment

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

medium

Consider adding error handling to the API call to improve the robustness of the sample.

    try {
      await client.deleteParameterVersion({
        name: name,
      });
    } catch (err) {
      console.error(`Failed to delete parameter version: ${err}`);
      throw err;
    }

});

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);
80 changes: 80 additions & 0 deletions parametermanager/disableParamVersion.js
Original file line number Diff line number Diff line change
@@ -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);

Comment on lines +65 to +67
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Consider adding error handling to the API call to improve the robustness of the sample.

Suggested change
// Make the API call to update the parameter version
const [response] = await client.updateParameterVersion(request);
try {
const [response] = await client.updateParameterVersion(request);
return response;
} catch (err) {
console.error(`Failed to disable parameter version: ${err}`);
throw err;
}

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);
80 changes: 80 additions & 0 deletions parametermanager/enableParamVersion.js
Original file line number Diff line number Diff line change
@@ -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);

Comment on lines +65 to +67
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Consider adding error handling to the API call to improve the robustness of the sample.

Suggested change
// Make the API call to update the parameter version
const [response] = await client.updateParameterVersion(request);
try {
const [response] = await client.updateParameterVersion(request);
return response;
} catch (err) {
console.error(`Failed to enable parameter version: ${err}`);
throw err;
}

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);
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