From 2d25080a033ba5e94b819591ec902a205093eb9b Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 21 Jan 2014 13:55:49 -0600 Subject: [PATCH] Initial blob of thoughts from me --- README.rst | 19 +++++++++++++++++++ __init__.py | 0 auth.py | 41 +++++++++++++++++++++++++++++++++++++++++ client.py | 12 ++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 README.rst create mode 100644 __init__.py create mode 100644 auth.py create mode 100644 client.py diff --git a/README.rst b/README.rst new file mode 100644 index 000000000..3d75c9c99 --- /dev/null +++ b/README.rst @@ -0,0 +1,19 @@ +An OpenStack all-in-one SDK +=========================== + +Example: + +.. code-block:: python + + import io + + from openstack import OpenStackClient, KeystoneAuth + + + client = OpenStackClient(KeystoneAuth('http://localhost:8000/', 'alex', '****')) + image = client.compute.images.list()[0] + server = client.compute.servers.create(image=image) + print server.public_ips[0] + + container = client.object_storage.container.create(name='stuff') + container.objects.create(name='a thing', contents=io.BytesIO(b'all the bytes')) diff --git a/__init__.py b/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/auth.py b/auth.py new file mode 100644 index 000000000..9eaa1e1e4 --- /dev/null +++ b/auth.py @@ -0,0 +1,41 @@ +class KeystoneAuth(object): + def __init__(self, authurl, username, password): + self._authurl = authurl + self._username = username + self._password = password + + def connect(self): + return KeystoneTransport(self) + + +class KeystoneTransport(object): + def __init__(self, auth_info): + # Set ourselves up: + # * Open HTTP session + # * Get a token + # * Configure session to raise on HTTP error + self.auth_info = auth_info + + def make_http_request(method): + def inner(self, *args, **kwargs): + # get_session() will make sure that the token is valid. + return self.get_session().request(method, *args, **kwargs) + return inner + + get = make_http_request("GET") + post = make_http_request("POST") + patch = make_http_request("PATCH") + delete = make_http_request("DELETE") + + + +# In reality this would not live inside the main package because it would be +# vendor agnostic, but for the purpose of demonstrating that other auth +# strategies work fine, it goes here. +class RackspaceAuth(object): + def __init__(self, username, api_key): + self._username = username + self._api_key = api_key + + def connect(self): + # ... diff --git a/client.py b/client.py new file mode 100644 index 000000000..133ff809a --- /dev/null +++ b/client.py @@ -0,0 +1,12 @@ +class OpenStackClient(object): + def __init__(self, auth): + self._transport = auth.connect() + self._auth = auth + + @property + def compute(self): + return ComputeClient(self._transport) + + @property + def object_storage(self): + return ObjectStorageClient(self._transport)