blob: b64e86f24d43397b08ab7791cefee5604588a790 [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):
gongysh6c0804c2018-01-27 16:38:28 +0800120 if 'version' == name:
121 version = self.url.rpartition('/')[2]
122 return 'v2.0' if 'v2.0' == version else 'v3.0'
Scott Bakerb63ea792016-08-11 10:24:48 -0700123 return getattr(self.client, name)
124
125
Scott Bakerb63ea792016-08-11 10:24:48 -0700126class GlanceClient(Client):
127 def __init__(self, version, endpoint, token, cacert=None, *args, **kwds):
128 Client.__init__(self, *args, **kwds)
129 if has_openstack:
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700130 self.client = glanceclient.Client(version,
131 endpoint=endpoint,
yong sheng gongfe5d3152018-01-04 16:03:41 +0800132 session=self.get_session()
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700133 )
Scott Bakerb63ea792016-08-11 10:24:48 -0700134
135 @require_enabled
136 def __getattr__(self, name):
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700137 return getattr(self.client, name)
138
Scott Bakerb63ea792016-08-11 10:24:48 -0700139
140class NovaClient(Client):
141 def __init__(self, *args, **kwds):
142 Client.__init__(self, *args, **kwds)
143 if has_openstack:
144 self.client = nova_client.client.Client(
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700145 "2",
yong sheng gongfe5d3152018-01-04 16:03:41 +0800146 session=self.get_session(),
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700147 username=self.username,
148 api_key=self.password,
149 project_id=self.tenant,
150 auth_url=self.url,
151 region_name='',
152 extensions=[],
153 service_type='compute',
154 service_name='',
155 cacert=self.cacert
156 )
Scott Bakerb63ea792016-08-11 10:24:48 -0700157
158 @require_enabled
159 def connect(self, *args, **kwds):
160 self.__init__(*args, **kwds)
161
162 @require_enabled
163 def __getattr__(self, name):
164 return getattr(self.client, name)
165
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700166
Scott Bakerb63ea792016-08-11 10:24:48 -0700167class NeutronClient(Client):
168 def __init__(self, *args, **kwds):
169 Client.__init__(self, *args, **kwds)
170 if has_openstack:
yong sheng gongfe5d3152018-01-04 16:03:41 +0800171 self.client = neutron_client.Client(session=self.get_session())
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700172
Scott Bakerb63ea792016-08-11 10:24:48 -0700173 @require_enabled
174 def connect(self, *args, **kwds):
175 self.__init__(*args, **kwds)
176
177 @require_enabled
178 def __getattr__(self, name):
179 return getattr(self.client, name)
180
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700181
Scott Bakerb63ea792016-08-11 10:24:48 -0700182class OpenStackClient:
183 """
184 A simple native shell to the openstack backend services.
185 This class can receive all nova calls to the underlying testbed
186 """
187
Matteo Scandoloceccb1f2017-06-05 10:35:44 -0700188 def __init__(self, *args, **kwds):
Scott Bakerb63ea792016-08-11 10:24:48 -0700189 # instantiate managers
190 self.keystone = KeystoneClient(*args, **kwds)
Scott Bakerb63ea792016-08-11 10:24:48 -0700191 self.nova = NovaClient(*args, **kwds)
Scott Bakerb63ea792016-08-11 10:24:48 -0700192 self.neutron = NeutronClient(*args, **kwds)
Scott Bakerb63ea792016-08-11 10:24:48 -0700193
194 @require_enabled
195 def connect(self, *args, **kwds):
196 self.__init__(*args, **kwds)
197
198 @require_enabled
199 def authenticate(self):
200 return self.keystone.authenticate()