Skip to content
This repository was archived by the owner on Jan 20, 2020. It is now read-only.

Commit 7cfd1a6

Browse files
committed
support swarm api
1 parent 957af1c commit 7cfd1a6

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

dockercloud/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from dockercloud.api.exceptions import ApiError, AuthError, ObjectNotFound, NonUniqueIdentifier
2424
from dockercloud.api.utils import Utils
2525
from dockercloud.api.events import Events
26+
from dockercloud.api.swarm import Swarm
2627
from dockercloud.api.nodeaz import AZ
2728

2829
__version__ = '1.0.9'

dockercloud/api/swarm.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from __future__ import absolute_import
2+
3+
from .base import Mutable
4+
5+
6+
class Swarm(Mutable):
7+
subsystem = "infra"
8+
endpoint = "/swarm"

dockercloud/api/utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .nodecluster import NodeCluster
1010
from .service import Service
1111
from .stack import Stack
12+
from .swarm import Swarm
1213

1314

1415
def is_uuid4(identifier):
@@ -191,3 +192,30 @@ def fetch_remote_action(identifier, raise_exceptions=True):
191192
if not raise_exceptions:
192193
return e
193194
raise e
195+
196+
@staticmethod
197+
def fetch_remote_swarm(identifier, raise_exceptions=True):
198+
try:
199+
terms = identifier.split("/", 1)
200+
if len(terms) == 1:
201+
namespace = ""
202+
name = identifier
203+
else:
204+
namespace = terms[0]
205+
name = terms[1]
206+
try:
207+
return Swarm.fetch(name, namespace=namespace)
208+
except Exception:
209+
objects_same_identifier = Swarm.list(name__startswith=name, namespace=namespace)
210+
if len(objects_same_identifier) == 1:
211+
swarm_id = objects_same_identifier[0].swarm_id
212+
return Swarm.fetch(swarm_id, namespace=namespace)
213+
elif len(objects_same_identifier) == 0:
214+
raise ObjectNotFound("Cannot find a swarm cluster with name '%s'" % identifier)
215+
raise NonUniqueIdentifier(
216+
"More than one action has the same identifier, please use swarm id")
217+
218+
except (NonUniqueIdentifier, ObjectNotFound) as e:
219+
if not raise_exceptions:
220+
return e
221+
raise e

tests/test_utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,34 @@ def test_fetch_remote_nodecluster(self, mock_fetch, mock_list):
222222
mock_list.side_effect = [ApiError, ApiError]
223223
self.assertRaises(ApiError, dockercloud.Utils.fetch_remote_nodecluster, 'uuid_or_name', True)
224224
self.assertRaises(ApiError, dockercloud.Utils.fetch_remote_nodecluster, 'uuid_or_name', False)
225+
226+
@mock.patch('dockercloud.Swarm.list')
227+
@mock.patch('dockercloud.Swarm.fetch')
228+
def test_fetch_remote_swarm(self, mock_fetch, mock_list):
229+
# test the identifier w/ and w/o namespace
230+
mock_fetch.return_value = 'returned'
231+
self.assertEqual(dockercloud.Utils.fetch_remote_swarm('abc', True), 'returned')
232+
mock_fetch.assert_called_with("abc", namespace="")
233+
234+
self.assertEqual(dockercloud.Utils.fetch_remote_swarm('tifayuki/abc', True), 'returned')
235+
mock_fetch.assert_called_with("abc", namespace="tifayuki")
236+
237+
self.assertEqual(dockercloud.Utils.fetch_remote_swarm('tifayuki/abc/xyz', True), 'returned')
238+
mock_fetch.assert_called_with("abc/xyz", namespace="tifayuki")
239+
240+
# test no swarm found
241+
mock_fetch.side_effect = ObjectNotFound
242+
self.assertRaises(ObjectNotFound, dockercloud.Utils.fetch_remote_swarm, 'swarm_id', True)
243+
self.assertIsInstance(dockercloud.Utils.fetch_remote_swarm('swarm_id', False), ObjectNotFound)
244+
245+
# test multi-swarm found
246+
mock_fetch.return_value = 'returned'
247+
mock_list.side_effect = [['swarm1', 'swarm2'], []]
248+
self.assertRaises(NonUniqueIdentifier, dockercloud.Utils.fetch_remote_swarm, 'swarm_id', True)
249+
mock_list.side_effect = [['swarm1', 'swarm2'], []]
250+
self.assertIsInstance(dockercloud.Utils.fetch_remote_swarm('swarm_id', False), NonUniqueIdentifier)
251+
252+
# test api error
253+
mock_list.side_effect = [ApiError, ApiError]
254+
self.assertRaises(ApiError, dockercloud.Utils.fetch_remote_swarm, 'swarm_id', True)
255+
self.assertRaises(ApiError, dockercloud.Utils.fetch_remote_swarm, 'swarm_id', False)

0 commit comments

Comments
 (0)