1 from flask_restful
import Resource
2 from flask
import request
, Response
3 from emuvim
.api
.openstack
.openstack_dummies
.base_openstack_dummy
import BaseOpenstackDummy
8 class KeystoneDummyApi(BaseOpenstackDummy
):
9 def __init__(self
, in_ip
, in_port
):
10 super(KeystoneDummyApi
, self
).__init
__(in_ip
, in_port
)
12 self
.api
.add_resource(KeystoneListVersions
, "/", resource_class_kwargs
={'api': self
})
13 self
.api
.add_resource(Shutdown
, "/shutdown")
14 self
.api
.add_resource(KeystoneShowAPIv2
, "/v2.0", resource_class_kwargs
={'api': self
})
15 self
.api
.add_resource(KeystoneGetToken
, "/v2.0/tokens", resource_class_kwargs
={'api': self
})
16 self
.api
.add_resource(KeystoneShowAPIv3
, "/v3", resource_class_kwargs
={'api': self
})
17 self
.api
.add_resource(KeystoneGetTokenv3
, "/v3/auth/tokens", resource_class_kwargs
={'api': self
})
19 def _start_flask(self
):
20 logging
.info("Starting %s endpoint @ http://%s:%d" % (__name__
, self
.ip
, self
.port
))
21 if self
.app
is not None:
22 self
.app
.before_request(self
.dump_playbook
)
23 self
.app
.run(self
.ip
, self
.port
, debug
=True, use_reloader
=False)
26 class Shutdown(Resource
):
28 A get request to /shutdown will shut down this endpoint.
32 logging
.debug(("%s is beeing shut down") % (__name__
))
33 func
= request
.environ
.get('werkzeug.server.shutdown')
35 raise RuntimeError('Not running with the Werkzeug Server')
39 class KeystoneListVersions(Resource
):
41 List all known keystone versions.
42 Hardcoded for our version!
45 def __init__(self
, api
):
52 :return: Returns the api versions.
53 :rtype: :class:`flask.response` containing a static json encoded dict.
55 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
57 resp
['versions'] = dict()
63 "href": "http://%s:%d/v2.0" % (self
.api
.ip
, self
.api
.port
),
69 "base": "application/json",
70 "type": "application/vnd.openstack.identity-v2.0+json"
74 "updated": "2014-04-17T00:00:00Z"
76 resp
['versions']['values'] = version
78 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
81 class KeystoneShowAPIv2(Resource
):
83 Entrypoint for all openstack clients.
84 This returns all current entrypoints running on son-emu.
87 def __init__(self
, api
):
94 :return: Returns an openstack style response for all entrypoints.
95 :rtype: :class:`flask.response`
97 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
99 neutron_port
= self
.api
.port
+ 4696
100 heat_port
= self
.api
.port
+ 3004
107 "base": "application/json",
108 "type": "application/vnd.openstack.identity-v2.0+json"
114 "href": "http://%s:%d/v2.0" % (self
.api
.ip
, self
.api
.port
),
118 "href": "http://%s:%d/v2.0/tokens" % (self
.api
.ip
, self
.api
.port
),
122 "href": "http://%s:%d/v2.0/networks" % (self
.api
.ip
, neutron_port
),
126 "href": "http://%s:%d/v2.0/subnets" % (self
.api
.ip
, neutron_port
),
130 "href": "http://%s:%d/v2.0/ports" % (self
.api
.ip
, neutron_port
),
134 "href": "http://%s:%d/v1/<tenant_id>/stacks" % (self
.api
.ip
, heat_port
),
140 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
143 class KeystoneShowAPIv3(Resource
):
145 Entrypoint for all openstack clients.
146 This returns all current entrypoints running on son-emu.
149 def __init__(self
, api
):
154 List API entrypoints.
156 :return: Returns an openstack style response for all entrypoints.
157 :rtype: :class:`flask.response`
159 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
161 neutron_port
= self
.api
.port
+ 4696
162 heat_port
= self
.api
.port
+ 3004
169 "base": "application/json",
170 "type": "application/vnd.openstack.identity-v2.0+json"
176 "href": "http://%s:%d/v2.0" % (self
.api
.ip
, self
.api
.port
),
180 "href": "http://%s:%d/v2.0/tokens" % (self
.api
.ip
, self
.api
.port
),
184 "href": "http://%s:%d/v2.0/networks" % (self
.api
.ip
, neutron_port
),
188 "href": "http://%s:%d/v2.0/subnets" % (self
.api
.ip
, neutron_port
),
192 "href": "http://%s:%d/v2.0/ports" % (self
.api
.ip
, neutron_port
),
196 "href": "http://%s:%d/v1/<tenant_id>/stacks" % (self
.api
.ip
, heat_port
),
202 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
205 class KeystoneGetToken(Resource
):
207 Returns a static keystone token.
208 We don't do any validation so we don't care.
211 def __init__(self
, api
):
216 List API entrypoints.
218 This is hardcoded. For a working "authentication" use these ENVVARS:
220 * OS_AUTH_URL=http://<ip>:<port>/v2.0
221 * OS_IDENTITY_API_VERSION=2.0
222 * OS_TENANT_ID=fc394f2ab2df4114bde39905f800dc57
223 * OS_REGION_NAME=RegionOne
227 :return: Returns an openstack style response for all entrypoints.
228 :rtype: :class:`flask.response`
231 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
234 req
= json
.loads(request
.data
)
235 ret
['access'] = dict()
236 ret
['access']['token'] = dict()
237 token
= ret
['access']['token']
239 token
['issued_at'] = "2014-01-30T15:30:58.819Z"
240 token
['expires'] = "2999-01-30T15:30:58.819Z"
241 token
['id'] = req
['auth'].get('token', {'id': 'fc394f2ab2df4114bde39905f800dc57'}).get('id')
242 token
['tenant'] = dict()
243 token
['tenant']['description'] = None
244 token
['tenant']['enabled'] = True
245 token
['tenant']['id'] = req
['auth'].get('tenantId', 'fc394f2ab2df4114bde39905f800dc57')
246 token
['tenant']['name'] = "tenantName"
248 ret
['access']['user'] = dict()
249 user
= ret
['access']['user']
250 user
['username'] = req
.get('username', "username")
251 user
['name'] = "tenantName"
252 user
['roles_links'] = list()
253 user
['id'] = token
['tenant'].get('id', "fc394f2ab2df4114bde39905f800dc57")
254 user
['roles'] = [{'name': 'Member'}]
256 ret
['access']['region_name'] = "RegionOne"
258 ret
['access']['serviceCatalog'] = [{
261 "adminURL": "http://%s:%s/v2.1/%s" % (self
.api
.ip
, self
.api
.port
+ 3774, user
['id']),
262 "region": "RegionOne",
263 "internalURL": "http://%s:%s/v2.1/%s" % (self
.api
.ip
, self
.api
.port
+ 3774, user
['id']),
264 "id": "2dad48f09e2a447a9bf852bcd93548ef",
265 "publicURL": "http://%s:%s/v2.1/%s" % (self
.api
.ip
, self
.api
.port
+ 3774, user
['id'])
268 "endpoints_links": [],
275 "adminURL": "http://%s:%s/v2.0" % (self
.api
.ip
, self
.api
.port
),
276 "region": "RegionOne",
277 "internalURL": "http://%s:%s/v2.0" % (self
.api
.ip
, self
.api
.port
),
278 "id": "2dad48f09e2a447a9bf852bcd93543fc",
279 "publicURL": "http://%s:%s/v2" % (self
.api
.ip
, self
.api
.port
)
282 "endpoints_links": [],
289 "adminURL": "http://%s:%s" % (self
.api
.ip
, self
.api
.port
+ 4696),
290 "region": "RegionOne",
291 "internalURL": "http://%s:%s" % (self
.api
.ip
, self
.api
.port
+ 4696),
292 "id": "2dad48f09e2a447a9bf852bcd93548cf",
293 "publicURL": "http://%s:%s" % (self
.api
.ip
, self
.api
.port
+ 4696)
296 "endpoints_links": [],
303 "adminURL": "http://%s:%s" % (self
.api
.ip
, self
.api
.port
+ 4242),
304 "region": "RegionOne",
305 "internalURL": "http://%s:%s" % (self
.api
.ip
, self
.api
.port
+ 4242),
306 "id": "2dad48f09e2a447a9bf852bcd93548cf",
307 "publicURL": "http://%s:%s" % (self
.api
.ip
, self
.api
.port
+ 4242)
310 "endpoints_links": [],
317 "adminURL": "http://%s:%s/v1/%s" % (self
.api
.ip
, self
.api
.port
+ 3004, user
['id']),
318 "region": "RegionOne",
319 "internalURL": "http://%s:%s/v1/%s" % (self
.api
.ip
, self
.api
.port
+ 3004, user
['id']),
320 "id": "2dad48f09e2a447a9bf852bcd93548bf",
321 "publicURL": "http://%s:%s/v1/%s" % (self
.api
.ip
, self
.api
.port
+ 3004, user
['id'])
324 "endpoints_links": [],
325 "type": "orchestration",
330 ret
['access']["metadata"] = {
333 "7598ac3c634d4c3da4b9126a5f67ca2b"
336 ret
['access']['trust'] = {
337 "id": "394998fa61f14736b1f0c1f322882949",
338 "trustee_user_id": "269348fdd9374b8885da1418e0730af1",
339 "trustor_user_id": "3ec3164f750146be97f21559ee4d9c51",
340 "impersonation": False
342 return Response(json
.dumps(ret
), status
=200, mimetype
='application/json')
344 except Exception as ex
:
345 logging
.exception("Keystone: Get token failed.")
346 return ex
.message
, 500
348 class KeystoneGetTokenv3(Resource
):
350 Returns a static keystone token.
351 We don't do any validation so we don't care.
354 def __init__(self
, api
):
359 List API entrypoints.
361 This is hardcoded. For a working "authentication" use these ENVVARS:
363 * OS_AUTH_URL=http://<ip>:<port>/v3
364 * OS_IDENTITY_API_VERSION=2.0
365 * OS_TENANT_ID=fc394f2ab2df4114bde39905f800dc57
366 * OS_REGION_NAME=RegionOne
370 :return: Returns an openstack style response for all entrypoints.
371 :rtype: :class:`flask.response`
374 logging
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
377 req
= json
.loads(request
.data
)
378 ret
['token'] = dict()
381 token
['issued_at'] = "2014-01-30T15:30:58.819Z"
382 token
['expires_at'] = "2999-01-30T15:30:58.819Z"
383 token
['methods'] = ["password"]
384 token
['extras'] = dict()
385 token
['user'] = dict()
387 user
['id'] = req
['auth'].get('token', {'id': 'fc394f2ab2df4114bde39905f800dc57'}).get('id')
388 user
['name'] = "tenantName"
389 user
['password_expires_at'] = None
390 user
['domain'] = {"id": "default", "name": "Default"}
391 token
['audit_ids'] = ["ZzZwkUflQfygX7pdYDBCQQ"]
399 "id": "8538a3f13f9541b28c2620eb19065e45",
404 token
['catalog'] = [{
407 "url": "http://%s:%s/v2.1/%s" % (self
.api
.ip
, self
.api
.port
+ 3774, user
['id']),
408 "region": "RegionOne",
409 "interface": "public",
410 "id": "2dad48f09e2a447a9bf852bcd93548ef"
413 "id": "2dad48f09e2a447a9bf852bcd93548ef",
420 "url": "http://%s:%s/v2.0" % (self
.api
.ip
, self
.api
.port
),
421 "region": "RegionOne",
422 "interface": "public",
423 "id": "2dad48f09e2a447a9bf852bcd93543fc"
426 "id": "2dad48f09e2a447a9bf852bcd93543fc",
433 "url": "http://%s:%s" % (self
.api
.ip
, self
.api
.port
+ 4696),
434 "region": "RegionOne",
435 "interface": "public",
436 "id": "2dad48f09e2a447a9bf852bcd93548cf"
439 "id": "2dad48f09e2a447a9bf852bcd93548cf",
446 "url": "http://%s:%s" % (self
.api
.ip
, self
.api
.port
+ 4242),
447 "region": "RegionOne",
448 "interface": "public",
449 "id": "2dad48f09e2a447a9bf852bcd93548cf"
452 "id": "2dad48f09e2a447a9bf852bcd93548cf",
459 "url": "http://%s:%s/v1/%s" % (self
.api
.ip
, self
.api
.port
+ 3004, user
['id']),
460 "region": "RegionOne",
461 "interface": "public",
462 "id": "2dad48f09e2a447a9bf852bcd93548bf"
465 "id": "2dad48f09e2a447a9bf852bcd93548bf",
466 "type": "orchestration",
471 return Response(json
.dumps(ret
), status
=201, mimetype
='application/json')
473 except Exception as ex
:
474 logging
.exception("Keystone: Get token failed.")
475 return ex
.message
, 500