Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/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 deleting an existing regional
secret.
"""

import argparse


# [START secretmanager_v1_delete_regional_secret_with_etag]
def delete_regional_secret_with_etag(project_id: str, location_id: str, secret_id: str, etag: str) -> None:
"""
Delete the regional secret with the given name, etag, and all of its versions.
"""

# Import the Secret Manager client library and types.
from google.cloud import secretmanager_v1
from google.cloud.secretmanager_v1.types import service

# 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 secret.
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}"

# Build the request
request = service.DeleteSecretRequest()
request.name = name
request.etag = etag

# Delete the secret.
client.delete_secret(request=request)


# [END secretmanager_v1_delete_regional_secret_with_etag]


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 location where secret is stored")
parser.add_argument("secret_id", help="id of the secret to delete")
parser.add_argument("etag", help="current etag of the secret to delete")
args = parser.parse_args()

delete_regional_secret_with_etag(args.project_id, args.location_id, args.secret_id, args.etag)
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/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 destroying a secret version.
"""

import argparse

from google.cloud import secretmanager_v1


# [START secretmanager_v1_destroy_regional_secret_version_with_etag]
def destroy_regional_secret_version_with_etag(
project_id: str, location_id: str, secret_id: str, version_id: str, etag: str
) -> secretmanager_v1.DestroySecretVersionRequest:
"""
Destroy the given secret version, making the payload irrecoverable. Other
secrets versions are unaffected.
"""

# Import the Secret Manager client library.
from google.cloud import secretmanager_v1
from google.cloud.secretmanager_v1.types import service

# 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 secret version
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}/versions/{version_id}"

# Build the request
request = service.DestroySecretVersionRequest()
request.name = name
request.etag = etag

# Destroy the secret version.
response = client.destroy_secret_version(request=request)

print(f"Destroyed secret version: {response.name}")
# [END secretmanager_v1_destroy_regional_secret_version_with_etag]

return response


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 location where secret is stored")
parser.add_argument("secret_id", help="id of the secret from which to act")
parser.add_argument("version_id", help="id of the version to destroy")
parser.add_argument("etag", help="current etag of the version")
args = parser.parse_args()

destroy_regional_secret_version_with_etag(
args.project_id, args.location_id, args.secret_id, args.version_id, args.etag
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/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 disabling a regional
secret version.
"""

import argparse

from google.cloud import secretmanager_v1


# [START secretmanager_v1_disable_regional_secret_version_with_etag]
def disable_regional_secret_version_with_etag(
project_id: str, location_id: str, secret_id: str, version_id: str, etag: str
) -> secretmanager_v1.DisableSecretVersionRequest:
"""
Disable the given secret version. Future requests will throw an error until
the secret version is enabled. Other secrets versions are unaffected.
"""

# Import the Secret Manager client library.
from google.cloud import secretmanager_v1
from google.cloud.secretmanager_v1.types import service

# 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 secret version
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}/versions/{version_id}"

# Build the request
request = service.DisableSecretVersionRequest()
request.name = name
request.etag = etag

# Disable the secret version.
response = client.disable_secret_version(request=request)

print(f"Disabled secret version: {response.name}")
# [END secretmanager_v1_disable_regional_secret_version_with_etag]

return response


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 location where secret is stored")
parser.add_argument("secret_id", help="id of the secret from which to act")
parser.add_argument("version_id", help="id of the version to disable")
parser.add_argument("etag", help="current etag of the version")
args = parser.parse_args()

disable_regional_secret_version_with_etag(
args.project_id, args.location_id, args.secret_id, args.version_id, args.etag
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/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 enabling a regional
secret version.
"""

import argparse

from google.cloud import secretmanager_v1


# [START secretmanager_v1_enable_regional_secret_version_with_etag]
def enable_regional_secret_version_with_etag(
project_id: str, location_id: str, secret_id: str, version_id: str, etag: str
) -> secretmanager_v1.EnableSecretVersionRequest:
"""
Enable the given secret version, enabling it to be accessed after
previously being disabled. Other secrets versions are unaffected.
"""

# Import the Secret Manager client library.
from google.cloud import secretmanager_v1
from google.cloud.secretmanager_v1.types import service

# 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 secret version
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}/versions/{version_id}"

# Build the request
request = service.EnableSecretVersionRequest()
request.name = name
request.etag = etag

# Disable the secret version.
response = client.enable_secret_version(request=request)

