forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(secretmanager): add samples for secret annotations and regional …
…labels (GoogleCloudPlatform#3825) * feat(secretmanager): add samples for secret annotations * feat: create samples for labels, regional SM * fix: fix linting issue * fix: lint fix * Update viewSecretAnnotations.js * fix: changed filename * fix: fixed tests * fix: fixed tests * fix: fixed linting --------- Co-authored-by: Tony Pujals <[email protected]>
- Loading branch information
Showing
8 changed files
with
491 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
58 changes: 58 additions & 0 deletions
58
secret-manager/regional_samples/createRegionalSecretWithLabels.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
67 changes: 67 additions & 0 deletions
67
secret-manager/regional_samples/deleteRegionalSecretLabel.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
68 changes: 68 additions & 0 deletions
68
secret-manager/regional_samples/editRegionalSecretLabel.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
52 changes: 52 additions & 0 deletions
52
secret-manager/regional_samples/viewRegionalSecretLabels.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_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); |
Oops, something went wrong.