Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

Commit

Permalink
docs(samples): Add Media CDN samples. Move Akamai sample to its own f…
Browse files Browse the repository at this point in the history
…ile. (#112)
  • Loading branch information
irataxy committed Jan 5, 2023
1 parent 2ecca54 commit b905053
Show file tree
Hide file tree
Showing 7 changed files with 421 additions and 113 deletions.
134 changes: 83 additions & 51 deletions samples/snippets/cdn_key_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,97 +15,135 @@
import os
import uuid

from google.api_core.exceptions import NotFound
from google.protobuf import timestamp_pb2
import pytest

import create_cdn_key
import create_cdn_key_akamai
import delete_cdn_key
import get_cdn_key
import list_cdn_keys
import update_cdn_key
import update_cdn_key_akamai
import utils

location = "us-central1"
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
gcdn_cdn_key_id = f"my-python-test-cdn-key-{uuid.uuid4()}"
akamai_cdn_key_id = f"my-python-test-cdn-key-{uuid.uuid4()}"
now = timestamp_pb2.Timestamp()
now.GetCurrentTime()

media_cdn_key_id = f"python-test-media-key-{uuid.uuid4().hex[:5]}-{now.seconds}"
cloud_cdn_key_id = f"python-test-cloud-key-{uuid.uuid4().hex[:5]}-{now.seconds}"
akamai_cdn_key_id = f"python-test-akamai-key-{uuid.uuid4().hex[:5]}-{now.seconds}"

hostname = "cdn.example.com"
updated_hostname = "updated.example.com"
key_name = "my-key"

gcdn_key_name = "gcdn-key"
gcdn_private_key = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg=="
updated_gcdn_private_key = "VGhpcyBpcyBhbiB1cGRhdGVkIHRlc3Qgc3RyaW5nLg=="
akamai_key = gcdn_private_key
updated_akamai_key = updated_gcdn_private_key
media_cdn_private_key = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNA"
updated_media_cdn_private_key = (
"ZZZzNDU2Nzg5MDEyMzQ1Njc4OTAxzg5MDEyMzQ1Njc4OTAxMjM0NTY3DkwMTIZZZ"
)
cloud_cdn_private_key = "VGhpcyBpcyBhIHRlc3Qgc3RyaW5nLg=="
updated_cloud_cdn_private_key = "VGhpcyBpcyBhbiB1cGRhdGVkIHRlc3Qgc3RyaW5nLg=="
akamai_key = cloud_cdn_private_key
updated_akamai_key = updated_cloud_cdn_private_key


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

try:
delete_cdn_key.delete_cdn_key(project_id, location, gcdn_cdn_key_id)
except NotFound as e:
print(f"Ignoring NotFound, details: {e}")
out, _ = capsys.readouterr()
utils.delete_stale_cdn_keys(project_id, location)

try:
delete_cdn_key.delete_cdn_key(project_id, location, akamai_cdn_key_id)
except NotFound as e:
print(f"Ignoring NotFound, details: {e}")
out, _ = capsys.readouterr()

# GCDN CDN key tests
# Media CDN key tests

create_cdn_key.create_cdn_key(
project_id,
location,
gcdn_cdn_key_id,
media_cdn_key_id,
hostname,
gcdn_key_name,
gcdn_private_key,
key_name,
media_cdn_private_key,
False,
)
out, _ = capsys.readouterr()
assert gcdn_cdn_key_id in out
assert media_cdn_key_id in out

list_cdn_keys.list_cdn_keys(project_id, location)
out, _ = capsys.readouterr()
assert gcdn_cdn_key_id in out
assert media_cdn_key_id in out

# Update the hostname only
# Update the hostname and private key; the private key value
# is not returned by the client
response = update_cdn_key.update_cdn_key(
project_id, location, gcdn_cdn_key_id, updated_hostname
project_id,
location,
media_cdn_key_id,
updated_hostname,
key_name,
updated_media_cdn_private_key,
False,
)
out, _ = capsys.readouterr()
assert gcdn_cdn_key_id in out
assert media_cdn_key_id in out
assert updated_hostname in response.hostname

# Update the private key; the private key value is not returned by the client
response = update_cdn_key.update_cdn_key(
get_cdn_key.get_cdn_key(project_id, location, media_cdn_key_id)
out, _ = capsys.readouterr()
assert media_cdn_key_id in out

delete_cdn_key.delete_cdn_key(project_id, location, media_cdn_key_id)
out, _ = capsys.readouterr()
assert "Deleted CDN key" in out

# Cloud CDN key tests

create_cdn_key.create_cdn_key(
project_id,
location,
gcdn_cdn_key_id,
cloud_cdn_key_id,
hostname,
gcdn_key_name,
updated_gcdn_private_key,
key_name,
cloud_cdn_private_key,
True,
)
out, _ = capsys.readouterr()
assert cloud_cdn_key_id in out

list_cdn_keys.list_cdn_keys(project_id, location)
out, _ = capsys.readouterr()
assert cloud_cdn_key_id in out

# Update the hostname and private key; the private key value
# is not returned by the client
response = update_cdn_key.update_cdn_key(
project_id,
location,
cloud_cdn_key_id,
updated_hostname,
key_name,
updated_cloud_cdn_private_key,
True,
)
out, _ = capsys.readouterr()
assert gcdn_cdn_key_id in out
assert cloud_cdn_key_id in out
assert updated_hostname in response.hostname

get_cdn_key.get_cdn_key(project_id, location, gcdn_cdn_key_id)
get_cdn_key.get_cdn_key(project_id, location, cloud_cdn_key_id)
out, _ = capsys.readouterr()
assert gcdn_cdn_key_id in out
assert cloud_cdn_key_id in out

delete_cdn_key.delete_cdn_key(project_id, location, gcdn_cdn_key_id)
delete_cdn_key.delete_cdn_key(project_id, location, cloud_cdn_key_id)
out, _ = capsys.readouterr()
assert "Deleted CDN key" in out

# Akamai CDN key tests

create_cdn_key.create_cdn_key(
create_cdn_key_akamai.create_cdn_key_akamai(
project_id,
location,
akamai_cdn_key_id,
hostname,
akamai_token_key=akamai_key,
akamai_key,
)
out, _ = capsys.readouterr()
assert akamai_cdn_key_id in out
Expand All @@ -114,24 +152,18 @@ def test_cdn_key_operations(capsys: pytest.fixture) -> None:
out, _ = capsys.readouterr()
assert akamai_cdn_key_id in out

# Update the hostname only
response = update_cdn_key.update_cdn_key(
project_id, location, akamai_cdn_key_id, updated_hostname
)
out, _ = capsys.readouterr()
assert akamai_cdn_key_id in out
assert updated_hostname in response.hostname

# Update the private key; the private key value is not returned by the client
response = update_cdn_key.update_cdn_key(
# Update the hostname and private key; the private key value
# is not returned by the client
response = update_cdn_key_akamai.update_cdn_key_akamai(
project_id,
location,
akamai_cdn_key_id,
hostname,
akamai_token_key=updated_akamai_key,
updated_hostname,
updated_akamai_key,
)
out, _ = capsys.readouterr()
assert akamai_cdn_key_id in out
assert updated_hostname in response.hostname

get_cdn_key.get_cdn_key(project_id, location, akamai_cdn_key_id)
out, _ = capsys.readouterr()
Expand Down
68 changes: 41 additions & 27 deletions samples/snippets/create_cdn_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Google Cloud Video Stitcher sample for creating a CDN key. A CDN key is used
to retrieve protected media.
"""Google Cloud Video Stitcher sample for creating a Media CDN key
or a Cloud CDN key. A CDN key is used to retrieve protected media.
Example usage:
python create_cdn_key.py --project_id <project-id> --location <location> --cdn_key_id <cdn_key_id> \
--hostname <hostname> [--gcdn_keyname <name> --gcdn_private_key <secret> | --akamai_token_key <token-key>]
python create_cdn_key.py --project_id <project-id> --location <location> \
--cdn_key_id <cdn_key_id> --hostname <hostname> \
--key_name <name> --private_key <key> [--is_cloud_cdn]
"""

# [START videostitcher_create_cdn_key]
Expand All @@ -36,19 +37,23 @@ def create_cdn_key(
location: str,
cdn_key_id: str,
hostname: str,
gcdn_keyname: str = None,
gcdn_private_key: str = None,
akamai_token_key: str = None,
key_name: str,
private_key: str,
is_cloud_cdn: bool,
) -> str:
"""Creates a Google Cloud or Akamai CDN key.
"""Creates a Cloud CDN or Media CDN key.
Args:
project_id: The GCP project ID.
location: The location in which to create the CDN key.
cdn_key_id: The user-defined CDN key ID.
hostname: The hostname to which this CDN key applies.
gcdn_keyname: Applies to a Google Cloud CDN key. A base64-encoded string secret.
gcdn_private_key: Applies to a Google Cloud CDN key. Public name of the key.
akamai_token_key: Applies to an Akamai CDN key. A base64-encoded string token key."""
key_name: For a Media CDN key, this is the keyset name.
For a Cloud CDN key, this is the public name of the CDN key.
private_key: For a Media CDN key, this is a 64-byte Ed25519 private
key encoded as a base64-encoded string.
See https://cloud.google.com/video-stitcher/docs/how-to/managing-cdn-keys#create-private-key-media-cdn
for more information. For a Cloud CDN key, this is a base64-encoded string secret.
is_cloud_cdn: If true, create a Cloud CDN key. If false, create a Media CDN key."""

client = VideoStitcherServiceClient()

Expand All @@ -59,14 +64,15 @@ def create_cdn_key(
hostname=hostname,
)

if akamai_token_key is not None:
cdn_key.akamai_cdn_key = stitcher_v1.types.AkamaiCdnKey(
token_key=akamai_token_key,
)
elif gcdn_keyname is not None:
if is_cloud_cdn:
cdn_key.google_cdn_key = stitcher_v1.types.GoogleCdnKey(
key_name=gcdn_keyname,
private_key=gcdn_private_key,
key_name=key_name,
private_key=private_key,
)
else:
cdn_key.media_cdn_key = stitcher_v1.types.MediaCdnKey(
key_name=key_name,
private_key=private_key,
)

response = client.create_cdn_key(
Expand Down Expand Up @@ -97,24 +103,32 @@ def create_cdn_key(
required=True,
)
parser.add_argument(
"--gcdn_keyname",
help="Applies to a Google Cloud CDN key. The base64-encoded string secret.",
"--key_name",
help="For a Media CDN key, this is the keyset name. For a Cloud CDN"
+ " key, this is the public name of the CDN key.",
required=True,
)
parser.add_argument(
"--gcdn_private_key",
help="Applies to a Google Cloud CDN key. Public name of the key.",
"--private_key",
help="For a Media CDN key, this is a 64-byte Ed25519 private key"
+ "encoded as a base64-encoded string. See"
+ " https://cloud.google.com/video-stitcher/docs/how-to/managing-cdn-keys#create-private-key-media-cdn"
+ " for more information. For a Cloud CDN key, this is a"
+ " base64-encoded string secret.",
required=True,
)
parser.add_argument(
"--akamai_token_key",
help="Applies to an Akamai CDN key. The base64-encoded string token key.",
"--is_cloud_cdn",
action="store_true",
help="If included, create a Cloud CDN key. If absent, create a Media CDN key.",
)
args = parser.parse_args()
create_cdn_key(
args.project_id,
args.location,
args.cdn_key_id,
args.hostname,
args.gcdn_keyname,
args.gcdn_private_key,
args.akamai_token_key,
args.key_name,
args.private_key,
args.is_cloud_cdn,
)
Loading

0 comments on commit b905053

Please sign in to comment.