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