Skip to content

Commit e69c7b0

Browse files
gwhitehawkMiroslava Sotakova
authored andcommitted
chore: added code samples for etag usage (#129) (#130)
Co-authored-by: Miroslava Sotakova <[email protected]>
1 parent 933ca90 commit e69c7b0

6 files changed

+360
-17
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 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 secret.
17+
"""
18+
19+
import argparse
20+
21+
22+
# [START secretmanager_delete_secret_with_etag]
23+
def delete_secret_with_etag(project_id, secret_id, etag):
24+
"""
25+
Delete the secret with the given name, etag, and all of its versions.
26+
"""
27+
28+
# Import the Secret Manager client library and types.
29+
from google.cloud import secretmanager
30+
from google.cloud.secretmanager_v1.types import service
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+
# Build the request
39+
request = service.DeleteSecretRequest()
40+
request.name = name
41+
request.etag = etag
42+
43+
# Delete the secret.
44+
client.delete_secret(request=request)
45+
46+
47+
# [END secretmanager_delete_secret_with_etag]
48+
49+
50+
if __name__ == "__main__":
51+
parser = argparse.ArgumentParser(
52+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
53+
)
54+
parser.add_argument("project_id", help="id of the GCP project")
55+
parser.add_argument("secret_id", help="id of the secret to delete")
56+
parser.add_argument("etag", help="current etag of the secret to delete")
57+
args = parser.parse_args()
58+
59+
delete_secret_with_etag(args.project_id, args.secret_id, args.etag)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 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 destroying a secret verison.
17+
"""
18+
19+
import argparse
20+
21+
22+
# [START secretmanager_destroy_secret_version_with_etag]
23+
def destroy_secret_version_with_etag(project_id, secret_id, version_id, etag):
24+
"""
25+
Destroy the given secret version, making the payload irrecoverable. Other
26+
secrets versions are unaffected.
27+
"""
28+
29+
# Import the Secret Manager client library.
30+
from google.cloud import secretmanager
31+
from google.cloud.secretmanager_v1.types import service
32+
33+
# Create the Secret Manager client.
34+
client = secretmanager.SecretManagerServiceClient()
35+
36+
# Build the resource name of the secret version
37+
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"
38+
39+
# Build the request
40+
request = service.DestroySecretVersionRequest()
41+
request.name = name
42+
request.etag = etag
43+
44+
# Destroy the secret version.
45+
response = client.destroy_secret_version(request=request)
46+
47+
print("Destroyed secret version: {}".format(response.name))
48+
# [END secretmanager_destroy_secret_version_with_etag]
49+
50+
return response
51+
52+
53+
if __name__ == "__main__":
54+
parser = argparse.ArgumentParser(
55+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
56+
)
57+
parser.add_argument("project_id", help="id of the GCP project")
58+
parser.add_argument("secret_id", help="id of the secret from which to act")
59+
parser.add_argument("version_id", help="id of the version to destroy")
60+
parser.add_argument("etag", help="current etag of the version")
61+
args = parser.parse_args()
62+
63+
destroy_secret_version_with_etag(
64+
args.project_id, args.secret_id, args.version_id, args.etag)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 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 disabling a secret version.
17+
"""
18+
19+
import argparse
20+
21+
22+
# [START secretmanager_disable_secret_version_with_etag]
23+
def disable_secret_version_with_etag(project_id, secret_id, version_id, etag):
24+
"""
25+
Disable the given secret version. Future requests will throw an error until
26+
the secret version is enabled. Other secrets versions are unaffected.
27+
"""
28+
29+
# Import the Secret Manager client library.
30+
from google.cloud import secretmanager
31+
from google.cloud.secretmanager_v1.types import service
32+
33+
# Create the Secret Manager client.
34+
client = secretmanager.SecretManagerServiceClient()
35+
36+
# Build the resource name of the secret version
37+
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"
38+
39+
# Build the request
40+
request = service.DisableSecretVersionRequest()
41+
request.name = name
42+
request.etag = etag
43+
44+
# Disable the secret version.
45+
response = client.disable_secret_version(request=request)
46+
47+
print("Disabled secret version: {}".format(response.name))
48+
# [END secretmanager_disable_secret_version_with_etag]
49+
50+
return response
51+
52+
53+
if __name__ == "__main__":
54+
parser = argparse.ArgumentParser(
55+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
56+
)
57+
parser.add_argument("project_id", help="id of the GCP project")
58+
parser.add_argument("secret_id", help="id of the secret from which to act")
59+
parser.add_argument("version_id", help="id of the version to disable")
60+
parser.add_argument("etag", help="current etag of the version")
61+
args = parser.parse_args()
62+
63+
disable_secret_version_with_etag(
64+
args.project_id, args.secret_id, args.version_id, args.etag)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 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 enabling a secret version.
17+
"""
18+
19+
import argparse
20+
21+
22+
# [START secretmanager_enable_secret_version_with_etag]
23+
def enable_secret_version_with_etag(project_id, secret_id, version_id, etag):
24+
"""
25+
Enable the given secret version, enabling it to be accessed after
26+
previously being disabled. Other secrets versions are unaffected.
27+
"""
28+
29+
# Import the Secret Manager client library.
30+
from google.cloud import secretmanager
31+
from google.cloud.secretmanager_v1.types import service
32+
33+
# Create the Secret Manager client.
34+
client = secretmanager.SecretManagerServiceClient()
35+
36+
# Build the resource name of the secret version
37+
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"
38+
39+
# Build the request
40+
request = service.EnableSecretVersionRequest()
41+
request.name = name
42+
request.etag = etag
43+
44+
# Disable the secret version.
45+
response = client.enable_secret_version(request=request)
46+
47+
print("Enabled secret version: {}".format(response.name))
48+
# [END secretmanager_enable_secret_version_with_etag]
49+
50+
return response
51+
52+
53+
if __name__ == "__main__":
54+
parser = argparse.ArgumentParser(
55+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
56+
)
57+
parser.add_argument("project_id", help="id of the GCP project")
58+
parser.add_argument("secret_id", help="id of the secret from which to act")
59+
parser.add_argument("version_id", help="id of the version to enable")
60+
parser.add_argument("etag", help="current etag of the version")
61+
args = parser.parse_args()
62+
63+
enable_secret_version_with_etag(
64+
args.project_id, args.secret_id, args.version_id, args.etag)

0 commit comments

Comments
 (0)