Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions secretmanager/snippets/create_secret_with_delayed_destroy.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,21 @@ def create_secret_with_delayed_destroy(

return response


Comment thread
XrossFox marked this conversation as resolved.
# [END secretmanager_create_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("project_id", help="id of the Google Cloud project")
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")
parser.add_argument(
"version_destroy_ttl", type=int, help="version_destroy_ttl you want to add"
)
Comment thread
XrossFox marked this conversation as resolved.
args = parser.parse_args()

create_secret_with_delayed_destroy(args.project_id, args.secret_id, args.version_destroy_ttl)
create_secret_with_delayed_destroy(
args.project_id, args.secret_id, args.version_destroy_ttl
)
11 changes: 4 additions & 7 deletions secretmanager/snippets/create_secret_with_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ def create_secret_with_tags(
request={
"parent": parent,
"secret_id": secret_id,
"secret": {
"replication": {"automatic": {}},
"tags": {
tag_key: tag_value
}
},
"secret": {"replication": {"automatic": {}}, "tags": {tag_key: tag_value}},
}
)

Expand All @@ -74,4 +69,6 @@ def create_secret_with_tags(
parser.add_argument("tag_value", help="value of the tag you want to add")
args = parser.parse_args()

create_secret_with_tags(args.project_id, args.secret_id, args.tag_key, args.tag_value)
create_secret_with_tags(
args.project_id, args.secret_id, args.tag_key, args.tag_value
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ def disable_secret_with_delayed_destroy(
# Delayed destroy of the secret version.
secret = {"name": name}
update_mask = {"paths": ["version_destroy_ttl"]}
response = client.update_secret(request={"secret": secret, "update_mask": update_mask})
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_secret_delayed_destroy]


Expand All @@ -55,6 +58,4 @@ def disable_secret_with_delayed_destroy(
parser.add_argument("secret_id", help="id of the secret from which to act")
args = parser.parse_args()

disable_secret_with_delayed_destroy(
args.project_id, args.secret_id
)
disable_secret_with_delayed_destroy(args.project_id, args.secret_id)
2 changes: 1 addition & 1 deletion secretmanager/snippets/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pytest==9.0.3; python_version >= "3.10"
pytest==9.0.3
8 changes: 4 additions & 4 deletions secretmanager/snippets/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
google-cloud-resource-manager==1.14.2
google-cloud-secret-manager==2.24.0
google-crc32c==1.6.0
protobuf==5.29.4
protobuf==6.33.6
google-cloud-resource-manager==1.18.0
google-cloud-secret-manager==2.29.0
google-crc32c==1.8.0
28 changes: 19 additions & 9 deletions secretmanager/snippets/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@
# See the License for the specific language governing permissions and

import base64
from datetime import timedelta
import uuid
import os
import time
from datetime import timedelta
from typing import Iterator, Optional, Tuple, Union
import uuid

from google.api_core import exceptions, retry
from google.cloud import resourcemanager_v3
from google.cloud import secretmanager
from google.protobuf.duration_pb2 import Duration

import pytest

from access_secret_version import access_secret_version
from add_secret_version import add_secret_version
from bind_tags_to_secret import bind_tags_to_secret
Expand Down Expand Up @@ -64,6 +62,8 @@
from view_secret_annotations import view_secret_annotations
from view_secret_labels import view_secret_labels

import pytest


@pytest.fixture()
def client() -> secretmanager.SecretManagerServiceClient:
Expand Down Expand Up @@ -468,9 +468,13 @@ def test_create_secret_with_annotations(

def test_create_secret_with_delayed_destroy(
client: secretmanager.SecretManagerServiceClient,
project_id: str, secret_id: str, version_destroy_ttl: int
project_id: str,
secret_id: str,
version_destroy_ttl: int,
) -> None:
secret = create_secret_with_delayed_destroy(project_id, secret_id, version_destroy_ttl)
secret = create_secret_with_delayed_destroy(
project_id, secret_id, version_destroy_ttl
)
assert secret_id in secret.name
assert timedelta(seconds=version_destroy_ttl) == secret.version_destroy_ttl

Expand Down Expand Up @@ -740,8 +744,14 @@ def test_update_secret_with_alias(secret_version: Tuple[str, str, str, str]) ->
assert secret.version_aliases["test"] == 1


def test_update_secret_with_delayed_destroy(secret_with_delayed_destroy: Tuple[str, str], version_destroy_ttl: str) -> None:
def test_update_secret_with_delayed_destroy(
secret_with_delayed_destroy: Tuple[str, str]
) -> None:
Comment thread
XrossFox marked this conversation as resolved.
project_id, secret_id = secret_with_delayed_destroy
updated_version_destroy_ttl_value = 118400
updated_secret = update_secret_with_delayed_destroy(project_id, secret_id, updated_version_destroy_ttl_value)
assert updated_secret.version_destroy_ttl == timedelta(seconds=updated_version_destroy_ttl_value)
updated_secret = update_secret_with_delayed_destroy(
project_id, secret_id, updated_version_destroy_ttl_value
)
assert updated_secret.version_destroy_ttl == timedelta(
seconds=updated_version_destroy_ttl_value
)
14 changes: 10 additions & 4 deletions secretmanager/snippets/update_secret_with_delayed_destroy.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ def update_secret_with_delayed_destroy(
name = client.secret_path(project_id, secret_id)

# Update the version_destroy_ttl.
secret = {"name": name, "version_destroy_ttl": Duration(seconds=new_version_destroy_ttl)}
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}
Expand All @@ -47,16 +50,19 @@ def update_secret_with_delayed_destroy(

return response


# [END secretmanager_update_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("project_id", help="id of the Google Cloud project")
parser.add_argument("secret-id", help="id of the secret to act on")
parser.add_argument("version_destroy_ttl", "new version destroy ttl to be added")
parser.add_argument("version_destroy_ttl", type=int, help="new version destroy ttl to be added")
args = parser.parse_args()

update_secret_with_delayed_destroy(args.project_id, args.secret_id, args.version_destroy_ttl)
update_secret_with_delayed_destroy(
args.project_id, args.secret_id, args.version_destroy_ttl
)
Comment thread
XrossFox marked this conversation as resolved.
1 change: 1 addition & 0 deletions secretmanager/snippets/view_secret_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
command line application and sample code for listing annotations of a
secret.
"""

# [START secretmanager_view_secret_annotations]
import argparse

Expand Down
1 change: 1 addition & 0 deletions secretmanager/snippets/view_secret_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
command line application and sample code for listing labels of a
secret.
"""

# [START secretmanager_view_secret_labels]
import argparse

Expand Down