Skip to content

Commit 0d0182c

Browse files
committed
Move to using an ACL model
1 parent 053196f commit 0d0182c

2 files changed

Lines changed: 49 additions & 87 deletions

File tree

consulate/api/acl.py

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55
import logging
66

7+
from consulate.models import acl
78
from consulate.api import base
89
from consulate import exceptions
910

@@ -37,10 +38,7 @@ def bootstrap(self):
3738
:raises: :exc:`~consulate.exceptions.Forbidden`
3839
3940
"""
40-
response = self._adapter.put(self._build_uri(['bootstrap']))
41-
if response.status_code == 403:
42-
raise exceptions.Forbidden(response.body)
43-
return response.body.get('ID')
41+
return self._put_response_body(['bootstrap'])['ID']
4442

4543
def create(self, name, acl_type='client', rules=None):
4644
"""The create endpoint is used to make a new token. A token has a name,
@@ -71,13 +69,9 @@ def create(self, name, acl_type='client', rules=None):
7169
:raises: consulate.exceptions.Forbidden
7270
7371
"""
74-
payload = {'Name': name, 'Type': acl_type}
75-
if rules:
76-
payload['Rules'] = rules
77-
response = self._adapter.put(self._build_uri(['create']), payload)
78-
if response.status_code == 403:
79-
raise exceptions.Forbidden(response.body)
80-
return response.body.get('ID') or None
72+
return self._put_response_body(
73+
['create'], {}, dict(acl.ACL(
74+
name=name, type=acl_type, rules=rules)))['ID']
8175

8276
def clone(self, acl_id):
8377
"""Clone an existing ACL returning the new ACL ID
@@ -87,10 +81,7 @@ def clone(self, acl_id):
8781
:raises: consulate.exceptions.Forbidden
8882
8983
"""
90-
response = self._adapter.put(self._build_uri(['clone', acl_id]))
91-
if response.status_code == 403:
92-
raise exceptions.Forbidden(response.body)
93-
return response.body.get('ID')
84+
return self._put_response_body(['clone', acl_id])['ID']
9485

9586
def destroy(self, acl_id):
9687
"""Delete the specified ACL
@@ -114,15 +105,15 @@ def info(self, acl_id):
114105
:raises: consulate.exceptions.NotFound
115106
116107
"""
117-
result = self._get(['info', acl_id])
118-
if not result:
119-
raise exceptions.NotFound()
120-
return result
108+
response = self._get(['info', acl_id], raise_on_404=True)
109+
if not response:
110+
raise exceptions.NotFound('ACL not found')
111+
return response
121112

122113
def list(self):
123114
"""Return a list of all ACLs
124115
125-
:rtype: list
116+
:rtype: list([dict])
126117
:raises: consulate.exceptions.Forbidden
127118
128119
"""
@@ -140,26 +131,22 @@ def replication(self):
140131
:raises: consulate.exceptions.Forbidden
141132
142133
"""
143-
result = self._get(['replication'])
144-
LOGGER.info('Result: %r', result)
145-
return result
134+
return self._get(['replication'])
146135

147136
def update(self, acl_id, name, acl_type='client', rules=None):
148137
"""Update an existing ACL, updating its values or add a new ACL if
149-
the ACL Id specified is not found.
138+
the ACL ID specified is not found.
139+
140+
The call will return the ID of the ACL.
150141
151142
:param str acl_id: The ACL id
152143
:param str name: The name of the ACL
153144
:param str acl_type: The ACL type
154145
:param str rules: The ACL rules document
155-
:rtype: bool
146+
:rtype: str
156147
:raises: consulate.exceptions.Forbidden
157148
158149
"""
159-
payload = {'ID': acl_id, 'Name': name, 'Type': acl_type}
160-
if rules:
161-
payload['Rules'] = rules
162-
response = self._adapter.put(self._build_uri(['update']), payload)
163-
if response.status_code == 403:
164-
raise exceptions.Forbidden(response.body)
165-
return response.status_code == 200
150+
return self._put_response_body(
151+
['update'], {}, dict(acl.ACL(
152+
id=acl_id, name=name, type=acl_type, rules=rules)))['ID']

tests/acl_tests.py

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@
2020

2121

2222
class TestCase(base.TestCase):
23-
def setUp(self):
24-
super(TestCase, self).setUp()
25-
self.acl_list = list()
2623

27-
def tearDown(self):
28-
for acl_id in self.acl_list:
29-
self.consul.acl.destroy(acl_id)
24+
@staticmethod
25+
def uuidv4():
26+
return str(uuid.uuid4())
3027

3128
def test_bootstrap_success(self):
32-
expectation = str(uuid.uuid4())
29+
expectation = self.uuidv4()
3330

3431
@httmock.all_requests
3532
def response_content(_url_unused, request):
@@ -46,97 +43,75 @@ def test_bootstrap_raises(self):
4643
self.consul.acl.bootstrap()
4744

4845
def test_clone_bad_acl_id(self):
49-
acl_id = str(uuid.uuid4())
5046
with self.assertRaises(consulate.Forbidden):
51-
self.forbidden_consul.acl.clone(acl_id)
47+
self.consul.acl.clone(self.uuidv4())
5248

53-
@base.generate_key
54-
def test_clone_forbidden(self, acl_id):
49+
def test_clone_forbidden(self):
5550
with self.assertRaises(consulate.Forbidden):
56-
self.forbidden_consul.acl.clone(acl_id)
51+
self.forbidden_consul.acl.clone(self.uuidv4())
5752

