def cleanup_pubsub_topics(pubsub_client: pubsub_v1.PublisherClient, project_id): yesterday = datetime.datetime.utcnow() - datetime.timedelta(days=1) for topic in pubsub_client.list_topics(project=f"projects/{project_id}"): topic_id = topic.name.split("/")[-1] if ( topic_id.startswith(RESOURCE_PREFIX) and resource_name_to_date(topic_id) < yesterday ): pubsub_client.delete_topic(topic=topic.name)
def topic(publisher: PublisherClient, route: Tuple[str, str, str]) -> Iterator[str]: name = route[1] try: publisher.create_topic(name) delete = True except AlreadyExists: delete = False try: yield name finally: if delete: publisher.delete_topic(name)
def topic_path( publisher_client: pubsub_v1.PublisherClient ) -> Generator[str, None, None]: topic_path = publisher_client.topic_path(PROJECT_ID, TOPIC_ID) try: topic = publisher_client.create_topic(request={"name": topic_path}) yield topic.name except AlreadyExists: yield topic_path publisher_client.delete_topic(request={"topic": topic_path})
def test_create(publisher_client: pubsub_v1.PublisherClient, capsys: CaptureFixture) -> None: # The scope of `topic_path` is limited to this function. topic_path = publisher_client.topic_path(PROJECT_ID, TOPIC_ID) try: publisher_client.delete_topic(request={"topic": topic_path}) except NotFound: pass publisher.create_topic(PROJECT_ID, TOPIC_ID) out, _ = capsys.readouterr() assert f"Created topic: {topic_path}" in out
def test_submit_pubsub_topic_not_found( integration_test: IntegrationTest, publisher: PublisherClient, subscriber: SubscriberClient, subscription: str, topic: str, ): publisher.delete_topic(topic) try: integration_test.assert_accepted_and_queued() finally: subscriber.delete_subscription(subscription) publisher.create_topic(topic) subscriber.create_subscription(subscription, topic) integration_test.assert_flushed_and_delivered()
def topic_path( publisher_client: pubsub_v1.PublisherClient, ) -> Generator[str, None, None]: topic_path = publisher_client.topic_path(PROJECT_ID, TOPIC_ID) try: topic = publisher_client.get_topic(request={"topic": topic_path}) except NotFound: topic = publisher_client.create_topic(request={"name": topic_path}) yield topic.name try: publisher_client.delete_topic(request={"topic": topic.name}) except NotFound: pass
def emulator(node_pools: List[str], options: Dict[str, Any]) -> Iterator[str]: if "emulator" in node_pools: kube_apply("kube/emulator.deploy.yml", **options) kube_apply("kube/emulator.svc.yml") yield "emulator:8000" elif "server" in node_pools: pubsub = PublisherClient() topic = f"projects/{options['project']}/topics/{options['topic']}" try: pubsub.create_topic(topic) except AlreadyExists: pass yield "" pubsub.delete_topic(topic) else: yield ""
def tearDownClass(cls): super().tearDownClass() publisher = PublisherClient() subscriber = SubscriberClient() for topic in ["test0", "test1"]: try: publisher.delete_topic(publisher.topic_path( cls.project, topic)) except Exception as ex: raise ex try: subscriber.delete_subscription( subscriber.subscription_path(cls.project, topic)) except Exception as ex: raise ex
def proto_topic(publisher_client: pubsub_v1.PublisherClient, proto_schema: str) -> Generator[str, None, None]: proto_topic_path = publisher_client.topic_path(PROJECT_ID, PROTO_TOPIC_ID) try: proto_topic = publisher_client.get_topic( request={"topic": proto_topic_path}) except NotFound: proto_topic = publisher_client.create_topic( request={ "name": proto_topic_path, "schema_settings": { "schema": proto_schema, "encoding": Encoding.BINARY, }, }) yield proto_topic.name publisher_client.delete_topic(request={"topic": proto_topic.name})
def avro_topic(publisher_client: pubsub_v1.PublisherClient, avro_schema: str) -> Generator[str, None, None]: from google.pubsub_v1.types import Encoding avro_topic_path = publisher_client.topic_path(PROJECT_ID, AVRO_TOPIC_ID) try: avro_topic = publisher_client.get_topic( request={"topic": avro_topic_path}) except NotFound: avro_topic = publisher_client.create_topic( request={ "name": avro_topic_path, "schema_settings": { "schema": avro_schema, "encoding": Encoding.BINARY, }, }) yield avro_topic.name publisher_client.delete_topic(request={"topic": avro_topic.name})
def pubsub_topic(pubsub_client: pubsub_v1.PublisherClient, project_id): topic_id = resource_prefix() topic_path = pubsub_v1.PublisherClient.topic_path(project_id, topic_id) pubsub_client.create_topic(name=topic_path) yield topic_path pubsub_client.delete_topic(topic=topic_path)