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

Topic subscriber models #451

Merged
merged 22 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions brewtils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"Garden",
"Operation",
"Resolvable",
"TopicSubscribers",
"Subscriber",
]


Expand Down Expand Up @@ -1649,3 +1651,60 @@ def __repr__(self):
self.storage,
self.details,
)


class TopicSubscribers:
schema = "TopicSubscribersSchema"

def __init__(self, topic=None, subscribers=[]):
self.topic = topic
self.subscribers = subscribers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purely a style comment, in our other models if there is a list, we set the default to None. Then on the assign we do:

self.subscribers = subscribers or []

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


def __str__(self):
return "%s: %s" % (self.topic, [str(s) for s in self.subscribers])

def __repr__(self):
return "<Topic Subscribers: topic=%s, subscribers=%s>" % (
self.topic,
self.subscribers,
)


class Subscriber:
schema = "SubscriberSchema"

def __init__(
self,
garden=None,
namespace=None,
system=None,
version=None,
instance=None,
command=None,
):
self.garden = garden
self.namespace = namespace
self.system = system
self.version = version
self.instance = instance
self.command = command

def __str__(self):
return (
f"{self.garden}.{self.namespace}.{self.system}.{self.version}.{self.instance}."
f"{self.command}"
)

def __repr__(self):
return (
"<Subscriber: garden=%s, namespace=%s, system=%s, version=%s, instance=%s, "
"command=%s>"
% (
self.garden,
self.namespace,
self.system,
self.version,
self.instance,
self.command,
)
)
2 changes: 2 additions & 0 deletions brewtils/schema_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class SchemaParser(object):
"OperationSchema": brewtils.models.Operation,
"RunnerSchema": brewtils.models.Runner,
"ResolvableSchema": brewtils.models.Resolvable,
"SubscriberSchema": brewtils.models.Subscriber,
"TopicSubscribersSchema": brewtils.models.TopicSubscribers,
}

logger = logging.getLogger(__name__)
Expand Down
18 changes: 18 additions & 0 deletions brewtils/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
"RoleAssignmentDomainSchema",
"GardenDomainIdentifierSchema",
"SystemDomainIdentifierSchema",
"TopicSubscribersSchema",
"SubscriberSchema",
]

# This will be updated after all the schema classes are defined
Expand Down Expand Up @@ -624,6 +626,20 @@ class RoleAssignmentSchema(BaseSchema):
role = fields.Nested(RoleSchema())


class SubscriberSchema(BaseSchema):
garden = fields.Str(allow_none=True)
namespace = fields.Str(allow_none=True)
system = fields.Str(allow_none=True)
version = fields.Str(allow_none=True)
instance = fields.Str(allow_none=True)
command = fields.Str(allow_none=True)


class TopicSubscribersSchema(BaseSchema):
topic = fields.Str(allow_none=True)
subscribers = fields.List(fields.Nested(SubscriberSchema, allow_none=True))


class UserSchema(BaseSchema):
id = fields.Str()
username = fields.Str()
Expand Down Expand Up @@ -671,6 +687,8 @@ class UserListSchema(BaseSchema):
"Operation": OperationSchema,
"Runner": RunnerSchema,
"Resolvable": ResolvableSchema,
"Subscriber": SubscriberSchema,
"TopicSubscribers": TopicSubscribersSchema,
# Compatibility for the Job trigger types
"interval": IntervalTriggerSchema,
"date": DateTriggerSchema,
Expand Down
21 changes: 21 additions & 0 deletions brewtils/test/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
Resolvable,
Runner,
System,
Subscriber,
TopicSubscribers,
)


Expand Down Expand Up @@ -903,3 +905,22 @@ def resolvable_chunk_dict():
@pytest.fixture
def bg_resolvable_chunk(resolvable_chunk_dict):
return Resolvable(**resolvable_chunk_dict)


@pytest.fixture
def subscriber_dict():
"""Subscribers as a dictionary."""
return {
"garden": "garden",
"namespace": "ns",
"system": "system",
"version": "1.0.0",
"instance": None,
"command": None,
}


@pytest.fixture
def topic_subscriber_dict(subscriber_dict):
"""Topic subscribers as dict"""
return {"topic": "foo", "subscribers": [subscriber_dict]}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need both the dict and bg_models for these objects. That way it can also be evaluated in the test/schema_parser_test.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Added tests in test/schema_parser_test.py.

33 changes: 33 additions & 0 deletions test/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
RequestFile,
RequestTemplate,
LegacyRole,
Subscriber,
TopicSubscribers,
)
from pytest_lazyfixture import lazy_fixture

Expand Down Expand Up @@ -675,3 +677,34 @@ def test_repr(self, bg_resolvable):
bg_resolvable.storage,
bg_resolvable.details,
)


@pytest.fixture
def subscriber1():
return Subscriber(
garden="g", namespace="n", system="s", version="v", instance="i", command="c"
)


@pytest.fixture
def topic_subscribers1(subscriber1):
return TopicSubscribers(topic="foo.*", subscribers=[subscriber1])


class TestSubscriber(object):
def test_str(self, subscriber1):
assert "g.n.s.v.i.c" == str(subscriber1)

def test_repr(self, subscriber1):
assert "g" in repr(subscriber1)
assert "n" in repr(subscriber1)
assert "s" in repr(subscriber1)
assert "v" in repr(subscriber1)
assert "i" in repr(subscriber1)
assert "c" in repr(subscriber1)


class TestTopicSubscribers:
def test_str(self, topic_subscribers1, subscriber1):
print(str(topic_subscribers1))
assert str(topic_subscribers1) == "foo.*: ['g.n.s.v.i.c']"
Loading