|
| 1 | +# Copyright (c) 2011 OpenStack, LLC. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | +# not use this file except in compliance with the License. You may obtain |
| 6 | +# a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | +# License for the specific language governing permissions and limitations |
| 14 | +# under the License. |
| 15 | + |
| 16 | +import time |
| 17 | +import urlparse |
| 18 | + |
| 19 | +try: |
| 20 | + import json |
| 21 | +except ImportError: |
| 22 | + import simplejson as json |
| 23 | + |
| 24 | + |
| 25 | +from novaclient.client import HTTPClient |
| 26 | +from novaclient.v1_1.client import Client |
| 27 | + |
| 28 | +from reddwarfclient import exceptions |
| 29 | + |
| 30 | +class ReddwarfHTTPClient(HTTPClient): |
| 31 | + """ |
| 32 | + Class for overriding the HTTP authenticate call and making it specific to |
| 33 | + reddwarf |
| 34 | + """ |
| 35 | + |
| 36 | + def __init__(self, user, apikey, tenant, auth_url, service_name, |
| 37 | + service_type=None, service_url=None, timeout=None): |
| 38 | + super(ReddwarfHTTPClient, self).__init__(user, apikey, tenant, |
| 39 | + auth_url, |
| 40 | + service_type=service_type, |
| 41 | + timeout=timeout) |
| 42 | + self.api_key = apikey |
| 43 | + self.tenant = tenant |
| 44 | + self.service = service_name |
| 45 | + self.management_url = service_url |
| 46 | + |
| 47 | + |
| 48 | + def _get_token(self, path, req_body): |
| 49 | + """Set the management url and auth token""" |
| 50 | + token_url = urlparse.urljoin(self.auth_url, path) |
| 51 | + resp, body = self.request(token_url, "POST", body=req_body) |
| 52 | + if 'access' in body: |
| 53 | + if not self.management_url: |
| 54 | + # Assume the new Keystone lite: |
| 55 | + catalog = body['access']['serviceCatalog'] |
| 56 | + for service in catalog: |
| 57 | + if service['name'] == self.service: |
| 58 | + self.management_url = service['adminURL'] |
| 59 | + self.auth_token = body['access']['token']['id'] |
| 60 | + else: |
| 61 | + # Assume pre-Keystone Light: |
| 62 | + try: |
| 63 | + if not self.management_url: |
| 64 | + self.management_url = body['auth']['serviceCatalog'] \ |
| 65 | + [self.service][0]['publicURL'] |
| 66 | + self.auth_token = body['auth']['token']['id'] |
| 67 | + except KeyError: |
| 68 | + raise NotImplementedError("Service: %s is not available" |
| 69 | + % self.service) |
| 70 | + |
| 71 | + def request(self, *args, **kwargs): |
| 72 | + #TODO(tim.simpson): Copy and pasted from novaclient, since we raise |
| 73 | + # extra exception subclasses not raised there. |
| 74 | + kwargs.setdefault('headers', kwargs.get('headers', {})) |
| 75 | + kwargs['headers']['User-Agent'] = self.USER_AGENT |
| 76 | + kwargs['headers']['Accept'] = 'application/json' |
| 77 | + if 'body' in kwargs: |
| 78 | + kwargs['headers']['Content-Type'] = 'application/json' |
| 79 | + kwargs['body'] = json.dumps(kwargs['body']) |
| 80 | + |
| 81 | + resp, body = super(HTTPClient, self).request(*args, **kwargs) |
| 82 | + |
| 83 | + self.http_log(args, kwargs, resp, body) |
| 84 | + |
| 85 | + if body: |
| 86 | + try: |
| 87 | + body = json.loads(body) |
| 88 | + except ValueError: |
| 89 | + pass |
| 90 | + else: |
| 91 | + body = None |
| 92 | + |
| 93 | + if resp.status in (400, 401, 403, 404, 408, 409, 413, 500, 501): |
| 94 | + raise exceptions.from_response(resp, body) |
| 95 | + |
| 96 | + return resp, body |
| 97 | + |
| 98 | + |
| 99 | +class Dbaas(Client): |
| 100 | + """ |
| 101 | + Top-level object to access the Rackspace Database as a Service API. |
| 102 | +
|
| 103 | + Create an instance with your creds:: |
| 104 | +
|
| 105 | + >>> red = Dbaas(USERNAME, API_KEY, TENANT, AUTH_URL, SERVICE_NAME, |
| 106 | + SERVICE_URL) |
| 107 | +
|
| 108 | + Then call methods on its managers:: |
| 109 | +
|
| 110 | + >>> red.instances.list() |
| 111 | + ... |
| 112 | + >>> red.flavors.list() |
| 113 | + ... |
| 114 | +
|
| 115 | + &c. |
| 116 | + """ |
| 117 | + |
| 118 | + def __init__(self, username, api_key, tenant=None, auth_url=None, |
| 119 | + service_type='reddwarf', service_name='Reddwarf Service', |
| 120 | + service_url=None): |
| 121 | + from reddwarfclient.versions import Versions |
| 122 | + from reddwarfclient.databases import Databases |
| 123 | + from reddwarfclient.flavors import Flavors |
| 124 | + from reddwarfclient.instances import Instances |
| 125 | + from reddwarfclient.users import Users |
| 126 | + from reddwarfclient.root import Root |
| 127 | + from reddwarfclient.hosts import Hosts |
| 128 | + from reddwarfclient.storage import StorageInfo |
| 129 | + from reddwarfclient.management import Management |
| 130 | + from reddwarfclient.accounts import Accounts |
| 131 | + from reddwarfclient.config import Configs |
| 132 | + from reddwarfclient.diagnostics import Interrogator |
| 133 | + |
| 134 | + super(Dbaas, self).__init__(self, username, api_key, tenant, auth_url) |
| 135 | + self.client = ReddwarfHTTPClient(username, api_key, tenant, auth_url, |
| 136 | + service_type=service_type, |
| 137 | + service_name=service_name, |
| 138 | + service_url=service_url) |
| 139 | + self.versions = Versions(self) |
| 140 | + self.databases = Databases(self) |
| 141 | + self.flavors = Flavors(self) |
| 142 | + self.instances = Instances(self) |
| 143 | + self.users = Users(self) |
| 144 | + self.root = Root(self) |
| 145 | + self.hosts = Hosts(self) |
| 146 | + self.storage = StorageInfo(self) |
| 147 | + self.management = Management(self) |
| 148 | + self.accounts = Accounts(self) |
| 149 | + self.configs = Configs(self) |
| 150 | + self.diagnostics = Interrogator(self) |
| 151 | + |
| 152 | + |
0 commit comments