1 from flask_restful
import Resource
2 from flask
import request
, Response
3 from emuvim
.api
.openstack
.openstack_dummies
.base_openstack_dummy
import BaseOpenstackDummy
4 from emuvim
.api
.openstack
.helper
import get_host
8 LOG
= logging
.getLogger("api.openstack.keystone")
11 class KeystoneDummyApi(BaseOpenstackDummy
):
12 def __init__(self
, in_ip
, in_port
):
13 super(KeystoneDummyApi
, self
).__init
__(in_ip
, in_port
)
15 self
.api
.add_resource(KeystoneListVersions
, "/", resource_class_kwargs
={'api': self
})
16 self
.api
.add_resource(Shutdown
, "/shutdown")
17 self
.api
.add_resource(KeystoneShowAPIv2
, "/v2.0", resource_class_kwargs
={'api': self
})
18 self
.api
.add_resource(KeystoneGetToken
, "/v2.0/tokens", resource_class_kwargs
={'api': self
})
19 self
.api
.add_resource(KeystoneShowAPIv3
, "/v3", resource_class_kwargs
={'api': self
})
20 self
.api
.add_resource(KeystoneGetTokenv3
, "/v3/auth/tokens", resource_class_kwargs
={'api': self
})
22 def _start_flask(self
):
23 LOG
.info("Starting %s endpoint @ http://%s:%d" % (__name__
, self
.ip
, self
.port
))
24 if self
.app
is not None:
25 self
.app
.before_request(self
.dump_playbook
)
26 self
.app
.run(self
.ip
, self
.port
, debug
=True, use_reloader
=False)
29 class Shutdown(Resource
):
31 A get request to /shutdown will shut down this endpoint.
35 LOG
.debug(("%s is beeing shut down") % (__name__
))
36 func
= request
.environ
.get('werkzeug.server.shutdown')
38 raise RuntimeError('Not running with the Werkzeug Server')
42 class KeystoneListVersions(Resource
):
44 List all known keystone versions.
45 Hardcoded for our version!
48 def __init__(self
, api
):
55 :return: Returns the api versions.
56 :rtype: :class:`flask.response` containing a static json encoded dict.
58 LOG
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
60 resp
['versions'] = dict()
66 "href": "http://%s:%d/v2.0" % (get_host(request
), self
.api
.port
),
72 "base": "application/json",
73 "type": "application/vnd.openstack.identity-v2.0+json"
77 "updated": "2014-04-17T00:00:00Z"
79 resp
['versions']['values'] = version
81 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
84 class KeystoneShowAPIv2(Resource
):
86 Entrypoint for all openstack clients.
87 This returns all current entrypoints running on son-emu.
90 def __init__(self
, api
):
97 :return: Returns an openstack style response for all entrypoints.
98 :rtype: :class:`flask.response`
100 LOG
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
102 neutron_port
= self
.api
.port
+ 4696
103 heat_port
= self
.api
.port
+ 3004
110 "base": "application/json",
111 "type": "application/vnd.openstack.identity-v2.0+json"
117 "href": "http://%s:%d/v2.0" % (get_host(request
), self
.api
.port
),
122 LOG
.debug(json
.dumps(resp
))
123 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
126 class KeystoneShowAPIv3(Resource
):
128 Entrypoint for all openstack clients.
129 This returns all current entrypoints running on son-emu.
132 def __init__(self
, api
):
137 List API entrypoints.
139 :return: Returns an openstack style response for all entrypoints.
140 :rtype: :class:`flask.response`
142 LOG
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
144 neutron_port
= self
.api
.port
+ 4696
145 heat_port
= self
.api
.port
+ 3004
152 "base": "application/json",
153 "type": "application/vnd.openstack.identity-v2.0+json"
159 "href": "http://%s:%d/v2.0" % (get_host(request
), self
.api
.port
),
165 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
168 class KeystoneGetToken(Resource
):
170 Returns a static keystone token.
171 We don't do any validation so we don't care.
174 def __init__(self
, api
):
179 List API entrypoints.
181 This is hardcoded. For a working "authentication" use these ENVVARS:
183 * OS_AUTH_URL=http://<ip>:<port>/v2.0
184 * OS_IDENTITY_API_VERSION=2.0
185 * OS_TENANT_ID=fc394f2ab2df4114bde39905f800dc57
186 * OS_REGION_NAME=RegionOne
190 :return: Returns an openstack style response for all entrypoints.
191 :rtype: :class:`flask.response`
194 LOG
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
197 req
= json
.loads(request
.data
)
198 ret
['access'] = dict()
199 ret
['access']['token'] = dict()
200 token
= ret
['access']['token']
202 token
['issued_at'] = "2014-01-30T15:30:58.819Z"
203 token
['expires'] = "2999-01-30T15:30:58.819Z"
204 token
['id'] = req
['auth'].get('token', {'id': 'fc394f2ab2df4114bde39905f800dc57'}).get('id')
205 token
['tenant'] = dict()
206 token
['tenant']['description'] = None
207 token
['tenant']['enabled'] = True
208 token
['tenant']['id'] = req
['auth'].get('tenantId', 'fc394f2ab2df4114bde39905f800dc57')
209 token
['tenant']['name'] = "tenantName"
211 ret
['access']['user'] = dict()
212 user
= ret
['access']['user']
213 user
['username'] = req
.get('username', "username")
214 user
['name'] = "tenantName"
215 user
['roles_links'] = list()
216 user
['id'] = token
['tenant'].get('id', "fc394f2ab2df4114bde39905f800dc57")
217 user
['roles'] = [{'name': 'Member'}]
219 ret
['access']['region_name'] = "RegionOne"
221 ret
['access']['serviceCatalog'] = [{
224 "adminURL": "http://%s:%s/v2.1/%s" % (get_host(request
), self
.api
.port
+ 3774, user
['id']),
225 "region": "RegionOne",
226 "internalURL": "http://%s:%s/v2.1/%s" % (get_host(request
), self
.api
.port
+ 3774, user
['id']),
227 "id": "2dad48f09e2a447a9bf852bcd93548ef",
228 "publicURL": "http://%s:%s/v2.1/%s" % (get_host(request
), self
.api
.port
+ 3774, user
['id'])
231 "endpoints_links": [],
238 "adminURL": "http://%s:%s/v2.0" % (get_host(request
), self
.api
.port
),
239 "region": "RegionOne",
240 "internalURL": "http://%s:%s/v2.0" % (get_host(request
), self
.api
.port
),
241 "id": "2dad48f09e2a447a9bf852bcd93543fc",
242 "publicURL": "http://%s:%s/v2" % (get_host(request
), self
.api
.port
)
245 "endpoints_links": [],
252 "adminURL": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4696),
253 "region": "RegionOne",
254 "internalURL": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4696),
255 "id": "2dad48f09e2a447a9bf852bcd93548cf",
256 "publicURL": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4696)
259 "endpoints_links": [],
266 "adminURL": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4242),
267 "region": "RegionOne",
268 "internalURL": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4242),
269 "id": "2dad48f09e2a447a9bf852bcd93548cf",
270 "publicURL": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4242)
273 "endpoints_links": [],
280 "adminURL": "http://%s:%s/v1/%s" % (get_host(request
), self
.api
.port
+ 3004, user
['id']),
281 "region": "RegionOne",
282 "internalURL": "http://%s:%s/v1/%s" % (get_host(request
), self
.api
.port
+ 3004, user
['id']),
283 "id": "2dad48f09e2a447a9bf852bcd93548bf",
284 "publicURL": "http://%s:%s/v1/%s" % (get_host(request
), self
.api
.port
+ 3004, user
['id'])
287 "endpoints_links": [],
288 "type": "orchestration",
293 ret
['access']["metadata"] = {
296 "7598ac3c634d4c3da4b9126a5f67ca2b"
299 ret
['access']['trust'] = {
300 "id": "394998fa61f14736b1f0c1f322882949",
301 "trustee_user_id": "269348fdd9374b8885da1418e0730af1",
302 "trustor_user_id": "3ec3164f750146be97f21559ee4d9c51",
303 "impersonation": False
305 return Response(json
.dumps(ret
), status
=200, mimetype
='application/json')
307 except Exception as ex
:
308 logging
.exception("Keystone: Get token failed.")
309 return ex
.message
, 500
311 class KeystoneGetTokenv3(Resource
):
313 Returns a static keystone token.
314 We don't do any validation so we don't care.
317 def __init__(self
, api
):
322 List API entrypoints.
324 This is hardcoded. For a working "authentication" use these ENVVARS:
326 * OS_AUTH_URL=http://<ip>:<port>/v3
327 * OS_IDENTITY_API_VERSION=2.0
328 * OS_TENANT_ID=fc394f2ab2df4114bde39905f800dc57
329 * OS_REGION_NAME=RegionOne
333 :return: Returns an openstack style response for all entrypoints.
334 :rtype: :class:`flask.response`
337 LOG
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
340 req
= json
.loads(request
.data
)
341 ret
['token'] = dict()
344 token
['issued_at'] = "2014-01-30T15:30:58.819Z"
345 token
['expires_at'] = "2999-01-30T15:30:58.819Z"
346 token
['methods'] = ["password"]
347 token
['extras'] = dict()
348 token
['user'] = dict()
350 user
['id'] = req
['auth'].get('token', {'id': 'fc394f2ab2df4114bde39905f800dc57'}).get('id')
351 user
['name'] = "tenantName"
352 user
['password_expires_at'] = None
353 user
['domain'] = {"id": "default", "name": "Default"}
354 token
['audit_ids'] = ["ZzZwkUflQfygX7pdYDBCQQ"]
362 "id": "8538a3f13f9541b28c2620eb19065e45",
367 token
['catalog'] = [{
370 "url": "http://%s:%s/v2.1/%s" % (get_host(request
), self
.api
.port
+ 3774, user
['id']),
371 "region": "RegionOne",
372 "interface": "public",
373 "id": "2dad48f09e2a447a9bf852bcd93548ef"
376 "id": "2dad48f09e2a447a9bf852bcd93548ef",
383 "url": "http://%s:%s/v2.0" % (get_host(request
), self
.api
.port
),
384 "region": "RegionOne",
385 "interface": "public",
386 "id": "2dad48f09e2a447a9bf852bcd93543fc"
389 "id": "2dad48f09e2a447a9bf852bcd93543fc",
396 "url": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4696),
397 "region": "RegionOne",
398 "interface": "public",
399 "id": "2dad48f09e2a447a9bf852bcd93548cf"
402 "id": "2dad48f09e2a447a9bf852bcd93548cf",
409 "url": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4242),
410 "region": "RegionOne",
411 "interface": "public",
412 "id": "2dad48f09e2a447a9bf852bcd93548cf"
415 "id": "2dad48f09e2a447a9bf852bcd93548cf",
422 "url": "http://%s:%s/v1/%s" % (get_host(request
), self
.api
.port
+ 3004, user
['id']),
423 "region": "RegionOne",
424 "interface": "public",
425 "id": "2dad48f09e2a447a9bf852bcd93548bf"
428 "id": "2dad48f09e2a447a9bf852bcd93548bf",
429 "type": "orchestration",
433 return Response(json
.dumps(ret
), status
=201, mimetype
='application/json')
435 except Exception as ex
:
436 logging
.exception("Keystone: Get token failed.")
437 return ex
.message
, 500