diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..463eb71 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.pyc +.tox/* +build/* +html/* +python_reddwarfclient.egg* diff --git a/README b/README deleted file mode 100644 index 4b03f97..0000000 --- a/README +++ /dev/null @@ -1 +0,0 @@ -This is a deprecated repo. Please visit the new reddwarf client at http://github.com/stackforge/python-reddwarfclient diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..929143b --- /dev/null +++ b/README.rst @@ -0,0 +1,21 @@ +Python bindings to the Reddwarf API +================================================== + +This is a client for the Reddwarf API. There's a Python API (the +``reddwarfclient`` module), and a command-line script (``reddwarf``). Each +implements 100% (or less ;) ) of the Reddwarf API. + +Command-line API +---------------- + +To use the command line API, first log in using your user name, api key, +tenant, and appropriate auth url. + +.. code-block:: bash + + $ reddwarf-cli --username=jsmith --apikey=abcdefg --tenant=12345 --auth_url=http://reddwarf_auth:35357/v2.0/tokens auth login + +At this point you will be authenticated and given a token, which is stored +at ~/.apitoken. From there you can make other calls to the CLI. + +TODO: Add docs diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 0000000..0ad4db2 --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +import sys, os + +extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.coverage'] + +templates_path = ['_templates'] + +source_suffix = '.rst' + +master_doc = 'index' + +project = u'python-reddwarfclient' +copyright = u'2012, OpenStack' + +version = '1.0' +release = '1.0' +exclude_trees = [] + +pygments_style = 'sphinx' + +html_theme = 'default' +html_static_path = ['_static'] +htmlhelp_basename = 'python-reddwarfclientdoc' +latex_documents = [ + ('index', 'python-reddwarfclient.tex', u'python-reddwarfclient Documentation', + u'OpenStack', 'manual'), +] + diff --git a/docs/source/conf.py b/docs/source/conf.py new file mode 100644 index 0000000..0ad4db2 --- /dev/null +++ b/docs/source/conf.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +import sys, os + +extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.coverage'] + +templates_path = ['_templates'] + +source_suffix = '.rst' + +master_doc = 'index' + +project = u'python-reddwarfclient' +copyright = u'2012, OpenStack' + +version = '1.0' +release = '1.0' +exclude_trees = [] + +pygments_style = 'sphinx' + +html_theme = 'default' +html_static_path = ['_static'] +htmlhelp_basename = 'python-reddwarfclientdoc' +latex_documents = [ + ('index', 'python-reddwarfclient.tex', u'python-reddwarfclient Documentation', + u'OpenStack', 'manual'), +] + diff --git a/docs/source/index.rst b/docs/source/index.rst new file mode 100644 index 0000000..454f8ab --- /dev/null +++ b/docs/source/index.rst @@ -0,0 +1,31 @@ +.. + Licensed under the Apache License, Version 2.0 (the "License"); you may + not use this file except in compliance with the License. You may obtain + a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + License for the specific language governing permissions and limitations + under the License. + +.. include:: ../../README.rst + :start-line: 0 + :end-line: 22 + + +.. contents:: Contents + :local: + +.. include:: ./usage.rst + +.. include:: ./pydocs.rst + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` diff --git a/docs/source/pydocs.rst b/docs/source/pydocs.rst new file mode 100644 index 0000000..eddda86 --- /dev/null +++ b/docs/source/pydocs.rst @@ -0,0 +1,16 @@ +PyDocs +================= + +reddwarfclient +-------------- + + +.. automodule:: reddwarfclient + :members: + :undoc-members: + :show-inheritance: + +.. autoclass:: reddwarfclient.client.Dbaas + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/usage.rst b/docs/source/usage.rst new file mode 100644 index 0000000..caaa12a --- /dev/null +++ b/docs/source/usage.rst @@ -0,0 +1,209 @@ +Using the Client Programmatically +================================= + + +.. testsetup:: + + # Creates some vars we don't show in the docs. + AUTH_URL="http://localhost:8779/v1.0/auth" + + from reddwarfclient import Dbaas + from reddwarfclient import auth + class FakeAuth(auth.Authenticator): + + def authenticate(self): + class FakeCatalog(object): + def __init__(self, auth): + self.auth = auth + + def get_public_url(self): + return "%s/%s" % ('http://localhost:8779/v1.0', + self.auth.tenant) + + def get_token(self): + return self.auth.tenant + + return FakeCatalog(self) + + from reddwarfclient import Dbaas + OLD_INIT = Dbaas.__init__ + def new_init(*args, **kwargs): + kwargs['auth_strategy'] = FakeAuth + OLD_INIT(*args, **kwargs) + + # Monkey patch init so it'll work with fake auth. + Dbaas.__init__ = new_init + + + client = Dbaas("jsmith", "abcdef", tenant="12345", + auth_url=AUTH_URL) + client.authenticate() + + # Delete all instances. + instances = [1] + while len(instances) > 0: + instances = client.instances.list() + for instance in instances: + try: + instance.delete() + except: + pass + + flavor_id = "1" + for i in range(30): + name = "Instance #%d" % i + client.instances.create(name, flavor_id, None) + + + +Authentication +-------------- + +Authenticating is necessary to use every feature of the client (except to +discover available versions). + +To create the client, create an instance of the Dbaas (Database as a Service) +class. The auth url, auth user, key, and tenant ID must be specified in the +call to the constructor. + +.. testcode:: + + from reddwarfclient import Dbaas + global AUTH_URL + + client = Dbaas("jsmith", "abcdef", tenant="12345", + auth_url=AUTH_URL) + client.authenticate() + +The default authentication strategy assumes a Keystone complaint auth system. +For Rackspace auth, use the keyword argument "auth_strategy='rax'". + + +Versions +-------- + +You can discover the available versions by querying the versions property as +follows: + + +.. testcode:: + + versions = client.versions.index("http://localhost:8779") + + +The "index" method returns a list of Version objects which have the ID as well +as a list of links, each with a URL to use to reach that particular version. + +.. testcode:: + + for version in versions: + print(version.id) + for link in version.links: + if link['rel'] == 'self': + print(" %s" % link['href']) + +.. testoutput:: + + v1.0 + http://localhost:8779/v1.0 + + +Instances +--------- + +The following example creates a 512 MB instance with a 1 GB volume: + +.. testcode:: + + client.authenticate() + flavor_id = "1" + volume = {'size':1} + databases = [{"name": "my_db", + "character_set": "latin2", # This and the next field are + "collate": "latin2_general_ci"}] # optional. + users = [{"name": "jsmith", "password": "12345", + "databases": [{"name": "my_db"}] + }] + instance = client.instances.create("My Instance", flavor_id, volume, + databases, users) + +To retrieve the instance, use the "get" method of "instances": + +.. testcode:: + + updated_instance = client.instances.get(instance.id) + print(updated_instance.name) + print(" Status=%s Flavor=%s" % + (updated_instance.status, updated_instance.flavor['id'])) + +.. testoutput:: + + My Instance + Status=BUILD Flavor=1 + +You can delete an instance by calling "delete" on the instance object itself, +or by using the delete method on "instances." + +.. testcode:: + + # Wait for the instance to be ready before we delete it. + import time + from reddwarfclient.exceptions import NotFound + + while instance.status == "BUILD": + instance.get() + time.sleep(1) + print("Ready in an %s state." % instance.status) + instance.delete() + # Delete and wait for the instance to go away. + while True: + try: + instance = client.instances.get(instance.id) + assert instance.status == "SHUTDOWN" + except NotFound: + break + +.. testoutput:: + + Ready in an ACTIVE state. + + +Listing instances and Pagination +-------------------------------- + +To list all instances, use the list method of "instances": + +.. testcode:: + + instances = client.instances.list() + + +Lists paginate after twenty items, meaning you'll only get twenty items back +even if there are more. To see the next set of items, send a marker. The marker +is a key value (in the case of instances, the ID) which is the non-inclusive +starting point for all returned items. + +The lists returned by the client always include a "next" property. This +can be used as the "marker" argument to get the next section of the list +back from the server. If no more items are available, then the next property +is None. + +.. testcode:: + + # There are currently 30 instances. + + instances = client.instances.list() + print(len(instances)) + print(instances.next is None) + + instances2 = client.instances.list(marker=instances.next) + print(len(instances2)) + print(instances2.next is None) + +.. testoutput:: + + 20 + False + 10 + True + diff --git a/reddwarfclient/__init__.py b/reddwarfclient/__init__.py new file mode 100644 index 0000000..0383828 --- /dev/null +++ b/reddwarfclient/__init__.py @@ -0,0 +1,31 @@ +# Copyright (c) 2011 OpenStack, LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +from reddwarfclient.accounts import Accounts +from reddwarfclient.databases import Databases +from reddwarfclient.flavors import Flavors +from reddwarfclient.instances import Instances +from reddwarfclient.hosts import Hosts +from reddwarfclient.management import Management +from reddwarfclient.management import RootHistory +from reddwarfclient.root import Root +from reddwarfclient.storage import StorageInfo +from reddwarfclient.users import Users +from reddwarfclient.versions import Versions +from reddwarfclient.diagnostics import DiagnosticsInterrogator +from reddwarfclient.diagnostics import HwInfoInterrogator +from reddwarfclient.client import Dbaas +from reddwarfclient.client import ReddwarfHTTPClient diff --git a/reddwarfclient/accounts.py b/reddwarfclient/accounts.py new file mode 100644 index 0000000..43e9135 --- /dev/null +++ b/reddwarfclient/accounts.py @@ -0,0 +1,67 @@ +# Copyright (c) 2011 OpenStack, LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from reddwarfclient import base +from reddwarfclient.common import check_for_exceptions + + +class Account(base.Resource): + """ + Account is an opaque instance used to hold account information. + """ + def __repr__(self): + return "" % self.name + + +class Accounts(base.ManagerWithFind): + """ + Manage :class:`Account` information. + """ + + resource_class = Account + + def _list(self, url, response_key): + resp, body = self.api.client.get(url) + if not body: + raise Exception("Call to " + url + " did not return a body.") + return self.resource_class(self, body[response_key]) + + def index(self): + """Get a list of all accounts with non-deleted instances""" + + url = "/mgmt/accounts" + resp, body = self.api.client.get(url) + check_for_exceptions(resp, body) + if not body: + raise Exception("Call to " + url + " did not return a body.") + return base.Resource(self, body) + + def show(self, account): + """ + Get details of one account. + + :rtype: :class:`Account`. + """ + + acct_name = self._get_account_name(account) + return self._list("/mgmt/accounts/%s" % acct_name, 'account') + + @staticmethod + def _get_account_name(account): + try: + if account.name: + return account.name + except AttributeError: + return account diff --git a/reddwarfclient/auth.py b/reddwarfclient/auth.py new file mode 100644 index 0000000..76ef176 --- /dev/null +++ b/reddwarfclient/auth.py @@ -0,0 +1,269 @@ +# Copyright 2012 OpenStack LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from reddwarfclient import exceptions + + +def get_authenticator_cls(cls_or_name): + """Factory method to retrieve Authenticator class.""" + if isinstance(cls_or_name, type): + return cls_or_name + elif isinstance(cls_or_name, basestring): + if cls_or_name == "keystone": + return KeyStoneV2Authenticator + elif cls_or_name == "rax": + return RaxAuthenticator + elif cls_or_name == "auth1.1": + return Auth1_1 + elif cls_or_name == "fake": + return FakeAuth + + raise ValueError("Could not determine authenticator class from the given " + "value %r." % cls_or_name) + + +class Authenticator(object): + """ + Helper class to perform Keystone or other miscellaneous authentication. + + The "authenticate" method returns a ServiceCatalog, which can be used + to obtain a token. + + """ + + URL_REQUIRED=True + + def __init__(self, client, type, url, username, password, tenant, + region=None, service_type=None, service_name=None, + service_url=None): + self.client = client + self.type = type + self.url = url + self.username = username + self.password = password + self.tenant = tenant + self.region = region + self.service_type = service_type + self.service_name = service_name + self.service_url = service_url + + def _authenticate(self, url, body, root_key='access'): + """Authenticate and extract the service catalog.""" + # Make sure we follow redirects when trying to reach Keystone + tmp_follow_all_redirects = self.client.follow_all_redirects + self.client.follow_all_redirects = True + + try: + resp, body = self.client._time_request(url, "POST", body=body) + finally: + self.client.follow_all_redirects = tmp_follow_all_redirects + + if resp.status == 200: # content must always present + try: + return ServiceCatalog(body, region=self.region, + service_type=self.service_type, + service_name=self.service_name, + service_url=self.service_url, + root_key=root_key) + except exceptions.AmbiguousEndpoints: + print "Found more than one valid endpoint. Use a more "\ + "restrictive filter" + raise + except KeyError: + raise exceptions.AuthorizationFailure() + except exceptions.EndpointNotFound: + print "Could not find any suitable endpoint. Correct region?" + raise + + elif resp.status == 305: + return resp['location'] + else: + raise exceptions.from_response(resp, body) + + def authenticate(self): + raise NotImplementedError("Missing authenticate method.") + + +class KeyStoneV2Authenticator(Authenticator): + + def authenticate(self): + if self.url is None: + raise exceptions.AuthUrlNotGiven() + return self._v2_auth(self.url) + + def _v2_auth(self, url): + """Authenticate against a v2.0 auth service.""" + body = {"auth": { + "passwordCredentials": { + "username": self.username, + "password": self.password} + } + } + + if self.tenant: + body['auth']['tenantName'] = self.tenant + + return self._authenticate(url, body) + + +class Auth1_1(Authenticator): + + def authenticate(self): + """Authenticate against a v2.0 auth service.""" + if self.url is None: + raise exceptions.AuthUrlNotGiven() + auth_url = self.url + body = {"credentials": {"username": self.username, + "key": self.password}} + return self._authenticate(auth_url, body, root_key='auth') + + try: + print(resp_body) + self.auth_token = resp_body['auth']['token']['id'] + except KeyError: + raise nova_exceptions.AuthorizationFailure() + + catalog = resp_body['auth']['serviceCatalog'] + if 'cloudDatabases' not in catalog: + raise nova_exceptions.EndpointNotFound() + endpoints = catalog['cloudDatabases'] + for endpoint in endpoints: + if self.region_name is None or \ + endpoint['region'] == self.region_name: + self.management_url = endpoint['publicURL'] + return + raise nova_exceptions.EndpointNotFound() + + +class RaxAuthenticator(Authenticator): + + def authenticate(self): + if self.url is None: + raise exceptions.AuthUrlNotGiven() + return self._rax_auth(self.url) + + def _rax_auth(self, url): + """Authenticate against the Rackspace auth service.""" + body = {'auth': { + 'RAX-KSKEY:apiKeyCredentials': { + 'username': self.username, + 'apiKey': self.password, + 'tenantName': self.tenant} + } + } + + return self._authenticate(self.url, body) + + +class FakeAuth(Authenticator): + """Useful for faking auth.""" + + def authenticate(self): + class FakeCatalog(object): + def __init__(self, auth): + self.auth = auth + + def get_public_url(self): + return "%s/%s" % ('http://localhost:8779/v1.0', + self.auth.tenant) + + def get_token(self): + return self.auth.tenant + + return FakeCatalog(self) + + +class ServiceCatalog(object): + """Represents a Keystone Service Catalog which describes a service. + + This class has methods to obtain a valid token as well as a public service + url and a management url. + + """ + + def __init__(self, resource_dict, region=None, service_type=None, + service_name=None, service_url=None, root_key='access'): + self.catalog = resource_dict + self.region = region + self.service_type = service_type + self.service_name = service_name + self.service_url = service_url + self.management_url = None + self.public_url = None + self.root_key = root_key + self._load() + + def _load(self): + if not self.service_url: + self.public_url = self._url_for(attr='region', + filter_value=self.region, + endpoint_type="publicURL") + self.management_url = self._url_for(attr='region', + filter_value=self.region, + endpoint_type="adminURL") + else: + self.public_url = self.service_url + self.management_url = self.service_url + + def get_token(self): + return self.catalog[self.root_key]['token']['id'] + + def get_management_url(self): + return self.management_url + + def get_public_url(self): + return self.public_url + + def _url_for(self, attr=None, filter_value=None, + endpoint_type='publicURL'): + """ + Fetch the public URL from the Reddwarf service for a particular + endpoint attribute. If none given, return the first. + """ + matching_endpoints = [] + if 'endpoints' in self.catalog: + # We have a bastardized service catalog. Treat it special. :/ + for endpoint in self.catalog['endpoints']: + if not filter_value or endpoint[attr] == filter_value: + matching_endpoints.append(endpoint) + if not matching_endpoints: + raise exceptions.EndpointNotFound() + + # We don't always get a service catalog back ... + if not 'serviceCatalog' in self.catalog[self.root_key]: + raise exceptions.EndpointNotFound() + + # Full catalog ... + catalog = self.catalog[self.root_key]['serviceCatalog'] + + for service in catalog: + if service.get("type") != self.service_type: + continue + + if (self.service_name and self.service_type == 'reddwarf' and + service.get('name') != self.service_name): + continue + + endpoints = service['endpoints'] + for endpoint in endpoints: + if not filter_value or endpoint.get(attr) == filter_value: + endpoint["serviceName"] = service.get("name") + matching_endpoints.append(endpoint) + + if not matching_endpoints: + raise exceptions.EndpointNotFound() + elif len(matching_endpoints) > 1: + raise exceptions.AmbiguousEndpoints(endpoints=matching_endpoints) + else: + return matching_endpoints[0].get(endpoint_type, None) diff --git a/reddwarfclient/base.py b/reddwarfclient/base.py new file mode 100644 index 0000000..6660ff4 --- /dev/null +++ b/reddwarfclient/base.py @@ -0,0 +1,293 @@ +# Copyright 2010 Jacob Kaplan-Moss + +# Copyright 2012 OpenStack LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +Base utilities to build API operation managers and objects on top of. +""" + +import contextlib +import hashlib +import os +from reddwarfclient import exceptions +from reddwarfclient import utils + + +# Python 2.4 compat +try: + all +except NameError: + def all(iterable): + return True not in (not x for x in iterable) + + +def getid(obj): + """ + Abstracts the common pattern of allowing both an object or an object's ID + as a parameter when dealing with relationships. + """ + try: + return obj.id + except AttributeError: + return obj + + +class Manager(utils.HookableMixin): + """ + Managers interact with a particular type of API (servers, flavors, images, + etc.) and provide CRUD operations for them. + """ + resource_class = None + + def __init__(self, api): + self.api = api + + def _list(self, url, response_key, obj_class=None, body=None): + resp = None + if body: + resp, body = self.api.client.post(url, body=body) + else: + resp, body = self.api.client.get(url) + + if obj_class is None: + obj_class = self.resource_class + + data = body[response_key] + # NOTE(ja): keystone returns values as list as {'values': [ ... ]} + # unlike other services which just return the list... + if isinstance(data, dict): + try: + data = data['values'] + except KeyError: + pass + + with self.completion_cache('human_id', obj_class, mode="w"): + with self.completion_cache('uuid', obj_class, mode="w"): + return [obj_class(self, res, loaded=True) + for res in data if res] + + @contextlib.contextmanager + def completion_cache(self, cache_type, obj_class, mode): + """ + The completion cache store items that can be used for bash + autocompletion, like UUIDs or human-friendly IDs. + + A resource listing will clear and repopulate the cache. + + A resource create will append to the cache. + + Delete is not handled because listings are assumed to be performed + often enough to keep the cache reasonably up-to-date. + """ + base_dir = utils.env('REDDWARFCLIENT_ID_CACHE_DIR', + default="~/.reddwarfclient") + + # NOTE(sirp): Keep separate UUID caches for each username + endpoint + # pair + username = utils.env('OS_USERNAME', 'USERNAME') + url = utils.env('OS_URL', 'SERVICE_URL') + uniqifier = hashlib.md5(username + url).hexdigest() + + cache_dir = os.path.expanduser(os.path.join(base_dir, uniqifier)) + + try: + os.makedirs(cache_dir, 0755) + except OSError: + # NOTE(kiall): This is typicaly either permission denied while + # attempting to create the directory, or the directory + # already exists. Either way, don't fail. + pass + + resource = obj_class.__name__.lower() + filename = "%s-%s-cache" % (resource, cache_type.replace('_', '-')) + path = os.path.join(cache_dir, filename) + + cache_attr = "_%s_cache" % cache_type + + try: + setattr(self, cache_attr, open(path, mode)) + except IOError: + # NOTE(kiall): This is typicaly a permission denied while + # attempting to write the cache file. + pass + + try: + yield + finally: + cache = getattr(self, cache_attr, None) + if cache: + cache.close() + delattr(self, cache_attr) + + def write_to_completion_cache(self, cache_type, val): + cache = getattr(self, "_%s_cache" % cache_type, None) + if cache: + cache.write("%s\n" % val) + + def _get(self, url, response_key=None): + resp, body = self.api.client.get(url) + if response_key: + return self.resource_class(self, body[response_key], loaded=True) + else: + return self.resource_class(self, body, loaded=True) + + def _create(self, url, body, response_key, return_raw=False, **kwargs): + self.run_hooks('modify_body_for_create', body, **kwargs) + resp, body = self.api.client.post(url, body=body) + if return_raw: + return body[response_key] + + with self.completion_cache('human_id', self.resource_class, mode="a"): + with self.completion_cache('uuid', self.resource_class, mode="a"): + return self.resource_class(self, body[response_key]) + + def _delete(self, url): + resp, body = self.api.client.delete(url) + + def _update(self, url, body, **kwargs): + self.run_hooks('modify_body_for_update', body, **kwargs) + resp, body = self.api.client.put(url, body=body) + return body + + +class ManagerWithFind(Manager): + """ + Like a `Manager`, but with additional `find()`/`findall()` methods. + """ + def find(self, **kwargs): + """ + Find a single item with attributes matching ``**kwargs``. + + This isn't very efficient: it loads the entire list then filters on + the Python side. + """ + matches = self.findall(**kwargs) + num_matches = len(matches) + if num_matches == 0: + msg = "No %s matching %s." % (self.resource_class.__name__, kwargs) + raise exceptions.NotFound(404, msg) + elif num_matches > 1: + raise exceptions.NoUniqueMatch + else: + return matches[0] + + def findall(self, **kwargs): + """ + Find all items with attributes matching ``**kwargs``. + + This isn't very efficient: it loads the entire list then filters on + the Python side. + """ + found = [] + searches = kwargs.items() + + for obj in self.list(): + try: + if all(getattr(obj, attr) == value + for (attr, value) in searches): + found.append(obj) + except AttributeError: + continue + + return found + + def list(self): + raise NotImplementedError + + +class Resource(object): + """ + A resource represents a particular instance of an object (server, flavor, + etc). This is pretty much just a bag for attributes. + + :param manager: Manager object + :param info: dictionary representing resource attributes + :param loaded: prevent lazy-loading if set to True + """ + HUMAN_ID = False + + def __init__(self, manager, info, loaded=False): + self.manager = manager + self._info = info + self._add_details(info) + self._loaded = loaded + + # NOTE(sirp): ensure `id` is already present because if it isn't we'll + # enter an infinite loop of __getattr__ -> get -> __init__ -> + # __getattr__ -> ... + if 'id' in self.__dict__ and len(str(self.id)) == 36: + self.manager.write_to_completion_cache('uuid', self.id) + + human_id = self.human_id + if human_id: + self.manager.write_to_completion_cache('human_id', human_id) + + @property + def human_id(self): + """Subclasses may override this provide a pretty ID which can be used + for bash completion. + """ + if 'name' in self.__dict__ and self.HUMAN_ID: + return utils.slugify(self.name) + return None + + def _add_details(self, info): + for (k, v) in info.iteritems(): + try: + setattr(self, k, v) + except AttributeError: + # In this case we already defined the attribute on the class + pass + + def __getattr__(self, k): + if k not in self.__dict__: + #NOTE(bcwaldon): disallow lazy-loading if already loaded once + if not self.is_loaded(): + self.get() + return self.__getattr__(k) + + raise AttributeError(k) + else: + return self.__dict__[k] + + def __repr__(self): + reprkeys = sorted(k for k in self.__dict__.keys() if k[0] != '_' and + k != 'manager') + info = ", ".join("%s=%s" % (k, getattr(self, k)) for k in reprkeys) + return "<%s %s>" % (self.__class__.__name__, info) + + def get(self): + # set_loaded() first ... so if we have to bail, we know we tried. + self.set_loaded(True) + if not hasattr(self.manager, 'get'): + return + + new = self.manager.get(self.id) + if new: + self._add_details(new._info) + + def __eq__(self, other): + if not isinstance(other, self.__class__): + return False + if hasattr(self, 'id') and hasattr(other, 'id'): + return self.id == other.id + return self._info == other._info + + def is_loaded(self): + return self._loaded + + def set_loaded(self, val): + self._loaded = val diff --git a/reddwarfclient/cli.py b/reddwarfclient/cli.py new file mode 100644 index 0000000..ea83d7b --- /dev/null +++ b/reddwarfclient/cli.py @@ -0,0 +1,263 @@ +#!/usr/bin/env python + +# Copyright 2011 OpenStack LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +Reddwarf Command line tool +""" + +#TODO(tim.simpson): optparse is deprecated. Replace with argparse. +import optparse +import os +import sys + + +# If ../reddwarf/__init__.py exists, add ../ to Python search path, so that +# it will override what happens to be installed in /usr/(local/)lib/python... +possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), + os.pardir, + os.pardir)) +if os.path.exists(os.path.join(possible_topdir, 'reddwarfclient', + '__init__.py')): + sys.path.insert(0, possible_topdir) + + +from reddwarfclient import common + + +class InstanceCommands(common.AuthedCommandsBase): + """Commands to perform various instances operations and actions""" + + params = [ + 'flavor', + 'id', + 'limit', + 'marker', + 'name', + 'size', + ] + + def create(self): + """Create a new instance""" + self._require('name', 'size') + # flavorRef is not required. + flavorRef = self.flavor or "http://localhost:8775/v1.0/flavors/1" + volume = {"size": self.size} + self._pretty_print(self.dbaas.instances.create, self.name, + flavorRef, volume) + + def delete(self): + """Delete the specified instance""" + self._require('id') + print self.dbaas.instances.delete(self.id) + + def get(self): + """Get details for the specified instance""" + self._require('id') + self._pretty_print(self.dbaas.instances.get, self.id) + + def list(self): + """List all instances for account""" + # limit and marker are not required. + limit = self.limit or None + if limit: + limit = int(limit, 10) + self._pretty_paged(self.dbaas.instances.list) + + def resize_volume(self): + """Resize an instance volume""" + self._require('id', 'size') + self._pretty_print(self.dbaas.instances.resize_volume, self.id, + self.size) + + def resize_instance(self): + """Resize an instance flavor""" + self._require('id', 'flavor') + self._pretty_print(self.dbaas.instances.resize_instance, self.id, + self.flavor) + + def restart(self): + """Restart the database""" + self._require('id') + self._pretty_print(self.dbaas.instances.restart, self.id) + + def reset_password(self): + """Reset the root user Password""" + self._require('id') + self._pretty_print(self.dbaas.instances.reset_password, self.id) + + +class FlavorsCommands(common.AuthedCommandsBase): + """Commands for listing Flavors""" + + params = [] + + def list(self): + """List the available flavors""" + self._pretty_list(self.dbaas.flavors.list) + + +class DatabaseCommands(common.AuthedCommandsBase): + """Database CRUD operations on an instance""" + + params = [ + 'name', + 'id', + 'limit', + 'marker', + ] + + def create(self): + """Create a database""" + self._require('id', 'name') + databases = [{'name': self.name}] + print self.dbaas.databases.create(self.id, databases) + + def delete(self): + """Delete a database""" + self._require('id', 'name') + print self.dbaas.databases.delete(self.id, self.name) + + def list(self): + """List the databases""" + self._require('id') + self._pretty_paged(self.dbaas.databases.list, self.id) + + +class UserCommands(common.AuthedCommandsBase): + """User CRUD operations on an instance""" + params = [ + 'id', + 'databases', + 'name', + 'password', + ] + + def create(self): + """Create a user in instance, with access to one or more databases""" + self._require('id', 'name', 'password', 'databases') + self._make_list('databases') + databases = [{'name': dbname} for dbname in self.databases] + users = [{'name': self.username, 'password': self.password, + 'databases': databases}] + self.dbaas.users.create(self.id, users) + + def delete(self): + """Delete the specified user""" + self._require('id', 'name') + self.users.delete(self.id, self.name) + + def list(self): + """List all the users for an instance""" + self._require('id') + self._pretty_paged(self.dbaas.users.list, self.id) + + +class RootCommands(common.AuthedCommandsBase): + """Root user related operations on an instance""" + + params = [ + 'id', + ] + + def create(self): + """Enable the instance's root user.""" + self._require('id') + try: + user, password = self.dbaas.root.create(self.id) + print "User:\t\t%s\nPassword:\t%s" % (user, password) + except: + print sys.exc_info()[1] + + def enabled(self): + """Check the instance for root access""" + self._require('id') + self._pretty_print(self.dbaas.root.is_root_enabled, self.id) + + +class VersionCommands(common.AuthedCommandsBase): + """List available versions""" + + params = [ + 'url', + ] + + def list(self): + """List all the supported versions""" + self._require('url') + self._pretty_print(self.dbaas.versions.index, self.url) + + +COMMANDS = {'auth': common.Auth, + 'instance': InstanceCommands, + 'flavor': FlavorsCommands, + 'database': DatabaseCommands, + 'user': UserCommands, + 'root': RootCommands, + 'version': VersionCommands, + } + +def main(): + # Parse arguments + oparser = common.CliOptions.create_optparser() + for k, v in COMMANDS.items(): + v._prepare_parser(oparser) + (options, args) = oparser.parse_args() + + if not args: + common.print_commands(COMMANDS) + + if options.verbose: + os.environ['RDC_PP'] = "True" + os.environ['REDDWARFCLIENT_DEBUG'] = "True" + + # Pop the command and check if it's in the known commands + cmd = args.pop(0) + if cmd in COMMANDS: + fn = COMMANDS.get(cmd) + command_object = None + try: + command_object = fn(oparser) + except Exception as ex: + if options.debug: + raise + print(ex) + + # Get a list of supported actions for the command + actions = common.methods_of(command_object) + + if len(args) < 1: + common.print_actions(cmd, actions) + + # Check for a valid action and perform that action + action = args.pop(0) + if action in actions: + if not options.debug: + try: + getattr(command_object, action)() + except Exception as ex: + if options.debug: + raise + print ex + else: + getattr(command_object, action)() + else: + common.print_actions(cmd, actions) + else: + common.print_commands(COMMANDS) + + +if __name__ == '__main__': + main() diff --git a/reddwarfclient/client.py b/reddwarfclient/client.py new file mode 100644 index 0000000..44757ad --- /dev/null +++ b/reddwarfclient/client.py @@ -0,0 +1,358 @@ +# Copyright (c) 2011 OpenStack, LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import httplib2 +import logging +import os +import time +import urlparse +import sys + +try: + import json +except ImportError: + import simplejson as json + +# Python 2.5 compat fix +if not hasattr(urlparse, 'parse_qsl'): + import cgi + urlparse.parse_qsl = cgi.parse_qsl + +from reddwarfclient import auth +from reddwarfclient import exceptions + + +_logger = logging.getLogger(__name__) +RDC_PP = os.environ.get("RDC_PP", "False") == "True" + + +def log_to_streamhandler(stream=None): + stream = stream or sys.stderr + ch = logging.StreamHandler(stream) + _logger.setLevel(logging.DEBUG) + _logger.addHandler(ch) + + +if 'REDDWARFCLIENT_DEBUG' in os.environ and os.environ['REDDWARFCLIENT_DEBUG']: + log_to_streamhandler() + + +class ReddwarfHTTPClient(httplib2.Http): + + USER_AGENT = 'python-reddwarfclient' + + def __init__(self, user, password, tenant, auth_url, service_name, + service_url=None, + auth_strategy=None, insecure=False, + timeout=None, proxy_tenant_id=None, + proxy_token=None, region_name=None, + endpoint_type='publicURL', service_type=None, + timings=False): + + super(ReddwarfHTTPClient, self).__init__(timeout=timeout) + + self.username = user + self.password = password + self.tenant = tenant + if auth_url: + self.auth_url = auth_url.rstrip('/') + else: + self.auth_url = None + self.region_name = region_name + self.endpoint_type = endpoint_type + self.service_url = service_url + self.service_type = service_type + self.service_name = service_name + self.timings = timings + + self.times = [] # [("item", starttime, endtime), ...] + + self.auth_token = None + self.proxy_token = proxy_token + self.proxy_tenant_id = proxy_tenant_id + + # httplib2 overrides + self.force_exception_to_status_code = True + self.disable_ssl_certificate_validation = insecure + + auth_cls = auth.get_authenticator_cls(auth_strategy) + + self.authenticator = auth_cls(self, auth_strategy, + self.auth_url, self.username, + self.password, self.tenant, + region=region_name, + service_type=service_type, + service_name=service_name, + service_url=service_url) + + def get_timings(self): + return self.times + + def http_log(self, args, kwargs, resp, body): + if not RDC_PP: + self.simple_log(args, kwargs, resp, body) + else: + self.pretty_log(args, kwargs, resp, body) + + def simple_log(self, args, kwargs, resp, body): + if not _logger.isEnabledFor(logging.DEBUG): + return + + string_parts = ['curl -i'] + for element in args: + if element in ('GET', 'POST'): + string_parts.append(' -X %s' % element) + else: + string_parts.append(' %s' % element) + + for element in kwargs['headers']: + header = ' -H "%s: %s"' % (element, kwargs['headers'][element]) + string_parts.append(header) + + _logger.debug("REQ: %s\n" % "".join(string_parts)) + if 'body' in kwargs: + _logger.debug("REQ BODY: %s\n" % (kwargs['body'])) + _logger.debug("RESP:%s %s\n", resp, body) + + def pretty_log(self, args, kwargs, resp, body): + from reddwarfclient import common + if not _logger.isEnabledFor(logging.DEBUG): + return + + string_parts = ['curl -i'] + for element in args: + if element in ('GET', 'POST'): + string_parts.append(' -X %s' % element) + else: + string_parts.append(' %s' % element) + + for element in kwargs['headers']: + header = ' -H "%s: %s"' % (element, kwargs['headers'][element]) + string_parts.append(header) + + curl_cmd = "".join(string_parts) + _logger.debug("REQUEST:") + if 'body' in kwargs: + _logger.debug("%s -d '%s'" % (curl_cmd, kwargs['body'])) + try: + req_body = json.dumps(json.loads(kwargs['body']), + sort_keys=True, indent=4) + except: + req_body = kwargs['body'] + _logger.debug("BODY: %s\n" % (req_body)) + else: + _logger.debug(curl_cmd) + + try: + resp_body = json.dumps(json.loads(body), sort_keys=True, indent=4) + except: + resp_body = body + _logger.debug("RESPONSE HEADERS: %s" % resp) + _logger.debug("RESPONSE BODY : %s" % resp_body) + + def request(self, *args, **kwargs): + kwargs.setdefault('headers', kwargs.get('headers', {})) + kwargs['headers']['User-Agent'] = self.USER_AGENT + self.morph_request(kwargs) + + resp, body = super(ReddwarfHTTPClient, self).request(*args, **kwargs) + + # Save this in case anyone wants it. + self.last_response = (resp, body) + self.http_log(args, kwargs, resp, body) + + if body: + try: + body = self.morph_response_body(body) + except exceptions.ResponseFormatError: + # Acceptable only if the response status is an error code. + # Otherwise its the API or client misbehaving. + self.raise_error_from_status(resp, None) + raise # Not accepted! + else: + body = None + + if resp.status in (400, 401, 403, 404, 408, 409, 413, 500, 501): + raise exceptions.from_response(resp, body) + + return resp, body + + def raise_error_from_status(self, resp, body): + if resp.status in (400, 401, 403, 404, 408, 409, 413, 500, 501): + raise exceptions.from_response(resp, body) + + def morph_request(self, kwargs): + kwargs['headers']['Accept'] = 'application/json' + kwargs['headers']['Content-Type'] = 'application/json' + if 'body' in kwargs: + kwargs['body'] = json.dumps(kwargs['body']) + + def morph_response_body(self, body_string): + try: + return json.loads(body_string) + except ValueError: + raise exceptions.ResponseFormatError() + + def _time_request(self, url, method, **kwargs): + start_time = time.time() + resp, body = self.request(url, method, **kwargs) + self.times.append(("%s %s" % (method, url), + start_time, time.time())) + return resp, body + + def _cs_request(self, url, method, **kwargs): + def request(): + kwargs.setdefault('headers', {})['X-Auth-Token'] = self.auth_token + if self.tenant: + kwargs['headers']['X-Auth-Project-Id'] = self.tenant + + resp, body = self._time_request(self.service_url + url, method, + **kwargs) + return resp, body + + if not self.auth_token or not self.service_url: + self.authenticate() + + # Perform the request once. If we get a 401 back then it + # might be because the auth token expired, so try to + # re-authenticate and try again. If it still fails, bail. + try: + return request() + except exceptions.Unauthorized, ex: + self.authenticate() + return request() + + def get(self, url, **kwargs): + return self._cs_request(url, 'GET', **kwargs) + + def post(self, url, **kwargs): + return self._cs_request(url, 'POST', **kwargs) + + def put(self, url, **kwargs): + return self._cs_request(url, 'PUT', **kwargs) + + def delete(self, url, **kwargs): + return self._cs_request(url, 'DELETE', **kwargs) + + def authenticate(self): + """Auths the client and gets a token. May optionally set a service url. + + The client will get auth errors until the authentication step + occurs. Additionally, if a service_url was not explicitly given in + the clients __init__ method, one will be obtained from the auth + service. + + """ + catalog = self.authenticator.authenticate() + if self.service_url: + possible_service_url = None + else: + if self.endpoint_type == "publicURL": + possible_service_url = catalog.get_public_url() + elif self.endpoint_type == "adminURL": + possible_service_url = catalog.get_management_url() + self.authenticate_with_token(catalog.get_token(), possible_service_url) + + def authenticate_with_token(self, token, service_url=None): + self.auth_token = token + if not self.service_url: + if not service_url: + raise exceptions.ServiceUrlNotGiven() + else: + self.service_url = service_url + + + +class Dbaas(object): + """ + Top-level object to access the Rackspace Database as a Service API. + + Create an instance with your creds:: + + >>> red = Dbaas(USERNAME, API_KEY, TENANT, AUTH_URL, SERVICE_NAME, + SERVICE_URL) + + Then call methods on its managers:: + + >>> red.instances.list() + ... + >>> red.flavors.list() + ... + + &c. + """ + + def __init__(self, username, api_key, tenant=None, auth_url=None, + service_type='reddwarf', service_name='Reddwarf', + service_url=None, insecure=False, auth_strategy='keystone', + region_name=None, client_cls=ReddwarfHTTPClient): + from reddwarfclient.versions import Versions + from reddwarfclient.databases import Databases + from reddwarfclient.flavors import Flavors + from reddwarfclient.instances import Instances + from reddwarfclient.users import Users + from reddwarfclient.root import Root + from reddwarfclient.hosts import Hosts + from reddwarfclient.storage import StorageInfo + from reddwarfclient.management import Management + from reddwarfclient.accounts import Accounts + from reddwarfclient.diagnostics import DiagnosticsInterrogator + from reddwarfclient.diagnostics import HwInfoInterrogator + + self.client = client_cls(username, api_key, tenant, auth_url, + service_type=service_type, + service_name=service_name, + service_url=service_url, + insecure=insecure, + auth_strategy=auth_strategy, + region_name=region_name) + self.versions = Versions(self) + self.databases = Databases(self) + self.flavors = Flavors(self) + self.instances = Instances(self) + self.users = Users(self) + self.root = Root(self) + self.hosts = Hosts(self) + self.storage = StorageInfo(self) + self.management = Management(self) + self.accounts = Accounts(self) + self.diagnostics = DiagnosticsInterrogator(self) + self.hwinfo = HwInfoInterrogator(self) + + class Mgmt(object): + def __init__(self, dbaas): + self.instances = dbaas.management + self.hosts = dbaas.hosts + self.accounts = dbaas.accounts + self.storage = dbaas.storage + + self.mgmt = Mgmt(self) + + def set_management_url(self, url): + self.client.management_url = url + + def get_timings(self): + return self.client.get_timings() + + def authenticate(self): + """ + Authenticate against the server. + + This is called to perform an authentication to retrieve a token. + + Returns on success; raises :exc:`exceptions.Unauthorized` if the + credentials are wrong. + """ + self.client.authenticate() diff --git a/reddwarfclient/common.py b/reddwarfclient/common.py new file mode 100644 index 0000000..2e4ff80 --- /dev/null +++ b/reddwarfclient/common.py @@ -0,0 +1,391 @@ +# Copyright 2011 OpenStack LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import copy +import json +import optparse +import os +import pickle +import sys + +from reddwarfclient import client +from reddwarfclient.xml import ReddwarfXmlClient +from reddwarfclient import exceptions + + +def methods_of(obj): + """Get all callable methods of an object that don't start with underscore + returns a list of tuples of the form (method_name, method)""" + result = {} + for i in dir(obj): + if callable(getattr(obj, i)) and not i.startswith('_'): + result[i] = getattr(obj, i) + return result + + +def check_for_exceptions(resp, body): + if resp.status in (400, 422, 500): + raise exceptions.from_response(resp, body) + + +def print_actions(cmd, actions): + """Print help for the command with list of options and description""" + print ("Available actions for '%s' cmd:") % cmd + for k, v in actions.iteritems(): + print "\t%-20s%s" % (k, v.__doc__) + sys.exit(2) + + +def print_commands(commands): + """Print the list of available commands and description""" + + print "Available commands" + for k, v in commands.iteritems(): + print "\t%-20s%s" % (k, v.__doc__) + sys.exit(2) + + +def limit_url(url, limit=None, marker=None): + if not limit and not marker: + return url + query = [] + if marker: + query.append("marker=%s" % marker) + if limit: + query.append("limit=%s" % limit) + query = '?' + '&'.join(query) + return url + query + + +class CliOptions(object): + """A token object containing the user, apikey and token which + is pickleable.""" + + APITOKEN = os.path.expanduser("~/.apitoken") + + DEFAULT_VALUES = { + 'username':None, + 'apikey':None, + 'tenant_id':None, + 'auth_url':None, + 'auth_type':'keystone', + 'service_type':'reddwarf', + 'service_name':'Reddwarf', + 'region':'RegionOne', + 'service_url':None, + 'insecure':False, + 'verbose':False, + 'debug':False, + 'token':None, + 'xml':None, + } + + def __init__(self, **kwargs): + for key, value in self.DEFAULT_VALUES.items(): + setattr(self, key, value) + + @classmethod + def default(cls): + kwargs = copy.deepcopy(cls.DEFAULT_VALUES) + return cls(**kwargs) + + @classmethod + def load_from_file(cls): + try: + with open(cls.APITOKEN, 'rb') as token: + return pickle.load(token) + except IOError: + pass # File probably not found. + except: + print("ERROR: Token file found at %s was corrupt." % cls.APITOKEN) + return cls.default() + + + @classmethod + def save_from_instance_fields(cls, instance): + apitoken = cls.default() + for key, default_value in cls.DEFAULT_VALUES.items(): + final_value = getattr(instance, key, default_value) + setattr(apitoken, key, final_value) + with open(cls.APITOKEN, 'wb') as token: + pickle.dump(apitoken, token, protocol=2) + + + @classmethod + def create_optparser(cls): + oparser = optparse.OptionParser( + usage="%prog [options] ", + version='1.0', conflict_handler='resolve') + file = cls.load_from_file() + def add_option(*args, **kwargs): + if len(args) == 1: + name = args[0] + else: + name = args[1] + kwargs['default'] = getattr(file, name, cls.DEFAULT_VALUES[name]) + oparser.add_option("--%s" % name, **kwargs) + + add_option("verbose", action="store_true", + help="Show equivalent curl statement along " + "with actual HTTP communication.") + add_option("debug", action="store_true", + help="Show the stack trace on errors.") + add_option("auth_url", help="Auth API endpoint URL with port and " + "version. Default: http://localhost:5000/v2.0") + add_option("username", help="Login username") + add_option("apikey", help="Api key") + add_option("tenant_id", + help="Tenant Id associated with the account") + add_option("auth_type", + help="Auth type to support different auth environments, \ + Supported values are 'keystone', 'rax'.") + add_option("service_type", + help="Service type is a name associated for the catalog") + add_option("service_name", + help="Service name as provided in the service catalog") + add_option("service_url", + help="Service endpoint to use if the catalog doesn't have one.") + add_option("region", help="Region the service is located in") + add_option("insecure", action="store_true", + help="Run in insecure mode for https endpoints.") + add_option("token", help="Token from a prior login.") + add_option("xml", action="store_true", help="Changes format to XML.") + + + oparser.add_option("--secure", action="store_false", dest="insecure", + help="Run in insecure mode for https endpoints.") + oparser.add_option("--json", action="store_false", dest="xml", + help="Changes format to JSON.") + oparser.add_option("--terse", action="store_false", dest="verbose", + help="Toggles verbose mode off.") + oparser.add_option("--hide-debug", action="store_false", dest="debug", + help="Toggles debug mode off.") + return oparser + + +class ArgumentRequired(Exception): + def __init__(self, param): + self.param = param + + def __str__(self): + return 'Argument "--%s" required.' % self.param + + +class CommandsBase(object): + params = [] + + def __init__(self, parser): + self._parse_options(parser) + + def get_client(self): + """Creates the all important client object.""" + try: + if self.xml: + client_cls = ReddwarfXmlClient + else: + client_cls = client.ReddwarfHTTPClient + if self.verbose: + client.log_to_streamhandler(sys.stdout) + client.RDC_PP = True + return client.Dbaas(self.username, self.apikey, self.tenant_id, + auth_url=self.auth_url, + auth_strategy=self.auth_type, + service_type=self.service_type, + service_name=self.service_name, + region_name=self.region, + service_url=self.service_url, + insecure=self.insecure, + client_cls=client_cls) + except: + if self.debug: + raise + print sys.exc_info()[1] + + def _safe_exec(self, func, *args, **kwargs): + if not self.debug: + try: + return func(*args, **kwargs) + except: + print(sys.exc_info()[1]) + return None + else: + return func(*args, **kwargs) + + @classmethod + def _prepare_parser(cls, parser): + for param in cls.params: + parser.add_option("--%s" % param) + + def _parse_options(self, parser): + opts, args = parser.parse_args() + for param in opts.__dict__: + value = getattr(opts, param) + setattr(self, param, value) + + def _require(self, *params): + for param in params: + if not hasattr(self, param): + raise ArgumentRequired(param) + if not getattr(self, param): + raise ArgumentRequired(param) + + def _make_list(self, *params): + # Convert the listed params to lists. + for param in params: + raw = getattr(self, param) + if isinstance(raw, list): + return + raw = [item.strip() for item in raw.split(',')] + setattr(self, param, raw) + + def _pretty_print(self, func, *args, **kwargs): + if self.verbose: + self._safe_exec(func, *args, **kwargs) + return # Skip this, since the verbose stuff will show up anyway. + def wrapped_func(): + result = func(*args, **kwargs) + if result: + print(json.dumps(result._info, sort_keys=True, indent=4)) + else: + print("OK") + self._safe_exec(wrapped_func) + + def _dumps(self, item): + return json.dumps(item, sort_keys=True, indent=4) + + def _pretty_list(self, func, *args, **kwargs): + result = self._safe_exec(func, *args, **kwargs) + if self.verbose: + return + if result and len(result) > 0: + for item in result: + print(self._dumps(item._info)) + else: + print("OK") + + def _pretty_paged(self, func, *args, **kwargs): + try: + limit = self.limit + if limit: + limit = int(limit, 10) + result = func(*args, limit=limit, marker=self.marker, **kwargs) + if self.verbose: + return # Verbose already shows the output, so skip this. + if result and len(result) > 0: + for item in result: + print self._dumps(item._info) + if result.links: + print("Links:") + for link in result.links: + print self._dumps((link)) + else: + print("OK") + except: + if self.debug: + raise + print sys.exc_info()[1] + + +class Auth(CommandsBase): + """Authenticate with your username and api key""" + params = [ + 'apikey', + 'auth_strategy', + 'auth_type', + 'auth_url', + 'options', + 'region', + 'service_name', + 'service_type', + 'service_url', + 'tenant_id', + 'username', + ] + + def __init__(self, parser): + super(Auth, self).__init__(parser) + self.dbaas = None + + def login(self): + """Login to retrieve an auth token to use for other api calls""" + self._require('username', 'apikey', 'tenant_id', 'auth_url') + try: + self.dbaas = self.get_client() + self.dbaas.authenticate() + self.token = self.dbaas.client.auth_token + self.service_url = self.dbaas.client.service_url + CliOptions.save_from_instance_fields(self) + print(self.token) + except: + if self.debug: + raise + print sys.exc_info()[1] + + +class AuthedCommandsBase(CommandsBase): + """Commands that work only with an authicated client.""" + + def __init__(self, parser): + """Makes sure a token is available somehow and logs in.""" + super(AuthedCommandsBase, self).__init__(parser) + try: + self._require('token') + except ArgumentRequired: + if self.debug: + raise + print('No token argument supplied. Use the "auth login" command ' + 'to log in and get a token.\n') + sys.exit(1) + try: + self._require('service_url') + except ArgumentRequired: + if self.debug: + raise + print('No service_url given.\n') + sys.exit(1) + self.dbaas = self.get_client() + # Actually set the token to avoid a re-auth. + self.dbaas.client.auth_token = self.token + self.dbaas.client.authenticate_with_token(self.token, self.service_url) + + +class Paginated(object): + """ Pretends to be a list if you iterate over it, but also keeps a + next property you can use to get the next page of data. """ + + def __init__(self, items=[], next_marker=None, links=[]): + self.items = items + self.next = next_marker + self.links = links + + def __len__(self): + return len(self.items) + + def __iter__(self): + return self.items.__iter__() + + def __getitem__(self, key): + return self.items[key] + + def __setitem__(self, key, value): + self.items[key] = value + + def __delitem(self, key): + del self.items[key] + + def __reversed__(self): + return reversed(self.items) + + def __contains__(self, needle): + return needle in self.items diff --git a/reddwarfclient/databases.py b/reddwarfclient/databases.py new file mode 100644 index 0000000..d7f31e1 --- /dev/null +++ b/reddwarfclient/databases.py @@ -0,0 +1,79 @@ +from reddwarfclient import base +from reddwarfclient.common import check_for_exceptions +from reddwarfclient.common import limit_url +from reddwarfclient.common import Paginated +import exceptions +import urlparse + + +class Database(base.Resource): + """ + According to Wikipedia, "A database is a system intended to organize, + store, and retrieve + large amounts of data easily." + """ + def __repr__(self): + return "" % self.name + + +class Databases(base.ManagerWithFind): + """ + Manage :class:`Databases` resources. + """ + resource_class = Database + + def create(self, instance_id, databases): + """ + Create new databases within the specified instance + """ + body = {"databases": databases} + url = "/instances/%s/databases" % instance_id + resp, body = self.api.client.post(url, body=body) + check_for_exceptions(resp, body) + + def delete(self, instance_id, dbname): + """Delete an existing database in the specified instance""" + url = "/instances/%s/databases/%s" % (instance_id, dbname) + resp, body = self.api.client.delete(url) + check_for_exceptions(resp, body) + + def _list(self, url, response_key, limit=None, marker=None): + resp, body = self.api.client.get(limit_url(url, limit, marker)) + check_for_exceptions(resp, body) + if not body: + raise Exception("Call to " + url + + " did not return a body.") + links = body.get('links', []) + next_links = [link['href'] for link in links if link['rel'] == 'next'] + next_marker = None + for link in next_links: + # Extract the marker from the url. + parsed_url = urlparse.urlparse(link) + query_dict = dict(urlparse.parse_qsl(parsed_url.query)) + next_marker = query_dict.get('marker', None) + databases = body[response_key] + databases = [self.resource_class(self, res) for res in databases] + return Paginated(databases, next_marker=next_marker, links=links) + + def list(self, instance, limit=None, marker=None): + """ + Get a list of all Databases from the instance. + + :rtype: list of :class:`Database`. + """ + return self._list("/instances/%s/databases" % base.getid(instance), + "databases", limit, marker) + +# def get(self, instance, database): +# """ +# Get a specific instances. +# +# :param flavor: The ID of the :class:`Database` to get. +# :rtype: :class:`Database` +# """ +# assert isinstance(instance, Instance) +# assert isinstance(database, (Database, int)) +# instance_id = base.getid(instance) +# db_id = base.getid(database) +# url = "/instances/%s/databases/%s" % (instance_id, db_id) +# return self._get(url, "database") diff --git a/reddwarfclient/diagnostics.py b/reddwarfclient/diagnostics.py new file mode 100644 index 0000000..64904b7 --- /dev/null +++ b/reddwarfclient/diagnostics.py @@ -0,0 +1,58 @@ +# Copyright (c) 2011 OpenStack, LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from reddwarfclient import base +import exceptions + + +class Diagnostics(base.Resource): + """ + Account is an opaque instance used to hold account information. + """ + def __repr__(self): + return "" % self.version + + +class DiagnosticsInterrogator(base.ManagerWithFind): + """ + Manager class for Interrogator resource + """ + resource_class = Diagnostics + + def get(self, instance): + """ + Get the diagnostics of the guest on the instance. + """ + return self._get("/mgmt/instances/%s/diagnostics" % base.getid(instance), + "diagnostics") + + +class HwInfo(base.Resource): + + def __repr__(self): + return "" % self.version + + +class HwInfoInterrogator(base.ManagerWithFind): + """ + Manager class for HwInfo + """ + resource_class = HwInfo + + def get(self, instance): + """ + Get the hardware information of the instance. + """ + return self._get("/mgmt/instances/%s/hwinfo" % base.getid(instance)) diff --git a/reddwarfclient/exceptions.py b/reddwarfclient/exceptions.py new file mode 100644 index 0000000..eb305a3 --- /dev/null +++ b/reddwarfclient/exceptions.py @@ -0,0 +1,177 @@ +# Copyright 2011 OpenStack LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +class UnsupportedVersion(Exception): + """Indicates that the user is trying to use an unsupported + version of the API""" + pass + + +class CommandError(Exception): + pass + + +class AuthorizationFailure(Exception): + pass + + +class NoUniqueMatch(Exception): + pass + + +class NoTokenLookupException(Exception): + """This form of authentication does not support looking up + endpoints from an existing token.""" + pass + + +class EndpointNotFound(Exception): + """Could not find Service or Region in Service Catalog.""" + pass + + +class AuthUrlNotGiven(EndpointNotFound): + """The auth url was not given.""" + pass + + +class ServiceUrlNotGiven(EndpointNotFound): + """The service url was not given.""" + pass + + +class ResponseFormatError(Exception): + """Could not parse the response format.""" + pass + +class AmbiguousEndpoints(Exception): + """Found more than one matching endpoint in Service Catalog.""" + def __init__(self, endpoints=None): + self.endpoints = endpoints + + def __str__(self): + return "AmbiguousEndpoints: %s" % repr(self.endpoints) + + +class ClientException(Exception): + """ + The base exception class for all exceptions this library raises. + """ + def __init__(self, code, message=None, details=None, request_id=None): + self.code = code + self.message = message or self.__class__.message + self.details = details + self.request_id = request_id + + def __str__(self): + formatted_string = "%s (HTTP %s)" % (self.message, self.code) + if self.request_id: + formatted_string += " (Request-ID: %s)" % self.request_id + + return formatted_string + + +class BadRequest(ClientException): + """ + HTTP 400 - Bad request: you sent some malformed data. + """ + http_status = 400 + message = "Bad request" + + +class Unauthorized(ClientException): + """ + HTTP 401 - Unauthorized: bad credentials. + """ + http_status = 401 + message = "Unauthorized" + + +class Forbidden(ClientException): + """ + HTTP 403 - Forbidden: your credentials don't give you access to this + resource. + """ + http_status = 403 + message = "Forbidden" + + +class NotFound(ClientException): + """ + HTTP 404 - Not found + """ + http_status = 404 + message = "Not found" + + +class OverLimit(ClientException): + """ + HTTP 413 - Over limit: you're over the API limits for this time period. + """ + http_status = 413 + message = "Over limit" + + +# NotImplemented is a python keyword. +class HTTPNotImplemented(ClientException): + """ + HTTP 501 - Not Implemented: the server does not support this operation. + """ + http_status = 501 + message = "Not Implemented" + + +class UnprocessableEntity(ClientException): + """ + HTTP 422 - Unprocessable Entity: The request cannot be processed. + """ + http_status = 422 + message = "Unprocessable Entity" + + +# In Python 2.4 Exception is old-style and thus doesn't have a __subclasses__() +# so we can do this: +# _code_map = dict((c.http_status, c) +# for c in ClientException.__subclasses__()) +# +# Instead, we have to hardcode it: +_code_map = dict((c.http_status, c) for c in [BadRequest, Unauthorized, + Forbidden, NotFound, OverLimit, + HTTPNotImplemented, + UnprocessableEntity]) + + +def from_response(response, body): + """ + Return an instance of an ClientException or subclass + based on an httplib2 response. + + Usage:: + + resp, body = http.request(...) + if resp.status != 200: + raise exception_from_response(resp, body) + """ + cls = _code_map.get(response.status, ClientException) + if body: + message = "n/a" + details = "n/a" + if hasattr(body, 'keys'): + error = body[body.keys()[0]] + message = error.get('message', None) + details = error.get('details', None) + return cls(code=response.status, message=message, details=details) + else: + request_id = response.get('x-compute-request-id') + return cls(code=response.status, request_id=request_id) diff --git a/reddwarfclient/flavors.py b/reddwarfclient/flavors.py new file mode 100644 index 0000000..ba01a5f --- /dev/null +++ b/reddwarfclient/flavors.py @@ -0,0 +1,62 @@ +# Copyright (c) 2012 OpenStack, LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +from reddwarfclient import base + +import exceptions + +from reddwarfclient.common import check_for_exceptions + + +class Flavor(base.Resource): + """ + A Flavor is an Instance type, specifying among other things, RAM size. + """ + def __repr__(self): + return "" % self.name + + +class Flavors(base.ManagerWithFind): + """ + Manage :class:`Flavor` resources. + """ + resource_class = Flavor + + def __repr__(self): + return "" % id(self) + + def _list(self, url, response_key): + resp, body = self.api.client.get(url) + if not body: + raise Exception("Call to " + url + " did not return a body.") + return [self.resource_class(self, res) for res in body[response_key]] + + def list(self): + """ + Get a list of all flavors. + + :rtype: list of :class:`Flavor`. + """ + return self._list("/flavors", "flavors") + + def get(self, flavor): + """ + Get a specific flavor. + + :rtype: :class:`Flavor` + """ + return self._get("/flavors/%s" % base.getid(flavor), + "flavor") diff --git a/reddwarfclient/hosts.py b/reddwarfclient/hosts.py new file mode 100644 index 0000000..96bc621 --- /dev/null +++ b/reddwarfclient/hosts.py @@ -0,0 +1,61 @@ +# Copyright (c) 2011 OpenStack, LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from reddwarfclient import base + + +class Host(base.Resource): + """ + A Hosts is an opaque instance used to store Host instances. + """ + def __repr__(self): + return "" % self.name + + +class Hosts(base.ManagerWithFind): + """ + Manage :class:`Host` resources. + """ + resource_class = Host + + def _list(self, url, response_key): + resp, body = self.api.client.get(url) + if not body: + raise Exception("Call to " + url + " did not return a body.") + return [self.resource_class(self, res) for res in body[response_key]] + + def index(self): + """ + Get a list of all hosts. + + :rtype: list of :class:`Hosts`. + """ + return self._list("/mgmt/hosts", "hosts") + + def get(self, host): + """ + Get a specific host. + + :rtype: :class:`host` + """ + return self._get("/mgmt/hosts/%s" % self._get_host_name(host), "host") + + @staticmethod + def _get_host_name(host): + try: + if host.name: + return host.name + except AttributeError: + return host diff --git a/reddwarfclient/instances.py b/reddwarfclient/instances.py new file mode 100644 index 0000000..3e095f4 --- /dev/null +++ b/reddwarfclient/instances.py @@ -0,0 +1,169 @@ +# Copyright (c) 2011 OpenStack, LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from reddwarfclient import base + +import exceptions +import urlparse + +from reddwarfclient.common import check_for_exceptions +from reddwarfclient.common import limit_url +from reddwarfclient.common import Paginated + + +REBOOT_SOFT, REBOOT_HARD = 'SOFT', 'HARD' + + +class Instance(base.Resource): + """ + An Instance is an opaque instance used to store Database instances. + """ + def __repr__(self): + return "" % self.name + + def list_databases(self): + return self.manager.databases.list(self) + + def delete(self): + """ + Delete the instance. + """ + self.manager.delete(self) + + def restart(self): + """ + Restart the database instance + """ + self.manager.restart(self.id) + + +class Instances(base.ManagerWithFind): + """ + Manage :class:`Instance` resources. + """ + resource_class = Instance + + def create(self, name, flavor_id, volume, databases=None, users=None): + """ + Create (boot) a new instance. + """ + body = {"instance": { + "name": name, + "flavorRef": flavor_id, + "volume": volume + }} + if databases: + body["instance"]["databases"] = databases + if users: + body["instance"]["users"] = users + + return self._create("/instances", body, "instance") + + def _list(self, url, response_key, limit=None, marker=None): + resp, body = self.api.client.get(limit_url(url, limit, marker)) + if not body: + raise Exception("Call to " + url + " did not return a body.") + links = body.get('links', []) + next_links = [link['href'] for link in links if link['rel'] == 'next'] + next_marker = None + for link in next_links: + # Extract the marker from the url. + parsed_url = urlparse.urlparse(link) + query_dict = dict(urlparse.parse_qsl(parsed_url.query)) + next_marker = query_dict.get('marker', None) + instances = body[response_key] + instances = [self.resource_class(self, res) for res in instances] + return Paginated(instances, next_marker=next_marker, links=links) + + def list(self, limit=None, marker=None): + """ + Get a list of all instances. + + :rtype: list of :class:`Instance`. + """ + return self._list("/instances", "instances", limit, marker) + + def get(self, instance): + """ + Get a specific instances. + + :rtype: :class:`Instance` + """ + return self._get("/instances/%s" % base.getid(instance), + "instance") + + def delete(self, instance): + """ + Delete the specified instance. + + :param instance_id: The instance id to delete + """ + resp, body = self.api.client.delete("/instances/%s" % + base.getid(instance)) + if resp.status in (422, 500): + raise exceptions.from_response(resp, body) + + def _action(self, instance_id, body): + """ + Perform a server "action" -- reboot/rebuild/resize/etc. + """ + url = "/instances/%s/action" % instance_id + resp, body = self.api.client.post(url, body=body) + check_for_exceptions(resp, body) + return body + + def resize_volume(self, instance_id, volume_size): + """ + Resize the volume on an existing instances + """ + body = {"resize": {"volume": {"size": volume_size}}} + self._action(instance_id, body) + + def resize_instance(self, instance_id, flavor_id): + """ + Resize the volume on an existing instances + """ + body = {"resize": {"flavorRef": flavor_id}} + self._action(instance_id, body) + + def restart(self, instance_id): + """ + Restart the database instance. + + :param instance_id: The :class:`Instance` (or its ID) to share onto. + """ + body = {'restart': {}} + self._action(instance_id, body) + + def reset_password(self, instance_id): + """ + Resets the database instance root password. + + :param instance_id: The :class:`Instance` (or its ID) to share onto. + """ + body = {'reset-password': {}} + return self._action(instance_id, body) + +Instances.resize_flavor = Instances.resize_instance + +class InstanceStatus(object): + + ACTIVE = "ACTIVE" + BLOCKED = "BLOCKED" + BUILD = "BUILD" + FAILED = "FAILED" + REBOOT = "REBOOT" + RESIZE = "RESIZE" + SHUTDOWN = "SHUTDOWN" diff --git a/reddwarfclient/management.py b/reddwarfclient/management.py new file mode 100644 index 0000000..5a695f7 --- /dev/null +++ b/reddwarfclient/management.py @@ -0,0 +1,113 @@ +# Copyright (c) 2011 OpenStack, LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from reddwarfclient import base +import urlparse + +from reddwarfclient.common import check_for_exceptions +from reddwarfclient.common import limit_url +from reddwarfclient.common import Paginated +from reddwarfclient.instances import Instance + + +class RootHistory(base.Resource): + def __repr__(self): + return ("" + % (self.id, self.created, self.user)) + + +class Management(base.ManagerWithFind): + """ + Manage :class:`Instances` resources. + """ + resource_class = Instance + + def _list(self, url, response_key, limit=None, marker=None): + resp, body = self.api.client.get(limit_url(url, limit, marker)) + if not body: + raise Exception("Call to " + url + " did not return a body.") + links = body.get('links', []) + next_links = [link['href'] for link in links if link['rel'] == 'next'] + next_marker = None + for link in next_links: + # Extract the marker from the url. + parsed_url = urlparse.urlparse(link) + query_dict = dict(urlparse.parse_qsl(parsed_url.query)) + next_marker = query_dict.get('marker', None) + instances = body[response_key] + instances = [self.resource_class(self, res) for res in instances] + return Paginated(instances, next_marker=next_marker, links=links) + + def show(self, instance): + """ + Get details of one instance. + + :rtype: :class:`Instance`. + """ + + return self._get("/mgmt/instances/%s" % base.getid(instance), + 'instance') + + def index(self, deleted=None, limit=None, marker=None): + """ + Show an overview of all local instances. + Optionally, filter by deleted status. + + :rtype: list of :class:`Instance`. + """ + form = '' + if deleted is not None: + if deleted: + form = "?deleted=true" + else: + form = "?deleted=false" + + url = "/mgmt/instances%s" % form + return self._list(url, "instances", limit, marker) + + def root_enabled_history(self, instance): + """ + Get root access history of one instance. + + """ + url = "/mgmt/instances/%s/root" % base.getid(instance) + resp, body = self.api.client.get(url) + if not body: + raise Exception("Call to " + url + " did not return a body.") + return RootHistory(self, body['root_history']) + + def _action(self, instance_id, body): + """ + Perform a server "action" -- reboot/rebuild/resize/etc. + """ + url = "/mgmt/instances/%s/action" % instance_id + resp, body = self.api.client.post(url, body=body) + check_for_exceptions(resp, body) + + def reboot(self, instance_id): + """ + Reboot the underlying OS. + + :param instance_id: The :class:`Instance` (or its ID) to share onto. + """ + body = {'reboot': {}} + self._action(instance_id, body) + + def update(self, instance_id): + """ + Update the guest agent via apt-get. + """ + body = {'update': {}} + self._action(instance_id, body) diff --git a/reddwarfclient/mcli.py b/reddwarfclient/mcli.py new file mode 100644 index 0000000..c6ca904 --- /dev/null +++ b/reddwarfclient/mcli.py @@ -0,0 +1,214 @@ +#!/usr/bin/env python + +# Copyright 2011 OpenStack LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +""" +Reddwarf Management Command line tool +""" + +import json +import optparse +import os +import sys + + +# If ../reddwarf/__init__.py exists, add ../ to Python search path, so that +# it will override what happens to be installed in /usr/(local/)lib/python... +possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), + os.pardir, + os.pardir)) +if os.path.exists(os.path.join(possible_topdir, 'reddwarfclient', + '__init__.py')): + sys.path.insert(0, possible_topdir) + + +from reddwarfclient import common + + +oparser = None + + +def _pretty_print(info): + print json.dumps(info, sort_keys=True, indent=4) + + +class HostCommands(common.AuthedCommandsBase): + """Commands to list info on hosts""" + + params = [ + 'name', + ] + + def update_all(self): + """Update all instances on a host""" + self._require('name') + self.dbaas.hosts.update_all(self.name) + + def get(self): + """List details for the specified host""" + self._require('name') + self._pretty_print(self.dbaas.hosts.get, self.name) + + def list(self): + """List all compute hosts""" + self._pretty_list(self.dbaas.hosts.index) + + +class RootCommands(common.AuthedCommandsBase): + """List details about the root info for an instance.""" + + params = [ + 'id', + ] + + def history(self): + """List root history for the instance.""" + self._require('id') + self._pretty_print(self.dbaas.management.root_enabled_history, self.id) + + +class AccountCommands(common.AuthedCommandsBase): + """Commands to list account info""" + + params = [ + 'id', + ] + + def list(self): + """List all accounts with non-deleted instances""" + self._pretty_print(self.dbaas.accounts.index) + + def get(self): + """List details for the account provided""" + self._require('id') + self._pretty_print(self.dbaas.accounts.show, self.id) + + +class InstanceCommands(common.AuthedCommandsBase): + """List details about an instance.""" + + params = [ + 'deleted', + 'id', + 'limit', + 'marker', + ] + + def get(self): + """List details for the instance.""" + self._require('id') + self._pretty_print(self.dbaas.management.show, self.id) + + def list(self): + """List all instances for account""" + deleted = None + if self.deleted is not None: + if self.deleted.lower() in ['true']: + deleted = True + elif self.deleted.lower() in ['false']: + deleted = False + self._pretty_paged(self.dbaas.management.index, deleted=deleted) + + def hwinfo(self): + """Show hardware information details about an instance.""" + self._require('id') + self._pretty_print(self.dbaas.hwinfo.get, self.id) + + def diagnostic(self): + """List diagnostic details about an instance.""" + self._require('id') + self._pretty_print(self.dbaas.diagnostics.get, self.id) + + def stop(self): + """Stop MySQL on the given instance.""" + self._require('id') + self._pretty_print(self.dbaas.management.stop, self.id) + + def reboot(self): + """Reboot the instance.""" + self._require('id') + self._pretty_print(self.dbaas.management.reboot, self.id) + + +class StorageCommands(common.AuthedCommandsBase): + """Commands to list devices info""" + + params = [] + + def list(self): + """List details for the storage device""" + dbaas = common.get_client() + self._pretty_list(self.dbaas.storage.index) + + +def config_options(oparser): + oparser.add_option("-u", "--url", default="http://localhost:5000/v1.1", + help="Auth API endpoint URL with port and version. \ + Default: http://localhost:5000/v1.1") + + +COMMANDS = {'account': AccountCommands, + 'host': HostCommands, + 'instance': InstanceCommands, + 'root': RootCommands, + 'storage': StorageCommands, + } + + +def main(): + # Parse arguments + oparser = common.CliOptions.create_optparser() + for k, v in COMMANDS.items(): + v._prepare_parser(oparser) + (options, args) = oparser.parse_args() + + if not args: + common.print_commands(COMMANDS) + + # Pop the command and check if it's in the known commands + cmd = args.pop(0) + if cmd in COMMANDS: + fn = COMMANDS.get(cmd) + command_object = None + try: + command_object = fn(oparser) + except Exception as ex: + if options.debug: + raise + print(ex) + + # Get a list of supported actions for the command + actions = common.methods_of(command_object) + + if len(args) < 1: + common.print_actions(cmd, actions) + + # Check for a valid action and perform that action + action = args.pop(0) + if action in actions: + try: + getattr(command_object, action)() + except Exception as ex: + if options.debug: + raise + print ex + else: + common.print_actions(cmd, actions) + else: + common.print_commands(COMMANDS) + + +if __name__ == '__main__': + main() diff --git a/reddwarfclient/root.py b/reddwarfclient/root.py new file mode 100644 index 0000000..33b0da7 --- /dev/null +++ b/reddwarfclient/root.py @@ -0,0 +1,44 @@ +# Copyright (c) 2011 OpenStack, LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from reddwarfclient import base + +from reddwarfclient import users +from reddwarfclient.common import check_for_exceptions +import exceptions + + +class Root(base.ManagerWithFind): + """ + Manager class for Root resource + """ + resource_class = users.User + url = "/instances/%s/root" + + def create(self, instance_id): + """ + Enable the root user and return the root password for the + sepcified db instance + """ + resp, body = self.api.client.post(self.url % instance_id) + check_for_exceptions(resp, body) + return body['user']['name'], body['user']['password'] + + def is_root_enabled(self, instance_id): + """ Return True if root is enabled for the instance; + False otherwise""" + resp, body = self.api.client.get(self.url % instance_id) + check_for_exceptions(resp, body) + return body['rootEnabled'] diff --git a/reddwarfclient/storage.py b/reddwarfclient/storage.py new file mode 100644 index 0000000..653096e --- /dev/null +++ b/reddwarfclient/storage.py @@ -0,0 +1,45 @@ +# Copyright (c) 2011 OpenStack, LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from reddwarfclient import base + + +class Device(base.Resource): + """ + Storage is an opaque instance used to hold storage information. + """ + def __repr__(self): + return "" % self.name + + +class StorageInfo(base.ManagerWithFind): + """ + Manage :class:`Storage` resources. + """ + resource_class = Device + + def _list(self, url, response_key): + resp, body = self.api.client.get(url) + if not body: + raise Exception("Call to " + url + " did not return a body.") + return [self.resource_class(self, res) for res in body[response_key]] + + def index(self): + """ + Get a list of all storages. + + :rtype: list of :class:`Storages`. + """ + return self._list("/mgmt/storage", "devices") diff --git a/reddwarfclient/users.py b/reddwarfclient/users.py new file mode 100644 index 0000000..4ee6d33 --- /dev/null +++ b/reddwarfclient/users.py @@ -0,0 +1,77 @@ +# Copyright (c) 2011 OpenStack, LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from reddwarfclient import base +from reddwarfclient.common import check_for_exceptions +from reddwarfclient.common import limit_url +from reddwarfclient.common import Paginated +import exceptions +import urlparse + + +class User(base.Resource): + """ + A database user + """ + def __repr__(self): + return "" % self.name + + +class Users(base.ManagerWithFind): + """ + Manage :class:`Users` resources. + """ + resource_class = User + + def create(self, instance_id, users): + """ + Create users with permissions to the specified databases + """ + body = {"users": users} + url = "/instances/%s/users" % instance_id + resp, body = self.api.client.post(url, body=body) + check_for_exceptions(resp, body) + + def delete(self, instance_id, user): + """Delete an existing user in the specified instance""" + url = "/instances/%s/users/%s" % (instance_id, user) + resp, body = self.api.client.delete(url) + check_for_exceptions(resp, body) + + def _list(self, url, response_key, limit=None, marker=None): + resp, body = self.api.client.get(limit_url(url, limit, marker)) + check_for_exceptions(resp, body) + if not body: + raise Exception("Call to " + url + + " did not return a body.") + links = body.get('links', []) + next_links = [link['href'] for link in links if link['rel'] == 'next'] + next_marker = None + for link in next_links: + # Extract the marker from the url. + parsed_url = urlparse.urlparse(link) + query_dict = dict(urlparse.parse_qsl(parsed_url.query)) + next_marker = query_dict.get('marker', None) + users = [self.resource_class(self, res) for res in body[response_key]] + return Paginated(users, next_marker=next_marker, links=links) + + def list(self, instance, limit=None, marker=None): + """ + Get a list of all Users from the instance's Database. + + :rtype: list of :class:`User`. + """ + return self._list("/instances/%s/users" % base.getid(instance), + "users", limit, marker) diff --git a/reddwarfclient/utils.py b/reddwarfclient/utils.py new file mode 100644 index 0000000..3deb806 --- /dev/null +++ b/reddwarfclient/utils.py @@ -0,0 +1,68 @@ +# Copyright 2012 OpenStack LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import os +import re +import sys + + +class HookableMixin(object): + """Mixin so classes can register and run hooks.""" + _hooks_map = {} + + @classmethod + def add_hook(cls, hook_type, hook_func): + if hook_type not in cls._hooks_map: + cls._hooks_map[hook_type] = [] + + cls._hooks_map[hook_type].append(hook_func) + + @classmethod + def run_hooks(cls, hook_type, *args, **kwargs): + hook_funcs = cls._hooks_map.get(hook_type) or [] + for hook_func in hook_funcs: + hook_func(*args, **kwargs) + + +def env(*vars, **kwargs): + """ + returns the first environment variable set + if none are non-empty, defaults to '' or keyword arg default + """ + for v in vars: + value = os.environ.get(v, None) + if value: + return value + return kwargs.get('default', '') + + +_slugify_strip_re = re.compile(r'[^\w\s-]') +_slugify_hyphenate_re = re.compile(r'[-\s]+') + + +# http://code.activestate.com/recipes/ +# 577257-slugify-make-a-string-usable-in-a-url-or-filename/ +def slugify(value): + """ + Normalizes string, converts to lowercase, removes non-alpha characters, + and converts spaces to hyphens. + + From Django's "django/template/defaultfilters.py". + """ + import unicodedata + if not isinstance(value, unicode): + value = unicode(value) + value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore') + value = unicode(_slugify_strip_re.sub('', value).strip().lower()) + return _slugify_hyphenate_re.sub('-', value) diff --git a/reddwarfclient/versions.py b/reddwarfclient/versions.py new file mode 100644 index 0000000..f7b52c4 --- /dev/null +++ b/reddwarfclient/versions.py @@ -0,0 +1,41 @@ +# Copyright (c) 2011 OpenStack, LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from reddwarfclient import base + + +class Version(base.Resource): + """ + Version is an opaque instance used to hold version information. + """ + def __repr__(self): + return "" % self.id + + +class Versions(base.ManagerWithFind): + """ + Manage :class:`Versions` information. + """ + + resource_class = Version + + def index(self, url): + """ + Get a list of all versions. + + :rtype: list of :class:`Versions`. + """ + resp, body = self.api.client.request(url, "GET") + return [self.resource_class(self, res) for res in body['versions']] diff --git a/reddwarfclient/xml.py b/reddwarfclient/xml.py new file mode 100644 index 0000000..d0b7f9b --- /dev/null +++ b/reddwarfclient/xml.py @@ -0,0 +1,209 @@ +from lxml import etree +import json +from numbers import Number + +from reddwarfclient import exceptions +from reddwarfclient.client import ReddwarfHTTPClient + + +XML_NS = { None: "http://docs.openstack.org/database/api/v1.0" } + +# This dictionary contains XML paths of things that should become list items. +LISTIFY = { + "accounts":[[]], + "databases":[[]], + "flavors": [[]], + "instances": [[]], + "links" : [["flavor", "instance", "instances"], + ["instance", "instances"]], + "hosts": [[]], + "devices": [[]], + "users": [[]], + "versions": [[]], +} + +REQUEST_AS_LIST = set(['databases', 'users']) + +def element_ancestors_match_list(element, list): + """ + For element root at matches against + list ["blah", "foo"]. + """ + itr_elem = element.getparent() + for name in list: + if itr_elem is None: + break + if name != normalize_tag(itr_elem): + return False + itr_elem = itr_elem.getparent() + return True + + +def element_must_be_list(parent_element, name): + """Determines if an element to be created should be a dict or list.""" + if name in LISTIFY: + list_of_lists = LISTIFY[name] + for tag_list in list_of_lists: + if element_ancestors_match_list(parent_element, tag_list): + return True + return False + + +def element_to_json(name, element): + if element_must_be_list(element, name): + return element_to_list(element) + else: + return element_to_dict(element) + +def root_element_to_json(name, element): + """Returns a tuple of the root JSON value, plus the links if found.""" + if name == "rootEnabled": # Why oh why were we inconsistent here? :'( + return bool(element.text), None + elif element_must_be_list(element, name): + return element_to_list(element, True) + else: + return element_to_dict(element), None + + +def element_to_list(element, check_for_links=False): + """ + For element "foo" in + Returns [{}, {}] + """ + links = None + result = [] + for child_element in element: + # The "links" element gets jammed into the root element. + if check_for_links and normalize_tag(child_element) == "links": + links = element_to_list(child_element) + else: + result.append(element_to_dict(child_element)) + if check_for_links: + return result, links + else: + return result + + +def element_to_dict(element): + result = {} + for name, value in element.items(): + result[name] = value + for child_element in element: + name = normalize_tag(child_element) + result[name] = element_to_json(name, child_element) + return result + + +def standardize_json_lists(json_dict): + """ + In XML, we might see something like {'instances':{'instances':[...]}}, + which we must change to just {'instances':[...]} to be compatable with + the true JSON format. + + If any items are dictionaries with only one item which is a list, + simply remove the dictionary and insert its list directly. + """ + found_items = [] + for key, value in json_dict.items(): + value = json_dict[key] + if isinstance(value, dict): + if len(value) == 1 and isinstance(value.values()[0], list): + found_items.append(key) + else: + standardize_json_lists(value) + for key in found_items: + json_dict[key] = json_dict[key].values()[0] + + +def normalize_tag(elem): + """Given an element, returns the tag minus the XMLNS junk. + + IOW, .tag may sometimes return the XML namespace at the start of the + string. This gets rids of that. + """ + try: + prefix = "{" + elem.nsmap[None] + "}" + if elem.tag.startswith(prefix): + return elem.tag[len(prefix):] + except KeyError: + pass + return elem.tag + + +def create_root_xml_element(name, value): + """Create the first element using a name and a dictionary.""" + element = etree.Element(name, nsmap=XML_NS) + if name in REQUEST_AS_LIST: + add_subelements_from_list(element, name, value) + else: + populate_element_from_dict(element, value) + return element + + +def create_subelement(parent_element, name, value): + """Attaches a new element onto the parent element.""" + if isinstance(value, dict): + create_subelement_from_dict(parent_element, name, value) + elif isinstance(value, list): + create_subelement_from_list(parent_element, name, value) + else: + raise TypeError("Can't handle type %s." % type(value)) + + +def create_subelement_from_dict(parent_element, name, dict): + element = etree.SubElement(parent_element, name) + populate_element_from_dict(element, dict) + + +def create_subelement_from_list(parent_element, name, list): + element = etree.SubElement(parent_element, name) + add_subelements_from_list(element, name, list) + + +def add_subelements_from_list(element, name, list): + if name.endswith("s"): + item_name = name[:len(name) - 1] + else: + item_name = name + for item in list: + create_subelement(element, item_name, item) + + +def populate_element_from_dict(element, dict): + for key, value in dict.items(): + if isinstance(value, basestring): + element.set(key, value) + elif isinstance(value, Number): + element.set(key, str(value)) + else: + create_subelement(element, key, value) + + +class ReddwarfXmlClient(ReddwarfHTTPClient): + + @classmethod + def morph_request(self, kwargs): + kwargs['headers']['Accept'] = 'application/xml' + kwargs['headers']['Content-Type'] = 'application/xml' + if 'body' in kwargs: + body = kwargs['body'] + root_name = body.keys()[0] + xml = create_root_xml_element(root_name, body[root_name]) + xml_string = etree.tostring(xml, pretty_print=True) + kwargs['body'] = xml_string + + @classmethod + def morph_response_body(self, body_string): + # The root XML element always becomes a dictionary with a single + # field, which has the same key as the elements name. + result = {} + try: + root_element = etree.XML(body_string) + except etree.XMLSyntaxError: + raise exceptions.ResponseFormatError() + root_name = normalize_tag(root_element) + root_value, links = root_element_to_json(root_name, root_element) + result = { root_name:root_value } + if links: + result['links'] = links + return result diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..037af38 --- /dev/null +++ b/setup.py @@ -0,0 +1,61 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import os +import setuptools +import sys + + +requirements = ["httplib2", "lxml", "prettytable"] +if sys.version_info < (2, 6): + requirements.append("simplejson") +if sys.version_info < (2, 7): + requirements.append("argparse") + + +def read_file(file_name): + return open(os.path.join(os.path.dirname(__file__), file_name)).read() + + +setuptools.setup( + name="python-reddwarfclient", + version="2012.3", + author="Rackspace", + description="Rich client bindings for Reddwarf REST API.", + long_description="""Rich client bindings for Reddwarf REST API.""", + license="Apache License, Version 2.0", + url="https://github.com/openstack/python-reddwarfclient", + packages=["reddwarfclient"], + install_requires=requirements, + tests_require=["nose", "mock"], + test_suite="nose.collector", + classifiers=[ + "Development Status :: 5 - Production/Stable", + "Environment :: Console", + "Intended Audience :: Developers", + "Intended Audience :: Information Technology", + "License :: OSI Approved :: Apache Software License", + "Operating System :: OS Independent", + "Programming Language :: Python" + ], + entry_points={ + "console_scripts": ["reddwarf-cli = reddwarfclient.cli:main", + "reddwarf-mgmt-cli = reddwarfclient.mcli:main", + ] + } +) diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..fa600d3 --- /dev/null +++ b/tox.ini @@ -0,0 +1,13 @@ +# Python Reddwarf Client + +[tox] +envlist = py26, docs + +[testenv:docs] +deps = + coverage + httplib2 + sphinx +commands = + sphinx-build -b doctest {toxinidir}/docs/source {envtmpdir}/html + sphinx-build -b html {toxinidir}/docs/source {envtmpdir}/html