Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 1 | try: |
| 2 | from keystoneclient.v2_0 import client as keystone_client |
| 3 | from glance import client as glance_client |
| 4 | from novaclient.v1_1 import client as nova_client |
| 5 | from quantumclient.v2_0 import client as quantum_client |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 6 | from nova.db.sqlalchemy import api as nova_db_api |
| 7 | from nova.context import get_admin_context |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 8 | from keystone.common.sql import core |
| 9 | core.CONF(args=[], project='keystone', default_config_files=['/etc/keystone/keystone.conf']) |
| 10 | from keystone.identity.backends.sql import Metadata |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 11 | has_openstack = True |
| 12 | except: |
| 13 | has_openstack = False |
| 14 | |
| 15 | from planetstack.config import Config |
| 16 | |
| 17 | def require_enabled(callable): |
| 18 | def wrapper(*args, **kwds): |
| 19 | if has_openstack: |
| 20 | return callable(*args, **kwds) |
| 21 | else: |
| 22 | return None |
| 23 | return wrapper |
| 24 | |
| 25 | def parse_novarc(filename): |
| 26 | opts = {} |
| 27 | f = open(filename, 'r') |
| 28 | for line in f: |
| 29 | try: |
| 30 | line = line.replace('export', '').strip() |
| 31 | parts = line.split('=') |
| 32 | if len(parts) > 1: |
| 33 | value = parts[1].replace("\'", "") |
| 34 | value = value.replace('\"', '') |
| 35 | opts[parts[0]] = value |
| 36 | except: |
| 37 | pass |
| 38 | f.close() |
| 39 | return opts |
| 40 | |
| 41 | class Client: |
| 42 | def __init__(self, username=None, password=None, tenant=None, url=None, config=None, *args, **kwds): |
| 43 | if config: |
| 44 | config = Config(config) |
| 45 | else: |
| 46 | config = Config() |
| 47 | self.has_openstack = has_openstack |
| 48 | self.username = config.nova_admin_user |
| 49 | self.password = config.nova_admin_password |
| 50 | self.tenant = config.nova_admin_tenant |
| 51 | self.url = config.nova_url |
| 52 | |
| 53 | if username: |
| 54 | self.username = username |
| 55 | if password: |
| 56 | self.password = password |
| 57 | if tenant: |
| 58 | self.tenant = tenant |
| 59 | if url: |
| 60 | self.url = url |
| 61 | |
| 62 | if '@' in self.username: |
| 63 | self.username = self.username[:self.username.index('@')] |
| 64 | |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 65 | class KeystoneDB: |
| 66 | @require_enabled |
| 67 | def get_session(self): |
| 68 | return core.Base().get_session() |
| 69 | |
| 70 | @require_enabled |
| 71 | def get_metadata(self): |
| 72 | session = self.get_session() |
| 73 | return session.query(Metadata).all() |
| 74 | |
| 75 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 76 | class KeystoneClient(Client): |
| 77 | def __init__(self, *args, **kwds): |
| 78 | Client.__init__(self, *args, **kwds) |
| 79 | if has_openstack: |
| 80 | self.client = keystone_client.Client(username=self.username, |
| 81 | password=self.password, |
| 82 | tenant_name=self.tenant, |
| 83 | auth_url=self.url) |
| 84 | |
| 85 | @require_enabled |
| 86 | def connect(self, *args, **kwds): |
| 87 | self.__init__(*args, **kwds) |
| 88 | |
| 89 | @require_enabled |
| 90 | def __getattr__(self, name): |
| 91 | return getattr(self.client, name) |
| 92 | |
| 93 | |
| 94 | class GlanceClient(Client): |
| 95 | def __init__(self, *args, **kwds): |
| 96 | Client.__init__(self, *args, **kwds) |
| 97 | if has_openstack: |
| 98 | self.client = glance_client.get_client(host='0.0.0.0', |
| 99 | username=self.username, |
| 100 | password=self.password, |
| 101 | tenant=self.tenant, |
| 102 | auth_url=self.url) |
| 103 | @require_enabled |
| 104 | def __getattr__(self, name): |
| 105 | return getattr(self.client, name) |
| 106 | |
| 107 | class NovaClient(Client): |
| 108 | def __init__(self, *args, **kwds): |
| 109 | Client.__init__(self, *args, **kwds) |
| 110 | if has_openstack: |
| 111 | self.client = nova_client.Client(username=self.username, |
| 112 | api_key=self.password, |
| 113 | project_id=self.tenant, |
| 114 | auth_url=self.url, |
| 115 | region_name='', |
| 116 | extensions=[], |
| 117 | service_type='compute', |
| 118 | service_name='', |
| 119 | ) |
| 120 | |
| 121 | @require_enabled |
| 122 | def connect(self, *args, **kwds): |
| 123 | self.__init__(*args, **kwds) |
| 124 | |
| 125 | @require_enabled |
| 126 | def __getattr__(self, name): |
| 127 | return getattr(self.client, name) |
| 128 | |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 129 | class NovaDB(Client): |
| 130 | def __init__(self, *args, **kwds): |
| 131 | Client.__init__(self, *args, **kwds) |
| 132 | if has_openstack: |
| 133 | self.ctx = get_admin_context() |
Tony Mack | 7b0dad0 | 2013-06-10 13:42:21 -0400 | [diff] [blame] | 134 | nova_db_api.FLAGS(default_config_files=['/etc/nova/nova.conf']) |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 135 | self.client = nova_db_api |
| 136 | |
| 137 | |
| 138 | @require_enabled |
| 139 | def connect(self, *args, **kwds): |
| 140 | self.__init__(*args, **kwds) |
| 141 | |
| 142 | @require_enabled |
| 143 | def __getattr__(self, name): |
| 144 | return getattr(self.client, name) |
| 145 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 146 | class QuantumClient(Client): |
| 147 | def __init__(self, *args, **kwds): |
| 148 | Client.__init__(self, *args, **kwds) |
| 149 | if has_openstack: |
| 150 | self.client = quantum_client.Client(username=self.username, |
| 151 | password=self.password, |
| 152 | tenant_name=self.tenant, |
| 153 | auth_url=self.url) |
| 154 | @require_enabled |
| 155 | def connect(self, *args, **kwds): |
| 156 | self.__init__(*args, **kwds) |
| 157 | |
| 158 | @require_enabled |
| 159 | def __getattr__(self, name): |
| 160 | return getattr(self.client, name) |
| 161 | |
| 162 | class OpenStackClient: |
| 163 | """ |
| 164 | A simple native shell to the openstack backend services. |
| 165 | This class can receive all nova calls to the underlying testbed |
| 166 | """ |
| 167 | |
| 168 | def __init__ ( self, *args, **kwds) : |
| 169 | # instantiate managers |
| 170 | self.keystone = KeystoneClient(*args, **kwds) |
Tony Mack | 79a49c8 | 2013-06-15 23:51:57 -0400 | [diff] [blame] | 171 | self.keystone_db = KeystoneDB() |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 172 | self.glance = GlanceClient(*args, **kwds) |
| 173 | self.nova = NovaClient(*args, **kwds) |
Tony Mack | b0d9742 | 2013-06-10 09:57:45 -0400 | [diff] [blame] | 174 | self.nova_db = NovaDB(*args, **kwds) |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 175 | self.quantum = QuantumClient(*args, **kwds) |
| 176 | |
| 177 | @require_enabled |
| 178 | def connect(self, *args, **kwds): |
| 179 | self.__init__(*args, **kwds) |
| 180 | |
| 181 | @require_enabled |
| 182 | def authenticate(self): |
| 183 | return self.keystone.authenticate() |