Skip to content

Commit 3003630

Browse files
author
Benjamin E. Coe
authored
feat!: initial generation of library (#1)
0 parents  commit 3003630

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

secret-manager/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "nodejs-secret-manager-samples",
3+
"private": true,
4+
"license": "Apache-2.0",
5+
"files": [
6+
"*.js"
7+
],
8+
"author": "Google LLC",
9+
"repository": "googleapis/nodejs-secret-manager",
10+
"engines": {
11+
"node": ">=8"
12+
},
13+
"scripts": {
14+
"test": "c8 mocha --recursive test/ --timeout=800000"
15+
},
16+
"dependencies": {
17+
"@google-cloud/secret-manager": "^0.1.0"
18+
},
19+
"devDependencies": {
20+
"c8": "^6.0.1",
21+
"chai": "^4.2.0",
22+
"mocha": "^6.0.0",
23+
"uuid": "^3.3.3"
24+
}
25+
}

secret-manager/quickstart.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2019 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+
// ** This file is automatically generated by gapic-generator-typescript. **
16+
// ** https://github.com/googleapis/gapic-generator-typescript **
17+
// ** All changes to this file may be overwritten. **
18+
19+
'use strict';
20+
21+
async function main(
22+
project = 'YOUR_PROJECT_NAME', // Project to manage secrets for.
23+
secretId = 'foo', // Identifier for secret.
24+
secretStringPayload = 'hello world!' // A secret string.
25+
) {
26+
// [START secret_manager_quickstart]
27+
// Import the Secret Manager client and instantiate it:
28+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
29+
const client = new SecretManagerServiceClient();
30+
31+
// const project = 'YOUR_PROJECT_NAME', // Project to manage secrets for.
32+
// const secretId = 'foo', // Identifier for secret.
33+
// const secretStringPayload = 'hello world!', // A secret string.
34+
35+
async function setAndAccessSecret() {
36+
// Create the secret, ignoring errors related to the secret
37+
// already existing:
38+
try {
39+
await client.createSecret({
40+
parent: `projects/${project}`,
41+
secret: {
42+
name: secretId,
43+
replication: {
44+
automatic: {},
45+
},
46+
},
47+
secretId,
48+
});
49+
} catch (err) {
50+
if (err.message.includes('ALREADY_EXISTS')) {
51+
console.info(`secret '${secretId}' already exists`);
52+
} else {
53+
throw err; // Unexpected error.
54+
}
55+
}
56+
57+
// Update the latest version of the secret to the value provided:
58+
await client.addSecretVersion({
59+
parent: `projects/${project}/secrets/${secretId}`,
60+
payload: {
61+
data: Buffer.from(secretStringPayload, 'utf8'),
62+
},
63+
});
64+
console.info(
65+
`set current version of '${secretId}' to '${secretStringPayload}'`
66+
);
67+
68+
// Fetch the latest version of the secret:
69+
const [secret] = await client.accessSecretVersion({
70+
name: `projects/${project}/secrets/${secretId}/versions/latest`,
71+
});
72+
const secretString = secret.payload.data.toString('utf8');
73+
console.info(`get latest '${secretId}' with value '${secretString}'`);
74+
}
75+
setAndAccessSecret();
76+
// [END secret_manager_quickstart]
77+
}
78+
79+
main(...process.argv.slice(2)).catch(err => {
80+
console.error(err);
81+
process.exitCode = 1;
82+
});

secret-manager/test/.eslintrc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
env:
3+
mocha: true

secret-manager/test/quickstart.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2019 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+
// ** This file is automatically generated by gapic-generator-typescript. **
16+
// ** https://github.com/googleapis/gapic-generator-typescript **
17+
// ** All changes to this file may be overwritten. **
18+
19+
'use strict';
20+
21+
const {assert} = require('chai');
22+
const cp = require('child_process');
23+
const uuid = require('uuid');
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
26+
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
27+
const client = new SecretManagerServiceClient();
28+
29+
const path = require('path');
30+
const cwd = path.join(__dirname, '..');
31+
32+
describe('Quickstart', () => {
33+
const project = process.env.GCLOUD_PROJECT;
34+
const secretId = uuid.v4();
35+
after(async () => {
36+
await client.deleteSecret({
37+
name: `projects/${project}/secrets/${secretId}`,
38+
});
39+
});
40+
41+
it('should run quickstart', async () => {
42+
const stdout = execSync(`node quickstart.js ${project} ${secretId} bar`, {
43+
cwd,
44+
});
45+
assert.match(stdout, new RegExp(`get latest.*${secretId}`));
46+
});
47+
});

0 commit comments

Comments
 (0)