|
14 | 14 |
|
15 | 15 | 'use strict';
|
16 | 16 |
|
17 |
| -async function main(name = 'projects/my-project/secrets/my-secret') { |
18 |
| - // [START secretmanager_get_secret] |
19 |
| - /** |
20 |
| - * TODO(developer): Uncomment these variables before running the sample. |
21 |
| - */ |
22 |
| - // const name = 'projects/my-project/secrets/my-secret'; |
| 17 | +const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); |
23 | 18 |
|
24 |
| - // Imports the Secret Manager library |
25 |
| - const {SecretManagerServiceClient} = require('@google-cloud/secret-manager'); |
26 |
| - |
27 |
| - // Instantiates a client |
| 19 | +// [START secretmanager_get_secret] |
| 20 | +/** |
| 21 | + * Get metadata about a secret. |
| 22 | + * |
| 23 | + * @param {string} projectId The ID of the Google Cloud project. |
| 24 | + * @param {string} secretId The ID of the secret to retrieve. |
| 25 | + */ |
| 26 | +async function getSecret(projectId, secretId) { |
28 | 27 | const client = new SecretManagerServiceClient();
|
29 |
| - |
30 |
| - async function getSecret() { |
| 28 | + const name = `projects/${projectId}/secrets/${secretId}`; |
| 29 | + try { |
31 | 30 | const [secret] = await client.getSecret({
|
32 | 31 | name: name,
|
33 | 32 | });
|
34 | 33 |
|
35 |
| - const policy = secret.replication.replication; |
36 |
| - |
37 |
| - console.info(`Found secret ${secret.name} (${policy})`); |
| 34 | + if (secret.replication && secret.replication.replication) { |
| 35 | + const policy = secret.replication.replication; |
| 36 | + console.info( |
| 37 | + `Found secret ${secret.name} with replication policy ${policy}` |
| 38 | + ); |
| 39 | + } else { |
| 40 | + console.info(`Found secret ${secret.name} with no replication policy.`); |
| 41 | + } |
| 42 | + return secret; |
| 43 | + } catch (err) { |
| 44 | + console.error(`Failed to retrieve secret ${name}:`, err); |
| 45 | + } finally { |
| 46 | + await client.close(); |
38 | 47 | }
|
| 48 | +} |
| 49 | +// [END secretmanager_get_secret] |
| 50 | + |
| 51 | +async function main() { |
| 52 | + const projectId = process.argv[2] || process.env.PROJECT_ID; |
| 53 | + const secretId = process.argv[3] || process.env.SECRET_ID; |
| 54 | + |
| 55 | + await getSecret(projectId, secretId); |
| 56 | +} |
39 | 57 |
|
40 |
| - getSecret(); |
41 |
| - // [END secretmanager_get_secret] |
| 58 | +if (require.main === module) { |
| 59 | + main().catch(err => { |
| 60 | + console.error(err.message); |
| 61 | + }); |
42 | 62 | }
|
43 | 63 |
|
44 |
| -const args = process.argv.slice(2); |
45 |
| -main(...args).catch(console.error); |
| 64 | +module.exports.getSecret = getSecret; |
0 commit comments