def clean(self): """Validate the lv_size.""" cleaned_data = super(LVMStorageLayout, self).clean() lv_size = self.get_lv_size() if lv_size is not None: root_size = self.get_root_size() if root_size is None: root_size = ( self.boot_disk.size - EFI_PARTITION_SIZE - self.get_boot_size() ) if is_percentage(lv_size): lv_size = calculate_size_from_percentage(root_size, lv_size) if lv_size < MIN_ROOT_PARTITION_SIZE: set_form_error( self, "lv_size", "Size is too small. Minimum size is %s." % MIN_ROOT_PARTITION_SIZE, ) if lv_size > root_size: set_form_error( self, "lv_size", "Size is too large. Maximum size is %s." % root_size, ) cleaned_data["lv_size"] = lv_size return cleaned_data
def clean(self): # Circular imports. from maasserver.models.blockdevice import MIN_BLOCK_DEVICE_SIZE cleaned_data = super(BcacheStorageLayoutBase, self).clean() cache_device = self.get_cache_device() cache_size = self.get_cache_size() cache_no_part = self.get_cache_no_part() if cache_size is not None and cache_no_part: error_msg = ( "Cannot use cache_size and cache_no_part at the same time." ) set_form_error(self, "cache_size", error_msg) set_form_error(self, "cache_no_part", error_msg) elif cache_device is not None and cache_size is not None: if is_percentage(cache_size): cache_size = calculate_size_from_percentage( cache_device.size, cache_size ) if cache_size < MIN_BLOCK_DEVICE_SIZE: set_form_error( self, "cache_size", "Size is too small. Minimum size is %s." % MIN_BLOCK_DEVICE_SIZE, ) if cache_size > cache_device.size: set_form_error( self, "cache_size", "Size is too large. Maximum size is %s." % (cache_device.size), ) cleaned_data["cache_size"] = cache_size return cleaned_data
def _clean_size(self, field, min_size=None, max_size=None): """Clean a size field.""" size = self.cleaned_data[field] if size is None: return None if is_percentage(size): # Calculate the percentage not counting the EFI partition. size = calculate_size_from_percentage( self.boot_disk.size - EFI_PARTITION_SIZE, size) if min_size is not None and size < min_size: raise ValidationError("Size is too small. Minimum size is %s." % min_size) if max_size is not None and size > max_size: raise ValidationError("Size is too large. Maximum size is %s." % max_size) return size