4b9b9c682af515ca6469242bda814260fdde34e1
1 from flask_restful
import Resource
2 from flask
import Response
, request
3 from emuvim
.api
.openstack
.openstack_dummies
.base_openstack_dummy
import BaseOpenstackDummy
8 LOG
= logging
.getLogger("api.openstack.glance")
11 class GlanceDummyApi(BaseOpenstackDummy
):
12 def __init__(self
, in_ip
, in_port
, compute
):
13 super(GlanceDummyApi
, self
).__init
__(in_ip
, in_port
)
14 self
.compute
= compute
15 self
.api
.add_resource(Shutdown
,
17 self
.api
.add_resource(GlanceListApiVersions
,
19 self
.api
.add_resource(GlanceSchema
,
21 "/v2/schemas/metadefs/namespace",
22 "/v2/schemas/metadefs/resource_type")
23 self
.api
.add_resource(GlanceListImagesApi
,
28 resource_class_kwargs
={'api': self
})
29 self
.api
.add_resource(GlanceImageByIdApi
,
32 resource_class_kwargs
={'api': self
})
34 def _start_flask(self
):
35 LOG
.info("Starting %s endpoint @ http://%s:%d" % ("GlanceDummyApi", self
.ip
, self
.port
))
36 if self
.app
is not None:
37 self
.app
.before_request(self
.dump_playbook
)
38 self
.app
.run(self
.ip
, self
.port
, debug
=True, use_reloader
=False)
41 class Shutdown(Resource
):
43 LOG
.debug(("%s is beeing shut down") % (__name__
))
44 func
= request
.environ
.get('werkzeug.server.shutdown')
46 raise RuntimeError('Not running with the Werkzeug Server')
50 class GlanceListApiVersions(Resource
):
52 LOG
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
54 resp
['versions'] = dict()
60 "href": request
.url_root
+ '/v2',
65 resp
['versions'] = versions
66 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
69 class GlanceSchema(Resource
):
71 LOG
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
73 resp
['name'] = 'someImageName'
74 resp
['properties'] = dict()
75 # just an ugly hack to allow the openstack client to work
76 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
79 class GlanceListImagesApi(Resource
):
80 def __init__(self
, api
):
84 LOG
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
88 resp
['first'] = "/v2/images"
89 resp
['schema'] = "/v2/schemas/images"
90 resp
['images'] = list()
93 for image
in self
.api
.compute
.images
.values():
96 f
['name'] = str(image
.name
).replace(":latest", "")
97 f
['checksum'] = "2dad48f09e2a447a9bf852bcd93548c1"
98 f
['container_format'] = "docker"
99 f
['disk_format'] = "raw"
101 f
['created_at'] = "2016-03-15T15:09:07.000000"
103 f
['deleted_at'] = None
104 f
['is_public'] = True
107 f
['owner'] = "3dad48f09e2a447a9bf852bcd93548c1"
109 f
['protected'] = False
110 f
['status'] = "active"
111 f
['updated_at'] = "2016-03-15T15:09:07.000000"
112 f
['virtual_size'] = 1
114 resp
['images'].append(f
)
116 if c
> limit
: # ugly hack to stop buggy glance client to do infinite requests
118 if "marker" in request
.args
: # ugly hack to fix pageination of openstack client
119 resp
['images'] = None
120 return Response(json
.dumps(resp
), status
=200, mimetype
="application/json")
122 except Exception as ex
:
123 LOG
.exception(u
"%s: Could not retrieve the list of images." % __name__
)
124 return ex
.message
, 500
128 This one is a real fake! It does not really create anything and the mentioned image
129 should already be registered with Docker. However, this function returns a reply that looks
130 like the image was just created to make orchestrators, like OSM, happy.
132 LOG
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
133 # lets see what we should create
134 img_name
= request
.headers
.get("X-Image-Meta-Name")
135 img_size
= request
.headers
.get("X-Image-Meta-Size")
136 img_disk_format
= request
.headers
.get("X-Image-Meta-Disk-Format")
137 img_is_public
= request
.headers
.get("X-Image-Meta-Is-Public")
138 img_container_format
= request
.headers
.get("X-Image-Meta-Container-Format")
139 # try to find ID of already existing image (matched by name)
141 for image
in self
.api
.compute
.images
.values():
142 if img_name
in image
.name
:
144 LOG
.debug("Image name: %s" % img_name
)
145 LOG
.debug("Image id: %s" % img_id
)
146 # build a response body that looks like a real one
151 f
['checksum'] = "2dad48f09e2a447a9bf852bcd93548c1"
152 f
['container_format'] = img_container_format
153 f
['disk_format'] = img_disk_format
155 f
['created_at'] = "2016-03-15T15:09:07.000000"
157 f
['deleted_at'] = None
158 f
['is_public'] = img_is_public
161 f
['owner'] = "3dad48f09e2a447a9bf852bcd93548c1"
163 f
['protected'] = False
164 f
['status'] = "active"
165 f
['updated_at'] = "2016-03-15T15:09:07.000000"
166 f
['virtual_size'] = 1
168 # build actual response with headers and everything
169 r
= Response(json
.dumps(resp
), status
=201, mimetype
="application/json")
170 r
.headers
.add("Location", "http://%s:%d/v1/images/%s" % (self
.api
.ip
,
176 class GlanceImageByIdApi(Resource
):
177 def __init__(self
, api
):
181 LOG
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
182 from emuvim
.api
.heat
.openstack_dummies
.nova_dummy_api
import NovaListImages
183 nova
= NovaListImages(self
.api
)
187 LOG
.debug("API CALL: %s " % str(self
.__class
__.__name
__))
188 LOG
.warning("Endpoint not implemented")