blob: 817f184c6a444937aa49d3635e49ee3d8169346a [file] [log] [blame]
Matteo Scandolof0441032017-08-08 13:05:26 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain 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,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Scott Bakerb63ea792016-08-11 10:24:48 -070016try:
Scott Bakerb63ea792016-08-11 10:24:48 -070017 import glanceclient
yong sheng gongfe5d3152018-01-04 16:03:41 +080018 from keystoneauth1 import identity
19 from keystoneauth1 import session
20 from keystoneclient import client
Scott Bakerb63ea792016-08-11 10:24:48 -070021 from novaclient.v2 import client as nova_client
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070022 from neutronclient.v2_0 import client as neutron_client
Scott Bakerb63ea792016-08-11 10:24:48 -070023 has_openstack = True
24except:
25 has_openstack = False
26
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070027from xosconfig import Config
28
Scott Bakerb63ea792016-08-11 10:24:48 -070029
30def require_enabled(callable):
31 def wrapper(*args, **kwds):
32 if has_openstack:
33 return callable(*args, **kwds)
34 else:
35 return None
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070036
Scott Bakerb63ea792016-08-11 10:24:48 -070037 return wrapper
38
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070039
Scott Bakerb63ea792016-08-11 10:24:48 -070040def parse_novarc(filename):
41 opts = {}
42 f = open(filename, 'r')
43 for line in f:
44 try:
45 line = line.replace('export', '').strip()
46 parts = line.split('=')
47 if len(parts) > 1:
48 value = parts[1].replace("\'", "")
49 value = value.replace('\"', '')
50 opts[parts[0]] = value
51 except:
52 pass
53 f.close()
54 return opts
55
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070056
Scott Bakerb63ea792016-08-11 10:24:48 -070057class Client:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070058 def __init__(self, username=None, password=None, tenant=None, url=None, token=None, endpoint=None, controller=None,
59 cacert=None, admin=True, *args, **kwds):
60
Scott Bakerb63ea792016-08-11 10:24:48 -070061 self.has_openstack = has_openstack
62 self.url = controller.auth_url
63 if admin:
64 self.username = controller.admin_user
65 self.password = controller.admin_password
66 self.tenant = controller.admin_tenant
67 else:
68 self.username = None
69 self.password = None
70 self.tenant = None
71
72 if username:
73 self.username = username
74 if password:
75 self.password = password
76 if tenant:
77 self.tenant = tenant
78 if url:
79 self.url = url
80 if token:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070081 self.token = token
Scott Bakerb63ea792016-08-11 10:24:48 -070082 if endpoint:
83 self.endpoint = endpoint
84
85 if cacert:
86 self.cacert = cacert
87 else:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -070088 self.cacert = Config.get("nova.ca_ssl_cert")
Scott Bakerb63ea792016-08-11 10:24:48 -070089
yong sheng gongfe5d3152018-01-04 16:03:41 +080090 def get_session(self):
91 if has_openstack:
92 version = self.url.rpartition('/')[2]
93 if version == 'v2.0':
94 auth_plugin = identity.v2.Password(username=self.username,
95 password=self.password,
96 tenant_name=self.tenant,
97 auth_url=self.url,)
98 else:
99 auth_plugin = identity.v3.Password(
100 auth_url=self.url,
101 username=self.username,
102 password=self.password,
103 project_name=self.tenant,
104 user_domain_id='default',
105 project_domain_id='default')
106 return session.Session(auth=auth_plugin, verify=self.cacert)
107
Scott Bakerb63ea792016-08-11 10:24:48 -0700108
109class KeystoneClient(Client):
110 def __init__(self, *args, **kwds):
111 Client.__init__(self, *args, **kwds)
yong sheng gongfe5d3152018-01-04 16:03:41 +0800112 self.client = client.Client(session=self.get_session())
Scott Bakerb63ea792016-08-11 10:24:48 -0700113
114 @require_enabled
115 def connect(self, *args, **kwds):
116 self.__init__(*args, **kwds)
117
118 @require_enabled
119 def __getattr__(self, name):
120 return getattr(self.client, name)
121
122
Scott Bakerb63ea792016-08-11 10:24:48 -0700123class GlanceClient(Client):
124 def __init__(self, version, endpoint, token, cacert=None, *args, **kwds):
125 Client.__init__(self, *args, **kwds)
126 if has_openstack:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700127 self.client = glanceclient.Client(version,
128 endpoint=endpoint,
yong sheng gongfe5d3152018-01-04 16:03:41 +0800129 session=self.get_session()
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700130 )
Scott Bakerb63ea792016-08-11 10:24:48 -0700131
132 @require_enabled
133 def __getattr__(self, name):
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700134 return getattr(self.client, name)
135
Scott Bakerb63ea792016-08-11 10:24:48 -0700136
137class NovaClient(Client):
138 def __init__(self, *args, **kwds):
139 Client.__init__(self, *args, **kwds)
140 if has_openstack:
141 self.client = nova_client.client.Client(
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700142 "2",
yong sheng gongfe5d3152018-01-04 16:03:41 +0800143 session=self.get_session(),
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700144 username=self.username,
145 api_key=self.password,
146 project_id=self.tenant,
147 auth_url=self.url,
148 region_name='',
149 extensions=[],
150 service_type='compute',
151 service_name='',
152 cacert=self.cacert
153 )
Scott Bakerb63ea792016-08-11 10:24:48 -0700154
155 @require_enabled
156 def connect(self, *args, **kwds):
157 self.__init__(*args, **kwds)
158
159 @require_enabled
160 def __getattr__(self, name):
161 return getattr(self.client, name)
162
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700163
Scott Bakerb63ea792016-08-11 10:24:48 -0700164class NeutronClient(Client):
165 def __init__(self, *args, **kwds):
166 Client.__init__(self, *args, **kwds)
167 if has_openstack:
yong sheng gongfe5d3152018-01-04 16:03:41 +0800168 self.client = neutron_client.Client(session=self.get_session())
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700169
Scott Bakerb63ea792016-08-11 10:24:48 -0700170 @require_enabled
171 def connect(self, *args, **kwds):
172 self.__init__(*args, **kwds)
173
174 @require_enabled
175 def __getattr__(self, name):
176 return getattr(self.client, name)
177
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700178
Scott Bakerb63ea792016-08-11 10:24:48 -0700179class OpenStackClient:
180 """
181 A simple native shell to the openstack backend services.
182 This class can receive all nova calls to the underlying testbed
183 """
184
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700185 def __init__(self, *args, **kwds):
Scott Bakerb63ea792016-08-11 10:24:48 -0700186 # instantiate managers
187 self.keystone = KeystoneClient(*args, **kwds)
Scott Bakerb63ea792016-08-11 10:24:48 -0700188 self.nova = NovaClient(*args, **kwds)
Scott Bakerb63ea792016-08-11 10:24:48 -0700189 self.neutron = NeutronClient(*args, **kwds)
Scott Bakerb63ea792016-08-11 10:24:48 -0700190
191 @require_enabled
192 def connect(self, *args, **kwds):
193 self.__init__(*args, **kwds)
194
195 @require_enabled
196 def authenticate(self):
197 return self.keystone.authenticate()