Skip to content

Commit 933a30e

Browse files
authored
Merge pull request #8 from sandwichcloud/fixinstancetoimage
fix converting instances to images
2 parents 6657709 + 6968694 commit 933a30e

File tree

6 files changed

+29
-23
lines changed

6 files changed

+29
-23
lines changed

deli_counter/http/mounts/root/mount.py

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def validate_token(self):
3333

3434
fernets = []
3535
for key in settings.AUTH_FERNET_KEYS:
36-
self.logger.info("FK: " + key)
3736
fernets.append(Fernet(key))
3837
fernet = MultiFernet(fernets)
3938

deli_counter/http/mounts/root/routes/v1/images.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,17 @@ def create(self):
3333
with cherrypy.request.db_session() as session:
3434
project = cherrypy.request.project
3535

36+
region = session.query(Region).filter(Region.id == request.region_id).first()
37+
if region is None:
38+
raise cherrypy.HTTPError(404, "A region with the requested id does not exist.")
39+
40+
if region.state != RegionState.CREATED:
41+
raise cherrypy.HTTPError(412,
42+
"The requested region is not in the following state: %s" %
43+
RegionState.CREATED.value)
44+
3645
image = session.query(Image).filter(Image.project_id == project.id).filter(
37-
Image.name == request.name).first()
46+
Image.name == request.name).filter(Image.region_id == region.id).first()
3847

3948
if image is not None:
4049
raise cherrypy.HTTPError(409, 'An image with the requested name already exists.')
@@ -45,15 +54,6 @@ def create(self):
4554
if image is not None:
4655
raise cherrypy.HTTPError(409, 'An image with the requested file already exists.')
4756

48-
region = session.query(Region).filter(Region.id == request.region_id).first()
49-
if region is None:
50-
raise cherrypy.HTTPError(404, "A region with the requested id does not exist.")
51-
52-
if region.state != RegionState.CREATED:
53-
raise cherrypy.HTTPError(412,
54-
"The requested region is not in the following state: %s" %
55-
RegionState.CREATED.value)
56-
5757
image = Image()
5858
image.name = request.name
5959
image.file_name = request.file_name
@@ -105,7 +105,9 @@ def list(self, region_id, limit: int, marker: uuid.UUID):
105105
@cherrypy.tools.resource_object(id_param="image_id", cls=Image)
106106
@cherrypy.tools.enforce_policy(policy_name="images:delete")
107107
def delete(self, image_id):
108-
cherrypy.response.status = 202
108+
cherrypy.response.status = 204
109+
# Fix for https://github.com/cherrypy/cherrypy/issues/1657
110+
del cherrypy.response.headers['Content-Type']
109111
with cherrypy.request.db_session() as session:
110112
image: Image = session.merge(cherrypy.request.resource_object, load=False)
111113

deli_counter/http/mounts/root/routes/v1/instance.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ def list(self, image_id, region_id, zone_id, limit: int, marker: uuid.UUID):
194194
@cherrypy.tools.resource_object(id_param="instance_id", cls=Instance)
195195
@cherrypy.tools.enforce_policy(policy_name="instances:delete")
196196
def delete(self, instance_id: uuid.UUID):
197-
cherrypy.response.status = 202
197+
cherrypy.response.status = 204
198+
# Fix for https://github.com/cherrypy/cherrypy/issues/1657
199+
del cherrypy.response.headers['Content-Type']
198200
with cherrypy.request.db_session() as session:
199201
instance: Instance = session.merge(cherrypy.request.resource_object, load=False)
200202

@@ -302,8 +304,13 @@ def action_image(self, instance_id: uuid.UUID):
302304
raise cherrypy.HTTPError(409, "Can only image an instance in the following state: %s" %
303305
InstanceState.STOPPED.value)
304306

305-
region = session.query(Region).join(Zone, Region.id == Zone.region_id).filter(
306-
Zone.id == instance.zone_id).one()
307+
region = session.query(Region).filter(Region.id == instance.region_id).one()
308+
309+
image = session.query(Image).filter(Image.project_id == instance.project_id).filter(
310+
Image.name == request.name).filter(Image.region_id == region.id).first()
311+
312+
if image is not None:
313+
raise cherrypy.HTTPError(409, 'An image with the requested name already exists.')
307314

308315
instance.state = InstanceState.IMAGING
309316

@@ -317,12 +324,10 @@ def action_image(self, instance_id: uuid.UUID):
317324
session.add(image)
318325
session.flush()
319326

320-
# Delete vm without actually deleting the backing
321-
create_task(session, instance, delete_instance, instance_id=instance.id, delete_backing=False)
322-
323-
# Convert the vm to a template
324-
create_task(session, image, convert_vm, image_id=image.id, from_instance=True)
327+
# Clone the vm to a template
328+
create_task(session, image, convert_vm, image_id=image.id, instance_id=instance.id)
325329

330+
instance.current_task_id = image.current_task_id
326331
session.commit()
327332

328333
return ResponseImage.from_database(image)

deli_counter/test/test_images.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def test_delete(self, wsgi, app):
9898
# Test correct
9999
with patch('ingredients_tasks.tasks.image.delete_image.apply_async') as apply_async_mock:
100100
apply_async_mock.return_value = None
101-
self.delete(wsgi, "/v1/images/%s" % image.id, token=token, status=202)
101+
self.delete(wsgi, "/v1/images/%s" % image.id, token=token, status=204)
102102
# Check to make sure it's in deleting state
103103
with app.database.session() as session:
104104
image = session.query(Image).filter(Image.id == image.id).first()

deli_counter/test/test_instances.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def test_delete(self, wsgi, app):
8282
# Test correct
8383
with patch('ingredients_tasks.tasks.instance.delete_instance.apply_async') as apply_async_mock:
8484
apply_async_mock.return_value = None
85-
self.delete(wsgi, "/v1/instances/%s" % instance.id, token=token, status=202)
85+
self.delete(wsgi, "/v1/instances/%s" % instance.id, token=token, status=204)
8686
# Check to make sure it's in deleting state
8787
with app.database.session() as session:
8888
image = session.query(Instance).filter(Instance.id == instance.id).first()

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ requests==2.18.4 # Apache 2.0
55
PyGithub==1.35 # LGPL
66
cryptography==2.1.4 # Apache 2.0 or BSD
77
ingredients.http==0.0.12 # MIT
8-
ingredients.tasks==0.0.8 # MIT
8+
ingredients.tasks==0.0.9 # MIT
99
ingredients.db==0.0.11 # MIT

0 commit comments

Comments
 (0)