Skip to content

Commit ba23150

Browse files
authored
docs(samples): Add Media CDN samples. Move Akamai sample to its own file. (#112)
1 parent 8904a36 commit ba23150

7 files changed

+421
-113
lines changed

video/stitcher/cdn_key_test.py

Lines changed: 83 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,97 +15,135 @@
1515
import os
1616
import uuid
1717

18-
from google.api_core.exceptions import NotFound
18+
from google.protobuf import timestamp_pb2
1919
import pytest
2020

2121
import create_cdn_key
22+
import create_cdn_key_akamai
2223
import delete_cdn_key
2324
import get_cdn_key
2425
import list_cdn_keys
2526
import update_cdn_key
27+
import update_cdn_key_akamai
28+
import utils
2629

2730
location = "us-central1"
2831
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()}"
32+
now = timestamp_pb2.Timestamp()
33+
now.GetCurrentTime()
34+
35+
media_cdn_key_id = f"python-test-media-key-{uuid.uuid4().hex[:5]}-{now.seconds}"
36+
cloud_cdn_key_id = f"python-test-cloud-key-{uuid.uuid4().hex[:5]}-{now.seconds}"
37+
akamai_cdn_key_id = f"python-test-akamai-key-{uuid.uuid4().hex[:5]}-{now.seconds}"
3138

3239
hostname = "cdn.example.com"
3340
updated_hostname = "updated.example.com"
41+
key_name = "my-key"
3442

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
43+
media_cdn_private_key = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNA"
44+
updated_media_cdn_private_key = (
45+
"ZZZzNDU2Nzg5MDEyMzQ1Njc4OTAxzg5MDEyMzQ1Njc4OTAxMjM0NTY3DkwMTIZZZ"
46+
)
47+
cloud_cdn_private_key = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg=="
48+
updated_cloud_cdn_private_key = "VGhpcyBpcyBhbiB1cGRhdGVkIHRlc3Qgc3RyaW5nLg=="
49+
akamai_key = cloud_cdn_private_key
50+
updated_akamai_key = updated_cloud_cdn_private_key
4051

4152

4253
def test_cdn_key_operations(capsys: pytest.fixture) -> None:
4354

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()
55+
utils.delete_stale_cdn_keys(project_id, location)
4956

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+
# Media CDN key tests
5758

5859
create_cdn_key.create_cdn_key(
5960
project_id,
6061
location,
61-
gcdn_cdn_key_id,
62+
media_cdn_key_id,
6263
hostname,
63-
gcdn_key_name,
64-
gcdn_private_key,
64+
key_name,
65+
media_cdn_private_key,
66+
False,
6567
)
6668
out, _ = capsys.readouterr()
67-
assert gcdn_cdn_key_id in out
69+
assert media_cdn_key_id in out
6870

6971
list_cdn_keys.list_cdn_keys(project_id, location)
7072
out, _ = capsys.readouterr()
71-
assert gcdn_cdn_key_id in out
73+
assert media_cdn_key_id in out
7274

73-
# Update the hostname only
75+
# Update the hostname and private key; the private key value
76+
# is not returned by the client
7477
response = update_cdn_key.update_cdn_key(
75-
project_id, location, gcdn_cdn_key_id, updated_hostname
78+
project_id,
79+
location,
80+
media_cdn_key_id,
81+
updated_hostname,
82+
key_name,
83+
updated_media_cdn_private_key,
84+
False,
7685
)
7786
out, _ = capsys.readouterr()
78-
assert gcdn_cdn_key_id in out
87+
assert media_cdn_key_id in out
7988
assert updated_hostname in response.hostname
8089

