Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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,
annotationKey,
annotationValue
) {
// [START secretmanager_create_regional_secret_with_annotations]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'my-project'
// const locationId = 'locationId';
// const secretId = 'my-secret';
// const annotationKey = 'exampleannotationkey';
// const annotationValue = 'exampleannotationvalue';

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 createRegionalSecretWithAnnotations() {
const [secret] = await client.createSecret({
parent: parent,
secretId: secretId,
secret: {
annotations: {
[annotationKey]: annotationValue,
},
},
});

console.log(`Created secret ${secret.name}`);
}

createRegionalSecretWithAnnotations();
// [END secretmanager_create_regional_secret_with_annotations]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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,
annotationKey,
annotationValue
) {
// [START secretmanager_edit_regional_secret_annotations]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'my-project'
// const locationId = 'locationId';
// const secretId = 'my-secret';
// const annotationKey = 'updatedannotationkey';
// const annotationValue = 'updatedannotationvalue';

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 editRegionalSecretAnnotations() {
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}`);
}

editRegionalSecretAnnotations();
// [END secretmanager_edit_regional_secret_annotations]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
Original file line number Diff line number Diff line change
@@ -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_annotations]
/**
* 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 viewRegionalSecretAnnotations() {
const [secret] = await client.getSecret({
name: name,
});

for (const key in secret.annotations) {
console.log(`${key} : ${secret.annotations[key]}`);
}
}

viewRegionalSecretAnnotations();
// [END secretmanager_view_regional_secret_annotations]
}

const args = process.argv.slice(2);
main(...args).catch(console.error);
41 changes: 41 additions & 0 deletions secret-manager/test/secretmanager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const labelKey = 'secretmanager';
const labelValue = 'rocks';
const labelKeyUpdated = 'gcp';
const labelValueUpdated = 'rock';
const annotationKey = 'annotationkey';
const annotationValue = 'annotationvalue';
const annotationKeyUpdated = 'updatedannotationekey';
const annotationValueUpdated = 'updatedannotationvalue';

let secret;
let regionalSecret;
Expand Down Expand Up @@ -64,6 +68,11 @@ describe('Secret Manager samples', () => {
[regionalSecret] = await regionalClient.createSecret({
parent: `projects/${projectId}/locations/${locationId}`,
secretId: secretId,
secret: {
annotations: {
[annotationKey]: annotationValue,
},
},
});

[version] = await client.addSecretVersion({
Expand Down Expand Up @@ -170,6 +179,16 @@ describe('Secret Manager samples', () => {
throw err;
}
}

try {
await regionalClient.deleteSecret({
name: `${regionalSecret.name}-4`,
});
} catch (err) {
if (!err.message.includes('NOT_FOUND')) {
throw err;
}
}
});

it('runs the quickstart', async () => {
Expand Down Expand Up @@ -218,6 +237,13 @@ describe('Secret Manager samples', () => {
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}`
);
assert.match(output, new RegExp('Created secret'));
});

it('lists secrets', async () => {
const output = execSync(`node listSecrets.js projects/${projectId}`);
assert.match(output, new RegExp(`${secret.name}`));
Expand All @@ -240,6 +266,14 @@ describe('Secret Manager samples', () => {
assert.match(output, new RegExp(`${labelKey}`));
});

it('view a regional secret annotations', async () => {
const output = execSync(
`node regional_samples/viewRegionalSecretAnnotations.js ${projectId} ${locationId} ${secretId}`
);

assert.match(output, new RegExp(`${annotationKey}`));
});

it('gets a regional secret', async () => {
const output = execSync(
`node regional_samples/getRegionalSecret.js ${projectId} ${locationId} ${secretId}`
Expand Down Expand Up @@ -278,6 +312,13 @@ describe('Secret Manager samples', () => {
assert.match(output, new RegExp(`Updated secret ${regionalSecret.name}`));
});

it('edits a regional secret annotations', async () => {
const output = execSync(
`node regional_samples/editRegionalSecretAnnotations.js ${projectId} ${locationId} ${secretId} ${annotationKeyUpdated} ${annotationValueUpdated}`
);
assert.match(output, new RegExp(`Updated secret ${regionalSecret.name}`));
});

it('deletes a secret', async () => {
const output = execSync(
`node deleteSecret.js projects/${projectId}/secrets/${secretId}-2`
Expand Down