Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: add samples #37

Merged
merged 23 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ steps:
- '-c'
- |
python3.6 -m pip uninstall --yes --quiet nox-automation && python3.6 -m pip install --upgrade --quiet nox && python3.6 -m nox

images:
- gcr.io/cloud-devrel-public-resources/python-multi
96 changes: 96 additions & 0 deletions samples/snippets/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python

# Copyright 2019 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.

"""This application demonstrates basic administrative operations with the
Cloud Pub/Sub Lite API. For more information, see the root level README.md
and the documentation at https://cloud.google.com/pubsub/lite/docs.
"""

import argparse


def create_lite_topic(project_number, topic_id, cloud_region, zone_id, num_partitions):
# [START pubsublite_create_topic]
from google.cloud.pubsublite.make_admin_client import make_admin_client
from google.cloud.pubsublite_v1 import Topic
from google.protobuf.duration_pb2 import Duration

# TODO(developer):
# project_number = 1122334455
# toic_id = "your-topic-id"
# cloud_region = "us-central1"
# zone_id = "a"
# num_partitions = 1

client = make_admin_client(cloud_region)

topic = Topic(
{
"name": f"projects/{project_number}/locations/{cloud_region}-{zone_id}/topics/{topic_id}",
anguillanneuf marked this conversation as resolved.
Show resolved Hide resolved
"partition_config": Topic.PartitionConfig(
{
# The product of the partition count and the scaling factor equals the number of
# partitions that your topic is charged.
"count": num_partitions,
# Set publishing throughput to 1x standard partition throughput of 4 MiB per second.
# This vlaue must in the range [1,4].
"scale": 1,
}
),
"retention_config": Topic.RetentionConfig(
{
# Set storage per partition to 30 GiB. This must be in the range 30 GiB-10TiB. If the
# number of byptes stored in any of the topic's partitions grows beyond this value,
# older messages will be dropped to make room for newer ones, regardless of the value
# of `period`.
"per_partition_bytes": 30 * 1024 * 1024 * 1024,
# How long messages are retained.
"period": Duration(seconds=60 * 60 * 24 * 7),
}
),
}
)

response = client.create_topic(topic)
print(f"{response}\ncreated successfully.")
# [END pubsublite_create_topic]


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("project_number", help="Your Google Cloud Project Number")

subparsers = parser.add_subparsers(dest="command")

create_lite_topic_parser = subparsers.add_parser(
"create-topic", help=create_lite_topic.__doc__
)
create_lite_topic_parser.add_argument(
"topic_id", "cloud_region", "zone_id", "num_partitions"
)

args = parser.parse_args()

if args.command == "create-topic":
create_lite_topic(
args.project_number,
args.topic_id,
args.cloud_region,
args.zone_id,
args.num_partitions,
)
36 changes: 36 additions & 0 deletions samples/snippets/admin_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python

# Copyright 2020 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 random
import uuid

import admin

uuid_hex = uuid.uuid4().hex
project_number = os.environ["GOOGLE_CLOUD_PROJECT_NUMBER"]
topic_id = "py-lite-topic-" + uuid_hex
cloud_region = "us-central1"
zone_id = ["a", "b", "c"][random.randint(0, 2)]
num_partitions = 1


def test_create_lite_topic(capsys):
admin.create_lite_topic(
project_number, topic_id, cloud_region, zone_id, num_partitions
)
out, _ = capsys.readouterr()
assert "created successfully." in out
8 changes: 3 additions & 5 deletions samples/snippets/noxfile_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@

TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
'ignored_versions': ["2.7"],

"ignored_versions": ["2.7"],
# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',

# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
'envs': {},
"envs": {},
}