Skip to content

Commit 06ef306

Browse files
feat(secretmanager): Added samples for tags field (#13484)
* feat(secretmanager): added samples for tags field * fix(secretmanager): update return type and description
1 parent 538cd37 commit 06ef306

File tree

7 files changed

+647
-1
lines changed

7 files changed

+647
-1
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2025 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 then
17+
bind the tag to that secret.
18+
"""
19+
20+
# [START secretmanager_bind_tags_to_secret]
21+
import argparse
22+
23+
# Import the Secret Manager and Resource Manager client library.
24+
from google.cloud import resourcemanager_v3
25+
from google.cloud import secretmanager
26+
27+
28+
def bind_tags_to_secret(
29+
project_id: str,
30+
secret_id: str,
31+
tag_value: str,
32+
) -> resourcemanager_v3.TagBinding:
33+
"""
34+
Create a new secret with the given name, and then bind an existing tag to it.
35+
A secret is a logical wrapper around a collection of secret versions. Secret
36+
versions hold the actual 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+
secret_response = client.create_secret(
47+
request={
48+
"parent": parent,
49+
"secret_id": secret_id,
50+
"secret": {
51+
"replication": {"automatic": {}},
52+
},
53+
}
54+
)
55+
56+
# Print the new secret name.
57+
print(f"Created secret: {secret_response.name}")
58+
59+
# Create the resource manager client
60+
resource_manager_client = resourcemanager_v3.TagBindingsClient()
61+
62+
# Create the tag binding
63+
request = resourcemanager_v3.CreateTagBindingRequest(
64+
tag_binding=resourcemanager_v3.TagBinding(
65+
parent=f"//secretmanager.googleapis.com/{secret_response.name}",
66+
tag_value=f"{tag_value}",
67+
),
68+
)
69+
70+
# Create the tag binding
71+
operation = resource_manager_client.create_tag_binding(request=request)
72+
73+
# Wait for the operation to complete
74+
response = operation.result()
75+
76+
# Print the tag binding
77+
print(f"Created tag binding: {response.name}")
78+
79+
return response
80+
81+
82+
# [END secretmanager_bind_tags_to_secret]
83+
84+
if __name__ == "__main__":
85+
parser = argparse.ArgumentParser(
86+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
87+
)
88+
parser.add_argument("project_id", help="id of the GCP project")
89+
parser.add_argument("secret_id", help="id of the secret to create")
90+
parser.add_argument("tag_value", help="value of the tag you want to add")
91+
args = parser.parse_args()
92+
93+
bind_tags_to_secret(args.project_id, args.secret_id, args.tag_value)
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 2025 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+
tags.
18+
"""
19+
20+
# [START secretmanager_create_secret_with_tags]
21+
import argparse
22+
23+
# Import the Secret Manager client library.
24+
from google.cloud import secretmanager
25+
26+
27+
def create_secret_with_tags(
28+
project_id: str,
29+
secret_id: str,
30+
tag_key: str,
31+
tag_value: str,
32+
) -> secretmanager.Secret:
33+
"""
34+
Create a new secret with the given name and associated tags. A secret is a
35+
logical wrapper around a collection of secret versions. Secret versions hold
36+
the actual 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+
"tags": {
53+
tag_key: tag_value
54+
}
55+
},
56+
}
57+
)
58+
59+
# Print the new secret name.
60+
print(f"Created secret: {response.name}")
61+
62+
return response
63+
64+
65+
# [END secretmanager_create_secret_with_tags]
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("secret_id", help="id of the secret to create")
73+
parser.add_argument("tag_key", help="key of the tag you want to add")
74+
parser.add_argument("tag_value", help="value of the tag you want to add")
75+
args = parser.parse_args()
76+
77+
create_secret_with_tags(args.project_id, args.secret_id, args.tag_key, args.tag_value)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2025 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 then
17+
bind the tag to that secret.
18+
"""
19+
20+
# [START secretmanager_bind_tags_to_regional_secret]
21+
import argparse
22+
23+
# Import the Secret Manager and Resource Manager client library.
24+
from google.cloud import resourcemanager_v3
25+
from google.cloud import secretmanager
26+
27+
28+
def bind_tags_to_regional_secret(
29+
project_id: str,
30+
location_id: str,
31+
secret_id: str,
32+
tag_value: str,
33+
) -> resourcemanager_v3.TagBinding:
34+
"""
35+
Create a new regional secret with the given name, and then bind an existing
36+
tag to it. A secret is a logical wrapper around a collection of secret
37+
versions. Secret versions hold the actual 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.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+
secret_response = client.create_secret(
53+
request={
54+
"parent": parent,
55+
"secret_id": secret_id,
56+
}
57+
)
58+
59+
# Print the new secret name.
60+
print(f"Created secret: {secret_response.name}")
61+
62+
# Endpoint to call the regional secret manager sever
63+
resource_manager_api_endpoint = f"{location_id}-cloudresourcemanager.googleapis.com"
64+
65+
# Create the resource manager client
66+
resource_manager_client = resourcemanager_v3.TagBindingsClient(
67+
client_options={"api_endpoint": resource_manager_api_endpoint},
68+
)
69+
70+
# Create the tag binding
71+
request = resourcemanager_v3.CreateTagBindingRequest(
72+
tag_binding=resourcemanager_v3.TagBinding(
73+
parent=f"//secretmanager.googleapis.com/{secret_response.name}",
74+
tag_value=f"{tag_value}",
75+
),
76+
)
77+
78+
# Create the tag binding
79+
operation = resource_manager_client.create_tag_binding(request=request)
80+
81+
# Wait for the operation to complete
82+
response = operation.result()
83+
84+
# Print the tag binding
85+
print(f"Created tag binding: {response.name}")
86+
87+
return response
88+
89+
90+
# [END secretmanager_bind_tags_to_regional_secret]
91+
92+
if __name__ == "__main__":
93+
parser = argparse.ArgumentParser(
94+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
95+
)
96+
parser.add_argument("project_id", help="id of the GCP project")
97+
parser.add_argument(
98+
"location_id", help="id of the location where secret is to be created"
99+
)
100+
parser.add_argument("secret_id", help="id of the secret to create")
101+
parser.add_argument("tag_value", help="value of the tag you want to add")
102+
args = parser.parse_args()
103+
104+
bind_tags_to_regional_secret(args.project_id, args.location_id, args.secret_id, args.tag_value)
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 2025 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+
tags.
18+
"""
19+
20+
# [START secretmanager_create_regional_secret_with_tags]
21+
import argparse
22+
23+
# Import the Secret Manager client library.
24+
from google.cloud import secretmanager_v1
25+
26+
27+
def create_regional_secret_with_tags(
28+
project_id: str,
29+
location_id: str,
30+
secret_id: str,
31+
tag_key: str,
32+
tag_value: str,
33+
) -> secretmanager_v1.Secret:
34+
"""
35+
Create a new regional secret with the given name and associated tags. A
36+
secret is a logical wrapper around a collection of secret versions. Secret
37+
versions hold the actual secret material.
38+
"""
39+
40+
# Endpoint to call the regional Secret Manager API.
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 secret.
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": {
57+
"tags": {
58+
tag_key: tag_value
59+
}
60+
},
61+
}
62+
)
63+
64+
# Print the new secret name.
65+
print(f"Created secret: {response.name}")
66+
67+
return response
68+
69+
70+
# [END secretmanager_create_regional_secret_with_tags]
71+
72+
if __name__ == "__main__":
73+
parser = argparse.ArgumentParser(
74+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
75+
)
76+
parser.add_argument("project_id", help="id of the GCP project")
77+
parser.add_argument(
78+
"location_id", help="id of the location where secret is to be created"
79+
)
80+
parser.add_argument("secret_id", help="id of the secret to create")
81+
parser.add_argument("tag_key", help="key of the tag you want to add")
82+
parser.add_argument("tag_value", help="value of the tag you want to add")
83+
args = parser.parse_args()
84+
85+
create_regional_secret_with_tags(
86+
args.project_id, args.location_id, args.secret_id, args.tag_key, args.tag_value
87+
)

0 commit comments

Comments
 (0)