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

Commit 648d107

Browse files
author
Feng Honglin
authored
Merge pull request #11 from docker/staging
v1.0.6
2 parents ac5e672 + 13a8bec commit 648d107

File tree

8 files changed

+55
-8
lines changed

8 files changed

+55
-8
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ The authentication can be configured in the following ways:
2929
export DOCKERCLOUD_USER=username
3030
export DOCKERCLOUD_APIKEY=apikey
3131

32+
## Namespace
33+
34+
To support teams and orgs, you can specify the namespace in the following ways:
35+
36+
* Set it in the Python code:
37+
38+
import dockercloud
39+
dockercloud.namespace = "yourteam"
40+
41+
* Set it in the environment variable:
42+
43+
export DOCKERCLOUD_NAMESPACE=yourteam
44+
3245
## Errors
3346

3447
Errors in the HTTP API will be returned with status codes in the 4xx and 5xx ranges.

dockercloud/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from dockercloud.api.events import Events
2626
from dockercloud.api.nodeaz import AZ
2727

28-
__version__ = '1.0.5'
28+
__version__ = '1.0.6'
2929

3030
dockercloud_auth = os.environ.get('DOCKERCLOUD_AUTH')
3131
basic_auth = auth.load_from_file("~/.docker/config.json")
@@ -38,6 +38,8 @@
3838
rest_host = os.environ.get("DOCKERCLOUD_REST_HOST") or 'https://cloud.docker.com/'
3939
stream_host = os.environ.get("DOCKERCLOUD_STREAM_HOST") or 'wss://ws.cloud.docker.com/'
4040

41+
namespace = os.environ.get('DOCKERCLOUD_NAMESPACE')
42+
4143
user_agent = None
4244

4345
logging.basicConfig()

dockercloud/api/base.py

+29-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(self, **kwargs):
2222

2323
class Restful(BasicObject):
2424
_detail_uri = None
25+
namespaced = True
2526

2627
def __init__(self, **kwargs):
2728
"""Simply reflect all the values in kwargs"""
@@ -58,7 +59,11 @@ def _loaddict(self, dict):
5859
assert subsystem, "Subsystem not specified for %s" % self.__class__.__name__
5960
for k, v in list(dict.items()):
6061
setattr(self, k, v)
61-
self._detail_uri = "/".join(["api", subsystem, self._api_version, endpoint.strip("/"), self.pk])
62+
if self.namespaced and dockercloud.namespace:
63+
self._detail_uri = "/".join(["api", subsystem, self._api_version, dockercloud.namespace,
64+
endpoint.strip("/"), self.pk])
65+
else:
66+
self._detail_uri = "/".join(["api", subsystem, self._api_version, endpoint.strip("/"), self.pk])
6267
self.__setchanges__([])
6368

6469
@property
@@ -126,7 +131,10 @@ def fetch(cls, pk):
126131
subsystem = getattr(cls, 'subsystem', None)
127132
assert endpoint, "Endpoint not specified for %s" % cls.__name__
128133
assert subsystem, "Subsystem not specified for %s" % cls.__name__
129-
detail_uri = "/".join(["api", subsystem, cls._api_version, endpoint.strip("/"), pk])
134+
if cls.namespaced and dockercloud.namespace:
135+
detail_uri = "/".join(["api", subsystem, cls._api_version, dockercloud.namespace, endpoint.strip("/"), pk])
136+
else:
137+
detail_uri = "/".join(["api", subsystem, cls._api_version, endpoint.strip("/"), pk])
130138
json = send_request('GET', detail_uri)
131139
if json:
132140
instance = cls()
@@ -141,7 +149,10 @@ def list(cls, limit=None, **kwargs):
141149
assert endpoint, "Endpoint not specified for %s" % cls.__name__
142150
assert subsystem, "Subsystem not specified for %s" % cls.__name__
143151

