def create_logical_volume(self, params): """Create a logical volume.""" # Only admin users can perform delete. if not reload_object(self.user).is_superuser: raise HandlerPermissionError() node = self.get_object(params) volume_group = VolumeGroup.objects.get(id=params['volume_group_id']) if volume_group.get_node() != node: raise VolumeGroup.DoesNotExist() form = CreateLogicalVolumeForm(volume_group, { 'name': params['name'], 'size': params['size'], }) if not form.is_valid(): raise HandlerError(form.errors) else: logical_volume = form.save() self._update_obj_tags(logical_volume, params) if 'fstype' in params: self.update_blockdevice_filesystem(logical_volume, params.get("fstype"), params.get("mount_point"), params.get("mount_options"))
def test_is_not_valid_if_invalid_uuid(self): volume_group = factory.make_VolumeGroup() name = factory.make_name("lv") data = { 'name': name, 'uuid': factory.make_string(size=32), 'size': volume_group.get_size() - 1, } form = CreateLogicalVolumeForm(volume_group, data=data) self.assertFalse(form.is_valid(), "Should be invalid because of an invalid uuid.") self.assertEqual({'uuid': ["Enter a valid value."]}, form._errors)
def test_is_not_valid_if_size_less_than_minimum_block_size(self): volume_group = factory.make_VolumeGroup() name = factory.make_name("lv") data = {"name": name, "size": MIN_BLOCK_DEVICE_SIZE - 1} form = CreateLogicalVolumeForm(volume_group, data=data) self.assertFalse(form.is_valid(), "Should be invalid because of an invalid size.") self.assertEqual( { "size": [ "Ensure this value is greater than or equal to %s." % MIN_BLOCK_DEVICE_SIZE ] }, form._errors, )
def create_logical_volume(self, request, system_id, id): """@description-title Create a logical volume @description Create a logical volume in the volume group with the given id on the machine with the given system_id. @param (string) "{system_id}" [required=true] The machine system_id containing the volume group. @param (int) "{id}" [required=true] The id of the volume group. @param (string) "name" [required=true] Name of the logical volume. @param (string) "uuid" [required=false] (optional) UUID of the logical volume. @param (string) "size" [required=true] Size of the logical volume. Must be larger than or equal to 4,194,304 bytes. E.g. ``4194304``. @success (http-status-code) "server-success" 200 @success (json) "success-json" A JSON object containing the requested volume group. @success-example "success-json" [exkey=vol-groups-create-log-vol] placeholder text @error (http-status-code) "404" 404 @error (content) "not-found" The requested machine is not found. @error-example "not-found" Not Found @error (http-status-code) "409" 409 @error (content) "not-ready" The requested machine is not ready. """ volume_group = VolumeGroup.objects.get_object_or_404( system_id, id, request.user, NodePermission.admin ) node = volume_group.get_node() if node.status != NODE_STATUS.READY: raise NodeStateViolation( "Cannot create logical volume because the machine is not " "Ready." ) form = CreateLogicalVolumeForm(volume_group, data=request.data) if not form.is_valid(): raise MAASAPIValidationError(form.errors) else: return form.save()
def test_creates_logical_volume(self): volume_group = factory.make_VolumeGroup() name = factory.make_name("lv") vguuid = "%s" % uuid.uuid4() size = random.randint(MIN_BLOCK_DEVICE_SIZE, volume_group.get_size()) data = {"name": name, "uuid": vguuid, "size": size} form = CreateLogicalVolumeForm(volume_group, data=data) self.assertTrue(form.is_valid(), form._errors) logical_volume = form.save() expected_size = round_size_to_nearest_block( size, PARTITION_ALIGNMENT_SIZE, False ) self.assertThat( logical_volume, MatchesStructure.byEquality( name=name, uuid=vguuid, size=expected_size ), )
def test_is_not_valid_if_free_space_less_than_min_size(self): volume_group = factory.make_VolumeGroup() volume_group.create_logical_volume(factory.make_name("lv"), size=volume_group.get_size()) name = factory.make_name("lv") data = {"name": name, "size": MIN_BLOCK_DEVICE_SIZE} form = CreateLogicalVolumeForm(volume_group, data=data) self.assertFalse(form.is_valid(), "Should be invalid because of an no free space.") self.assertEqual( { "__all__": [ "Volume group (%s) cannot hold any more logical volumes, " "because it doesn't have enough free space." % (volume_group.name) ] }, form._errors, )
def test_is_not_valid_if_size_greater_than_free_space(self): volume_group = factory.make_VolumeGroup() volume_group.create_logical_volume(factory.make_name("lv"), size=volume_group.get_size() - MIN_BLOCK_DEVICE_SIZE - 1) name = factory.make_name("lv") free_space = volume_group.get_lvm_free_space() data = { 'name': name, 'size': free_space + 2, } form = CreateLogicalVolumeForm(volume_group, data=data) self.assertFalse(form.is_valid(), "Should be invalid because of an invalid size.") self.assertEqual( { 'size': [ "Ensure this value is less than or equal to %s." % (volume_group.get_lvm_free_space()), ] }, form._errors)
def create_logical_volume(self, params): """Create a logical volume.""" node = self._get_node_or_permission_error( params, permission=self._meta.edit_permission) volume_group = VolumeGroup.objects.get(id=params['volume_group_id']) if volume_group.get_node() != node: raise VolumeGroup.DoesNotExist() form = CreateLogicalVolumeForm( volume_group, { 'name': params['name'], 'size': params['size'], }) if not form.is_valid(): raise HandlerError(form.errors) else: logical_volume = form.save() self._update_obj_tags(logical_volume, params) if 'fstype' in params: self.update_blockdevice_filesystem( logical_volume, params.get("fstype"), params.get("mount_point"), params.get("mount_options"))
def create_logical_volume(self, request, system_id, id): """Create a logical volume in the volume group. :param name: Name of the logical volume. :param uuid: (optional) UUID of the logical volume. :param size: Size of the logical volume. Returns 404 if the machine or volume group is not found. Returns 409 if the machine is not Ready. """ volume_group = VolumeGroup.objects.get_object_or_404( system_id, id, request.user, NodePermission.admin) node = volume_group.get_node() if node.status != NODE_STATUS.READY: raise NodeStateViolation( "Cannot create logical volume because the machine is not " "Ready.") form = CreateLogicalVolumeForm(volume_group, data=request.data) if not form.is_valid(): raise MAASAPIValidationError(form.errors) else: return form.save()
def test_requires_no_fields(self): volume_group = factory.make_VolumeGroup() form = CreateLogicalVolumeForm(volume_group, data={}) self.assertFalse(form.is_valid(), form.errors) self.assertItemsEqual(['name', 'size'], form.errors.keys())