Skip to content

Commit

Permalink
docs(samples): add samples and tests for pools and assets (#180)
Browse files Browse the repository at this point in the history
* 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 <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: Anthonios Partheniou <partheniou@google.com>
  • Loading branch information
3 people authored Aug 11, 2023
1 parent 6b2b788 commit 5649d1c
Show file tree
Hide file tree
Showing 28 changed files with 635 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -41,53 +41,39 @@ 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)

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)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <project-id> --location <location> \
--asset_id <asset-id> --asset_uri <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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <project-id> --location <location> --asset_id <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,
)
Loading

0 comments on commit 5649d1c

Please sign in to comment.