e39cfeb18be80129320cef83e875ba6d3009ca9f
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
),
121 "href": "http://%s:%d/v2.0/tokens" % (get_host(request
), self
.api
.port
),
125 "href": "http://%s:%d/v2.0/networks" % (get_host(request
), neutron_port
),
129 "href": "http://%s:%d/v2.0/subnets" % (get_host(request
), neutron_port
),
133 "href": "http://%s:%d/v2.0/ports" % (get_host(request
), neutron_port
),
137 "href": "http://%s:%d/v1/<tenant_id>/stacks" % (get_host(request
), heat_port
),
143 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
146 class KeystoneShowAPIv3(Resource
):
148 Entrypoint for all openstack clients.
149 This returns all current entrypoints running on son-emu.
152 def __init__(self
, api
):
157 List API entrypoints.
159 :return: Returns an openstack style response for all entrypoints.
160 :rtype: :class:`flask.response`
162 LOG
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
164 neutron_port
= self
.api
.port
+ 4696
165 heat_port
= self
.api
.port
+ 3004
172 "base": "application/json",
173 "type": "application/vnd.openstack.identity-v2.0+json"
179 "href": "http://%s:%d/v2.0" % (get_host(request
), self
.api
.port
),
183 "href": "http://%s:%d/v2.0/tokens" % (get_host(request
), self
.api
.port
),
187 "href": "http://%s:%d/v2.0/networks" % (get_host(request
), neutron_port
),
191 "href": "http://%s:%d/v2.0/subnets" % (get_host(request
), neutron_port
),
195 "href": "http://%s:%d/v2.0/ports" % (get_host(request
), neutron_port
),
199 "href": "http://%s:%d/v1/<tenant_id>/stacks" % (get_host(request
), heat_port
),
205 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
208 class KeystoneGetToken(Resource
):
210 Returns a static keystone token.
211 We don't do any validation so we don't care.
214 def __init__(self
, api
):
219 List API entrypoints.
221 This is hardcoded. For a working "authentication" use these ENVVARS:
223 * OS_AUTH_URL=http://<ip>:<port>/v2.0
224 * OS_IDENTITY_API_VERSION=2.0
225 * OS_TENANT_ID=fc394f2ab2df4114bde39905f800dc57
226 * OS_REGION_NAME=RegionOne
230 :return: Returns an openstack style response for all entrypoints.
231 :rtype: :class:`flask.response`
234 LOG
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
237 req
= json
.loads(request
.data
)
238 ret
['access'] = dict()
239 ret
['access']['token'] = dict()
240 token
= ret
['access']['token']
242 token
['issued_at'] = "2014-01-30T15:30:58.819Z"
243 token
['expires'] = "2999-01-30T15:30:58.819Z"
244 token
['id'] = req
['auth'].get('token', {'id': 'fc394f2ab2df4114bde39905f800dc57'}).get('id')
245 token
['tenant'] = dict()
246 token
['tenant']['description'] = None
247 token
['tenant']['enabled'] = True
248 token
['tenant']['id'] = req
['auth'].get('tenantId', 'fc394f2ab2df4114bde39905f800dc57')
249 token
['tenant']['name'] = "tenantName"
251 ret
['access']['user'] = dict()
252 user
= ret
['access']['user']
253 user
['username'] = req
.get('username', "username")
254 user
['name'] = "tenantName"
255 user
['roles_links'] = list()
256 user
['id'] = token
['tenant'].get('id', "fc394f2ab2df4114bde39905f800dc57")
257 user
['roles'] = [{'name': 'Member'}]
259 ret
['access']['region_name'] = "RegionOne"
261 ret
['access']['serviceCatalog'] = [{
264 "adminURL": "http://%s:%s/v2.1/%s" % (get_host(request
), self
.api
.port
+ 3774, user
['id']),
265 "region": "RegionOne",
266 "internalURL": "http://%s:%s/v2.1/%s" % (get_host(request
), self
.api
.port
+ 3774, user
['id']),
267 "id": "2dad48f09e2a447a9bf852bcd93548ef",
268 "publicURL": "http://%s:%s/v2.1/%s" % (get_host(request
), self
.api
.port
+ 3774, user
['id'])
271 "endpoints_links": [],
278 "adminURL": "http://%s:%s/v2.0" % (get_host(request
), self
.api
.port
),
279 "region": "RegionOne",
280 "internalURL": "http://%s:%s/v2.0" % (get_host(request
), self
.api
.port
),
281 "id": "2dad48f09e2a447a9bf852bcd93543fc",
282 "publicURL": "http://%s:%s/v2" % (get_host(request
), self
.api
.port
)
285 "endpoints_links": [],
292 "adminURL": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4696),
293 "region": "RegionOne",
294 "internalURL": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4696),
295 "id": "2dad48f09e2a447a9bf852bcd93548cf",
296 "publicURL": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4696)
299 "endpoints_links": [],
306 "adminURL": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4242),
307 "region": "RegionOne",
308 "internalURL": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4242),
309 "id": "2dad48f09e2a447a9bf852bcd93548cf",
310 "publicURL": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4242)
313 "endpoints_links": [],
320 "adminURL": "http://%s:%s/v1/%s" % (get_host(request
), self
.api
.port
+ 3004, user
['id']),
321 "region": "RegionOne",
322 "internalURL": "http://%s:%s/v1/%s" % (get_host(request
), self
.api
.port
+ 3004, user
['id']),
323 "id": "2dad48f09e2a447a9bf852bcd93548bf",
324 "publicURL": "http://%s:%s/v1/%s" % (get_host(request
), self
.api
.port
+ 3004, user
['id'])
327 "endpoints_links": [],
328 "type": "orchestration",
333 ret
['access']["metadata"] = {
336 "7598ac3c634d4c3da4b9126a5f67ca2b"
339 ret
['access']['trust'] = {
340 "id": "394998fa61f14736b1f0c1f322882949",
341 "trustee_user_id": "269348fdd9374b8885da1418e0730af1",
342 "trustor_user_id": "3ec3164f750146be97f21559ee4d9c51",
343 "impersonation": False
345 return Response(json
.dumps(ret
), status
=200, mimetype
='application/json')
347 except Exception as ex
:
348 logging
.exception("Keystone: Get token failed.")
349 return ex
.message
, 500
351 class KeystoneGetTokenv3(Resource
):
353 Returns a static keystone token.
354 We don't do any validation so we don't care.
357 def __init__(self
, api
):
362 List API entrypoints.
364 This is hardcoded. For a working "authentication" use these ENVVARS:
366 * OS_AUTH_URL=http://<ip>:<port>/v3
367 * OS_IDENTITY_API_VERSION=2.0
368 * OS_TENANT_ID=fc394f2ab2df4114bde39905f800dc57
369 * OS_REGION_NAME=RegionOne
373 :return: Returns an openstack style response for all entrypoints.
374 :rtype: :class:`flask.response`
377 LOG
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
380 req
= json
.loads(request
.data
)
381 ret
['token'] = dict()
384 token
['issued_at'] = "2014-01-30T15:30:58.819Z"
385 token
['expires_at'] = "2999-01-30T15:30:58.819Z"
386 token
['methods'] = ["password"]
387 token
['extras'] = dict()
388 token
['user'] = dict()
390 user
['id'] = req
['auth'].get('token', {'id': 'fc394f2ab2df4114bde39905f800dc57'}).get('id')
391 user
['name'] = "tenantName"
392 user
['password_expires_at'] = None
393 user
['domain'] = {"id": "default", "name": "Default"}
394 token
['audit_ids'] = ["ZzZwkUflQfygX7pdYDBCQQ"]
402 "id": "8538a3f13f9541b28c2620eb19065e45",
407 token
['catalog'] = [{
410 "url": "http://%s:%s/v2.1/%s" % (get_host(request
), self
.api
.port
+ 3774, user
['id']),
411 "region": "RegionOne",
412 "interface": "public",
413 "id": "2dad48f09e2a447a9bf852bcd93548ef"
416 "id": "2dad48f09e2a447a9bf852bcd93548ef",
423 "url": "http://%s:%s/v2.0" % (get_host(request
), self
.api
.port
),
424 "region": "RegionOne",
425 "interface": "public",
426 "id": "2dad48f09e2a447a9bf852bcd93543fc"
429 "id": "2dad48f09e2a447a9bf852bcd93543fc",
436 "url": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4696),
437 "region": "RegionOne",
438 "interface": "public",
439 "id": "2dad48f09e2a447a9bf852bcd93548cf"
442 "id": "2dad48f09e2a447a9bf852bcd93548cf",
449 "url": "http://%s:%s" % (get_host(request
), self
.api
.port
+ 4242),
450 "region": "RegionOne",
451 "interface": "public",
452 "id": "2dad48f09e2a447a9bf852bcd93548cf"
455 "id": "2dad48f09e2a447a9bf852bcd93548cf",
462 "url": "http://%s:%s/v1/%s" % (get_host(request
), self
.api
.port
+ 3004, user
['id']),
463 "region": "RegionOne",
464 "interface": "public",
465 "id": "2dad48f09e2a447a9bf852bcd93548bf"
468 "id": "2dad48f09e2a447a9bf852bcd93548bf",
469 "type": "orchestration",
474 return Response(json
.dumps(ret
), status
=201, mimetype
='application/json')
476 except Exception as ex
:
477 logging
.exception("Keystone: Get token failed.")
478 return ex
.message
, 500