-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(samples): add samples and tests for pools and assets (#180)
* 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
Showing
28 changed files
with
635 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
Oops, something went wrong.