-
Notifications
You must be signed in to change notification settings - Fork 6.5k
chore(secretmanager): Add regional samples for delayed destory #13317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env python | ||
|
||
# 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 | ||
# | ||
# http://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 | ||
""" | ||
Command line application and sample code for creating a new secret with | ||
delayed destroy. | ||
""" | ||
|
||
import argparse | ||
|
||
# [START secretmanager_create_regional_secret_with_delayed_destroy] | ||
|
||
# Import the Secret Manager client library. | ||
from google.cloud import secretmanager_v1 | ||
from google.protobuf.duration_pb2 import Duration | ||
|
||
|
||
def create_regional_secret_with_delayed_destroy( | ||
project_id: str, | ||
location_id: str, | ||
secret_id: str, | ||
version_destroy_ttl: int, | ||
) -> secretmanager_v1.Secret: | ||
""" | ||
Create a new secret with the given name and version_destroy_ttl. A secret is a logical wrapper | ||
around a collection of secret versions. Secret versions hold the actual | ||
secret material. | ||
""" | ||
|
||
# Endpoint to call the regional secret manager sever | ||
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com" | ||
|
||
# Create the Secret Manager client. | ||
client = secretmanager_v1.SecretManagerServiceClient( | ||
client_options={"api_endpoint": api_endpoint}, | ||
) | ||
|
||
# Build the resource name of the parent project. | ||
parent = f"projects/{project_id}/locations/{location_id}" | ||
|
||
# Create the secret. | ||
response = client.create_secret( | ||
request={ | ||
"parent": parent, | ||
"secret_id": secret_id, | ||
"secret": {"version_destroy_ttl": Duration(seconds=version_destroy_ttl)}, | ||
} | ||
) | ||
|
||
# Print the new secret name. | ||
print(f"Created secret: {response.name}") | ||
|
||
return response | ||
|
||
# [END secretmanager_create_regional_secret_with_delayed_destroy] | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
) | ||
parser.add_argument("project_id", help="id of the GCP project") | ||
parser.add_argument( | ||
"location_id", help="id of the location where secret is to be created" | ||
) | ||
parser.add_argument("secret_id", help="id of the secret to create") | ||
parser.add_argument( | ||
"version_destroy_ttl", help="version_destroy_ttl you want to add" | ||
) | ||
args = parser.parse_args() | ||
|
||
create_regional_secret_with_delayed_destroy( | ||
args.project_id, args.location_id, args.secret_id, args.version_destroy_ttl | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#!/usr/bin/env python | ||
|
||
# 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 | ||
# | ||
# http://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 | ||
|
||
import argparse | ||
|
||
# [START secretmanager_disable_regional_secret_delayed_destroy] | ||
|
||
# Import the Secret Manager client library. | ||
from google.cloud import secretmanager_v1 | ||
|
||
|
||
def disable_regional_secret_delayed_destroy( | ||
project_id: str, location_id: str, secret_id: str | ||
) -> secretmanager_v1.Secret: | ||
""" | ||
Disable delayed destroy on an existing secret with a version destroy ttl. | ||
""" | ||
|
||
# Endpoint to call the regional secret manager sever | ||
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com" | ||
|
||
# Create the Secret Manager client. | ||
client = secretmanager_v1.SecretManagerServiceClient( | ||
client_options={"api_endpoint": api_endpoint}, | ||
) | ||
|
||
# Build the resource name. | ||
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}" | ||
|
||
# Disable delayed destroy on secret | ||
secret = {"name": name} | ||
update_mask = {"paths": ["version_destroy_ttl"]} | ||
response = client.update_secret( | ||
request={"secret": secret, "update_mask": update_mask} | ||
) | ||
|
||
# Print the new secret name. | ||
print(f"Disabled delayed destroy on secret: {response.name}") | ||
|
||
return response | ||
|
||
# [END secretmanager_disable_regional_secret_delayed_destroy] | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
) | ||
parser.add_argument("project_id", help="id of the GCP project") | ||
parser.add_argument( | ||
"location_id", help="id of the location where secret is to be created" | ||
) | ||
parser.add_argument("secret_id", help="id of the secret to act on") | ||
args = parser.parse_args() | ||
|
||
disable_regional_secret_delayed_destroy( | ||
args.project_id, args.location_id, args.secret_id | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/usr/bin/env python | ||
|
||
# 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 | ||
# | ||
# http://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 | ||
|
||
import argparse | ||
|
||
# [START secretmanager_update_regional_secret_delayed_destroy] | ||
|
||
# Import the Secret Manager client library. | ||
from google.cloud import secretmanager_v1 | ||
from google.protobuf.duration_pb2 import Duration | ||
|
||
|
||
def update_regional_secret_with_delayed_destroy( | ||
project_id: str, location_id: str, secret_id: str, new_version_destroy_ttl: int | ||
) -> secretmanager_v1.UpdateSecretRequest: | ||
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function signature uses def update_regional_secret_with_delayed_destroy(
project_id: str, location_id: str, secret_id: str, new_version_destroy_ttl: int
) -> secretmanager_v1.UpdateSecretRequest: |
||
""" | ||
Comment on lines
+27
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type annotation is incorrect. The function returns a def update_regional_secret_with_delayed_destroy(
project_id: str, location_id: str, secret_id: str, new_version_destroy_ttl: int
) -> secretmanager_v1.Secret: |
||
Update the version destroy ttl on an existing secret. | ||
""" | ||
|
||
# Endpoint to call the regional secret manager sever | ||
api_endpoint = f"secretmanager.{location_id}.rep.googleapis.com" | ||
|
||
# Create the Secret Manager client. | ||
client = secretmanager_v1.SecretManagerServiceClient( | ||
client_options={"api_endpoint": api_endpoint}, | ||
) | ||
|
||
# Build the resource name. | ||
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}" | ||
|
||
# Get the secret. | ||
response = client.get_secret(request={"name": name}) | ||
|
||
# Update the secret. | ||
secret = {"name": name, "version_destroy_ttl": Duration(seconds=new_version_destroy_ttl)} | ||
update_mask = {"paths": ["version_destroy_ttl"]} | ||
response = client.update_secret( | ||
request={"secret": secret, "update_mask": update_mask} | ||
) | ||
|
||
# Print the new secret name. | ||
print(f"Updated secret: {response.name}") | ||
|
||
return response | ||
|
||
# [END secretmanager_update_regional_secret_delayed_destroy] | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser( | ||
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter | ||
) | ||
parser.add_argument("project_id", help="id of the GCP project") | ||
parser.add_argument( | ||
"location_id", help="id of the location where secret is to be created" | ||
) | ||
parser.add_argument("secret_id", help="id of the secret to act on") | ||
parser.add_argument( | ||
"version_destroy_ttl", help="version_destroy_ttl you want to add" | ||
) | ||
Comment on lines
+71
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The argument name "new_version_destroy_ttl", help="version_destroy_ttl you want to add"
)
args = parser.parse_args()
update_regional_secret_with_delayed_destroy(
args.project_id, args.location_id, args.secret_id, args.new_version_destroy_ttl
) |
||
args = parser.parse_args() | ||
|
||
update_regional_secret_with_delayed_destroy( | ||
args.project_id, args.location_id, args.secret_id, args.version_destroy_ttl | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
version_destroy_ttl
argument is passed as a string from the command line, but it's used as an integer in thecreate_regional_secret_with_delayed_destroy
function. Consider converting it to an integer here to avoid potential type errors.