From 5649d1ce2f74b2949afa49edfe094422021fb255 Mon Sep 17 00:00:00 2001 From: Nicholas Cook Date: Fri, 11 Aug 2023 13:51:13 -0700 Subject: [PATCH] docs(samples): add samples and tests for pools and assets (#180) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs(samples): add samples and tests for pools and assets * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot Co-authored-by: Anthonios Partheniou --- .../samples/snippets/asset_test.py | 64 +++++++++++++ .../samples/snippets/channel_event_test.py | 30 ++----- .../samples/snippets/channel_test.py | 24 +++-- .../samples/snippets/create_asset.py | 86 ++++++++++++++++++ .../samples/snippets/create_channel.py | 2 +- .../samples/snippets/create_channel_event.py | 2 +- .../create_channel_with_backup_input.py | 2 +- .../samples/snippets/create_input.py | 4 +- .../samples/snippets/delete_asset.py | 69 ++++++++++++++ .../samples/snippets/delete_channel.py | 7 +- .../samples/snippets/delete_channel_event.py | 4 +- .../samples/snippets/delete_input.py | 7 +- .../samples/snippets/get_asset.py | 70 +++++++++++++++ .../samples/snippets/get_channel.py | 5 +- .../samples/snippets/get_channel_event.py | 3 +- .../samples/snippets/get_input.py | 5 +- .../samples/snippets/get_pool.py | 68 ++++++++++++++ .../samples/snippets/input_test.py | 19 ++-- .../samples/snippets/list_assets.py | 66 ++++++++++++++ .../samples/snippets/list_channel_events.py | 5 +- .../samples/snippets/list_channels.py | 3 +- .../samples/snippets/list_inputs.py | 3 +- .../samples/snippets/pool_test.py | 41 +++++++++ .../samples/snippets/start_channel.py | 9 +- .../samples/snippets/stop_channel.py | 9 +- .../samples/snippets/update_channel.py | 2 +- .../samples/snippets/update_input.py | 4 +- .../samples/snippets/update_pool.py | 90 +++++++++++++++++++ 28 files changed, 635 insertions(+), 68 deletions(-) create mode 100644 packages/google-cloud-video-live-stream/samples/snippets/asset_test.py create mode 100644 packages/google-cloud-video-live-stream/samples/snippets/create_asset.py create mode 100644 packages/google-cloud-video-live-stream/samples/snippets/delete_asset.py create mode 100644 packages/google-cloud-video-live-stream/samples/snippets/get_asset.py create mode 100644 packages/google-cloud-video-live-stream/samples/snippets/get_pool.py create mode 100644 packages/google-cloud-video-live-stream/samples/snippets/list_assets.py create mode 100644 packages/google-cloud-video-live-stream/samples/snippets/pool_test.py create mode 100644 packages/google-cloud-video-live-stream/samples/snippets/update_pool.py diff --git a/packages/google-cloud-video-live-stream/samples/snippets/asset_test.py b/packages/google-cloud-video-live-stream/samples/snippets/asset_test.py new file mode 100644 index 000000000000..df6d1d4881c3 --- /dev/null +++ b/packages/google-cloud-video-live-stream/samples/snippets/asset_test.py @@ -0,0 +1,64 @@ +# Copyright 2023 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. + +import os +import uuid + +from google.api_core.exceptions import FailedPrecondition, NotFound +from google.protobuf import empty_pb2 as empty +import pytest + +import create_asset +import delete_asset +import get_asset +import list_assets +import utils + +project_name = os.environ["GOOGLE_CLOUD_PROJECT"] +location = "us-central1" +asset_id = f"my-python-test-asset-{uuid.uuid4()}" +asset_uri = "gs://cloud-samples-data/media/ForBiggerEscapes.mp4" + + +def test_asset_operations(capsys: pytest.fixture) -> None: + # Clean up old resources in the test project + responses = list_assets.list_assets(project_name, location) + for response in responses: + next_asset_id = response.name.rsplit("/", 1)[-1] + if utils.is_resource_stale(response.create_time): + try: + delete_asset.delete_asset(project_name, location, next_asset_id) + except FailedPrecondition as e: + print(f"Ignoring FailedPrecondition, details: {e}") + except NotFound as e: + print(f"Ignoring NotFound, details: {e}") + + asset_name_project_id = ( + f"projects/{project_name}/locations/{location}/assets/{asset_id}" + ) + + # Tests + + response = create_asset.create_asset(project_name, location, asset_id, asset_uri) + assert asset_name_project_id in response.name + + list_assets.list_assets(project_name, location) + out, _ = capsys.readouterr() + assert asset_name_project_id in out + + response = get_asset.get_asset(project_name, location, asset_id) + assert asset_name_project_id in response.name + + response = delete_asset.delete_asset(project_name, location, asset_id) + assert response == empty.Empty() diff --git a/packages/google-cloud-video-live-stream/samples/snippets/channel_event_test.py b/packages/google-cloud-video-live-stream/samples/snippets/channel_event_test.py index c90868a612bc..bb44c9e39202 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/channel_event_test.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/channel_event_test.py @@ -41,9 +41,6 @@ def test_channel_event_operations(capsys: pytest.fixture) -> None: # Set up - channel_name_project_id = ( - f"projects/{project_name}/locations/{location}/channels/{channel_id}" - ) event_name_project_id = f"projects/{project_name}/locations/{location}/channels/{channel_id}/events/{event_id}" create_input.create_input(project_name, location, input_id) @@ -51,43 +48,32 @@ def test_channel_event_operations(capsys: pytest.fixture) -> None: create_channel.create_channel( project_name, location, channel_id, input_id, output_uri ) - out, _ = capsys.readouterr() - assert channel_name_project_id in out start_channel.start_channel(project_name, location, channel_id) - out, _ = capsys.readouterr() - assert "Started channel" in out # Tests - create_channel_event.create_channel_event( + response = create_channel_event.create_channel_event( project_name, location, channel_id, event_id ) - out, _ = capsys.readouterr() - assert event_name_project_id in out + assert event_name_project_id in response.name - get_channel_event.get_channel_event(project_name, location, channel_id, event_id) - out, _ = capsys.readouterr() - assert event_name_project_id in out + response = get_channel_event.get_channel_event( + project_name, location, channel_id, event_id + ) + assert event_name_project_id in response.name list_channel_events.list_channel_events(project_name, location, channel_id) out, _ = capsys.readouterr() assert event_name_project_id in out - delete_channel_event.delete_channel_event( + response = delete_channel_event.delete_channel_event( project_name, location, channel_id, event_id ) - out, _ = capsys.readouterr() - assert "Deleted channel event" in out + assert response is None # Clean up stop_channel.stop_channel(project_name, location, channel_id) - out, _ = capsys.readouterr() - assert "Stopped channel" in out - delete_channel.delete_channel(project_name, location, channel_id) - out, _ = capsys.readouterr() - assert "Deleted channel" in out - delete_input.delete_input(project_name, location, input_id) diff --git a/packages/google-cloud-video-live-stream/samples/snippets/channel_test.py b/packages/google-cloud-video-live-stream/samples/snippets/channel_test.py index 1b4ac7856b7c..99416ce9cf71 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/channel_test.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/channel_test.py @@ -16,6 +16,7 @@ import uuid from google.api_core.exceptions import FailedPrecondition, NotFound +from google.protobuf import empty_pb2 as empty import pytest import create_channel @@ -101,11 +102,10 @@ def test_channel_operations(capsys: pytest.fixture) -> None: # Tests - create_channel.create_channel( + response = create_channel.create_channel( project_name, location, channel_id, input_id, output_uri ) - out, _ = capsys.readouterr() - assert channel_name_project_id in out + assert channel_name_project_id in response.name list_channels.list_channels(project_name, location) out, _ = capsys.readouterr() @@ -114,14 +114,12 @@ def test_channel_operations(capsys: pytest.fixture) -> None: response = update_channel.update_channel( project_name, location, channel_id, updated_input_id ) - out, _ = capsys.readouterr() - assert channel_name_project_id in out + assert channel_name_project_id in response.name for input_attachment in response.input_attachments: assert "updated-input" in input_attachment.key - get_channel.get_channel(project_name, location, channel_id) - out, _ = capsys.readouterr() - assert channel_name_project_id in out + response = get_channel.get_channel(project_name, location, channel_id) + assert channel_name_project_id in response.name start_channel.start_channel(project_name, location, channel_id) out, _ = capsys.readouterr() @@ -131,15 +129,13 @@ def test_channel_operations(capsys: pytest.fixture) -> None: out, _ = capsys.readouterr() assert "Stopped channel" in out - delete_channel.delete_channel(project_name, location, channel_id) - out, _ = capsys.readouterr() - assert "Deleted channel" in out + response = delete_channel.delete_channel(project_name, location, channel_id) + assert response == empty.Empty() - create_channel_with_backup_input.create_channel_with_backup_input( + response = create_channel_with_backup_input.create_channel_with_backup_input( project_name, location, channel_id, input_id, updated_input_id, output_uri ) - out, _ = capsys.readouterr() - assert channel_name_project_id in out + assert channel_name_project_id in response.name # Clean up diff --git a/packages/google-cloud-video-live-stream/samples/snippets/create_asset.py b/packages/google-cloud-video-live-stream/samples/snippets/create_asset.py new file mode 100644 index 000000000000..b76e1f52c307 --- /dev/null +++ b/packages/google-cloud-video-live-stream/samples/snippets/create_asset.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python + +# Copyright 2023 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 Live Stream sample for creating an asset. You use an + asset to create a slate. +Example usage: + python create_asset.py --project_id --location \ + --asset_id --asset_uri +""" + +# [START livestream_create_asset] + +import argparse + +from google.cloud.video import live_stream_v1 +from google.cloud.video.live_stream_v1.services.livestream_service import ( + LivestreamServiceClient, +) + + +def create_asset( + project_id: str, location: str, asset_id: str, asset_uri: str +) -> live_stream_v1.types.Asset: + """Creates an asset. + Args: + project_id: The GCP project ID. + location: The location in which to create the asset. + asset_id: The user-defined asset ID. + asset_uri: The asset URI (e.g., 'gs://my-bucket/my-video.mp4').""" + + client = LivestreamServiceClient() + + parent = f"projects/{project_id}/locations/{location}" + + asset = live_stream_v1.types.Asset( + video=live_stream_v1.types.Asset.VideoAsset( + uri=asset_uri, + ) + ) + operation = client.create_asset(parent=parent, asset=asset, asset_id=asset_id) + response = operation.result(600) + print(f"Asset: {response.name}") + + return response + + +# [END livestream_create_asset] + +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 asset.", + default="us-central1", + ) + parser.add_argument( + "--asset_id", + help="The user-defined asset ID.", + required=True, + ) + parser.add_argument( + "--asset_uri", + help="The asset URI (e.g., 'gs://my-bucket/my-video.mp4').", + required=True, + ) + args = parser.parse_args() + create_asset( + args.project_id, + args.location, + args.asset_id, + args.asset_uri, + ) diff --git a/packages/google-cloud-video-live-stream/samples/snippets/create_channel.py b/packages/google-cloud-video-live-stream/samples/snippets/create_channel.py index 5bb2326ebfb9..41ccab51407b 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/create_channel.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/create_channel.py @@ -34,7 +34,7 @@ def create_channel( project_id: str, location: str, channel_id: str, input_id: str, output_uri: str -) -> str: +) -> live_stream_v1.types.Channel: """Creates a channel. Args: project_id: The GCP project ID. diff --git a/packages/google-cloud-video-live-stream/samples/snippets/create_channel_event.py b/packages/google-cloud-video-live-stream/samples/snippets/create_channel_event.py index 1096e1d5bccd..c4ac5c1a3bd7 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/create_channel_event.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/create_channel_event.py @@ -35,7 +35,7 @@ def create_channel_event( project_id: str, location: str, channel_id: str, event_id: str -) -> str: +) -> live_stream_v1.types.Event: """Creates a channel event. Args: project_id: The GCP project ID. diff --git a/packages/google-cloud-video-live-stream/samples/snippets/create_channel_with_backup_input.py b/packages/google-cloud-video-live-stream/samples/snippets/create_channel_with_backup_input.py index 5e49b7a88843..0fda7a75f12c 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/create_channel_with_backup_input.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/create_channel_with_backup_input.py @@ -42,7 +42,7 @@ def create_channel_with_backup_input( primary_input_id: str, backup_input_id: str, output_uri: str, -) -> str: +) -> live_stream_v1.types.Channel: """Creates a channel. Args: project_id: The GCP project ID. diff --git a/packages/google-cloud-video-live-stream/samples/snippets/create_input.py b/packages/google-cloud-video-live-stream/samples/snippets/create_input.py index 45f5a508e156..4481ce52c0c6 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/create_input.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/create_input.py @@ -30,7 +30,9 @@ ) -def create_input(project_id: str, location: str, input_id: str) -> str: +def create_input( + project_id: str, location: str, input_id: str +) -> live_stream_v1.types.Input: """Creates an input. Args: project_id: The GCP project ID. diff --git a/packages/google-cloud-video-live-stream/samples/snippets/delete_asset.py b/packages/google-cloud-video-live-stream/samples/snippets/delete_asset.py new file mode 100644 index 000000000000..c5c9c19b5990 --- /dev/null +++ b/packages/google-cloud-video-live-stream/samples/snippets/delete_asset.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python + +# Copyright 2023 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 Live Stream sample for deleting an asset. +Example usage: + python delete_asset.py --project_id --location --asset_id +""" + +# [START livestream_delete_asset] + +import argparse + +from google.cloud.video.live_stream_v1.services.livestream_service import ( + LivestreamServiceClient, +) +from google.protobuf import empty_pb2 as empty + + +def delete_asset(project_id: str, location: str, asset_id: str) -> empty.Empty: + """Deletes an asset. + Args: + project_id: The GCP project ID. + location: The location of the asset. + asset_id: The user-defined asset ID.""" + + client = LivestreamServiceClient() + + name = f"projects/{project_id}/locations/{location}/assets/{asset_id}" + operation = client.delete_asset(name=name) + response = operation.result(600) + print("Deleted asset") + + return response + + +# [END livestream_delete_asset] + +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 asset.", + required=True, + ) + parser.add_argument( + "--asset_id", + help="The user-defined asset ID.", + required=True, + ) + args = parser.parse_args() + delete_asset( + args.project_id, + args.location, + args.asset_id, + ) diff --git a/packages/google-cloud-video-live-stream/samples/snippets/delete_channel.py b/packages/google-cloud-video-live-stream/samples/snippets/delete_channel.py index b7bd485358c6..a7ae03f6e509 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/delete_channel.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/delete_channel.py @@ -26,9 +26,10 @@ from google.cloud.video.live_stream_v1.services.livestream_service import ( LivestreamServiceClient, ) +from google.protobuf import empty_pb2 as empty -def delete_channel(project_id: str, location: str, channel_id: str) -> None: +def delete_channel(project_id: str, location: str, channel_id: str) -> empty.Empty: """Deletes a channel. Args: project_id: The GCP project ID. @@ -39,9 +40,11 @@ def delete_channel(project_id: str, location: str, channel_id: str) -> None: name = f"projects/{project_id}/locations/{location}/channels/{channel_id}" operation = client.delete_channel(name=name) - operation.result(600) + response = operation.result(600) print("Deleted channel") + return response + # [END livestream_delete_channel] diff --git a/packages/google-cloud-video-live-stream/samples/snippets/delete_channel_event.py b/packages/google-cloud-video-live-stream/samples/snippets/delete_channel_event.py index 649f65c031fa..040f712b7d7f 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/delete_channel_event.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/delete_channel_event.py @@ -42,9 +42,11 @@ def delete_channel_event( client = LivestreamServiceClient() name = f"projects/{project_id}/locations/{location}/channels/{channel_id}/events/{event_id}" - client.delete_event(name=name) + response = client.delete_event(name=name) print("Deleted channel event") + return response + # [END livestream_delete_channel_event] diff --git a/packages/google-cloud-video-live-stream/samples/snippets/delete_input.py b/packages/google-cloud-video-live-stream/samples/snippets/delete_input.py index fe74a6438d83..0278082afe4d 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/delete_input.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/delete_input.py @@ -26,9 +26,10 @@ from google.cloud.video.live_stream_v1.services.livestream_service import ( LivestreamServiceClient, ) +from google.protobuf import empty_pb2 as empty -def delete_input(project_id: str, location: str, input_id: str) -> None: +def delete_input(project_id: str, location: str, input_id: str) -> empty.Empty: """Deletes an input. Args: project_id: The GCP project ID. @@ -39,9 +40,11 @@ def delete_input(project_id: str, location: str, input_id: str) -> None: name = f"projects/{project_id}/locations/{location}/inputs/{input_id}" operation = client.delete_input(name=name) - operation.result(600) + response = operation.result(600) print("Deleted input") + return response + # [END livestream_delete_input] diff --git a/packages/google-cloud-video-live-stream/samples/snippets/get_asset.py b/packages/google-cloud-video-live-stream/samples/snippets/get_asset.py new file mode 100644 index 000000000000..66d9b25d367b --- /dev/null +++ b/packages/google-cloud-video-live-stream/samples/snippets/get_asset.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python + +# Copyright 2023 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 Live Stream sample for getting an asset. +Example usage: + python get_asset.py --project_id --location --asset_id +""" + +# [START livestream_get_asset] + +import argparse + +from google.cloud.video import live_stream_v1 +from google.cloud.video.live_stream_v1.services.livestream_service import ( + LivestreamServiceClient, +) + + +def get_asset( + project_id: str, location: str, asset_id: str +) -> live_stream_v1.types.Asset: + """Gets an asset. + Args: + project_id: The GCP project ID. + location: The location of the asset. + asset_id: The user-defined asset ID.""" + + client = LivestreamServiceClient() + + name = f"projects/{project_id}/locations/{location}/assets/{asset_id}" + response = client.get_asset(name=name) + print(f"Asset: {response.name}") + + return response + + +# [END livestream_get_asset] + +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 asset.", + required=True, + ) + parser.add_argument( + "--asset_id", + help="The user-defined asset ID.", + required=True, + ) + args = parser.parse_args() + get_asset( + args.project_id, + args.location, + args.asset_id, + ) diff --git a/packages/google-cloud-video-live-stream/samples/snippets/get_channel.py b/packages/google-cloud-video-live-stream/samples/snippets/get_channel.py index 47df66d10772..bf9a39f650b9 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/get_channel.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/get_channel.py @@ -23,12 +23,15 @@ import argparse +from google.cloud.video import live_stream_v1 from google.cloud.video.live_stream_v1.services.livestream_service import ( LivestreamServiceClient, ) -def get_channel(project_id: str, location: str, channel_id: str) -> str: +def get_channel( + project_id: str, location: str, channel_id: str +) -> live_stream_v1.types.Channel: """Gets a channel. Args: project_id: The GCP project ID. diff --git a/packages/google-cloud-video-live-stream/samples/snippets/get_channel_event.py b/packages/google-cloud-video-live-stream/samples/snippets/get_channel_event.py index 9a70563e58c4..59adc03ebcae 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/get_channel_event.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/get_channel_event.py @@ -24,6 +24,7 @@ import argparse +from google.cloud.video import live_stream_v1 from google.cloud.video.live_stream_v1.services.livestream_service import ( LivestreamServiceClient, ) @@ -31,7 +32,7 @@ def get_channel_event( project_id: str, location: str, channel_id: str, event_id: str -) -> str: +) -> live_stream_v1.types.Event: """Gets a channel. Args: project_id: The GCP project ID. diff --git a/packages/google-cloud-video-live-stream/samples/snippets/get_input.py b/packages/google-cloud-video-live-stream/samples/snippets/get_input.py index d840cb4e79be..21cdbbbdd56d 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/get_input.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/get_input.py @@ -23,12 +23,15 @@ import argparse +from google.cloud.video import live_stream_v1 from google.cloud.video.live_stream_v1.services.livestream_service import ( LivestreamServiceClient, ) -def get_input(project_id: str, location: str, input_id: str) -> str: +def get_input( + project_id: str, location: str, input_id: str +) -> live_stream_v1.types.Input: """Gets an input. Args: project_id: The GCP project ID. diff --git a/packages/google-cloud-video-live-stream/samples/snippets/get_pool.py b/packages/google-cloud-video-live-stream/samples/snippets/get_pool.py new file mode 100644 index 000000000000..db56c1181759 --- /dev/null +++ b/packages/google-cloud-video-live-stream/samples/snippets/get_pool.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python + +# Copyright 2023 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 Live Stream sample for getting a pool. +Example usage: + python get_pool.py --project_id --location --pool_id +""" + +# [START livestream_get_pool] + +import argparse + +from google.cloud.video import live_stream_v1 +from google.cloud.video.live_stream_v1.services.livestream_service import ( + LivestreamServiceClient, +) + + +def get_pool(project_id: str, location: str, pool_id: str) -> live_stream_v1.types.Pool: + """Gets a pool. + Args: + project_id: The GCP project ID. + location: The location of the pool. + pool_id: The user-defined pool ID.""" + + client = LivestreamServiceClient() + + name = f"projects/{project_id}/locations/{location}/pools/{pool_id}" + response = client.get_pool(name=name) + print(f"Pool: {response.name}") + + return response + + +# [END livestream_get_pool] + +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 pool.", + required=True, + ) + parser.add_argument( + "--pool_id", + help="The user-defined pool ID.", + required=True, + ) + args = parser.parse_args() + get_pool( + args.project_id, + args.location, + args.pool_id, + ) diff --git a/packages/google-cloud-video-live-stream/samples/snippets/input_test.py b/packages/google-cloud-video-live-stream/samples/snippets/input_test.py index a0224b4f4d56..63f7b02ec836 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/input_test.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/input_test.py @@ -16,6 +16,7 @@ import uuid from google.api_core.exceptions import FailedPrecondition, NotFound +from google.protobuf import empty_pb2 as empty import pytest import create_input @@ -49,23 +50,19 @@ def test_input_operations(capsys: pytest.fixture) -> None: # Tests - create_input.create_input(project_name, location, input_id) - out, _ = capsys.readouterr() - assert input_name_project_id in out + response = create_input.create_input(project_name, location, input_id) + assert input_name_project_id in response.name list_inputs.list_inputs(project_name, location) out, _ = capsys.readouterr() assert input_name_project_id in out response = update_input.update_input(project_name, location, input_id) - out, _ = capsys.readouterr() - assert input_name_project_id in out + assert input_name_project_id in response.name assert response.preprocessing_config.crop.top_pixels == 5 - get_input.get_input(project_name, location, input_id) - out, _ = capsys.readouterr() - assert input_name_project_id in out + response = get_input.get_input(project_name, location, input_id) + assert input_name_project_id in response.name - delete_input.delete_input(project_name, location, input_id) - out, _ = capsys.readouterr() - assert "Deleted input" in out + response = delete_input.delete_input(project_name, location, input_id) + assert response == empty.Empty() diff --git a/packages/google-cloud-video-live-stream/samples/snippets/list_assets.py b/packages/google-cloud-video-live-stream/samples/snippets/list_assets.py new file mode 100644 index 000000000000..7136cab5822c --- /dev/null +++ b/packages/google-cloud-video-live-stream/samples/snippets/list_assets.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python + +# Copyright 2023 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 Live Stream sample for listing all assets in a location. +Example usage: + python list_assets.py --project_id --location +""" + +# [START livestream_list_assets] + +import argparse + +from google.cloud.video.live_stream_v1.services.livestream_service import ( + LivestreamServiceClient, + pagers, +) + + +def list_assets(project_id: str, location: str) -> pagers.ListAssetsPager: + """Lists all assets in a location. + Args: + project_id: The GCP project ID. + location: The location of the assets.""" + + client = LivestreamServiceClient() + + parent = f"projects/{project_id}/locations/{location}" + page_result = client.list_assets(parent=parent) + print("Assets:") + + responses = [] + for response in page_result: + print(response.name) + responses.append(response) + + return responses + + +# [END livestream_list_assets] + +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 assets.", + required=True, + ) + args = parser.parse_args() + list_assets( + args.project_id, + args.location, + ) diff --git a/packages/google-cloud-video-live-stream/samples/snippets/list_channel_events.py b/packages/google-cloud-video-live-stream/samples/snippets/list_channel_events.py index 54d60b9dbe5c..ab3cfc961dcc 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/list_channel_events.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/list_channel_events.py @@ -25,10 +25,13 @@ from google.cloud.video.live_stream_v1.services.livestream_service import ( LivestreamServiceClient, + pagers, ) -def list_channel_events(project_id: str, location: str, channel_id: str) -> list: +def list_channel_events( + project_id: str, location: str, channel_id: str +) -> pagers.ListEventsPager: """Lists all events for a channel. Args: project_id: The GCP project ID. diff --git a/packages/google-cloud-video-live-stream/samples/snippets/list_channels.py b/packages/google-cloud-video-live-stream/samples/snippets/list_channels.py index 009f5676d6c5..731c04eda181 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/list_channels.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/list_channels.py @@ -25,10 +25,11 @@ from google.cloud.video.live_stream_v1.services.livestream_service import ( LivestreamServiceClient, + pagers, ) -def list_channels(project_id: str, location: str) -> list: +def list_channels(project_id: str, location: str) -> pagers.ListChannelsPager: """Lists all channels in a location. Args: project_id: The GCP project ID. diff --git a/packages/google-cloud-video-live-stream/samples/snippets/list_inputs.py b/packages/google-cloud-video-live-stream/samples/snippets/list_inputs.py index d04e3ba21527..e273d7eccc60 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/list_inputs.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/list_inputs.py @@ -25,10 +25,11 @@ from google.cloud.video.live_stream_v1.services.livestream_service import ( LivestreamServiceClient, + pagers, ) -def list_inputs(project_id: str, location: str) -> list: +def list_inputs(project_id: str, location: str) -> pagers.ListInputsPager: """Lists all inputs in a location. Args: project_id: The GCP project ID. diff --git a/packages/google-cloud-video-live-stream/samples/snippets/pool_test.py b/packages/google-cloud-video-live-stream/samples/snippets/pool_test.py new file mode 100644 index 000000000000..315c327f0201 --- /dev/null +++ b/packages/google-cloud-video-live-stream/samples/snippets/pool_test.py @@ -0,0 +1,41 @@ +# Copyright 2023 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. + +import os + +import pytest + +import get_pool + +# import update_pool + +project_name = os.environ["GOOGLE_CLOUD_PROJECT"] +location = "us-central1" +pool_id = "default" # only 1 pool supported per location +peered_network = "" + + +def test_pool_operations(capsys: pytest.fixture) -> None: + pool_name_project_id = ( + f"projects/{project_name}/locations/{location}/pools/{pool_id}" + ) + + # All channels must be stopped to update the pool. Pool operations take a + # long time to complete, so don't run this test on the test network. + # response = update_pool.update_pool(project_name, location, pool_id, peered_network) + # assert pool_name_project_id in response.name + # assert response.network_config.peered_network == peered_network + + response = get_pool.get_pool(project_name, location, pool_id) + assert pool_name_project_id in response.name diff --git a/packages/google-cloud-video-live-stream/samples/snippets/start_channel.py b/packages/google-cloud-video-live-stream/samples/snippets/start_channel.py index 9e05aaa3abba..8bb9204ee98e 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/start_channel.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/start_channel.py @@ -23,12 +23,15 @@ import argparse +from google.cloud.video import live_stream_v1 from google.cloud.video.live_stream_v1.services.livestream_service import ( LivestreamServiceClient, ) -def start_channel(project_id: str, location: str, channel_id: str) -> None: +def start_channel( + project_id: str, location: str, channel_id: str +) -> live_stream_v1.types.ChannelOperationResponse: """Starts a channel. Args: project_id: The GCP project ID. @@ -39,9 +42,11 @@ def start_channel(project_id: str, location: str, channel_id: str) -> None: name = f"projects/{project_id}/locations/{location}/channels/{channel_id}" operation = client.start_channel(name=name) - operation.result(900) + response = operation.result(900) print("Started channel") + return response + # [END livestream_start_channel] diff --git a/packages/google-cloud-video-live-stream/samples/snippets/stop_channel.py b/packages/google-cloud-video-live-stream/samples/snippets/stop_channel.py index 6440a8d7a972..c7ccff2ae8bb 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/stop_channel.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/stop_channel.py @@ -23,12 +23,15 @@ import argparse +from google.cloud.video import live_stream_v1 from google.cloud.video.live_stream_v1.services.livestream_service import ( LivestreamServiceClient, ) -def stop_channel(project_id: str, location: str, channel_id: str) -> None: +def stop_channel( + project_id: str, location: str, channel_id: str +) -> live_stream_v1.types.ChannelOperationResponse: """Stops a channel. Args: project_id: The GCP project ID. @@ -39,9 +42,11 @@ def stop_channel(project_id: str, location: str, channel_id: str) -> None: name = f"projects/{project_id}/locations/{location}/channels/{channel_id}" operation = client.stop_channel(name=name) - operation.result(600) + response = operation.result(600) print("Stopped channel") + return response + # [END livestream_stop_channel] diff --git a/packages/google-cloud-video-live-stream/samples/snippets/update_channel.py b/packages/google-cloud-video-live-stream/samples/snippets/update_channel.py index 9bd25deb0cda..b1179433cc18 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/update_channel.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/update_channel.py @@ -33,7 +33,7 @@ def update_channel( project_id: str, location: str, channel_id: str, input_id: str -) -> str: +) -> live_stream_v1.types.Channel: """Updates a channel. Args: project_id: The GCP project ID. diff --git a/packages/google-cloud-video-live-stream/samples/snippets/update_input.py b/packages/google-cloud-video-live-stream/samples/snippets/update_input.py index 64f2dfb19287..04236b0d797a 100644 --- a/packages/google-cloud-video-live-stream/samples/snippets/update_input.py +++ b/packages/google-cloud-video-live-stream/samples/snippets/update_input.py @@ -31,7 +31,9 @@ from google.protobuf import field_mask_pb2 as field_mask -def update_input(project_id: str, location: str, input_id: str) -> str: +def update_input( + project_id: str, location: str, input_id: str +) -> live_stream_v1.types.Input: """Updates an input. Args: project_id: The GCP project ID. diff --git a/packages/google-cloud-video-live-stream/samples/snippets/update_pool.py b/packages/google-cloud-video-live-stream/samples/snippets/update_pool.py new file mode 100644 index 000000000000..26d0839520d6 --- /dev/null +++ b/packages/google-cloud-video-live-stream/samples/snippets/update_pool.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python + +# Copyright 2023 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 Live Stream sample for updating a pool's peered network. +Example usage: + python update_pool.py --project_id --location \ + --pool_id --peered_network +""" + +# [START livestream_update_pool] + +import argparse + +from google.cloud.video import live_stream_v1 +from google.cloud.video.live_stream_v1.services.livestream_service import ( + LivestreamServiceClient, +) +from google.protobuf import field_mask_pb2 as field_mask + + +def update_pool( + project_id: str, location: str, pool_id: str, peered_network: str +) -> live_stream_v1.types.Pool: + """Updates an pool. + Args: + project_id: The GCP project ID. + location: The location of the pool. + pool_id: The user-defined pool ID. + peered_network: The updated peer network (e.g., + 'projects/my-network-project-number/global/networks/my-network-name').""" + + client = LivestreamServiceClient() + + name = f"projects/{project_id}/locations/{location}/pools/{pool_id}" + + pool = live_stream_v1.types.Pool( + name=name, + network_config=live_stream_v1.types.Pool.NetworkConfig( + peered_network=peered_network, + ), + ) + update_mask = field_mask.FieldMask(paths=["network_config"]) + + operation = client.update_pool(pool=pool, update_mask=update_mask) + response = operation.result() + print(f"Updated pool: {response.name}") + + return response + + +# [END livestream_update_pool] + +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 pool.", + required=True, + ) + parser.add_argument( + "--pool_id", + help="The user-defined pool ID.", + required=True, + ) + parser.add_argument( + "--peered_network", + help="The updated peer network.", + required=True, + ) + args = parser.parse_args() + update_pool( + args.project_id, + args.location, + args.pool_id, + args.peered_network, + )