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

Commit

Permalink
Merge pull request #11 from docker/staging
Browse files Browse the repository at this point in the history
v1.0.6
  • Loading branch information
Feng Honglin authored Jun 13, 2016
2 parents ac5e672 + 13a8bec commit 648d107
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 8 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ The authentication can be configured in the following ways:
export DOCKERCLOUD_USER=username
export DOCKERCLOUD_APIKEY=apikey

## Namespace

To support teams and orgs, you can specify the namespace in the following ways:

* Set it in the Python code:

import dockercloud
dockercloud.namespace = "yourteam"

* Set it in the environment variable:

export DOCKERCLOUD_NAMESPACE=yourteam

## Errors

Errors in the HTTP API will be returned with status codes in the 4xx and 5xx ranges.
Expand Down
4 changes: 3 additions & 1 deletion dockercloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from dockercloud.api.events import Events
from dockercloud.api.nodeaz import AZ

__version__ = '1.0.5'
__version__ = '1.0.6'

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

namespace = os.environ.get('DOCKERCLOUD_NAMESPACE')

user_agent = None

logging.basicConfig()
Expand Down
35 changes: 29 additions & 6 deletions dockercloud/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, **kwargs):

class Restful(BasicObject):
_detail_uri = None
namespaced = True

def __init__(self, **kwargs):
"""Simply reflect all the values in kwargs"""
Expand Down Expand Up @@ -58,7 +59,11 @@ def _loaddict(self, dict):
assert subsystem, "Subsystem not specified for %s" % self.__class__.__name__
for k, v in list(dict.items()):
setattr(self, k, v)
self._detail_uri = "/".join(["api", subsystem, self._api_version, endpoint.strip("/"), self.pk])
if self.namespaced and dockercloud.namespace:
self._detail_uri = "/".join(["api", subsystem, self._api_version, dockercloud.namespace,
endpoint.strip("/"), self.pk])
else:
self._detail_uri = "/".join(["api", subsystem, self._api_version, endpoint.strip("/"), self.pk])
self.__setchanges__([])

@property
Expand Down Expand Up @@ -126,7 +131,10 @@ def fetch(cls, pk):
subsystem = getattr(cls, 'subsystem', None)
assert endpoint, "Endpoint not specified for %s" % cls.__name__
assert subsystem, "Subsystem not specified for %s" % cls.__name__
detail_uri = "/".join(["api", subsystem, cls._api_version, endpoint.strip("/"), pk])
if cls.namespaced and dockercloud.namespace:
detail_uri = "/".join(["api", subsystem, cls._api_version, dockercloud.namespace, endpoint.strip("/"), pk])
else:
detail_uri = "/".join(["api", subsystem, cls._api_version, endpoint.strip("/"), pk])
json = send_request('GET', detail_uri)
if json:
instance = cls()
Expand All @@ -141,7 +149,10 @@ def list(cls, limit=None, **kwargs):
assert endpoint, "Endpoint not specified for %s" % cls.__name__
assert subsystem, "Subsystem not specified for %s" % cls.__name__

detail_uri = "/".join(["api", subsystem, cls._api_version, endpoint.strip("/")])
if cls.namespaced and dockercloud.namespace:
detail_uri = "/".join(["api", subsystem, cls._api_version, dockercloud.namespace, endpoint.strip("/")])
else:
detail_uri = "/".join(["api", subsystem, cls._api_version, endpoint.strip("/")])
objects = []
while True:
if limit and len(objects) >= limit:
Expand Down Expand Up @@ -219,7 +230,10 @@ def save(self):
# Figure out whether we should do a create or update
if not self._detail_uri:
action = "POST"
path = "/".join(["api", subsystem, self._api_version, endpoint.lstrip("/")])
if cls.namespaced and dockercloud.namespace:
path = "/".join(["api", subsystem, self._api_version, dockercloud.namespace, endpoint.lstrip("/")])
else:
path = "/".join(["api", subsystem, self._api_version, endpoint.lstrip("/")])
else:
action = "PATCH"
path = self._detail_uri
Expand Down Expand Up @@ -316,7 +330,12 @@ def __init__(self, subsystem, resource, uuid, tail, follow):
endpoint = "%s/%s/logs/?follow=%s" % (resource, uuid, str(follow).lower())
if tail:
endpoint = "%s&tail=%d" % (endpoint, tail)
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", subsystem, self._api_version, endpoint.lstrip("/")])
if dockercloud.namespace:
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", subsystem, self._api_version,
dockercloud.namespace, endpoint.lstrip("/")])
else:
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", subsystem, self._api_version,
endpoint.lstrip("/")])
super(self.__class__, self).__init__(url)

@staticmethod
Expand All @@ -335,7 +354,11 @@ def run_forever(self, *args, **kwargs):
class Exec(StreamingAPI):
def __init__(self, uuid, cmd='sh'):
endpoint = "container/%s/exec/?command=%s" % (uuid, urllib.quote_plus(cmd))
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", "app", self._api_version, endpoint.lstrip("/")])
if dockercloud.namespace:
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", "app", self._api_version,
dockercloud.namespace, endpoint.lstrip("/")])
else:
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", "app", self._api_version, endpoint.lstrip("/")])
super(self.__class__, self).__init__(url)

@staticmethod
Expand Down
7 changes: 6 additions & 1 deletion dockercloud/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
class Events(StreamingAPI):
def __init__(self):
endpoint = "events"
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", "audit", self._api_version, endpoint.lstrip("/")])
if dockercloud.namespace:
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", "audit", self._api_version,
dockercloud.namespace, endpoint.lstrip("/")])
else:
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", "audit", self._api_version,
endpoint.lstrip("/")])
self.invaid_auth_headers = set()
self.auth_error = ""
super(self.__class__, self).__init__(url)
Expand Down
1 change: 1 addition & 0 deletions dockercloud/api/nodeaz.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class AZ(Immutable):
subsystem = "infra"
endpoint = "/az"
namespaced = False

@classmethod
def _pk_key(cls):
Expand Down
1 change: 1 addition & 0 deletions dockercloud/api/nodeprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class Provider(Immutable):
subsystem = "infra"
endpoint = "/provider"
namespaced = False

@classmethod
def _pk_key(cls):
Expand Down
1 change: 1 addition & 0 deletions dockercloud/api/noderegion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class Region(Immutable):
subsystem = "infra"
endpoint = "/region"
namespaced = False

@classmethod
def _pk_key(cls):
Expand Down
1 change: 1 addition & 0 deletions dockercloud/api/nodetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class NodeType(Immutable):
subsystem = "infra"
endpoint = "/nodetype"
namespaced = False

@classmethod
def _pk_key(cls):
Expand Down

0 comments on commit 648d107

Please sign in to comment.