81-
# Update the private key; the private key value is not returned by the client
82-
response = update_cdn_key.update_cdn_key(
90+
get_cdn_key.get_cdn_key(project_id, location, media_cdn_key_id)
91+
out, _ = capsys.readouterr()
92+
assert media_cdn_key_id in out
93+
94+
delete_cdn_key.delete_cdn_key(project_id, location, media_cdn_key_id)
95+
out, _ = capsys.readouterr()
96+
assert "Deleted CDN key" in out
97+
98+
# Cloud CDN key tests
99+
100+
create_cdn_key.create_cdn_key(
83101
project_id,
84102
location,
85-
gcdn_cdn_key_id,
103+
cloud_cdn_key_id,
86104
hostname,
87-
gcdn_key_name,
88-
updated_gcdn_private_key,
105+
key_name,
106+
cloud_cdn_private_key,
107+
True,
108+
)
109+
out, _ = capsys.readouterr()
110+
assert cloud_cdn_key_id in out
111+
112+
list_cdn_keys.list_cdn_keys(project_id, location)
113+
out, _ = capsys.readouterr()
114+
assert cloud_cdn_key_id in out
115+
116+
# Update the hostname and private key; the private key value
117+
# is not returned by the client
118+
response = update_cdn_key.update_cdn_key(
119+
project_id,
120+
location,
121+
cloud_cdn_key_id,
122+
updated_hostname,
123+
key_name,
124+
updated_cloud_cdn_private_key,
125+
True,
89126
)
90127
out, _ = capsys.readouterr()
91-
assert gcdn_cdn_key_id in out
128+
assert cloud_cdn_key_id in out
129+
assert updated_hostname in response.hostname
92130

93-
get_cdn_key.get_cdn_key(project_id, location, gcdn_cdn_key_id)
131+
get_cdn_key.get_cdn_key(project_id, location, cloud_cdn_key_id)
94132
out, _ = capsys.readouterr()
95-
assert gcdn_cdn_key_id in out
133+
assert cloud_cdn_key_id in out
96134

97-
delete_cdn_key.delete_cdn_key(project_id, location, gcdn_cdn_key_id)
135+
delete_cdn_key.delete_cdn_key(project_id, location, cloud_cdn_key_id)
98136
out, _ = capsys.readouterr()
99137
assert "Deleted CDN key" in out
100138

101139
# Akamai CDN key tests
102140

103-
create_cdn_key.create_cdn_key(
141+
create_cdn_key_akamai.create_cdn_key_akamai(
104142
project_id,
105143
location,
106144
akamai_cdn_key_id,
107145
hostname,
108-
akamai_token_key=akamai_key,
146+
akamai_key,
109147
)
110148
out, _ = capsys.readouterr()
111149
assert akamai_cdn_key_id in out
@@ -114,24 +152,18 @@ def test_cdn_key_operations(capsys: pytest.fixture) -> None:
114152
out, _ = capsys.readouterr()
115153
assert akamai_cdn_key_id in out
116154

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(
155+
# Update the hostname and private key; the private key value
156+
# is not returned by the client
157+
response = update_cdn_key_akamai.update_cdn_key_akamai(
127158
project_id,
128159
location,
129160
akamai_cdn_key_id,
130-
hostname,
131-
akamai_token_key=updated_akamai_key,
161+
updated_hostname,
162+
updated_akamai_key,
132163
)
133164
out, _ = capsys.readouterr()
134165
assert akamai_cdn_key_id in out
166+
assert updated_hostname in response.hostname
135167

136168
get_cdn_key.get_cdn_key(project_id, location, akamai_cdn_key_id)
137169
out, _ = capsys.readouterr()

video/stitcher/create_cdn_key.py

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
"""Google Cloud Video Stitcher sample for creating a CDN key. A CDN key is used
18-
to retrieve protected media.
17+
"""Google Cloud Video Stitcher sample for creating a Media CDN key
18+
or a Cloud CDN key. A CDN key is used to retrieve protected media.
1919
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>]
20+
python create_cdn_key.py --project_id <project-id> --location <location> \
21+
--cdn_key_id <cdn_key_id> --hostname <hostname> \
22+
--key_name <name> --private_key <key> [--is_cloud_cdn]
2223
"""
2324

2425
# [START videostitcher_create_cdn_key]
@@ -36,19 +37,23 @@ def create_cdn_key(
3637
location: str,
3738
cdn_key_id: str,
3839
hostname: str,
39-
gcdn_keyname: str = None,
40-
gcdn_private_key: str = None,
41-
akamai_token_key: str = None,
40+
key_name: str,
41+
private_key: str,
42+
is_cloud_cdn: bool,
4243
) -> str:
43-
"""Creates a Google Cloud or Akamai CDN key.
44+
"""Creates a Cloud CDN or Media CDN key.
4445
Args:
4546
project_id: The GCP project ID.
4647
location: The location in which to create the CDN key.
4748
cdn_key_id: The user-defined CDN key ID.
4849
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."""
50+
key_name: For a Media CDN key, this is the keyset name.
51+
For a Cloud CDN key, this is the public name of the CDN key.
52+
private_key: For a Media CDN key, this is a 64-byte Ed25519 private
53+
key encoded as a base64-encoded string.
54+
See https://cloud.google.com/video-stitcher/docs/how-to/managing-cdn-keys#create-private-key-media-cdn
55+
for more information. For a Cloud CDN key, this is a base64-encoded string secret.
56+
is_cloud_cdn: If true, create a Cloud CDN key. If false, create a Media CDN key."""
5257

5358
client = VideoStitcherServiceClient()
5459

@@ -59,14 +64,15 @@ def create_cdn_key(
5964
hostname=hostname,
6065
)
6166

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+
if is_cloud_cdn:
6768
cdn_key.google_cdn_key = stitcher_v1.types.GoogleCdnKey(
68-
key_name=gcdn_keyname,
69-
private_key=gcdn_private_key,
69+
key_name=key_name,
70+
private_key=private_key,
71+
)
72+
else:
73+
cdn_key.media_cdn_key = stitcher_v1.types.MediaCdnKey(
74+
key_name=key_name,
75+
private_key=private_key,
7076
)
7177

7278
response = client.create_cdn_key(
@@ -97,24 +103,32 @@ def create_cdn_key(
97103
required=True,
98104
)
99105
parser.add_argument(
100-
"--gcdn_keyname",
101-
help="Applies to a Google Cloud CDN key. The base64-encoded string secret.",
106+
"--key_name",
107+
help="For a Media CDN key, this is the keyset name. For a Cloud CDN"
108+
+ " key, this is the public name of the CDN key.",
109+
required=True,
102110
)
103111
parser.add_argument(
104-
"--gcdn_private_key",
105-
help="Applies to a Google Cloud CDN key. Public name of the key.",
112+
"--private_key",
113+
help="For a Media CDN key, this is a 64-byte Ed25519 private key"
114+
+ "encoded as a base64-encoded string. See"
115+
+ " https://cloud.google.com/video-stitcher/docs/how-to/managing-cdn-keys#create-private-key-media-cdn"
116+
+ " for more information. For a Cloud CDN key, this is a"
117+
+ " base64-encoded string secret.",
118+
required=True,
106119
)
107120
parser.add_argument(
108-
"--akamai_token_key",
109-
help="Applies to an Akamai CDN key. The base64-encoded string token key.",
121+
"--is_cloud_cdn",
122+
action="store_true",
123+
help="If included, create a Cloud CDN key. If absent, create a Media CDN key.",
110124
)
111125
args = parser.parse_args()
112126
create_cdn_key(
113127
args.project_id,
114128
args.location,
115129
args.cdn_key_id,
116130
args.hostname,
117-
args.gcdn_keyname,
118-
args.gcdn_private_key,
119-
args.akamai_token_key,
131+
args.key_name,
132+
args.private_key,
133+
args.is_cloud_cdn,
120134
)

0 commit comments

Comments
 (0)