support swarm api

This commit is contained in:
tifayuki 2016-12-22 15:02:11 -08:00
commit 7cfd1a668b
4 changed files with 68 additions and 0 deletions

View file

@ -23,6 +23,7 @@ from dockercloud.api.stack import Stack
from dockercloud.api.exceptions import ApiError, AuthError, ObjectNotFound, NonUniqueIdentifier
from dockercloud.api.utils import Utils
from dockercloud.api.events import Events
from dockercloud.api.swarm import Swarm
from dockercloud.api.nodeaz import AZ
__version__ = '1.0.9'

8
dockercloud/api/swarm.py Normal file
View file

@ -0,0 +1,8 @@
from __future__ import absolute_import
from .base import Mutable
class Swarm(Mutable):
subsystem = "infra"
endpoint = "/swarm"

View file

@ -9,6 +9,7 @@ from .node import Node
from .nodecluster import NodeCluster
from .service import Service
from .stack import Stack
from .swarm import Swarm
def is_uuid4(identifier):
@ -191,3 +192,30 @@ class Utils:
if not raise_exceptions:
return 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