diff --git a/.release-please-manifest.json b/.release-please-manifest.json index f1c1e58..210d290 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.5.0" + ".": "0.5.1" } diff --git a/CHANGELOG.md b/CHANGELOG.md index cfcf2d4..0be9b0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## [0.5.1](https://github.com/googleapis/python-video-stitcher/compare/v0.5.0...v0.5.1) (2023-01-05) + + +### Documentation + +* **samples:** Add Media CDN samples ([b905053](https://github.com/googleapis/python-video-stitcher/commit/b9050531174536744f967548829f4325ab4d53e3)) +* **samples:** Move Akamai sample to its own file ([b905053](https://github.com/googleapis/python-video-stitcher/commit/b9050531174536744f967548829f4325ab4d53e3)) + ## [0.5.0](https://github.com/googleapis/python-video-stitcher/compare/v0.4.0...v0.5.0) (2022-12-15) diff --git a/google/cloud/video/stitcher/gapic_version.py b/google/cloud/video/stitcher/gapic_version.py index 371eb6b..f501366 100644 --- a/google/cloud/video/stitcher/gapic_version.py +++ b/google/cloud/video/stitcher/gapic_version.py @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # -__version__ = "0.5.0" # {x-release-please-version} +__version__ = "0.5.1" # {x-release-please-version} diff --git a/google/cloud/video/stitcher_v1/gapic_version.py b/google/cloud/video/stitcher_v1/gapic_version.py index 371eb6b..f501366 100644 --- a/google/cloud/video/stitcher_v1/gapic_version.py +++ b/google/cloud/video/stitcher_v1/gapic_version.py @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. # -__version__ = "0.5.0" # {x-release-please-version} +__version__ = "0.5.1" # {x-release-please-version} diff --git a/samples/generated_samples/snippet_metadata_google.cloud.video.stitcher.v1.json b/samples/generated_samples/snippet_metadata_google.cloud.video.stitcher.v1.json index 18fa2b5..9bbcbc0 100644 --- a/samples/generated_samples/snippet_metadata_google.cloud.video.stitcher.v1.json +++ b/samples/generated_samples/snippet_metadata_google.cloud.video.stitcher.v1.json @@ -8,7 +8,7 @@ ], "language": "PYTHON", "name": "google-cloud-video-stitcher", - "version": "0.5.0" + "version": "0.5.1" }, "snippets": [ { diff --git a/samples/snippets/cdn_key_test.py b/samples/snippets/cdn_key_test.py index 86de02b..7e25aeb 100644 --- a/samples/snippets/cdn_key_test.py +++ b/samples/snippets/cdn_key_test.py @@ -15,97 +15,135 @@ import os import uuid -from google.api_core.exceptions import NotFound +from google.protobuf import timestamp_pb2 import pytest import create_cdn_key +import create_cdn_key_akamai import delete_cdn_key import get_cdn_key import list_cdn_keys import update_cdn_key +import update_cdn_key_akamai +import utils location = "us-central1" project_id = os.environ["GOOGLE_CLOUD_PROJECT"] -gcdn_cdn_key_id = f"my-python-test-cdn-key-{uuid.uuid4()}" -akamai_cdn_key_id = f"my-python-test-cdn-key-{uuid.uuid4()}" +now = timestamp_pb2.Timestamp() +now.GetCurrentTime() + +media_cdn_key_id = f"python-test-media-key-{uuid.uuid4().hex[:5]}-{now.seconds}" +cloud_cdn_key_id = f"python-test-cloud-key-{uuid.uuid4().hex[:5]}-{now.seconds}" +akamai_cdn_key_id = f"python-test-akamai-key-{uuid.uuid4().hex[:5]}-{now.seconds}" hostname = "cdn.example.com" updated_hostname = "updated.example.com" +key_name = "my-key" -gcdn_key_name = "gcdn-key" -gcdn_private_key = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg==" -updated_gcdn_private_key = "VGhpcyBpcyBhbiB1cGRhdGVkIHRlc3Qgc3RyaW5nLg==" -akamai_key = gcdn_private_key -updated_akamai_key = updated_gcdn_private_key +media_cdn_private_key = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNA" +updated_media_cdn_private_key = ( + "ZZZzNDU2Nzg5MDEyMzQ1Njc4OTAxzg5MDEyMzQ1Njc4OTAxMjM0NTY3DkwMTIZZZ" +) +cloud_cdn_private_key = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg==" +updated_cloud_cdn_private_key = "VGhpcyBpcyBhbiB1cGRhdGVkIHRlc3Qgc3RyaW5nLg==" +akamai_key = cloud_cdn_private_key +updated_akamai_key = updated_cloud_cdn_private_key def test_cdn_key_operations(capsys: pytest.fixture) -> None: - try: - delete_cdn_key.delete_cdn_key(project_id, location, gcdn_cdn_key_id) - except NotFound as e: - print(f"Ignoring NotFound, details: {e}") - out, _ = capsys.readouterr() + utils.delete_stale_cdn_keys(project_id, location) - try: - delete_cdn_key.delete_cdn_key(project_id, location, akamai_cdn_key_id) - except NotFound as e: - print(f"Ignoring NotFound, details: {e}") - out, _ = capsys.readouterr() - - # GCDN CDN key tests + # Media CDN key tests create_cdn_key.create_cdn_key( project_id, location, - gcdn_cdn_key_id, + media_cdn_key_id, hostname, - gcdn_key_name, - gcdn_private_key, + key_name, + media_cdn_private_key, + False, ) out, _ = capsys.readouterr() - assert gcdn_cdn_key_id in out + assert media_cdn_key_id in out list_cdn_keys.list_cdn_keys(project_id, location) out, _ = capsys.readouterr() - assert gcdn_cdn_key_id in out + assert media_cdn_key_id in out - # Update the hostname only + # Update the hostname and private key; the private key value + # is not returned by the client response = update_cdn_key.update_cdn_key( - project_id, location, gcdn_cdn_key_id, updated_hostname + project_id, + location, + media_cdn_key_id, + updated_hostname, + key_name, + updated_media_cdn_private_key, + False, ) out, _ = capsys.readouterr() - assert gcdn_cdn_key_id in out + assert media_cdn_key_id in out assert updated_hostname in response.hostname - # Update the private key; the private key value is not returned by the client - response = update_cdn_key.update_cdn_key( + get_cdn_key.get_cdn_key(project_id, location, media_cdn_key_id) + out, _ = capsys.readouterr() + assert media_cdn_key_id in out + + delete_cdn_key.delete_cdn_key(project_id, location, media_cdn_key_id) + out, _ = capsys.readouterr() + assert "Deleted CDN key" in out + + # Cloud CDN key tests + + create_cdn_key.create_cdn_key( project_id, location, - gcdn_cdn_key_id, + cloud_cdn_key_id, hostname, - gcdn_key_name, - updated_gcdn_private_key, + key_name, + cloud_cdn_private_key, + True, + ) + out, _ = capsys.readouterr() + assert cloud_cdn_key_id in out + + list_cdn_keys.list_cdn_keys(project_id, location) + out, _ = capsys.readouterr() + assert cloud_cdn_key_id in out + + # Update the hostname and private key; the private key value + # is not returned by the client + response = update_cdn_key.update_cdn_key( + project_id, + location, + cloud_cdn_key_id, + updated_hostname, + key_name, + updated_cloud_cdn_private_key, + True, ) out, _ = capsys.readouterr() - assert gcdn_cdn_key_id in out + assert cloud_cdn_key_id in out + assert updated_hostname in response.hostname - get_cdn_key.get_cdn_key(project_id, location, gcdn_cdn_key_id) + get_cdn_key.get_cdn_key(project_id, location, cloud_cdn_key_id) out, _ = capsys.readouterr() - assert gcdn_cdn_key_id in out + assert cloud_cdn_key_id in out - delete_cdn_key.delete_cdn_key(project_id, location, gcdn_cdn_key_id) + delete_cdn_key.delete_cdn_key(project_id, location, cloud_cdn_key_id) out, _ = capsys.readouterr() assert "Deleted CDN key" in out # Akamai CDN key tests - create_cdn_key.create_cdn_key( + create_cdn_key_akamai.create_cdn_key_akamai( project_id, location, akamai_cdn_key_id, hostname, - akamai_token_key=akamai_key, + akamai_key, ) out, _ = capsys.readouterr() assert akamai_cdn_key_id in out @@ -114,24 +152,18 @@ def test_cdn_key_operations(capsys: pytest.fixture) -> None: out, _ = capsys.readouterr() assert akamai_cdn_key_id in out - # Update the hostname only - response = update_cdn_key.update_cdn_key( - project_id, location, akamai_cdn_key_id, updated_hostname - ) - out, _ = capsys.readouterr() - assert akamai_cdn_key_id in out - assert updated_hostname in response.hostname - - # Update the private key; the private key value is not returned by the client - response = update_cdn_key.update_cdn_key( + # Update the hostname and private key; the private key value + # is not returned by the client + response = update_cdn_key_akamai.update_cdn_key_akamai( project_id, location, akamai_cdn_key_id, - hostname, - akamai_token_key=updated_akamai_key, + updated_hostname, + updated_akamai_key, ) out, _ = capsys.readouterr() assert akamai_cdn_key_id in out + assert updated_hostname in response.hostname get_cdn_key.get_cdn_key(project_id, location, akamai_cdn_key_id) out, _ = capsys.readouterr() diff --git a/samples/snippets/create_cdn_key.py b/samples/snippets/create_cdn_key.py index d764253..9d3c449 100644 --- a/samples/snippets/create_cdn_key.py +++ b/samples/snippets/create_cdn_key.py @@ -14,11 +14,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Google Cloud Video Stitcher sample for creating a CDN key. A CDN key is used -to retrieve protected media. +"""Google Cloud Video Stitcher sample for creating a Media CDN key +or a Cloud CDN key. A CDN key is used to retrieve protected media. Example usage: - python create_cdn_key.py --project_id --location --cdn_key_id \ - --hostname [--gcdn_keyname --gcdn_private_key | --akamai_token_key ] + python create_cdn_key.py --project_id --location \ + --cdn_key_id --hostname \ + --key_name --private_key [--is_cloud_cdn] """ # [START videostitcher_create_cdn_key] @@ -36,19 +37,23 @@ def create_cdn_key( location: str, cdn_key_id: str, hostname: str, - gcdn_keyname: str = None, - gcdn_private_key: str = None, - akamai_token_key: str = None, + key_name: str, + private_key: str, + is_cloud_cdn: bool, ) -> str: - """Creates a Google Cloud or Akamai CDN key. + """Creates a Cloud CDN or Media CDN key. Args: project_id: The GCP project ID. location: The location in which to create the CDN key. cdn_key_id: The user-defined CDN key ID. hostname: The hostname to which this CDN key applies. - gcdn_keyname: Applies to a Google Cloud CDN key. A base64-encoded string secret. - gcdn_private_key: Applies to a Google Cloud CDN key. Public name of the key. - akamai_token_key: Applies to an Akamai CDN key. A base64-encoded string token key.""" + key_name: For a Media CDN key, this is the keyset name. + For a Cloud CDN key, this is the public name of the CDN key. + private_key: For a Media CDN key, this is a 64-byte Ed25519 private + key encoded as a base64-encoded string. + See https://cloud.google.com/video-stitcher/docs/how-to/managing-cdn-keys#create-private-key-media-cdn + for more information. For a Cloud CDN key, this is a base64-encoded string secret. + is_cloud_cdn: If true, create a Cloud CDN key. If false, create a Media CDN key.""" client = VideoStitcherServiceClient() @@ -59,14 +64,15 @@ def create_cdn_key( hostname=hostname, ) - if akamai_token_key is not None: - cdn_key.akamai_cdn_key = stitcher_v1.types.AkamaiCdnKey( - token_key=akamai_token_key, - ) - elif gcdn_keyname is not None: + if is_cloud_cdn: cdn_key.google_cdn_key = stitcher_v1.types.GoogleCdnKey( - key_name=gcdn_keyname, - private_key=gcdn_private_key, + key_name=key_name, + private_key=private_key, + ) + else: + cdn_key.media_cdn_key = stitcher_v1.types.MediaCdnKey( + key_name=key_name, + private_key=private_key, ) response = client.create_cdn_key( @@ -97,16 +103,24 @@ def create_cdn_key( required=True, ) parser.add_argument( - "--gcdn_keyname", - help="Applies to a Google Cloud CDN key. The base64-encoded string secret.", + "--key_name", + help="For a Media CDN key, this is the keyset name. For a Cloud CDN" + + " key, this is the public name of the CDN key.", + required=True, ) parser.add_argument( - "--gcdn_private_key", - help="Applies to a Google Cloud CDN key. Public name of the key.", + "--private_key", + help="For a Media CDN key, this is a 64-byte Ed25519 private key" + + "encoded as a base64-encoded string. See" + + " https://cloud.google.com/video-stitcher/docs/how-to/managing-cdn-keys#create-private-key-media-cdn" + + " for more information. For a Cloud CDN key, this is a" + + " base64-encoded string secret.", + required=True, ) parser.add_argument( - "--akamai_token_key", - help="Applies to an Akamai CDN key. The base64-encoded string token key.", + "--is_cloud_cdn", + action="store_true", + help="If included, create a Cloud CDN key. If absent, create a Media CDN key.", ) args = parser.parse_args() create_cdn_key( @@ -114,7 +128,7 @@ def create_cdn_key( args.location, args.cdn_key_id, args.hostname, - args.gcdn_keyname, - args.gcdn_private_key, - args.akamai_token_key, + args.key_name, + args.private_key, + args.is_cloud_cdn, ) diff --git a/samples/snippets/create_cdn_key_akamai.py b/samples/snippets/create_cdn_key_akamai.py new file mode 100644 index 0000000..d71141e --- /dev/null +++ b/samples/snippets/create_cdn_key_akamai.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python + +# Copyright 2022 Google Inc. All Rights Reserved. +# +# 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 +# limitations under the License. + +"""Google Cloud Video Stitcher sample for creating an Akamai CDN key. A CDN key is used +to retrieve protected media. +Example usage: + python create_cdn_key_akamai.py --project_id --location --cdn_key_id \ + --hostname --akamai_token_key +""" + +# [START videostitcher_create_cdn_key_akamai] + +import argparse + +from google.cloud.video import stitcher_v1 +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) + + +def create_cdn_key_akamai( + project_id: str, + location: str, + cdn_key_id: str, + hostname: str, + akamai_token_key: str, +) -> str: + """Creates an Akamai CDN key. + Args: + project_id: The GCP project ID. + location: The location in which to create the CDN key. + cdn_key_id: The user-defined CDN key ID. + hostname: The hostname to which this CDN key applies. + akamai_token_key: A base64-encoded string token key.""" + + client = VideoStitcherServiceClient() + + parent = f"projects/{project_id}/locations/{location}" + + cdn_key = stitcher_v1.types.CdnKey( + name=cdn_key_id, + hostname=hostname, + akamai_cdn_key=stitcher_v1.types.AkamaiCdnKey( + token_key=akamai_token_key, + ), + ) + + response = client.create_cdn_key( + parent=parent, cdn_key_id=cdn_key_id, cdn_key=cdn_key + ) + print(f"CDN key: {response.name}") + return response + + +# [END videostitcher_create_cdn_key_akamai] + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--project_id", help="Your Cloud project ID.", required=True) + parser.add_argument( + "--location", + help="The location in which to create the CDN key.", + default="us-central1", + ) + parser.add_argument( + "--cdn_key_id", + help="The user-defined CDN key ID.", + required=True, + ) + parser.add_argument( + "--hostname", + help="The hostname to which this CDN key applies.", + required=True, + ) + parser.add_argument( + "--akamai_token_key", + help="The base64-encoded string token key.", + required=True, + ) + args = parser.parse_args() + create_cdn_key_akamai( + args.project_id, + args.location, + args.cdn_key_id, + args.hostname, + args.akamai_token_key, + ) diff --git a/samples/snippets/requirements.txt b/samples/snippets/requirements.txt index 864f169..5f11145 100644 --- a/samples/snippets/requirements.txt +++ b/samples/snippets/requirements.txt @@ -1,3 +1,3 @@ -google-api-python-client==2.70.0 +google-api-python-client==2.71.0 grpcio==1.51.1 -google-cloud-video-stitcher==0.4.0 \ No newline at end of file +google-cloud-video-stitcher==0.5.0 \ No newline at end of file diff --git a/samples/snippets/slate_test.py b/samples/snippets/slate_test.py index 86079c5..6c186ae 100644 --- a/samples/snippets/slate_test.py +++ b/samples/snippets/slate_test.py @@ -15,7 +15,7 @@ import os import uuid -from google.api_core.exceptions import NotFound +from google.protobuf import timestamp_pb2 import pytest import create_slate @@ -23,11 +23,15 @@ import get_slate import list_slates import update_slate +import utils location = "us-west1" project_id = os.environ["GOOGLE_CLOUD_PROJECT"] project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"] -slate_id = f"my-python-test-slate-{uuid.uuid4()}" +now = timestamp_pb2.Timestamp() +now.GetCurrentTime() + +slate_id = f"python-test-slate-{uuid.uuid4().hex[:5]}-{now.seconds}" input_bucket_name = "cloud-samples-data/media/" slate_video_file_name = "ForBiggerEscapes.mp4" @@ -41,6 +45,8 @@ def test_slate_operations(capsys: pytest.fixture) -> None: + utils.delete_stale_slates(project_id, location) + slate_name_project_number = ( f"projects/{project_number}/locations/{location}/slates/{slate_id}" ) @@ -48,12 +54,6 @@ def test_slate_operations(capsys: pytest.fixture) -> None: f"projects/{project_id}/locations/{location}/slates/{slate_id}" ) - try: - delete_slate.delete_slate(project_id, location, slate_id) - except NotFound as e: - print(f"Ignoring NotFound, details: {e}") - out, _ = capsys.readouterr() - create_slate.create_slate(project_id, location, slate_id, slate_uri) out, _ = capsys.readouterr() assert slate_name_project_number in out diff --git a/samples/snippets/update_cdn_key.py b/samples/snippets/update_cdn_key.py index 5c06e05..14144db 100644 --- a/samples/snippets/update_cdn_key.py +++ b/samples/snippets/update_cdn_key.py @@ -14,11 +14,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""Google Cloud Video Stitcher sample for updating a CDN key. +"""Google Cloud Video Stitcher sample for updating a Media CDN or +Cloud CDN key. Example usage: python update_cdn_key.py --project_id --location \ --cdn_key_id --hostname \ - [--gcdn_keyname --gcdn_private_key | --akamai_token_key ] + --key_name --private_key [--is_cloud_cdn] """ # [START videostitcher_update_cdn_key] @@ -37,19 +38,23 @@ def update_cdn_key( location: str, cdn_key_id: str, hostname: str, - gcdn_keyname: str = None, - gcdn_private_key: str = None, - akamai_token_key: str = None, + key_name: str, + private_key: str, + is_cloud_cdn: bool, ) -> str: - """Updates a Google Cloud or Akamai CDN key. + """Updates a Media CDN or Cloud CDN key. Args: project_id: The GCP project ID. location: The location of the CDN key. cdn_key_id: The user-defined CDN key ID. hostname: The hostname to which this CDN key applies. - gcdn_keyname: Applies to a Google Cloud CDN key. A base64-encoded string secret. - gcdn_private_key: Applies to a Google Cloud CDN key. Public name of the key. - akamai_token_key: Applies to an Akamai CDN key. A base64-encoded string token key.""" + key_name: For a Media CDN key, this is the keyset name. + For a Cloud CDN key, this is the public name of the CDN key. + private_key: For a Media CDN key, this is a 64-byte Ed25519 private + key encoded as a base64-encoded string. + See https://cloud.google.com/video-stitcher/docs/how-to/managing-cdn-keys#create-private-key-media-cdn + for more information. For a Cloud CDN key, this is a base64-encoded string secret. + is_cloud_cdn: If true, update a Cloud CDN key. If false, update a Media CDN key.""" client = VideoStitcherServiceClient() @@ -60,19 +65,18 @@ def update_cdn_key( hostname=hostname, ) - if akamai_token_key is not None: - cdn_key.akamai_cdn_key = stitcher_v1.types.AkamaiCdnKey( - token_key=akamai_token_key, - ) - update_mask = field_mask.FieldMask(paths=["hostname", "akamai_cdn_key"]) - elif gcdn_keyname is not None: + if is_cloud_cdn: cdn_key.google_cdn_key = stitcher_v1.types.GoogleCdnKey( - key_name=gcdn_keyname, - private_key=gcdn_private_key, + key_name=key_name, + private_key=private_key, ) update_mask = field_mask.FieldMask(paths=["hostname", "google_cdn_key"]) else: - update_mask = field_mask.FieldMask(paths=["hostname"]) + cdn_key.media_cdn_key = stitcher_v1.types.MediaCdnKey( + key_name=key_name, + private_key=private_key, + ) + update_mask = field_mask.FieldMask(paths=["hostname", "media_cdn_key"]) response = client.update_cdn_key(cdn_key=cdn_key, update_mask=update_mask) print(f"Updated CDN key: {response.name}") @@ -87,6 +91,7 @@ def update_cdn_key( parser.add_argument( "--location", help="The location of the CDN key.", + required=True, ) parser.add_argument( "--cdn_key_id", @@ -99,24 +104,33 @@ def update_cdn_key( required=True, ) parser.add_argument( - "--gcdn_keyname", - help="Applies to a Google Cloud CDN key. The base64-encoded string secret.", + "--key_name", + help="For a Media CDN key, this is the keyset name. For a Cloud CDN" + + " key, this is the public name of the CDN key.", + required=True, ) parser.add_argument( - "--gcdn_private_key", - help="Applies to a Google Cloud CDN key. Public name of the key.", + "--private_key", + help="For a Media CDN key, this is a 64-byte Ed25519 private key" + + "encoded as a base64-encoded string. See" + + " https://cloud.google.com/video-stitcher/docs/how-to/managing-cdn-keys#create-private-key-media-cdn" + + " for more information. For a Cloud CDN key, this is a" + + " base64-encoded string secret.", + required=True, ) parser.add_argument( - "--akamai_token_key", - help="Applies to an Akamai CDN key. The base64-encoded string token key.", + "--is_cloud_cdn", + action="store_true", + help="If included, create a Cloud CDN key. If absent, create a Media CDN key.", ) + args = parser.parse_args() update_cdn_key( args.project_id, args.location, args.cdn_key_id, args.hostname, - args.gcdn_keyname, - args.gcdn_private_key, - args.akamai_token_key, + args.key_name, + args.private_key, + args.is_cloud_cdn, ) diff --git a/samples/snippets/update_cdn_key_akamai.py b/samples/snippets/update_cdn_key_akamai.py new file mode 100644 index 0000000..0c5ca09 --- /dev/null +++ b/samples/snippets/update_cdn_key_akamai.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python + +# Copyright 2022 Google Inc. All Rights Reserved. +# +# 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 +# limitations under the License. + +"""Google Cloud Video Stitcher sample for updating an Akamai CDN key. +Example usage: + python update_cdn_key_akamai.py --project_id --location \ + --cdn_key_id --hostname \ + --akamai_token_key +""" + +# [START videostitcher_update_cdn_key_akamai] + +import argparse + +from google.cloud.video import stitcher_v1 +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) +from google.protobuf import field_mask_pb2 as field_mask + + +def update_cdn_key_akamai( + project_id: str, + location: str, + cdn_key_id: str, + hostname: str, + akamai_token_key: str, +) -> str: + """Updates an Akamai CDN key. + Args: + project_id: The GCP project ID. + location: The location of the CDN key. + cdn_key_id: The user-defined CDN key ID. + hostname: The hostname to which this CDN key applies. + akamai_token_key: A base64-encoded string token key.""" + + client = VideoStitcherServiceClient() + + name = f"projects/{project_id}/locations/{location}/cdnKeys/{cdn_key_id}" + + cdn_key = stitcher_v1.types.CdnKey( + name=name, + hostname=hostname, + akamai_cdn_key=stitcher_v1.types.AkamaiCdnKey( + token_key=akamai_token_key, + ), + ) + update_mask = field_mask.FieldMask(paths=["hostname", "akamai_cdn_key"]) + + response = client.update_cdn_key(cdn_key=cdn_key, update_mask=update_mask) + print(f"Updated CDN key: {response.name}") + return response + + +# [END videostitcher_update_cdn_key_akamai] + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--project_id", help="Your Cloud project ID.", required=True) + parser.add_argument( + "--location", + help="The location of the CDN key.", + required=True, + ) + parser.add_argument( + "--cdn_key_id", + help="The user-defined CDN key ID.", + required=True, + ) + parser.add_argument( + "--hostname", + help="The hostname to which this CDN key applies.", + required=True, + ) + parser.add_argument( + "--akamai_token_key", + help="The base64-encoded string token key.", + required=True, + ) + args = parser.parse_args() + update_cdn_key_akamai( + args.project_id, + args.location, + args.cdn_key_id, + args.hostname, + args.akamai_token_key, + ) diff --git a/samples/snippets/utils.py b/samples/snippets/utils.py new file mode 100644 index 0000000..6cd5683 --- /dev/null +++ b/samples/snippets/utils.py @@ -0,0 +1,48 @@ +from google.cloud.video.stitcher_v1.services.video_stitcher_service import ( + VideoStitcherServiceClient, +) +from google.protobuf import timestamp_pb2 + +seconds_per_hour = 3600 + + +def delete_stale_slates(project_id: str, location: str) -> None: + """Lists all outdated slates in a location. + Args: + project_id: The GCP project ID. + location: The location of the slates.""" + + client = VideoStitcherServiceClient() + + parent = f"projects/{project_id}/locations/{location}" + response = client.list_slates(parent=parent) + + now = timestamp_pb2.Timestamp() + now.GetCurrentTime() + + for slate in response.slates: + tmp = slate.name.split("-") + creation_time_sec = int(tmp.pop()) + if (now.seconds - creation_time_sec) > (3 * seconds_per_hour): + response = client.delete_slate(name=slate.name) + + +def delete_stale_cdn_keys(project_id: str, location: str) -> None: + """Lists all outdated CDN keys in a location. + Args: + project_id: The GCP project ID. + location: The location of the CDN keys.""" + + client = VideoStitcherServiceClient() + + parent = f"projects/{project_id}/locations/{location}" + response = client.list_cdn_keys(parent=parent) + + now = timestamp_pb2.Timestamp() + now.GetCurrentTime() + + for cdn_key in response.cdn_keys: + tmp = cdn_key.name.split("-") + creation_time_sec = int(tmp.pop()) + if (now.seconds - creation_time_sec) > (3 * seconds_per_hour): + response = client.delete_cdn_key(name=cdn_key.name)