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
})
33 self
.api
.add_resource(GlanceImageByDockerNameApi
,
34 "/v1/images/<owner>/<container>",
35 "/v2/images/<owner>/<container>",
36 resource_class_kwargs
={'api': self
})
38 def _start_flask(self
):
39 LOG
.info("Starting %s endpoint @ http://%s:%d" % ("GlanceDummyApi", self
.ip
, self
.port
))
40 if self
.app
is not None:
41 self
.app
.before_request(self
.dump_playbook
)
42 self
.app
.run(self
.ip
, self
.port
, debug
=True, use_reloader
=False)
45 class Shutdown(Resource
):
47 LOG
.debug(("%s is beeing shut down") % (__name__
))
48 func
= request
.environ
.get('werkzeug.server.shutdown')
50 raise RuntimeError('Not running with the Werkzeug Server')
54 class GlanceListApiVersions(Resource
):
56 LOG
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
58 resp
['versions'] = dict()
64 "href": request
.url_root
+ '/v2',
69 resp
['versions'] = versions
70 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
73 class GlanceSchema(Resource
):
75 LOG
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
77 resp
['name'] = 'someImageName'
78 resp
['properties'] = dict()
79 # just an ugly hack to allow the openstack client to work
80 return Response(json
.dumps(resp
), status
=200, mimetype
='application/json')
83 class GlanceListImagesApi(Resource
):
84 def __init__(self
, api
):
88 LOG
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
92 resp
['first'] = "/v2/images"
93 resp
['schema'] = "/v2/schemas/images"
94 resp
['images'] = list()
97 for image
in self
.api
.compute
.images
.values():
100 f
['name'] = str(image
.name
).replace(":latest", "")
101 f
['checksum'] = "2dad48f09e2a447a9bf852bcd93548c1"
102 f
['container_format'] = "docker"
103 f
['disk_format'] = "raw"
105 f
['created_at'] = "2016-03-15T15:09:07.000000"
107 f
['deleted_at'] = None
108 f
['is_public'] = True
111 f
['owner'] = "3dad48f09e2a447a9bf852bcd93548c1"
113 f
['protected'] = False
114 f
['status'] = "active"
115 f
['updated_at'] = "2016-03-15T15:09:07.000000"
116 f
['virtual_size'] = 1
118 resp
['images'].append(f
)
120 if c
> limit
: # ugly hack to stop buggy glance client to do infinite requests
122 if "marker" in request
.args
: # ugly hack to fix pageination of openstack client
123 resp
['images'] = None
124 return Response(json
.dumps(resp
), status
=200, mimetype
="application/json")
126 except Exception as ex
:
127 LOG
.exception(u
"%s: Could not retrieve the list of images." % __name__
)
128 return ex
.message
, 500
132 This one is a real fake! It does not really create anything and the mentioned image
133 should already be registered with Docker. However, this function returns a reply that looks
134 like the image was just created to make orchestrators, like OSM, happy.
136 LOG
.debug("API CALL: %s POST" % str(self
.__class
__.__name
__))
138 body_data
= json
.loads(request
.data
)
141 # lets see what we should create
142 img_name
= request
.headers
.get("X-Image-Meta-Name")
143 img_size
= request
.headers
.get("X-Image-Meta-Size")
144 img_disk_format
= request
.headers
.get("X-Image-Meta-Disk-Format")
145 img_is_public
= request
.headers
.get("X-Image-Meta-Is-Public")
146 img_container_format
= request
.headers
.get("X-Image-Meta-Container-Format")
147 # try to use body payload if header fields are empty
149 img_name
= body_data
.get("name")
151 img_disk_format
= body_data
.get("disk_format")
152 img_is_public
= True if "public" in body_data
.get("visibility") else False
153 img_container_format
= body_data
.get("container_format")
154 # try to find ID of already existing image (matched by name)
156 for image
in self
.api
.compute
.images
.values():
157 if str(img_name
) in image
.name
:
159 LOG
.debug("Image name: %s" % img_name
)
160 LOG
.debug("Image id: %s" % img_id
)
161 # build a response body that looks like a real one
166 f
['checksum'] = "2dad48f09e2a447a9bf852bcd93548c1"
167 f
['container_format'] = img_container_format
168 f
['disk_format'] = img_disk_format
170 f
['created_at'] = "2016-03-15T15:09:07.000000"
172 f
['deleted_at'] = None
173 f
['is_public'] = img_is_public
176 f
['owner'] = "3dad48f09e2a447a9bf852bcd93548c1"
178 f
['protected'] = False
179 f
['status'] = "active"
180 f
['updated_at'] = "2016-03-15T15:09:07.000000"
181 f
['virtual_size'] = 1
183 # build actual response with headers and everything
184 r
= Response(json
.dumps(resp
), status
=201, mimetype
="application/json")
185 r
.headers
.add("Location", "http://%s:%d/v1/images/%s" % (self
.api
.ip
,
191 class GlanceImageByIdApi(Resource
):
192 def __init__(self
, api
):
196 LOG
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
199 for image
in self
.api
.compute
.images
.values():
200 if image
.id == id or image
.name
== id:
201 resp
['id'] = image
.id
202 resp
['name'] = image
.name
204 return Response(json
.dumps(resp
), status
=200, mimetype
="application/json")
206 response
= Response("Image with id or name %s does not exists." % id, status
=404)
207 response
.headers
['Access-Control-Allow-Origin'] = '*'
210 except Exception as ex
:
211 LOG
.exception(u
"%s: Could not retrieve image with id %s." % (__name__
, id))
212 return Response(ex
.message
, status
=500, mimetype
='application/json')
215 LOG
.debug("API CALL: %s " % str(self
.__class
__.__name
__))
216 LOG
.warning("Endpoint not implemented")
220 class GlanceImageByDockerNameApi(Resource
):
221 def __init__(self
, api
):
224 def get(self
, owner
, container
):
225 logging
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
227 name
= "%s/%s" % (owner
, container
)
228 if name
in self
.api
.compute
.images
:
229 image
= self
.api
.compute
.images
[name
]
231 resp
['id'] = image
.id
232 resp
['name'] = image
.name
233 return Response(json
.dumps(resp
), status
=200, mimetype
="application/json")
235 response
= Response("Image with id or name %s does not exists." % id, status
=404)
236 response
.headers
['Access-Control-Allow-Origin'] = '*'
239 except Exception as ex
:
240 logging
.exception(u
"%s: Could not retrieve image with id %s." % (__name__
, id))
241 return Response(ex
.message
, status
=500, mimetype
='application/json')