Skip to content

Implement __eq__ and __hash__ for ACL objects #1955

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

Merged
merged 2 commits into from
Dec 29, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
34 changes: 33 additions & 1 deletion kafka/admin/acl_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ def __repr__(self):
resource=self.resource_pattern
)

def __eq__(self, other):
return all((
self.principal == other.principal,
self.host == other.host,
self.operation == other.operation,
self.permission_type == other.permission_type,
self.resource_pattern == other.resource_pattern
))

def __hash__(self):
return hash((
self.principal,
self.host,
self.operation,
self.permission_type,
self.resource_pattern,
))


class ACL(ACLFilter):
"""Represents a concrete ACL for a specific ResourcePattern
Expand Down Expand Up @@ -181,6 +199,20 @@ def __repr__(self):
self.pattern_type.name
)

def __eq__(self, other):
return all((
self.resource_type == other.resource_type,
self.resource_name == other.resource_name,
self.pattern_type == other.pattern_type,
))

def __hash__(self):
return hash((
self.resource_type,
self.resource_name,
self.pattern_type
))


class ResourcePattern(ResourcePatternFilter):
"""A resource pattern to apply the ACL to
Expand Down Expand Up @@ -209,4 +241,4 @@ def validate(self):
if self.pattern_type in [ACLResourcePatternType.ANY, ACLResourcePatternType.MATCH]:
raise IllegalArgumentError(
"pattern_type cannot be {} on a concrete ResourcePattern".format(self.pattern_type.name)
)
)
92 changes: 92 additions & 0 deletions test/test_acl_comparisons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from kafka.admin.acl_resource import ACL
from kafka.admin.acl_resource import ACLOperation
from kafka.admin.acl_resource import ACLPermissionType
from kafka.admin.acl_resource import ResourcePattern
from kafka.admin.acl_resource import ResourceType
from kafka.admin.acl_resource import ACLResourcePatternType


def test_different_acls_are_different():
one = ACL(
principal='User:A',
host='*',
operation=ACLOperation.ALL,
permission_type=ACLPermissionType.ALLOW,
resource_pattern=ResourcePattern(
resource_type=ResourceType.TOPIC,
resource_name='some-topic',
pattern_type=ACLResourcePatternType.LITERAL
)
)

two = ACL(
principal='User:B', # Different principal
host='*',
operation=ACLOperation.ALL,
permission_type=ACLPermissionType.ALLOW,
resource_pattern=ResourcePattern(
resource_type=ResourceType.TOPIC,
resource_name='some-topic',
pattern_type=ACLResourcePatternType.LITERAL
)
)

assert one != two
assert hash(one) != hash(two)

def test_different_acls_are_different_with_glob_topics():
one = ACL(
principal='User:A',
host='*',
operation=ACLOperation.ALL,
permission_type=ACLPermissionType.ALLOW,
resource_pattern=ResourcePattern(
resource_type=ResourceType.TOPIC,
resource_name='*',
pattern_type=ACLResourcePatternType.LITERAL
)
)

two = ACL(
principal='User:B', # Different principal
host='*',
operation=ACLOperation.ALL,
permission_type=ACLPermissionType.ALLOW,
resource_pattern=ResourcePattern(
resource_type=ResourceType.TOPIC,
resource_name='*',
pattern_type=ACLResourcePatternType.LITERAL
)
)

assert one != two
assert hash(one) != hash(two)

def test_same_acls_are_same():
one = ACL(
principal='User:A',
host='*',
operation=ACLOperation.ALL,
permission_type=ACLPermissionType.ALLOW,
resource_pattern=ResourcePattern(
resource_type=ResourceType.TOPIC,
resource_name='some-topic',
pattern_type=ACLResourcePatternType.LITERAL
)
)

two = ACL(
principal='User:A',
host='*',
operation=ACLOperation.ALL,
permission_type=ACLPermissionType.ALLOW,
resource_pattern=ResourcePattern(
resource_type=ResourceType.TOPIC,
resource_name='some-topic',
pattern_type=ACLResourcePatternType.LITERAL
)
)

assert one == two
assert hash(one) == hash(two)
assert len(set((one, two))) == 1