Skip to content

Commit 3d63408

Browse files
fix:added regional samples for SM (#12475)
* fix:added regional samples for SM * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: updated tags to include return statements * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix:linting issues * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix:linting * fix:linting fix * fix: resolved comments * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: resolved comments * fix: resolved comments * fix: resolved comments * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix:added regional samples for SM (#12477) * Regional samples 2 (#12476) * regional samples for regional samples SM * fix:add samples for regional secret manager * fix:fixed tests * fix:linting issues * fix: updated tags to include return statements * fix: resolved comments * fix:resolved comments * fix:resolved comments * fix: resolved comments * fix: resolved comments * fix: resolved comments * fix:added regional samples for SM (#12479) * fix:added regional samples for SM * fix: updated tags to include return statements * fix: resolved comments * fix: resolved comments * fix:added regional samples for SM * fix: updated tags to include return statements * fix: resolved comments * fix: resolved comments * fix: resolved comments * fix: resolved comments * fix: resolved comments * fix(secretmanager): add new regional samples (#12480) * fix:added regional samples for SM * fix:regional samples for SM * fix: updated tags to include return statements * fix:regional samples for SM (#12481) * fix:regional samples for SM * fix: updated tags to include return statements * fix:resolved comments * fix:resolved comments * fix:added regional samples for SM * fix:regional samples for SM * fix: updated tags to include return statements * fix:regional samples for SM (#12481) * fix:regional samples for SM * fix: updated tags to include return statements * fix:resolved comments * fix: merge errors * fix:resolve commits * fix: resolved comments * fix: resolved comments * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix: resolved comments --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 388a55c commit 3d63408

26 files changed

+2084
-2
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import glob
2+
from os import path
3+
4+
modules = glob.glob(path.join(path.dirname(__file__), "*.py"))
5+
__all__ = [
6+
path.basename(f)[:-3]
7+
for f in modules
8+
if path.isfile(f)
9+
and not (f.endswith("__init__.py") or f.endswith("snippets_test.py"))
10+
]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 accessing a regional secret version.
17+
"""
18+
19+
import argparse
20+
21+
# [START secretmanager_access_regional_secret_version]
22+
from google.cloud import secretmanager_v1
23+
import google_crc32c
24+
25+
26+
def access_regional_secret_version(
27+
project_id: str,
28+
location_id: str,
29+
secret_id: str,
30+
version_id: str,
31+
) -> secretmanager_v1.AccessSecretVersionResponse:
32+
"""
33+
Access the payload for the given secret version if one exists. The version
34+
can be a version number as a string (e.g. "5") or an alias (e.g. "latest").
35+
"""
36+
37+
# Endpoint to call the regional secret manager sever.
38+
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com"
39+
40+
# Create the Secret Manager client.
41+
client = secretmanager_v1.SecretManagerServiceClient(
42+
client_options={"api_endpoint": api_endpoint},
43+
)
44+
45+
# Build the resource name of the secret version.
46+
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}/versions/{version_id}"
47+
48+
# Access the secret version.
49+
response = client.access_secret_version(request={"name": name})
50+
51+
# Verify payload checksum.
52+
crc32c = google_crc32c.Checksum()
53+
crc32c.update(response.payload.data)
54+
if response.payload.data_crc32c != int(crc32c.hexdigest(), 16):
55+
print("Data corruption detected.")
56+
return response
57+
58+
# Print the secret payload.
59+
#
60+
# WARNING: Do not print the secret in a production environment - this
61+
# snippet is showing how to access the secret material.
62+
payload = response.payload.data.decode("UTF-8")
63+
print(f"Plaintext: {payload}")
64+
65+
return response
66+
67+
68+
# [END secretmanager_access_regional_secret_version]
69+
70+
if __name__ == "__main__":
71+
parser = argparse.ArgumentParser(
72+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
73+
)
74+
parser.add_argument("project_id", help="id of the GCP project")
75+
parser.add_argument("location_id", help="id of location where secret is stored")
76+
parser.add_argument("secret_id", help="id of the secret to access")
77+
parser.add_argument("version_id", help="version of the secret to access")
78+
args = parser.parse_args()
79+
80+
access_regional_secret_version(
81+
args.project_id, args.location_id, args.secret_id, args.version_id
82+
)
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+
Command line application and sample code for adding a secret version to a
17+
regional secret with the specified payload to an existing regional secret.
18+
"""
19+
20+
import argparse
21+
22+
# [START secretmanager_v1_add_regional_secret_version]
23+
from google.cloud import secretmanager_v1
24+
import google_crc32c
25+
26+
27+
def add_regional_secret_version(
28+
project_id: str,
29+
location_id: str,
30+
secret_id: str,
31+
payload: str,
32+
) -> secretmanager_v1.SecretVersion:
33+
"""
34+
Adds a new secret version to the given secret with the provided payload.
35+
"""
36+
37+
# Endpoint to call the regional secret manager sever.
38+
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com"
39+
40+
# Create the Secret Manager client.
41+
client = secretmanager_v1.SecretManagerServiceClient(
42+
client_options={"api_endpoint": api_endpoint},
43+
)
44+
45+
# Build the resource name of the parent secret.
46+
parent = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}"
47+
48+
# Convert the string payload into a bytes. This step can be omitted if you
49+
# pass in bytes instead of a str for the payload argument.
50+
payload_bytes = payload.encode("UTF-8")
51+
52+
# Calculate payload checksum. Passing a checksum in add-version request
53+
# is optional.
54+
crc32c = google_crc32c.Checksum()
55+
crc32c.update(payload_bytes)
56+
57+
# Add the secret version.
58+
response = client.add_secret_version(
59+
request={
60+
"parent": parent,
61+
"payload": {
62+
"data": payload_bytes,
63+
"data_crc32c": int(crc32c.hexdigest(), 16),
64+
},
65+
}
66+
)
67+
68+
# Print the new secret version name.
69+
print(f"Added secret version: {response.name}")
70+
return response
71+
72+
73+
# [END secretmanager_v1_add_regional_secret_version]
74+
75+
if __name__ == "__main__":
76+
parser = argparse.ArgumentParser(
77+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
78+
)
79+
parser.add_argument("project_id", help="id of the GCP project")
80+
parser.add_argument("location_id", help="id of location where secret is stored")
81+
parser.add_argument("secret_id", help="id of the secret in which to add")
82+
parser.add_argument("payload", help="secret material payload")
83+
args = parser.parse_args()
84+
85+
add_regional_secret_version(
86+
args.project_id, args.location_id, args.secret_id, args.payload
87+
)
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+
Command line application and sample code for creating a new regional secret.
17+
"""
18+
19+
import argparse
20+
from typing import Optional
21+
22+
23+
# [START secretmanager_v1_create_regional_secret]
24+
from google.cloud import secretmanager_v1
25+
26+
27+
def create_regional_secret(
28+
project_id: str,
29+
location_id: str,
30+
secret_id: str,
31+
ttl: Optional[str] = None,
32+
) -> secretmanager_v1.Secret:
33+
"""
34+
Creates a new regional secret with the given name.
35+
"""
36+
37+
# Endpoint to call the regional secret manager sever
38+
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com"
39+
40+
# Create the Secret Manager client.
41+
client = secretmanager_v1.SecretManagerServiceClient(
42+
client_options={"api_endpoint": api_endpoint},
43+
)
44+
45+
# Build the resource name of the parent project.
46+
parent = f"projects/{project_id}/locations/{location_id}"
47+
48+
# Create the secret.
49+
response = client.create_secret(
50+
request={
51+
"parent": parent,
52+
"secret_id": secret_id,
53+
"secret": {"ttl": ttl},
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_v1_create_regional_secret]
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(
71+
"location_id", help="id of the location where secret is to be created"
72+
)
73+
parser.add_argument("secret_id", help="id of the secret to create")
74+
parser.add_argument("ttl", help="time to live for secrets, f.e. '600s' ")
75+
args = parser.parse_args()
76+
77+
create_regional_secret(args.project_id, args.location_id, args.secret_id, args.ttl)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 deleting an existing regional
17+
secret.
18+
"""
19+
20+
import argparse
21+
22+
23+
# [START secretmanager_v1_delete_regional_secret]
24+
# Import the Secret Manager client library.
25+
from google.cloud import secretmanager_v1
26+
27+
28+
def delete_regional_secret(project_id: str, location_id: str, secret_id: str) -> None:
29+
"""
30+
Deletes the secret with the given name and all of its versions.
31+
"""
32+
33+
# Endpoint to call the regional secret manager sever
34+
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com"
35+
36+
# Create the Secret Manager client.
37+
client = secretmanager_v1.SecretManagerServiceClient(
38+
client_options={"api_endpoint": api_endpoint},
39+
)
40+
41+
# Build the resource name of the secret.
42+
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}"
43+
44+
# Delete the secret.
45+
client.delete_secret(request={"name": name})
46+
47+
48+
# [END secretmanager_v1_delete_regional_secret]
49+
50+
51+
if __name__ == "__main__":
52+
parser = argparse.ArgumentParser(
53+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
54+
)
55+
parser.add_argument("project_id", help="id of the GCP project")
56+
parser.add_argument("location_id", help="id of location where secret is stored")
57+
parser.add_argument("secret_id", help="id of the secret to delete")
58+
args = parser.parse_args()
59+
60+
delete_regional_secret(args.project_id, args.location_id, args.secret_id)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 deleting an existing regional
17+
secret.
18+
"""
19+
20+
import argparse
21+
22+
23+
# [START secretmanager_v1_delete_regional_secret_with_etag]
24+
# Import the Secret Manager client library and types.
25+
from google.cloud import secretmanager_v1
26+
27+
28+
def delete_regional_secret_with_etag(
29+
project_id: str,
30+
location_id: str,
31+
secret_id: str,
32+
etag: str,
33+
) -> None:
34+
"""
35+
Deletes the regional secret with the given name, etag, and all of its versions.
36+
"""
37+
38+
# Endpoint to call the regional secret manager sever.
39+
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com"
40+
41+
# Create the Secret Manager client.
42+
client = secretmanager_v1.SecretManagerServiceClient(
43+
client_options={"api_endpoint": api_endpoint},
44+
)
45+
46+
# Build the resource name of the secret.
47+
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}"
48+
49+
# Build the request
50+
request = secretmanager_v1.types.service.DeleteSecretRequest(
51+
name=name,
52+
etag=etag,
53+
)
54+
55+
# Delete the secret.
56+
client.delete_secret(request=request)
57+
58+
59+
# [END secretmanager_v1_delete_regional_secret_with_etag]
60+
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("location_id", help="id of location where secret is stored")
68+
parser.add_argument("secret_id", help="id of the secret to delete")
69+
parser.add_argument("etag", help="current etag of the secret to delete")
70+
args = parser.parse_args()
71+
72+
delete_regional_secret_with_etag(
73+
args.project_id, args.location_id, args.secret_id, args.etag
74+
)

0 commit comments

Comments
 (0)