Skip to content

Commit

Permalink
ENH: Give feedback on (un)successful push subscription creation in CLI
Browse files Browse the repository at this point in the history
skipci
  • Loading branch information
cortadocodes committed Jun 5, 2024
1 parent 927f90a commit 8c9b54a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
8 changes: 6 additions & 2 deletions octue/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def create_push_subscription(
"""
sruid = create_sruid(namespace=service_namespace, name=service_name, revision_tag=revision_tag)

pub_sub.create_push_subscription(
subscription = pub_sub.create_push_subscription(
project_name,
sruid,
push_endpoint,
Expand All @@ -394,7 +394,11 @@ def create_push_subscription(
allow_existing=not no_allow_existing,
)

click.echo(sruid)
if subscription.creation_triggered_locally:
click.echo(f"Subscription for {sruid!r} created.")
return

click.echo(f"Subscription for {sruid!r} already exists.")


def _add_monitor_message_to_file(path, monitor_message):
Expand Down
3 changes: 2 additions & 1 deletion octue/cloud/pub_sub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create_push_subscription(
:param str|None subscription_filter: if specified, the filter to apply to the subscription; otherwise, no filter is applied
:param float|None expiration_time: the number of seconds of inactivity after which the subscription should expire. If not provided, no expiration time is applied to the subscription
:param bool allow_existing: if True, don't raise an error if the subscription already exists
:return None:
:return octue.cloud.pub_sub.subscription.Subscription:
"""
if expiration_time:
expiration_time = float(expiration_time)
Expand All @@ -41,3 +41,4 @@ def create_push_subscription(
)

subscription.create(allow_existing=allow_existing)
return subscription
37 changes: 36 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

from octue.cli import octue_cli
from octue.cloud import storage
from octue.cloud.emulators._pub_sub import MockService, MockTopic
from octue.cloud.emulators._pub_sub import MockService, MockSubscription, MockTopic
from octue.cloud.emulators.child import ServicePatcher
from octue.cloud.events import OCTUE_SERVICES_PREFIX
from octue.cloud.pub_sub import Topic
from octue.configuration import AppConfiguration, ServiceConfiguration
from octue.resources import Dataset
from octue.utils.patches import MultiPatcher
Expand Down Expand Up @@ -423,3 +425,36 @@ def test_create_push_subscription(self):
self.assertEqual(subscription.call_args.kwargs["name"], "octue.example-service.3-5-0")
self.assertEqual(subscription.call_args.kwargs["push_endpoint"], "https://example.com/endpoint")
self.assertEqual(subscription.call_args.kwargs["expiration_time"], expected_expiration_time)
self.assertEqual(result.output, "Subscription for 'octue/example-service:3.5.0' created.\n")

def test_create_push_subscription_when_already_exists(self):
"""Test attempting to create a push subscription for a service revision when one already exists for it."""
sruid = "octue.example-service.3-5-0"
push_endpoint = "https://example.com/endpoint"

with patch("octue.cloud.pub_sub.Topic", new=MockTopic):
with patch("octue.cloud.pub_sub.Subscription", new=MockSubscription):
subscription = MockSubscription(
name=sruid,
topic=Topic(name=OCTUE_SERVICES_PREFIX, project_name="my-project"),
push_endpoint=push_endpoint,
)

subscription.create()

result = CliRunner().invoke(
octue_cli,
[
"deploy",
"create-push-subscription",
"my-project",
"octue",
"example-service",
push_endpoint,
"--revision-tag=3.5.0",
],
)

self.assertIsNone(result.exception)
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.output, "Subscription for 'octue/example-service:3.5.0' already exists.\n")

0 comments on commit 8c9b54a

Please sign in to comment.