def get_context_data(self, **kwargs): context = super(CreateView, self).get_context_data(**kwargs) try: context['usages'] = quotas.tenant_limit_usages(self.request) except Exception: exceptions.handle(self.request) return context
def get_context_data(self, **kwargs): context = super(CreateSnapshotView, self).get_context_data(**kwargs) context['volume_id'] = self.kwargs['volume_id'] try: context['usages'] = quotas.tenant_limit_usages(self.request) except Exception: exceptions.handle(self.request) return context
def handle(self, request, data): try: usages = quotas.tenant_limit_usages(self.request) availableGB = usages["maxTotalVolumeGigabytes"] - usages["gigabytesUsed"] availableVol = usages["maxTotalVolumes"] - usages["volumesUsed"] snapshot_id = None image_id = None source_type = data.get("volume_source_type", None) if data.get("snapshot_source", None) and source_type in [None, "snapshot_source"]: # Create from Snapshot snapshot = self.get_snapshot(request, data["snapshot_source"]) snapshot_id = snapshot.id if data["size"] < snapshot.size: error_message = _("The volume size cannot be less than " "the snapshot size (%sGB)" % snapshot.size) raise ValidationError(error_message) elif data.get("image_source", None) and source_type in [None, "image_source"]: # Create from Snapshot image = self.get_image(request, data["image_source"]) image_id = image.id image_size = functions.bytes_to_gigabytes(image.size) if data["size"] < image_size: error_message = _( "The volume size cannot be less than " "the image size (%s)" % filesizeformat(image.size) ) raise ValidationError(error_message) else: if type(data["size"]) is str: data["size"] = int(data["size"]) if availableGB < data["size"]: error_message = _( "A volume of %(req)iGB cannot be created as " "you only have %(avail)iGB of your quota " "available." ) params = {"req": data["size"], "avail": availableGB} raise ValidationError(error_message % params) elif availableVol <= 0: error_message = _("You are already using all of your available" " volumes.") raise ValidationError(error_message) metadata = {} volume = cinder.volume_create( request, data["size"], data["name"], data["description"], data["type"], snapshot_id=snapshot_id, image_id=image_id, metadata=metadata, ) message = _('Creating volume "%s"') % data["name"] messages.info(request, message) return volume except ValidationError as e: self.api_error(e.messages[0]) return False except Exception: exceptions.handle(request, ignore=True) self.api_error(_("Unable to create volume.")) return False
def handle(self, request, data): try: usages = quotas.tenant_limit_usages(self.request) availableGB = usages['maxTotalVolumeGigabytes'] - \ usages['gigabytesUsed'] availableVol = usages['maxTotalVolumes'] - usages['volumesUsed'] snapshot_id = None image_id = None source_type = data.get('volume_source_type', None) if (data.get("snapshot_source", None) and source_type in [None, 'snapshot_source']): # Create from Snapshot snapshot = self.get_snapshot(request, data["snapshot_source"]) snapshot_id = snapshot.id if (data['size'] < snapshot.size): error_message = _('The volume size cannot be less than ' 'the snapshot size (%sGB)' % snapshot.size) raise ValidationError(error_message) elif (data.get("image_source", None) and source_type in [None, 'image_source']): # Create from Snapshot image = self.get_image(request, data["image_source"]) image_id = image.id image_size = functions.bytes_to_gigabytes(image.size) if (data['size'] < image_size): error_message = _('The volume size cannot be less than ' 'the image size (%s)' % filesizeformat(image.size)) raise ValidationError(error_message) else: if type(data['size']) is str: data['size'] = int(data['size']) if availableGB < data['size']: error_message = _('A volume of %(req)iGB cannot be created as ' 'you only have %(avail)iGB of your quota ' 'available.') params = {'req': data['size'], 'avail': availableGB} raise ValidationError(error_message % params) elif availableVol <= 0: error_message = _('You are already using all of your available' ' volumes.') raise ValidationError(error_message) metadata = {} volume = cinder.volume_create(request, data['size'], data['name'], data['description'], data['type'], snapshot_id=snapshot_id, image_id=image_id, metadata=metadata) message = _('Creating volume "%s"') % data['name'] messages.info(request, message) return volume except ValidationError as e: self.api_error(e.messages[0]) return False except Exception: exceptions.handle(request, ignore=True) self.api_error(_("Unable to create volume.")) return False