( parent = 'projects/my-project', // Project for which to manage secrets. secretId = 'foo', // Secret ID. payload = 'hello world!' // String or buffer source data. )
| 15 | 'use strict'; |
| 16 | |
| 17 | async function main( |
| 18 | parent = 'projects/my-project', // Project for which to manage secrets. |
| 19 | secretId = 'foo', // Secret ID. |
| 20 | payload = 'hello world!' // String or buffer source data. |
| 21 | ) { |
| 22 | // [START secretmanager_quickstart] |
| 23 | // Import the Secret Manager client and instantiate it: |
| 24 | const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); |
| 25 | const client = new SecretManagerServiceClient(); |
| 26 | |
| 27 | /** |
| 28 | * TODO(developer): Uncomment these variables before running the sample. |
| 29 | */ |
| 30 | // parent = 'projects/my-project', // Project for which to manage secrets. |
| 31 | // secretId = 'foo', // Secret ID. |
| 32 | // payload = 'hello world!' // String source data. |
| 33 | |
| 34 | async function createAndAccessSecret() { |
| 35 | // Create the secret with automation replication. |
| 36 | const [secret] = await client.createSecret({ |
| 37 | parent: parent, |
| 38 | secret: { |
| 39 | name: secretId, |
| 40 | replication: { |
| 41 | automatic: {}, |
| 42 | }, |
| 43 | }, |
| 44 | secretId, |
| 45 | }); |
| 46 | |
| 47 | console.info(`Created secret ${secret.name}`); |
| 48 | |
| 49 | // Add a version with a payload onto the secret. |
| 50 | const [version] = await client.addSecretVersion({ |
| 51 | parent: secret.name, |
| 52 | payload: { |
| 53 | data: Buffer.from(payload, 'utf8'), |
| 54 | }, |
| 55 | }); |
| 56 | |
| 57 | console.info(`Added secret version ${version.name}`); |
| 58 | |
| 59 | // Access the secret. |
| 60 | const [accessResponse] = await client.accessSecretVersion({ |
| 61 | name: version.name, |
| 62 | }); |
| 63 | |
| 64 | const responsePayload = accessResponse.payload.data.toString('utf8'); |
| 65 | console.info(`Payload: ${responsePayload}`); |
| 66 | } |
| 67 | createAndAccessSecret(); |
| 68 | // [END secretmanager_quickstart] |
| 69 | } |
| 70 | |
| 71 | const args = process.argv.slice(2); |
| 72 | main(...args).catch(console.error); |
no test coverage detected