Skip to content

Commit 2d8f95d

Browse files
feat:add samples for annotations, SM (#12689)
* feat:add samples for annotations, SM * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix:fixed files names * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: resolved tags mismatch * fix: fix linting issues * fix: removed ttl --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent f7199c2 commit 2d8f95d

8 files changed

+558
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2024 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
"""
16+
command line application and sample code for creating a new secret with
17+
annotations.
18+
"""
19+
20+
# [START secretmanager_create_secret_with_annotations]
21+
import argparse
22+
import typing
23+
24+
# Import the Secret Manager client library.
25+
from google.cloud import secretmanager
26+
27+
28+
def create_secret_with_annotations(
29+
project_id: str,
30+
secret_id: str,
31+
annotations: typing.Dict[str, str],
32+
) -> secretmanager.Secret:
33+
"""
34+
Create a new secret with the given name. A secret is a logical wrapper
35+
around a collection of secret versions. Secret versions hold the actual
36+
secret material.
37+
"""
38+
39+
# Create the Secret Manager client.
40+
client = secretmanager.SecretManagerServiceClient()
41+
42+
# Build the resource name of the parent project.
43+
parent = f"projects/{project_id}"
44+
45+
# Create the secret.
46+
response = client.create_secret(
47+
request={
48+
"parent": parent,
49+
"secret_id": secret_id,
50+
"secret": {
51+
"replication": {"automatic": {}},
52+
"annotations": annotations,
53+
},
54+
}
55+
)
56+
57+
# Print the new secret name.
58+
print(f"Created secret: {response.name}")
59+
60+
return response
61+
62+
63+
# [END secretmanager_create_secret_with_annotations]
64+
65+
if __name__ == "__main__":
66+
parser = argparse.ArgumentParser(
67+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
68+
)
69+
parser.add_argument("project_id", help="id of the GCP project")
70+
parser.add_argument("secret_id", help="id of the secret to create")
71+
parser.add_argument("annotation_key", help="key of the annotation you want to add")
72+
parser.add_argument(
73+
"annotation_value", help="value of the annotation you want to add"
74+
)
75+
args = parser.parse_args()
76+
77+
annotations = {args.annotation_key, args.annotation_value}
78+
create_secret_with_annotations(args.project_id, args.secret_id, annotations)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2024 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
16+
# [START secretmanager_edit_secret_annotations]
17+
18+
import argparse
19+
from typing import Dict
20+
21+
# Import the Secret Manager client library.
22+
from google.cloud import secretmanager
23+
24+
25+
def edit_secret_annotations(
26+
project_id: str, secret_id: str, new_annotations: Dict[str, str]
27+
) -> secretmanager.UpdateSecretRequest:
28+
"""
29+
Create or update a annotation on an existing secret.
30+
"""
31+
32+
# Create the Secret Manager client.
33+
client = secretmanager.SecretManagerServiceClient()
34+
35+
# Build the resource name of the secret.
36+
name = client.secret_path(project_id, secret_id)
37+
38+
# Get the secret.
39+
response = client.get_secret(request={"name": name})
40+
41+
annotations = response.annotations
42+
43+
# Update the annotations
44+
for annotation_key in new_annotations:
45+
annotations[annotation_key] = new_annotations[annotation_key]
46+
47+
# Update the secret.
48+
secret = {"name": name, "annotations": annotations}
49+
update_mask = {"paths": ["annotations"]}
50+
response = client.update_secret(
51+
request={"secret": secret, "update_mask": update_mask}
52+
)
53+
54+
# Print the new secret name.
55+
print(f"Updated secret: {response.name}")
56+
57+
return response
58+
59+
60+
# [END secretmanager_edit_secret_annotations]
61+
62+
if __name__ == "__main__":
63+
parser = argparse.ArgumentParser(
64+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
65+
)
66+
parser.add_argument("project_id", help="id of the GCP project")
67+
parser.add_argument("secret_id", help="id of the secret to act on")
68+
parser.add_argument(
69+
"annotation_key", help="key of the annotation to be added/updated"
70+
)
71+
parser.add_argument(
72+
"annotation_value", help="value of the annotation to be added/updated"
73+
)
74+
args = parser.parse_args()
75+
76+
annotations = {args.annotation_key, args.annotation_value}
77+
edit_secret_annotations(args.project_id, args.secret_id, annotations)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2024 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
"""
16+
command line application and sample code for creating a new secret with
17+
annotations.
18+
"""
19+
20+
# [START secretmanager_create_regional_secret_with_annotations]
21+
import argparse
22+
import typing
23+
24+
# Import the Secret Manager client library.
25+
from google.cloud import secretmanager_v1
26+
27+
28+
def create_regional_secret_with_annotations(
29+
project_id: str,
30+
location_id: str,
31+
secret_id: str,
32+
annotations: typing.Dict[str, str],
33+
) -> secretmanager_v1.Secret:
34+
"""
35+
Create a new secret with the given name. A secret is a logical wrapper
36+
around a collection of secret versions. Secret versions hold the actual
37+
secret material.
38+
"""
39+
40+
# Endpoint to call the regional secret manager sever
41+
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com"
42+
43+
# Create the Secret Manager client.
44+
client = secretmanager_v1.SecretManagerServiceClient(
45+
client_options={"api_endpoint": api_endpoint},
46+
)
47+
48+
# Build the resource name of the parent project.
49+
parent = f"projects/{project_id}/locations/{location_id}"
50+
51+
# Create the secret.
52+
response = client.create_secret(
53+
request={
54+
"parent": parent,
55+
"secret_id": secret_id,
56+
"secret": {"annotations": annotations},
57+
}
58+
)
59+
60+
# Print the new secret name.
61+
print(f"Created secret: {response.name}")
62+
63+
return response
64+
65+
66+
# [END secretmanager_create_regional_secret_with_annotations]
67+
68+
if __name__ == "__main__":
69+
parser = argparse.ArgumentParser(
70+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
71+
)
72+
parser.add_argument("project_id", help="id of the GCP project")
73+
parser.add_argument(
74+
"location_id", help="id of the location where secret is to be created"
75+
)
76+
parser.add_argument("secret_id", help="id of the secret to create")
77+
parser.add_argument("annotation_key", help="key of the annotation you want to add")
78+
parser.add_argument(
79+
"annotation_value", help="value of the annotation you want to add"
80+
)
81+
args = parser.parse_args()
82+
83+
annotations = {args.annotation_key, args.annotation_value}
84+
create_regional_secret_with_annotations(
85+
args.project_id, args.location_id, args.secret_id, annotations
86+
)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2024 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
16+
# [START secretmanager_edit_regional_secret_annotations]
17+
18+
import argparse
19+
from typing import Dict
20+
21+
# Import the Secret Manager client library.
22+
from google.cloud import secretmanager_v1
23+
24+
25+
def edit_regional_secret_annotations(
26+
project_id: str, location_id: str, secret_id: str, new_annotations: Dict[str, str]
27+
) -> secretmanager_v1.UpdateSecretRequest:
28+
"""
29+
Create or update a annotation on an existing secret.
30+
"""
31+
32+
# Endpoint to call the regional secret manager sever
33+
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com"
34+
35+
# Create the Secret Manager client.
36+
client = secretmanager_v1.SecretManagerServiceClient(
37+
client_options={"api_endpoint": api_endpoint},
38+
)
39+
40+
# Build the resource name.
41+
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}"
42+
43+
# Get the secret.
44+
response = client.get_secret(request={"name": name})
45+
46+
annotations = response.annotations
47+
48+
# Update the annotations
49+
for annotation_key in new_annotations:
50+
annotations[annotation_key] = new_annotations[annotation_key]
51+
52+
# Update the secret.
53+
secret = {"name": name, "annotations": annotations}
54+
update_mask = {"paths": ["annotations"]}
55+
response = client.update_secret(
56+
request={"secret": secret, "update_mask": update_mask}
57+
)
58+
59+
# Print the new secret name.
60+
print(f"Updated secret: {response.name}")
61+
62+
return response
63+
64+
65+
# [END secretmanager_edit_regional_secret_annotations]
66+
67+
if __name__ == "__main__":
68+
parser = argparse.ArgumentParser(
69+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
70+
)
71+
parser.add_argument("project_id", help="id of the GCP project")
72+
parser.add_argument(
73+
"location_id", help="id of the location where secret is to be created"
74+
)
75+
parser.add_argument("secret_id", help="id of the secret to act on")
76+
parser.add_argument(
77+
"annotation_key", help="key of the annotation to be added/updated"
78+
)
79+
parser.add_argument(
80+
"annotation_value", help="value of the annotation to be added/updated"
81+
)
82+
args = parser.parse_args()
83+
84+
annotations = {args.annotation_key, args.annotation_value}
85+
edit_regional_secret_annotations(
86+
args.project_id, args.location_id, args.secret_id, annotations
87+
)

0 commit comments

Comments
 (0)