Skip to content

Commit bc25877

Browse files
TylerLubeckdpkp
authored andcommitted
Implement __eq__ and __hash__ for ACL objects (#1955)
1 parent 5c477f2 commit bc25877

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

kafka/admin/acl_resource.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ def __repr__(self):
112112
resource=self.resource_pattern
113113
)
114114

115+
def __eq__(self, other):
116+
return all((
117+
self.principal == other.principal,
118+
self.host == other.host,
119+
self.operation == other.operation,
120+
self.permission_type == other.permission_type,
121+
self.resource_pattern == other.resource_pattern
122+
))
123+
124+
def __hash__(self):
125+
return hash((
126+
self.principal,
127+
self.host,
128+
self.operation,
129+
self.permission_type,
130+
self.resource_pattern,
131+
))
132+
115133

116134
class ACL(ACLFilter):
117135
"""Represents a concrete ACL for a specific ResourcePattern
@@ -181,6 +199,20 @@ def __repr__(self):
181199
self.pattern_type.name
182200
)
183201

202+
def __eq__(self, other):
203+
return all((
204+
self.resource_type == other.resource_type,
205+
self.resource_name == other.resource_name,
206+
self.pattern_type == other.pattern_type,
207+
))
208+
209+
def __hash__(self):
210+
return hash((
211+
self.resource_type,
212+
self.resource_name,
213+
self.pattern_type
214+
))
215+
184216

185217
class ResourcePattern(ResourcePatternFilter):
186218
"""A resource pattern to apply the ACL to
@@ -209,4 +241,4 @@ def validate(self):
209241
if self.pattern_type in [ACLResourcePatternType.ANY, ACLResourcePatternType.MATCH]:
210242
raise IllegalArgumentError(
211243
"pattern_type cannot be {} on a concrete ResourcePattern".format(self.pattern_type.name)
212-
)
244+
)

test/test_acl_comparisons.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from kafka.admin.acl_resource import ACL
2+
from kafka.admin.acl_resource import ACLOperation
3+
from kafka.admin.acl_resource import ACLPermissionType
4+
from kafka.admin.acl_resource import ResourcePattern
5+
from kafka.admin.acl_resource import ResourceType
6+
from kafka.admin.acl_resource import ACLResourcePatternType
7+
8+
9+
def test_different_acls_are_different():
10+
one = ACL(
11+
principal='User:A',
12+
host='*',
13+
operation=ACLOperation.ALL,
14+
permission_type=ACLPermissionType.ALLOW,
15+
resource_pattern=ResourcePattern(
16+
resource_type=ResourceType.TOPIC,
17+
resource_name='some-topic',
18+
pattern_type=ACLResourcePatternType.LITERAL
19+
)
20+
)
21+
22+
two = ACL(
23+
principal='User:B', # Different principal
24+
host='*',
25+
operation=ACLOperation.ALL,
26+
permission_type=ACLPermissionType.ALLOW,
27+
resource_pattern=ResourcePattern(
28+
resource_type=ResourceType.TOPIC,
29+
resource_name='some-topic',
30+
pattern_type=ACLResourcePatternType.LITERAL
31+
)
32+
)
33+
34+
assert one != two
35+
assert hash(one) != hash(two)
36+
37+
def test_different_acls_are_different_with_glob_topics():
38+
one = ACL(
39+
principal='User:A',
40+
host='*',
41+
operation=ACLOperation.ALL,
42+
permission_type=ACLPermissionType.ALLOW,
43+
resource_pattern=ResourcePattern(
44+
resource_type=ResourceType.TOPIC,
45+
resource_name='*',
46+
pattern_type=ACLResourcePatternType.LITERAL
47+
)
48+
)
49+
50+
two = ACL(
51+
principal='User:B', # Different principal
52+
host='*',
53+
operation=ACLOperation.ALL,
54+
permission_type=ACLPermissionType.ALLOW,
55+
resource_pattern=ResourcePattern(
56+
resource_type=ResourceType.TOPIC,
57+
resource_name='*',
58+
pattern_type=ACLResourcePatternType.LITERAL
59+
)
60+
)
61+
62+
assert one != two
63+
assert hash(one) != hash(two)
64+
65+
def test_same_acls_are_same():
66+
one = ACL(
67+
principal='User:A',
68+
host='*',
69+
operation=ACLOperation.ALL,
70+
permission_type=ACLPermissionType.ALLOW,
71+
resource_pattern=ResourcePattern(
72+
resource_type=ResourceType.TOPIC,
73+
resource_name='some-topic',
74+
pattern_type=ACLResourcePatternType.LITERAL
75+
)
76+
)
77+
78+
two = ACL(
79+
principal='User:A',
80+
host='*',
81+
operation=ACLOperation.ALL,
82+
permission_type=ACLPermissionType.ALLOW,
83+
resource_pattern=ResourcePattern(
84+
resource_type=ResourceType.TOPIC,
85+
resource_name='some-topic',
86+
pattern_type=ACLResourcePatternType.LITERAL
87+
)
88+
)
89+
90+
assert one == two
91+
assert hash(one) == hash(two)
92+
assert len(set((one, two))) == 1

0 commit comments

Comments
 (0)