58-
@base.generate_key
59-
def test_create_and_destroy(self, acl_id):
60-
acl_id = self.consul.acl.create(acl_id)
61-
self.acl_list.append(acl_id)
53+
def test_create_and_destroy(self):
54+
acl_id = self.consul.acl.create(self.uuidv4())
6255
self.assertTrue(self.consul.acl.destroy(acl_id))
6356

64-
@base.generate_key
65-
def test_create_with_rules(self, acl_id):
66-
acl_id = self.consul.acl.create(acl_id, rules=ACL_RULES)
67-
self.acl_list.append(acl_id)
57+
def test_create_with_rules(self):
58+
acl_id = self.consul.acl.create(self.uuidv4(), rules=ACL_RULES)
6859
value = self.consul.acl.info(acl_id)
6960
self.assertEqual(value['Rules'], ACL_RULES)
7061

71-
@base.generate_key
72-
def test_create_and_info(self, acl_id):
73-
acl_id = self.consul.acl.create(acl_id)
74-
self.acl_list.append(acl_id)
62+
def test_create_and_info(self):
63+
acl_id = self.consul.acl.create(self.uuidv4())
7564
self.assertIsNotNone(acl_id)
7665
data = self.consul.acl.info(acl_id)
7766
self.assertIsNotNone(data)
7867
self.assertEqual(acl_id, data.get('ID'))
7968

80-
@base.generate_key
81-
def test_create_and_list(self, acl_id):
82-
acl_id = self.consul.acl.create(acl_id)
83-
self.acl_list.append(acl_id)
69+
def test_create_and_list(self):
70+
acl_id = self.consul.acl.create(self.uuidv4())
8471
data = self.consul.acl.list()
8572
self.assertIn(acl_id, [r.get('ID') for r in data])
8673

87-
@base.generate_key
88-
def test_create_and_clone(self, acl_id):
89-
acl_id = self.consul.acl.create(acl_id)
90-
self.acl_list.append(acl_id)
74+
def test_create_and_clone(self):
75+
acl_id = self.consul.acl.create(self.uuidv4())
9176
clone_id = self.consul.acl.clone(acl_id)
92-
self.acl_list.append(clone_id)
9377
data = self.consul.acl.list()
9478
self.assertIn(clone_id, [r.get('ID') for r in data])
9579

96-
@base.generate_key
97-
def test_create_and_update(self, acl_id):
98-
acl_id = self.consul.acl.create(acl_id)
99-
self.acl_list.append(acl_id)
80+
def test_create_and_update(self):
81+
acl_id = self.consul.acl.create(self.uuidv4())
10082
self.consul.acl.update(acl_id, 'Foo')
10183
data = self.consul.acl.list()
10284
self.assertIn('Foo', [r.get('Name') for r in data])
10385
self.assertIn(acl_id, [r.get('ID') for r in data])
10486

105-
@base.generate_key
106-
def test_create_forbidden(self, acl_id):
87+
def test_create_forbidden(self):
10788
with self.assertRaises(consulate.Forbidden):
108-
self.forbidden_consul.acl.create(acl_id)
89+
self.forbidden_consul.acl.create(self.uuidv4())
10990

110-
@base.generate_key
111-
def test_destroy_forbidden(self, acl_id):
91+
def test_destroy_forbidden(self):
11292
with self.assertRaises(consulate.Forbidden):
113-
self.forbidden_consul.acl.destroy(acl_id)
93+
self.forbidden_consul.acl.destroy(self.uuidv4())
11494

11595
def test_info_acl_id_not_found(self):
116-
acl_id = str(uuid.uuid4())
11796
with self.assertRaises(consulate.NotFound):
118-
self.forbidden_consul.acl.info(acl_id)
97+
self.forbidden_consul.acl.info(self.uuidv4())
11998

12099
def test_replication(self):
121100
result = self.forbidden_consul.acl.replication()
122101
self.assertFalse(result['Enabled'])
123102
self.assertFalse(result['Running'])
124103

125-
@base.generate_key
126-
def test_update_not_found_adds_new_key(self, acl_id):
127-
self.assertTrue(self.consul.acl.update(acl_id, 'Foo2'))
104+
def test_update_not_found_adds_new_key(self):
105+
acl_id = self.consul.acl.update(self.uuidv4(), 'Foo2')
128106
data = self.consul.acl.list()
129107
self.assertIn('Foo2', [r.get('Name') for r in data])
130108
self.assertIn(acl_id, [r.get('ID') for r in data])
131109

132-
@base.generate_key
133-
def test_update_with_rules(self, acl_id):
134-
self.consul.acl.update(acl_id, name='test', rules=ACL_RULES)
135-
self.acl_list.append(acl_id)
110+
def test_update_with_rules(self):
111+
acl_id = self.consul.acl.update(self.uuidv4(), name='test', rules=ACL_RULES)
136112
value = self.consul.acl.info(acl_id)
137113
self.assertEqual(value['Rules'], ACL_RULES)
138114

139-
@base.generate_key
140-
def test_update_forbidden(self, acl_id):
115+
def test_update_forbidden(self):
141116
with self.assertRaises(consulate.Forbidden):
142-
self.forbidden_consul.acl.update(acl_id, name='test')
117+
self.forbidden_consul.acl.update(self.uuidv4(), name='test')

0 commit comments

Comments
 (0)