Skip to content

Commit aa3c4f0

Browse files
committed
Add unlock_swarm and get_unlock_key to APIClient
Signed-off-by: Joffrey F <[email protected]>
1 parent f238fe5 commit aa3c4f0

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

docker/api/swarm.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import logging
22
from six.moves import http_client
3+
from .. import errors
34
from .. import types
45
from .. import utils
6+
57
log = logging.getLogger(__name__)
68

79

@@ -68,6 +70,16 @@ def create_swarm_spec(self, *args, **kwargs):
6870
kwargs['external_cas'] = [ext_ca]
6971
return types.SwarmSpec(self._version, *args, **kwargs)
7072

73+
@utils.minimum_version('1.24')
74+
def get_unlock_key(self):
75+
"""
76+
Get the unlock key for this Swarm manager.
77+
78+
Returns:
79+
A ``dict`` containing an ``UnlockKey`` member
80+
"""
81+
return self._result(self._get(self._url('/swarm/unlockkey')), True)
82+
7183
@utils.minimum_version('1.24')
7284
def init_swarm(self, advertise_addr=None, listen_addr='0.0.0.0:2377',
7385
force_new_cluster=False, swarm_spec=None):
@@ -270,10 +282,46 @@ def remove_node(self, node_id, force=False):
270282
self._raise_for_status(res)
271283
return True
272284

285+
@utils.minimum_version('1.24')
286+
def unlock_swarm(self, key):
287+
"""
288+
Unlock a locked swarm.
289+
290+
Args:
291+
key (string): The unlock key as provided by
292+
:py:meth:`get_unlock_key`
293+
294+
Raises:
295+
:py:class:`docker.errors.InvalidArgument`
296+
If the key argument is in an incompatible format
297+
298+
:py:class:`docker.errors.APIError`
299+
If the server returns an error.
300+
301+
Returns:
302+
`True` if the request was successful.
303+
304+
Example:
305+
306+
>>> key = client.get_unlock_key()
307+
>>> client.unlock_node(key)
308+
309+
"""
310+
if isinstance(key, dict):
311+
if 'UnlockKey' not in key:
312+
raise errors.InvalidArgument('Invalid unlock key format')
313+
else:
314+
key = {'UnlockKey': key}
315+
316+
url = self._url('/swarm/unlock')
317+
res = self._post_json(url, data=key)
318+
self._raise_for_status(res)
319+
return True
320+
273321
@utils.minimum_version('1.24')
274322
def update_node(self, node_id, version, node_spec=None):
275323
"""
276-
Update the Node's configuration
324+
Update the node's configuration
277325
278326
Args:
279327

tests/integration/api_swarm_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ def setUp(self):
1313

1414
def tearDown(self):
1515
super(SwarmTest, self).tearDown()
16+
try:
17+
unlock_key = self.client.get_unlock_key()
18+
if unlock_key.get('UnlockKey'):
19+
self.unlock_swarm(unlock_key)
20+
except docker.errors.APIError:
21+
pass
22+
1623
force_leave_swarm(self.client)
1724

1825
@requires_api_version('1.24')
@@ -70,6 +77,9 @@ def test_init_swarm_with_autolock_managers(self):
7077
swarm_info['Spec']['EncryptionConfig']['AutoLockManagers'] is True
7178
)
7279

80+
unlock_key = self.get_unlock_key()
81+
assert unlock_key.get('UnlockKey')
82+
7383
@requires_api_version('1.25')
7484
@pytest.mark.xfail(
7585
reason="This doesn't seem to be taken into account by the engine"

0 commit comments

Comments
 (0)