144-
detail_uri = "/".join(["api", subsystem, cls._api_version, endpoint.strip("/")])
152+
if cls.namespaced and dockercloud.namespace:
153+
detail_uri = "/".join(["api", subsystem, cls._api_version, dockercloud.namespace, endpoint.strip("/")])
154+
else:
155+
detail_uri = "/".join(["api", subsystem, cls._api_version, endpoint.strip("/")])
145156
objects = []
146157
while True:
147158
if limit and len(objects) >= limit:
@@ -219,7 +230,10 @@ def save(self):
219230
# Figure out whether we should do a create or update
220231
if not self._detail_uri:
221232
action = "POST"
222-
path = "/".join(["api", subsystem, self._api_version, endpoint.lstrip("/")])
233+
if cls.namespaced and dockercloud.namespace:
234+
path = "/".join(["api", subsystem, self._api_version, dockercloud.namespace, endpoint.lstrip("/")])
235+
else:
236+
path = "/".join(["api", subsystem, self._api_version, endpoint.lstrip("/")])
223237
else:
224238
action = "PATCH"
225239
path = self._detail_uri
@@ -316,7 +330,12 @@ def __init__(self, subsystem, resource, uuid, tail, follow):
316330
endpoint = "%s/%s/logs/?follow=%s" % (resource, uuid, str(follow).lower())
317331
if tail:
318332
endpoint = "%s&tail=%d" % (endpoint, tail)
319-
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", subsystem, self._api_version, endpoint.lstrip("/")])
333+
if dockercloud.namespace:
334+
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", subsystem, self._api_version,
335+
dockercloud.namespace, endpoint.lstrip("/")])
336+
else:
337+
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", subsystem, self._api_version,
338+
endpoint.lstrip("/")])
320339
super(self.__class__, self).__init__(url)
321340

322341
@staticmethod
@@ -335,7 +354,11 @@ def run_forever(self, *args, **kwargs):
335354
class Exec(StreamingAPI):
336355
def __init__(self, uuid, cmd='sh'):
337356
endpoint = "container/%s/exec/?command=%s" % (uuid, urllib.quote_plus(cmd))
338-
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", "app", self._api_version, endpoint.lstrip("/")])
357+
if dockercloud.namespace:
358+
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", "app", self._api_version,
359+
dockercloud.namespace, endpoint.lstrip("/")])
360+
else:
361+
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", "app", self._api_version, endpoint.lstrip("/")])
339362
super(self.__class__, self).__init__(url)
340363

341364
@staticmethod

dockercloud/api/events.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
class Events(StreamingAPI):
1616
def __init__(self):
1717
endpoint = "events"
18-
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", "audit", self._api_version, endpoint.lstrip("/")])
18+
if dockercloud.namespace:
19+
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", "audit", self._api_version,
20+
dockercloud.namespace, endpoint.lstrip("/")])
21+
else:
22+
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", "audit", self._api_version,
23+
endpoint.lstrip("/")])
1924
self.invaid_auth_headers = set()
2025
self.auth_error = ""
2126
super(self.__class__, self).__init__(url)

dockercloud/api/nodeaz.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class AZ(Immutable):
77
subsystem = "infra"
88
endpoint = "/az"
9+
namespaced = False
910

1011
@classmethod
1112
def _pk_key(cls):

dockercloud/api/nodeprovider.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class Provider(Immutable):
77
subsystem = "infra"
88
endpoint = "/provider"
9+
namespaced = False
910

1011
@classmethod
1112
def _pk_key(cls):

dockercloud/api/noderegion.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class Region(Immutable):
77
subsystem = "infra"
88
endpoint = "/region"
9+
namespaced = False
910

1011
@classmethod
1112
def _pk_key(cls):

dockercloud/api/nodetype.py

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class NodeType(Immutable):
77
subsystem = "infra"
88
endpoint = "/nodetype"
9+
namespaced = False
910

1011
@classmethod
1112
def _pk_key(cls):

0 commit comments

Comments
 (0)