f02ba12de7d8b9d305e0bd7dba98c065854b716d
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
__))
134 body_data
= json
.loads(request
.data
)
137 # lets see what we should create
138 img_name
= request
.headers
.get("X-Image-Meta-Name")
139 img_size
= request
.headers
.get("X-Image-Meta-Size")
140 img_disk_format
= request
.headers
.get("X-Image-Meta-Disk-Format")
141 img_is_public
= request
.headers
.get("X-Image-Meta-Is-Public")
142 img_container_format
= request
.headers
.get("X-Image-Meta-Container-Format")
143 # try to use body payload if header fields are empty
145 img_name
= body_data
.get("name")
147 img_disk_format
= body_data
.get("disk_format")
148 img_is_public
= True if "public" in body_data
.get("visibility") else False
149 img_container_format
= body_data
.get("container_format")
150 # try to find ID of already existing image (matched by name)
152 for image
in self
.api
.compute
.images
.values():
153 if str(img_name
) in image
.name
:
155 LOG
.debug("Image name: %s" % img_name
)
156 LOG
.debug("Image id: %s" % img_id
)
157 # build a response body that looks like a real one
162 f
['checksum'] = "2dad48f09e2a447a9bf852bcd93548c1"
163 f
['container_format'] = img_container_format
164 f
['disk_format'] = img_disk_format
166 f
['created_at'] = "2016-03-15T15:09:07.000000"
168 f
['deleted_at'] = None
169 f
['is_public'] = img_is_public
172 f
['owner'] = "3dad48f09e2a447a9bf852bcd93548c1"
174 f
['protected'] = False
175 f
['status'] = "active"
176 f
['updated_at'] = "2016-03-15T15:09:07.000000"
177 f
['virtual_size'] = 1
179 # build actual response with headers and everything
180 r
= Response(json
.dumps(resp
), status
=201, mimetype
="application/json")
181 r
.headers
.add("Location", "http://%s:%d/v1/images/%s" % (self
.api
.ip
,
187 class GlanceImageByIdApi(Resource
):
188 def __init__(self
, api
):
192 LOG
.debug("API CALL: %s GET" % str(self
.__class
__.__name
__))
193 from emuvim
.api
.heat
.openstack_dummies
.nova_dummy_api
import NovaListImages
194 nova
= NovaListImages(self
.api
)
198 LOG
.debug("API CALL: %s " % str(self
.__class
__.__name
__))
199 LOG
.warning("Endpoint not implemented")