Commit 3ad100bb by Őry Máté

storage: refactor uuid generation to a method

parent 5f8dcc9e
...@@ -166,8 +166,10 @@ class Disk(AclBase, TimeStampedModel): ...@@ -166,8 +166,10 @@ class Disk(AclBase, TimeStampedModel):
if self.type not in type_mapping.keys(): if self.type not in type_mapping.keys():
raise self.WrongDiskTypeError(self.type) raise self.WrongDiskTypeError(self.type)
if self.type == 'iso':
filename = self.filename if self.type == 'iso' else str(uuid.uuid4()) filename = self.filename
else:
self.generate_filename()
new_type = type_mapping[self.type] new_type = type_mapping[self.type]
return Disk.objects.create(base=self, datastore=self.datastore, return Disk.objects.create(base=self, datastore=self.datastore,
...@@ -208,6 +210,8 @@ class Disk(AclBase, TimeStampedModel): ...@@ -208,6 +210,8 @@ class Disk(AclBase, TimeStampedModel):
def clean(self, *args, **kwargs): def clean(self, *args, **kwargs):
if self.size == "" and self.base: if self.size == "" and self.base:
self.size = self.base.size self.size = self.base.size
if self.filename is None:
self.generate_filename()
super(Disk, self).clean(*args, **kwargs) super(Disk, self).clean(*args, **kwargs)
def deploy(self, user=None, task_uuid=None): def deploy(self, user=None, task_uuid=None):
...@@ -274,6 +278,11 @@ class Disk(AclBase, TimeStampedModel): ...@@ -274,6 +278,11 @@ class Disk(AclBase, TimeStampedModel):
return local_tasks.create_empty.apply_async( return local_tasks.create_empty.apply_async(
args=[instance, params, user], queue="localhost.man") args=[instance, params, user], queue="localhost.man")
def generate_filename(self):
"""Generate a unique filename and set it on the object.
"""
self.filename = str(uuid.uuid4())
@classmethod @classmethod
def create_empty(cls, instance=None, params=None, def create_empty(cls, instance=None, params=None,
user=None, task_uuid=None): user=None, task_uuid=None):
...@@ -291,6 +300,8 @@ class Disk(AclBase, TimeStampedModel): ...@@ -291,6 +300,8 @@ class Disk(AclBase, TimeStampedModel):
disk = cls() disk = cls()
if params: if params:
disk.__dict__.update(params) disk.__dict__.update(params)
if disk.filename is None:
disk.generate_filename()
disk.save() disk.save()
act.disk = disk act.disk = disk
act.save() act.save()
...@@ -337,7 +348,7 @@ class Disk(AclBase, TimeStampedModel): ...@@ -337,7 +348,7 @@ class Disk(AclBase, TimeStampedModel):
:rtype: AsyncResult :rtype: AsyncResult
""" """
disk = cls() disk = cls()
disk.filename = str(uuid.uuid4()) disk.generate_filename()
disk.type = "iso" disk.type = "iso"
disk.size = 1 disk.size = 1
# TODO get proper datastore # TODO get proper datastore
...@@ -421,12 +432,11 @@ class Disk(AclBase, TimeStampedModel): ...@@ -421,12 +432,11 @@ class Disk(AclBase, TimeStampedModel):
with disk_activity(code_suffix='save_as', disk=self, with disk_activity(code_suffix='save_as', disk=self,
task_uuid=task_uuid, user=user): task_uuid=task_uuid, user=user):
filename = str(uuid.uuid4())
new_type, new_base = mapping[self.type] new_type, new_base = mapping[self.type]
disk = Disk.objects.create(base=new_base, datastore=self.datastore, disk = Disk.objects.create(base=new_base, datastore=self.datastore,
filename=filename, name=self.name, name=self.name, size=self.size,
size=self.size, type=new_type) type=new_type)
queue_name = self.get_remote_queue_name('storage') queue_name = self.get_remote_queue_name('storage')
remote_tasks.merge.apply_async(args=[self.get_disk_desc(), remote_tasks.merge.apply_async(args=[self.get_disk_desc(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment