Skip to content

Commit b52162b

Browse files
irataxypartheagcf-owl-bot[bot]
authored
docs(samples): add code samples (#17)
* docs(samples): add code samples * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * Rename *_ad_tag_details.py files for consistency * add commit to trigger gh actions * another commit to trigger samples tests * add tests for live session ad tag details - requires custom manifest * change slate URI to non-gcs uri * Use project ID rather than project number per DPE style * Change slate location for testing since we are going over the 3 slate quota limit * add noxfile_config.py; enforce type hints * add line break * use GOOGLE_CLOUD_PROJECT in noxfile_config.py * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fix linter errors * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Anthonios Partheniou <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
0 parents  commit b52162b

27 files changed

+1990
-0
lines changed

video/stitcher/cdn_key_test.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Copyright 2022 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import uuid
17+
18+
from google.api_core.exceptions import NotFound
19+
import pytest
20+
21+
import create_cdn_key
22+
import delete_cdn_key
23+
import get_cdn_key
24+
import list_cdn_keys
25+
import update_cdn_key
26+
27+
location = "us-central1"
28+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
29+
gcdn_cdn_key_id = f"my-python-test-cdn-key-{uuid.uuid4()}"
30+
akamai_cdn_key_id = f"my-python-test-cdn-key-{uuid.uuid4()}"
31+
32+
hostname = "cdn.example.com"
33+
updated_hostname = "updated.example.com"
34+
35+
gcdn_key_name = "gcdn-key"
36+
gcdn_private_key = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg=="
37+
updated_gcdn_private_key = "VGhpcyBpcyBhbiB1cGRhdGVkIHRlc3Qgc3RyaW5nLg=="
38+
akamai_key = gcdn_private_key
39+
updated_akamai_key = updated_gcdn_private_key
40+
41+
42+
def test_cdn_key_operations(capsys: pytest.fixture) -> None:
43+
44+
try:
45+
delete_cdn_key.delete_cdn_key(project_id, location, gcdn_cdn_key_id)
46+
except NotFound as e:
47+
print(f"Ignoring NotFound, details: {e}")
48+
out, _ = capsys.readouterr()
49+
50+
try:
51+
delete_cdn_key.delete_cdn_key(project_id, location, akamai_cdn_key_id)
52+
except NotFound as e:
53+
print(f"Ignoring NotFound, details: {e}")
54+
out, _ = capsys.readouterr()
55+
56+
# GCDN CDN key tests
57+
58+
create_cdn_key.create_cdn_key(
59+
project_id,
60+
location,
61+
gcdn_cdn_key_id,
62+
hostname,
63+
gcdn_key_name,
64+
gcdn_private_key,
65+
)
66+
out, _ = capsys.readouterr()
67+
assert gcdn_cdn_key_id in out
68+
69+
list_cdn_keys.list_cdn_keys(project_id, location)
70+
out, _ = capsys.readouterr()
71+
assert gcdn_cdn_key_id in out
72+
73+
# Update the hostname only
74+
response = update_cdn_key.update_cdn_key(
75+
project_id, location, gcdn_cdn_key_id, updated_hostname
76+
)
77+
out, _ = capsys.readouterr()
78+
assert gcdn_cdn_key_id in out
79+
assert updated_hostname in response.hostname
80+
81+
# Update the private key; the private key value is not returned by the client
82+
response = update_cdn_key.update_cdn_key(
83+
project_id,
84+
location,
85+
gcdn_cdn_key_id,
86+
hostname,
87+
gcdn_key_name,
88+
updated_gcdn_private_key,
89+
)
90+
out, _ = capsys.readouterr()
91+
assert gcdn_cdn_key_id in out
92+
93+
get_cdn_key.get_cdn_key(project_id, location, gcdn_cdn_key_id)
94+
out, _ = capsys.readouterr()
95+
assert gcdn_cdn_key_id in out
96+
97+
delete_cdn_key.delete_cdn_key(project_id, location, gcdn_cdn_key_id)
98+
out, _ = capsys.readouterr()
99+
assert "Deleted CDN key" in out
100+
101+
# Akamai CDN key tests
102+
103+
create_cdn_key.create_cdn_key(
104+
project_id,
105+
location,
106+
akamai_cdn_key_id,
107+
hostname,
108+
akamai_token_key=akamai_key,
109+
)
110+
out, _ = capsys.readouterr()
111+
assert akamai_cdn_key_id in out
112+
113+
list_cdn_keys.list_cdn_keys(project_id, location)
114+
out, _ = capsys.readouterr()
115+
assert akamai_cdn_key_id in out
116+
117+
# Update the hostname only
118+
response = update_cdn_key.update_cdn_key(
119+
project_id, location, akamai_cdn_key_id, updated_hostname
120+
)
121+
out, _ = capsys.readouterr()
122+
assert akamai_cdn_key_id in out
123+
assert updated_hostname in response.hostname
124+
125+
# Update the private key; the private key value is not returned by the client
126+
response = update_cdn_key.update_cdn_key(
127+
project_id,
128+
location,
129+
akamai_cdn_key_id,
130+
hostname,
131+
akamai_token_key=updated_akamai_key,
132+
)
133+
out, _ = capsys.readouterr()
134+
assert akamai_cdn_key_id in out
135+
136+
get_cdn_key.get_cdn_key(project_id, location, akamai_cdn_key_id)
137+
out, _ = capsys.readouterr()
138+
assert akamai_cdn_key_id in out
139+
140+
delete_cdn_key.delete_cdn_key(project_id, location, akamai_cdn_key_id)
141+
out, _ = capsys.readouterr()
142+
assert "Deleted CDN key" in out

video/stitcher/create_cdn_key.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Cloud Video Stitcher sample for creating a CDN key. A CDN key is used
18+
to retrieve protected media.
19+
Example usage:
20+
python create_cdn_key.py --project_id <project-id> --location <location> --cdn_key_id <cdn_key_id> \
21+
--hostname <hostname> [--gcdn_keyname <name> --gcdn_private_key <secret> | --akamai_token_key <token-key>]
22+
"""
23+
24+
# [START video_stitcher_create_cdn_key]
25+
26+
import argparse
27+
28+
from google.cloud.video import stitcher_v1
29+
from google.cloud.video.stitcher_v1.services.video_stitcher_service import (
30+
VideoStitcherServiceClient,
31+
)
32+
33+
34+
def create_cdn_key(
35+
project_id: str,
36+
location: str,
37+
cdn_key_id: str,
38+
hostname: str,
39+
gcdn_keyname: str = None,
40+
gcdn_private_key: str = None,
41+
akamai_token_key: str = None,
42+
) -> str:
43+
"""Creates a Google Cloud or Akamai CDN key.
44+
Args:
45+
project_id: The GCP project ID.
46+
location: The location in which to create the CDN key.
47+
cdn_key_id: The user-defined CDN key ID.
48+
hostname: The hostname to which this CDN key applies.
49+
gcdn_keyname: Applies to a Google Cloud CDN key. A base64-encoded string secret.
50+
gcdn_private_key: Applies to a Google Cloud CDN key. Public name of the key.
51+
akamai_token_key: Applies to an Akamai CDN key. A base64-encoded string token key."""
52+
53+
client = VideoStitcherServiceClient()
54+
55+
parent = f"projects/{project_id}/locations/{location}"
56+
57+
cdn_key = stitcher_v1.types.CdnKey(
58+
name=cdn_key_id,
59+
hostname=hostname,
60+
)
61+
62+
if akamai_token_key is not None:
63+
cdn_key.akamai_cdn_key = stitcher_v1.types.AkamaiCdnKey(
64+
token_key=akamai_token_key,
65+
)
66+
elif gcdn_keyname is not None:
67+
cdn_key.google_cdn_key = stitcher_v1.types.GoogleCdnKey(
68+
key_name=gcdn_keyname,
69+
private_key=gcdn_private_key,
70+
)
71+
72+
response = client.create_cdn_key(
73+
parent=parent, cdn_key_id=cdn_key_id, cdn_key=cdn_key
74+
)
75+
print(f"CDN key: {response.name}")
76+
return response
77+
78+
79+
# [END video_stitcher_create_cdn_key]
80+
81+
if __name__ == "__main__":
82+
parser = argparse.ArgumentParser()
83+
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
84+
parser.add_argument(
85+
"--location",
86+
help="The location in which to create the CDN key.",
87+
default="us-central1",
88+
)
89+
parser.add_argument(
90+
"--cdn_key_id",
91+
help="The user-defined CDN key ID.",
92+
required=True,
93+
)
94+
parser.add_argument(
95+
"--hostname",
96+
help="The hostname to which this CDN key applies.",
97+
required=True,
98+
)
99+
parser.add_argument(
100+
"--gcdn_keyname",
101+
help="Applies to a Google Cloud CDN key. The base64-encoded string secret.",
102+
)
103+
parser.add_argument(
104+
"--gcdn_private_key",
105+
help="Applies to a Google Cloud CDN key. Public name of the key.",
106+
)
107+
parser.add_argument(
108+
"--akamai_token_key",
109+
help="Applies to an Akamai CDN key. The base64-encoded string token key.",
110+
)
111+
args = parser.parse_args()
112+
create_cdn_key(
113+
args.project_id,
114+
args.location,
115+
args.cdn_key_id,
116+
args.hostname,
117+
args.gcdn_keyname,
118+
args.gcdn_private_key,
119+
args.akamai_token_key,
120+
)

video/stitcher/create_live_session.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Cloud Video Stitcher sample for creating a live stream session in
18+
which to insert ads.
19+
Example usage:
20+
python create_live_session.py --project_id <project-id> \
21+
--location <location> --live_stream_uri <uri> --ad_tag_uri <uri> \
22+
--slate_id <slate-id>
23+
"""
24+
25+
# [START video_stitcher_create_live_session]
26+
27+
import argparse
28+
29+
from google.cloud.video import stitcher_v1
30+
from google.cloud.video.stitcher_v1.services.video_stitcher_service import (
31+
VideoStitcherServiceClient,
32+
)
33+
34+
35+
def create_live_session(
36+
project_id: str, location: str, live_stream_uri: str, ad_tag_uri: str, slate_id: str
37+
) -> str:
38+
"""Creates a live session. Live sessions are ephemeral resources that expire
39+
after a few minutes.
40+
Args:
41+
project_id: The GCP project ID.
42+
location: The location in which to create the session.
43+
live_stream_uri: Uri of the livestream to stitch; this URI must reference either an MPEG-DASH
44+
manifest (.mpd) file or an M3U playlist manifest (.m3u8) file.
45+
ad_tag_uri: Uri of the ad tag.
46+
slate_id: The user-defined slate ID of the default slate to use when no slates are specified in an ad break's message."""
47+
48+
client = VideoStitcherServiceClient()
49+
50+
parent = f"projects/{project_id}/locations/{location}"
51+
52+
# Create dictionaries and pass them to the LiveSession constructor
53+
ad_tag_map = {"default": stitcher_v1.AdTag(uri=ad_tag_uri)}
54+
55+
live_session = stitcher_v1.types.LiveSession(
56+
source_uri=live_stream_uri, ad_tag_map=ad_tag_map, default_slate_id=slate_id
57+
)
58+
59+
response = client.create_live_session(parent=parent, live_session=live_session)
60+
print(f"Live session: {response.name}")
61+
return response
62+
63+
64+
# [END video_stitcher_create_live_session]
65+
66+
if __name__ == "__main__":
67+
parser = argparse.ArgumentParser()
68+
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
69+
parser.add_argument(
70+
"--location",
71+
help="The location in which to create the live session.",
72+
default="us-central1",
73+
)
74+
parser.add_argument(
75+
"--live_stream_uri",
76+
help="The Uri of the livestream to stitch (.mpd or .m3u8 file) in double quotes.",
77+
required=True,
78+
)
79+
parser.add_argument(
80+
"--ad_tag_uri",
81+
help="Uri of the ad tag in double quotes.",
82+
required=True,
83+
)
84+
parser.add_argument(
85+
"--slate_id",
86+
help="The user-defined slate ID of the default slate.",
87+
required=True,
88+
)
89+
args = parser.parse_args()
90+
create_live_session(
91+
args.project_id,
92+
args.location,
93+
args.live_stream_uri,
94+
args.ad_tag_uri,
95+
args.slate_id,
96+
)

0 commit comments

Comments
 (0)