print(f"Enabled secret version: {response.name}")
# [END secretmanager_v1_enable_regional_secret_version_with_etag]

return response


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 location where secret is stored")
parser.add_argument("secret_id", help="id of the secret from which to act")
parser.add_argument("version_id", help="id of the version to enable")
parser.add_argument("etag", help="current etag of the version")
args = parser.parse_args()

enable_regional_secret_version_with_etag(
args.project_id, args.location_id, args.secret_id, args.version_id, args.etag
)
52 changes: 52 additions & 0 deletions secretmanager/snippets/regional_samples/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,34 @@
from regional_samples.add_regional_secret_version import add_regional_secret_version
from regional_samples.create_regional_secret import create_regional_secret
from regional_samples.delete_regional_secret import delete_regional_secret
from regional_samples.delete_regional_secret_with_etag import (
delete_regional_secret_with_etag,
)
from regional_samples.destroy_regional_secret_version import (
destroy_regional_secret_version,
)
from regional_samples.destroy_regional_secret_version_with_etag import (
destroy_regional_secret_version_with_etag,
)
from regional_samples.disable_regional_secret_version import (
disable_regional_secret_version,
)
from regional_samples.disable_regional_secret_version_with_etag import (
disable_regional_secret_version_with_etag,
)
from regional_samples.enable_regional_secret_version import (
enable_regional_secret_version,
)
from regional_samples.enable_regional_secret_version_with_etag import (
enable_regional_secret_version_with_etag,
)
from regional_samples.get_regional_secret import get_regional_secret
from regional_samples.get_regional_secret_version import get_regional_secret_version
from regional_samples.regional_quickstart import regional_quickstart
from regional_samples.update_regional_secret import update_regional_secret
from regional_samples.update_regional_secret_with_etag import (
update_regional_secret_with_etag,
)

@pytest.fixture()
def location_id() -> str:
Expand Down Expand Up @@ -199,6 +214,19 @@ def test_delete_regional_secret(
regional_client, request={"name": name}
)

def test_delete_regional_secret_with_etag(
regional_client: secretmanager_v1.SecretManagerServiceClient,
regional_secret: Tuple[str, str, str, str],
) -> None:
project_id, location_id, secret_id, etag = regional_secret
delete_regional_secret_with_etag(project_id, location_id, secret_id, etag)
with pytest.raises(exceptions.NotFound):
print(f"{regional_client}")
name = f"projects/{project_id}/locations/{location_id}/secrets/{secret_id}/versions/latest"
retry_client_access_regional_secret_version(
regional_client, request={"name": name}
)

def test_destroy_regional_secret_version(
regional_client: secretmanager_v1.SecretManagerServiceClient,
regional_secret_version: Tuple[str, str, str, str, str],
Expand All @@ -224,6 +252,21 @@ def test_enable_disable_regional_secret_version(
)
assert version.state == secretmanager.SecretVersion.State.ENABLED

def test_enable_disable_regional_secret_version_with_etag(
regional_client: secretmanager_v1.SecretManagerServiceClient,
regional_secret_version: Tuple[str, str, str, str, str],
) -> None:
project_id, location_id, secret_id, version_id, etag = regional_secret_version
version = disable_regional_secret_version_with_etag(
project_id, location_id, secret_id, version_id, etag
)
assert version.state == secretmanager.SecretVersion.State.DISABLED

version = enable_regional_secret_version_with_etag(
project_id, location_id, secret_id, version_id, version.etag
)
assert version.state == secretmanager.SecretVersion.State.ENABLED

def test_get_regional_secret_version(
regional_client: secretmanager_v1.SecretManagerServiceClient,
regional_secret_version: Tuple[str, str, str, str, str],
Expand All @@ -239,3 +282,12 @@ def test_update_regional_secret(regional_secret: Tuple[str, str, str, str]) -> N
project_id, location_id, secret_id, _ = regional_secret
updated_regional_secret = update_regional_secret(project_id, location_id, secret_id)
assert updated_regional_secret.labels["secretmanager"] == "rocks"

def test_update_regional_secret_with_etag(
regional_secret: Tuple[str, str, str, str]
) -> None:
project_id, location_id, secret_id, etag = regional_secret
updated_regional_secret = update_regional_secret_with_etag(
project_id, location_id, secret_id, etag
)
assert updated_regional_secret.labels["secretmanager"] == "rocks"
Loading