|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +async function main(projectId, locationId, secretId, tagValue) { |
| 18 | + // [START secretmanager_bind_tags_to_regional_secret] |
| 19 | + /** |
| 20 | + * TODO(developer): Uncomment these variables before running the sample. |
| 21 | + */ |
| 22 | + // const projectId = 'my-project'; |
| 23 | + // const locationId = 'my-location'; |
| 24 | + // const secretId = 'my-secret'; |
| 25 | + // const tagValue = 'tagValues/281476592621530'; |
| 26 | + const parent = `projects/${projectId}/locations/${locationId}`; |
| 27 | + |
| 28 | + // Imports the library |
| 29 | + const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); |
| 30 | + const {TagBindingsClient} = require('@google-cloud/resource-manager').v3; |
| 31 | + |
| 32 | + // Adding the endpoint to call the regional |
| 33 | + const options = {}; |
| 34 | + const bindingOptions = {}; |
| 35 | + options.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`; |
| 36 | + bindingOptions.apiEndpoint = `${locationId}-cloudresourcemanager.googleapis.com`; |
| 37 | + |
| 38 | + // Instantiates a client |
| 39 | + const client = new SecretManagerServiceClient(options); |
| 40 | + const resourcemanagerClient = new TagBindingsClient(bindingOptions); |
| 41 | + |
| 42 | + async function bindTagsToRegionalSecret() { |
| 43 | + const [secret] = await client.createSecret({ |
| 44 | + parent: parent, |
| 45 | + secretId: secretId, |
| 46 | + }); |
| 47 | + |
| 48 | + console.log(`Created secret ${secret.name}`); |
| 49 | + |
| 50 | + const [operation] = await resourcemanagerClient.createTagBinding({ |
| 51 | + tagBinding: { |
| 52 | + parent: `//secretmanager.googleapis.com/${secret.name}`, |
| 53 | + tagValue: tagValue, |
| 54 | + }, |
| 55 | + }); |
| 56 | + const [response] = await operation.promise(); |
| 57 | + console.log('Created Tag Binding', response.name); |
| 58 | + } |
| 59 | + |
| 60 | + bindTagsToRegionalSecret(); |
| 61 | + // [END secretmanager_bind_tags_to_regional_secret] |
| 62 | +} |
| 63 | + |
| 64 | +const args = process.argv.slice(2); |
| 65 | +main(...args).catch(console.error); |
0 commit comments