support swarm api
This commit is contained in:
parent
957af1c3a3
commit
7cfd1a668b
4 changed files with 68 additions and 0 deletions
|
|
@ -23,6 +23,7 @@ from dockercloud.api.stack import Stack
|
||||||
from dockercloud.api.exceptions import ApiError, AuthError, ObjectNotFound, NonUniqueIdentifier
|
from dockercloud.api.exceptions import ApiError, AuthError, ObjectNotFound, NonUniqueIdentifier
|
||||||
from dockercloud.api.utils import Utils
|
from dockercloud.api.utils import Utils
|
||||||
from dockercloud.api.events import Events
|
from dockercloud.api.events import Events
|
||||||
|
from dockercloud.api.swarm import Swarm
|
||||||
from dockercloud.api.nodeaz import AZ
|
from dockercloud.api.nodeaz import AZ
|
||||||
|
|
||||||
__version__ = '1.0.9'
|
__version__ = '1.0.9'
|
||||||
|
|
|
||||||
8
dockercloud/api/swarm.py
Normal file
8
dockercloud/api/swarm.py
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from .base import Mutable
|
||||||
|
|
||||||
|
|
||||||
|
class Swarm(Mutable):
|
||||||
|
subsystem = "infra"
|
||||||
|
endpoint = "/swarm"
|
||||||
|
|
@ -9,6 +9,7 @@ from .node import Node
|
||||||
from .nodecluster import NodeCluster
|
from .nodecluster import NodeCluster
|
||||||
from .service import Service
|
from .service import Service
|
||||||
from .stack import Stack
|
from .stack import Stack
|
||||||
|
from .swarm import Swarm
|
||||||
|
|
||||||
|
|
||||||
def is_uuid4(identifier):
|
def is_uuid4(identifier):
|
||||||
|
|
@ -191,3 +192,30 @@ class Utils:
|
||||||
if not raise_exceptions:
|
if not raise_exceptions:
|
||||||
return e
|
return e
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def fetch_remote_swarm(identifier, raise_exceptions=True):
|
||||||
|
try:
|
||||||
|
terms = identifier.split("/", 1)
|
||||||
|
if len(terms) == 1:
|
||||||
|
namespace = ""
|
||||||
|
name = identifier
|
||||||
|
else:
|
||||||
|
namespace = terms[0]
|
||||||
|
name = terms[1]
|
||||||
|
try:
|
||||||
|
return Swarm.fetch(name, namespace=namespace)
|
||||||
|
except Exception:
|
||||||
|
objects_same_identifier = Swarm.list(name__startswith=name, namespace=namespace)
|
||||||
|
if len(objects_same_identifier) == 1:
|
||||||
|
swarm_id = objects_same_identifier[0].swarm_id
|
||||||
|
return Swarm.fetch(swarm_id, namespace=namespace)
|
||||||
|
elif len(objects_same_identifier) == 0:
|
||||||
|
raise ObjectNotFound("Cannot find a swarm cluster with name '%s'" % identifier)
|
||||||
|
raise NonUniqueIdentifier(
|
||||||
|
"More than one action has the same identifier, please use swarm id")
|
||||||
|
|
||||||
|
except (NonUniqueIdentifier, ObjectNotFound) as e:
|
||||||
|
if not raise_exceptions:
|
||||||
|
return e
|
||||||
|
raise e
|
||||||
|
|
|
||||||
|
|
@ -222,3 +222,34 @@ class FetchRemoteObjectTestCase(unittest.TestCase):
|
||||||
mock_list.side_effect = [ApiError, ApiError]
|
mock_list.side_effect = [ApiError, ApiError]
|
||||||
self.assertRaises(ApiError, dockercloud.Utils.fetch_remote_nodecluster, 'uuid_or_name', True)
|
self.assertRaises(ApiError, dockercloud.Utils.fetch_remote_nodecluster, 'uuid_or_name', True)
|
||||||
self.assertRaises(ApiError, dockercloud.Utils.fetch_remote_nodecluster, 'uuid_or_name', False)
|
self.assertRaises(ApiError, dockercloud.Utils.fetch_remote_nodecluster, 'uuid_or_name', False)
|
||||||
|
|
||||||
|
@mock.patch('dockercloud.Swarm.list')
|
||||||
|
@mock.patch('dockercloud.Swarm.fetch')
|
||||||
|
def test_fetch_remote_swarm(self, mock_fetch, mock_list):
|
||||||
|
# test the identifier w/ and w/o namespace
|
||||||
|
mock_fetch.return_value = 'returned'
|
||||||
|
self.assertEqual(dockercloud.Utils.fetch_remote_swarm('abc', True), 'returned')
|
||||||
|
mock_fetch.assert_called_with("abc", namespace="")
|
||||||
|
|
||||||
|
self.assertEqual(dockercloud.Utils.fetch_remote_swarm('tifayuki/abc', True), 'returned')
|
||||||
|
mock_fetch.assert_called_with("abc", namespace="tifayuki")
|
||||||
|
|
||||||
|
self.assertEqual(dockercloud.Utils.fetch_remote_swarm('tifayuki/abc/xyz', True), 'returned')
|
||||||
|
mock_fetch.assert_called_with("abc/xyz", namespace="tifayuki")
|
||||||
|
|
||||||
|
# test no swarm found
|
||||||
|
mock_fetch.side_effect = ObjectNotFound
|
||||||
|
self.assertRaises(ObjectNotFound, dockercloud.Utils.fetch_remote_swarm, 'swarm_id', True)
|
||||||
|
self.assertIsInstance(dockercloud.Utils.fetch_remote_swarm('swarm_id', False), ObjectNotFound)
|
||||||
|
|
||||||
|
# test multi-swarm found
|
||||||
|
mock_fetch.return_value = 'returned'
|
||||||
|
mock_list.side_effect = [['swarm1', 'swarm2'], []]
|
||||||
|
self.assertRaises(NonUniqueIdentifier, dockercloud.Utils.fetch_remote_swarm, 'swarm_id', True)
|
||||||
|
mock_list.side_effect = [['swarm1', 'swarm2'], []]
|
||||||
|
self.assertIsInstance(dockercloud.Utils.fetch_remote_swarm('swarm_id', False), NonUniqueIdentifier)
|
||||||
|
|
||||||
|
# test api error
|
||||||
|
mock_list.side_effect = [ApiError, ApiError]
|
||||||
|
self.assertRaises(ApiError, dockercloud.Utils.fetch_remote_swarm, 'swarm_id', True)
|
||||||
|
self.assertRaises(ApiError, dockercloud.Utils.fetch_remote_swarm, 'swarm_id', False)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue