diff --git a/secret-manager/createSecretWithAnnotations.js b/secret-manager/createSecretWithAnnotations.js new file mode 100644 index 0000000000..696e8278bd --- /dev/null +++ b/secret-manager/createSecretWithAnnotations.js @@ -0,0 +1,55 @@ +// Copyright 2024 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'; + +async function main(parent, secretId, annotationKey, annotationValue) { + // [START secretmanager_create_secret_with_annotations] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const parent = 'projects/my-project'; + // const secretId = 'my-secret'; + // const annotationKey = 'exampleannotationkey'; + // const annotationValue = 'exampleannotationvalue'; + + // Imports the Secret Manager library + const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); + + // Instantiates a client + const client = new SecretManagerServiceClient(); + + async function createSecretWithAnnotations() { + const [secret] = await client.createSecret({ + parent: parent, + secretId: secretId, + secret: { + replication: { + automatic: {}, + }, + annotations: { + [annotationKey]: annotationValue, + }, + }, + }); + + console.log(`Created secret ${secret.name}`); + } + + createSecretWithAnnotations(); + // [END secretmanager_create_secret_with_annotations] +} + +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/secret-manager/editSecretAnnotations.js b/secret-manager/editSecretAnnotations.js new file mode 100644 index 0000000000..32ca61ad19 --- /dev/null +++ b/secret-manager/editSecretAnnotations.js @@ -0,0 +1,61 @@ +// Copyright 2024 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'; + +async function main(name, annotationKey, annotationValue) { + // [START secretmanager_edit_secret_annotations] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const name = 'projects/my-project/secrets/my-secret'; + // const annotationKey = 'updatedannotationkey'; + // const annotationValue = 'updatedannotationvalue'; + + // Imports the Secret Manager library + const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); + + // Instantiates a client + const client = new SecretManagerServiceClient(); + + async function getSecret() { + const [secret] = await client.getSecret({ + name: name, + }); + + return secret; + } + + async function editSecretAnnotations() { + const oldSecret = await getSecret(); + oldSecret.annotations[annotationKey] = annotationValue; + const [secret] = await client.updateSecret({ + secret: { + name: name, + annotations: oldSecret.annotations, + }, + updateMask: { + paths: ['annotations'], + }, + }); + + console.info(`Updated secret ${secret.name}`); + } + + editSecretAnnotations(); + // [END secretmanager_edit_secret_annotations] +} + +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/secret-manager/regional_samples/createRegionalSecretWithLabels.js b/secret-manager/regional_samples/createRegionalSecretWithLabels.js new file mode 100644 index 0000000000..19cf6f04d4 --- /dev/null +++ b/secret-manager/regional_samples/createRegionalSecretWithLabels.js @@ -0,0 +1,58 @@ +// Copyright 2024 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'; + +async function main(projectId, locationId, secretId, labelKey, labelValue) { + // [START secretmanager_create_regional_secret_with_labels] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const project = 'my-project'; + // const locationId = 'my-location'; + // const secretId = 'my-secret'; + // const labelKey = 'secretmanager'; + // const labelValue = 'rocks'; + const parent = `projects/${projectId}/locations/${locationId}`; + + // Imports the Secret Manager library + const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); + + // Adding the endpoint to call the regional secret manager sever + const options = {}; + options.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`; + + // Instantiates a client + const client = new SecretManagerServiceClient(options); + + async function createRegionalSecretWithLabels() { + const [secret] = await client.createSecret({ + parent: parent, + secretId: secretId, + secret: { + labels: { + [labelKey]: labelValue, + }, + }, + }); + + console.log(`Created secret ${secret.name}`); + } + + createRegionalSecretWithLabels(); + // [END secretmanager_create_regional_secret_with_labels] +} + +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/secret-manager/regional_samples/deleteRegionalSecretLabel.js b/secret-manager/regional_samples/deleteRegionalSecretLabel.js new file mode 100644 index 0000000000..68e72274d9 --- /dev/null +++ b/secret-manager/regional_samples/deleteRegionalSecretLabel.js @@ -0,0 +1,67 @@ +// Copyright 2024 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'; + +async function main(projectId, locationId, secretId, labelKey) { + // [START secretmanager_delete_regional_secret_label] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project' + // const locationId = 'locationId'; + // const secretId = 'my-secret'; + // const labelKey = 'secretmanager'; + const name = `projects/${projectId}/locations/${locationId}/secrets/${secretId}`; + + // Imports the Secret Manager library + const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); + + // Adding the endpoint to call the regional secret manager sever + const options = {}; + options.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`; + + // Instantiates a client + const client = new SecretManagerServiceClient(options); + + async function getSecret() { + const [secret] = await client.getSecret({ + name: name, + }); + + return secret; + } + + async function deleteRegionalSecretLabel() { + const oldSecret = await getSecret(); + delete oldSecret.labels[labelKey]; + const [secret] = await client.updateSecret({ + secret: { + name: name, + labels: oldSecret.labels, + }, + updateMask: { + paths: ['labels'], + }, + }); + + console.info(`Updated secret ${secret.name}`); + } + + deleteRegionalSecretLabel(); + // [END secretmanager_delete_regional_secret_label] +} + +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/secret-manager/regional_samples/editRegionalSecretLabel.js b/secret-manager/regional_samples/editRegionalSecretLabel.js new file mode 100644 index 0000000000..329888ba01 --- /dev/null +++ b/secret-manager/regional_samples/editRegionalSecretLabel.js @@ -0,0 +1,68 @@ +// Copyright 2024 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'; + +async function main(projectId, locationId, secretId, labelKey, labelValue) { + // [START secretmanager_edit_regional_secret_label] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project' + // const locationId = 'locationId'; + // const secretId = 'my-secret'; + // const labelKey = 'gcp'; + // const labelValue = 'rocks'; + const name = `projects/${projectId}/locations/${locationId}/secrets/${secretId}`; + + // Imports the Secret Manager library + const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); + + // Adding the endpoint to call the regional secret manager sever + const options = {}; + options.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`; + + // Instantiates a client + const client = new SecretManagerServiceClient(options); + + async function getSecret() { + const [secret] = await client.getSecret({ + name: name, + }); + + return secret; + } + + async function createUpdateRegionalSecretLabel() { + const oldSecret = await getSecret(); + oldSecret.labels[labelKey] = labelValue; + const [secret] = await client.updateSecret({ + secret: { + name: name, + labels: oldSecret.labels, + }, + updateMask: { + paths: ['labels'], + }, + }); + + console.info(`Updated secret ${secret.name}`); + } + + createUpdateRegionalSecretLabel(); + // [END secretmanager_edit_regional_secret_label] +} + +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/secret-manager/regional_samples/viewRegionalSecretLabels.js b/secret-manager/regional_samples/viewRegionalSecretLabels.js new file mode 100644 index 0000000000..97e99c2459 --- /dev/null +++ b/secret-manager/regional_samples/viewRegionalSecretLabels.js @@ -0,0 +1,52 @@ +// Copyright 2024 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'; + +async function main(projectId, locationId, secretId) { + // [START secretmanager_view_regional_secret_labels] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const projectId = 'my-project' + // const locationId = 'locationId'; + // const secretId = 'my-secret'; + const name = `projects/${projectId}/locations/${locationId}/secrets/${secretId}`; + + // Imports the Secret Manager library + const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); + + // Adding the endpoint to call the regional secret manager sever + const options = {}; + options.apiEndpoint = `secretmanager.${locationId}.rep.googleapis.com`; + + // Instantiates a client + const client = new SecretManagerServiceClient(options); + + async function getRegionalSecretLabels() { + const [secret] = await client.getSecret({ + name: name, + }); + + for (const key in secret.labels) { + console.log(`${key} : ${secret.labels[key]}`); + } + } + + getRegionalSecretLabels(); + // [END secretmanager_view_regional_secret_labels] +} + +const args = process.argv.slice(2); +main(...args).catch(console.error); diff --git a/secret-manager/test/secretmanager.test.js b/secret-manager/test/secretmanager.test.js index 5c721d30cd..d9416ebca4 100644 --- a/secret-manager/test/secretmanager.test.js +++ b/secret-manager/test/secretmanager.test.js @@ -62,6 +62,9 @@ describe('Secret Manager samples', () => { labels: { [labelKey]: labelValue, }, + annotations: { + [annotationKey]: annotationValue, + }, }, }); @@ -69,6 +72,9 @@ describe('Secret Manager samples', () => { parent: `projects/${projectId}/locations/${locationId}`, secretId: secretId, secret: { + labels: { + [labelKey]: labelValue, + }, annotations: { [annotationKey]: annotationValue, }, @@ -180,6 +186,16 @@ describe('Secret Manager samples', () => { } } + try { + await client.deleteSecret({ + name: `${secret.name}-6`, + }); + } catch (err) { + if (!err.message.includes('NOT_FOUND')) { + throw err; + } + } + try { await regionalClient.deleteSecret({ name: `${regionalSecret.name}-4`, @@ -189,6 +205,26 @@ describe('Secret Manager samples', () => { throw err; } } + + try { + await regionalClient.deleteSecret({ + name: `${regionalSecret.name}-5`, + }); + } catch (err) { + if (!err.message.includes('NOT_FOUND')) { + throw err; + } + } + + try { + await regionalClient.deleteSecret({ + name: `${regionalSecret.name}-6`, + }); + } catch (err) { + if (!err.message.includes('NOT_FOUND')) { + throw err; + } + } }); it('runs the quickstart', async () => { @@ -237,9 +273,23 @@ describe('Secret Manager samples', () => { assert.match(output, new RegExp('Created secret')); }); + it('creates a regional secret with labels', async () => { + const output = execSync( + `node regional_samples/createRegionalSecretWithLabels.js ${projectId} ${locationId} ${secretId}-5 ${labelKey} ${labelValue}` + ); + assert.match(output, new RegExp('Created secret')); + }); + + it('creates a secret with annotations', async () => { + const output = execSync( + `node createSecretWithAnnotations.js projects/${projectId} ${secretId}-6 ${annotationKey} ${annotationValue}` + ); + assert.match(output, new RegExp('Created secret')); + }); + it('creates a regional secret with annotations', async () => { const output = execSync( - `node regional_samples/createRegionalSecretWithAnnotations.js ${projectId} ${locationId} ${secretId}-5 ${annotationKey} ${annotationValue}` + `node regional_samples/createRegionalSecretWithAnnotations.js ${projectId} ${locationId} ${secretId}-6 ${annotationKey} ${annotationValue}` ); assert.match(output, new RegExp('Created secret')); }); @@ -266,6 +316,19 @@ describe('Secret Manager samples', () => { assert.match(output, new RegExp(`${labelKey}`)); }); + it('view a regional secret labels', async () => { + const output = execSync( + `node regional_samples/viewRegionalSecretLabels.js ${projectId} ${locationId} ${secretId}` + ); + + assert.match(output, new RegExp(`${labelKey}`)); + }); + + it('view a secret annotations', async () => { + const output = execSync(`node viewSecretAnnotations.js ${secret.name}`); + assert.match(output, new RegExp(`${annotationKey}`)); + }); + it('view a regional secret annotations', async () => { const output = execSync( `node regional_samples/viewRegionalSecretAnnotations.js ${projectId} ${locationId} ${secretId}` @@ -305,6 +368,20 @@ describe('Secret Manager samples', () => { assert.match(output, new RegExp(`Updated secret ${secret.name}`)); }); + it('create or updates a regional secret labels', async () => { + const output = execSync( + `node regional_samples/editRegionalSecretLabel.js ${projectId} ${locationId} ${secretId} ${labelKeyUpdated} ${labelValueUpdated}` + ); + assert.match(output, new RegExp(`Updated secret ${regionalSecret.name}`)); + }); + + it('edits a secret annotation', async () => { + const output = execSync( + `node editSecretAnnotations.js ${secret.name} ${annotationKeyUpdated} ${annotationValueUpdated}` + ); + assert.match(output, new RegExp(`Updated secret ${secret.name}`)); + }); + it('updates a regional secret with an alias', async () => { const output = execSync( `node regional_samples/updateRegionalSecretWithAlias.js ${projectId} ${locationId} ${secretId}` @@ -333,6 +410,13 @@ describe('Secret Manager samples', () => { assert.match(output, new RegExp(`Updated secret ${secret.name}`)); }); + it('deletes a regional secret label', async () => { + const output = execSync( + `node regional_samples/deleteRegionalSecretLabel.js ${projectId} ${locationId} ${secretId} ${labelKey}` + ); + assert.match(output, new RegExp(`Updated secret ${regionalSecret.name}`)); + }); + it('deletes a regional secret', async () => { const output = execSync( `node regional_samples/deleteRegionalSecret.js ${projectId} ${locationId} ${secretId}-3` diff --git a/secret-manager/viewSecretAnnotations.js b/secret-manager/viewSecretAnnotations.js new file mode 100644 index 0000000000..c323ed6420 --- /dev/null +++ b/secret-manager/viewSecretAnnotations.js @@ -0,0 +1,45 @@ +// Copyright 2024 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'; + +async function main(name) { + // [START secretmanager_view_secret_annotations] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const parent = 'projects/my-project/secrets/my-secret'; + + // Imports the Secret Manager library + const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); + + // Instantiates a client + const client = new SecretManagerServiceClient(); + + async function viewSecretAnnotations() { + const [secret] = await client.getSecret({ + name: name, + }); + + for (const key in secret.annotations) { + console.log(`${key} : ${secret.annotations[key]}`); + } + } + + viewSecretAnnotations(); + // [END secretmanager_view_secret_annotations] +} + +const args = process.argv.slice(2); +main(...args).catch(console.error);