def update_partition_filesystem(self, node, partition_id, fstype, mount_point, mount_options): partition = Partition.objects.get( id=partition_id, partition_table__block_device__node=node) fs = partition.get_effective_filesystem() if not fstype: if fs: fs.delete() return if fs is None or fstype != fs.fstype: form = FormatPartitionForm(partition, {'fstype': fstype}) if not form.is_valid(): raise HandlerError(form.errors) form.save() fs = partition.get_effective_filesystem() if mount_point != fs.mount_point: # XXX: Elsewhere, a mount_point of "" would somtimes mean that the # filesystem is mounted, sometimes that it is *not* mounted. Which # is correct was not clear from the code history, so the existing # behaviour is maintained here. if mount_point is None or mount_point == "": fs.mount_point = None fs.mount_options = None fs.save() else: form = MountFilesystemForm( partition.get_effective_filesystem(), { 'mount_point': mount_point, 'mount_options': mount_options }) if not form.is_valid(): raise HandlerError(form.errors) else: form.save()
def test_creates_filesystem(self): fsuuid = "%s" % uuid.uuid4() fstype = factory.pick_filesystem_type() partition = factory.make_Partition() data = {"uuid": fsuuid, "fstype": fstype} form = FormatPartitionForm(partition, data=data) self.assertTrue(form.is_valid(), form._errors) form.save() filesystem = get_one(Filesystem.objects.filter(partition=partition)) self.assertIsNotNone(filesystem) self.assertEqual(fstype, filesystem.fstype) self.assertEqual(fsuuid, filesystem.uuid)
def format(self, request, system_id, device_id, id): """Format a partition. :param fstype: Type of filesystem. :param uuid: The UUID for the filesystem. :param label: The label for the filesystem. Returns 403 when the user doesn't have the ability to format the \ partition. Returns 404 if the node, block device, or partition is not found. """ device = BlockDevice.objects.get_block_device_or_404( system_id, device_id, request.user, NODE_PERMISSION.EDIT) partition_table = get_object_or_404( PartitionTable, block_device=device) partition = get_partition_by_id_or_name__or_404( id, partition_table) node = device.get_node() raise_error_for_invalid_state_on_allocated_operations( node, request.user, "format") form = FormatPartitionForm(partition, data=request.data) if not form.is_valid(): raise MAASAPIValidationError(form.errors) else: return form.save()
def test_deletes_old_filesystem_and_creates_new_one(self): fstype = factory.pick_filesystem_type() partition = factory.make_Partition() prev_filesystem = factory.make_Filesystem(partition=partition) data = {"fstype": fstype} form = FormatPartitionForm(partition, data=data) self.assertTrue(form.is_valid(), form._errors) form.save() self.assertEqual( 1, Filesystem.objects.filter(partition=partition).count(), "Should only be one filesystem that exists for partition.", ) self.assertIsNone(reload_object(prev_filesystem)) filesystem = get_one(Filesystem.objects.filter(partition=partition)) self.assertIsNotNone(filesystem) self.assertEqual(fstype, filesystem.fstype)
def format(self, request, system_id, device_id, id): """@description-title Format a partition @description Format the partition on machine system_id and device device_id with the given partition id. @param (string) "{system_id}" [required=true] The system_id. @param (int) "{device_id}" [required=true] The block device_id. @param (int) "{id}" [required=true] The partition id. @param (string) "fstype" [required=true] Type of filesystem. @param (string) "uuid" [required=false] The UUID for the filesystem. @param (string) "label" [required=false] The label for the filesystem. @success (http-status-code) "server-success" 200 @success (json) "success-json" A JSON object containing the updated partition object. @success-example "success-json" [exkey=partitions-format] placeholder text @error (http-status-code) "403" 403 @error (content) "no-perms" The user does not have permissions to format the partition. @error (http-status-code) "404" 404 @error (content) "not-found" The requested machine, device or partition is not found. @error-example "not-found" Not Found """ device = BlockDevice.objects.get_block_device_or_404( system_id, device_id, request.user, NodePermission.edit) partition_table = get_object_or_404(PartitionTable, block_device=device) partition = get_partition_by_id_or_name__or_404(id, partition_table) node = device.get_node() raise_error_for_invalid_state_on_allocated_operations( node, request.user, "format") form = FormatPartitionForm(partition, data=request.data) if not form.is_valid(): raise MAASAPIValidationError(form.errors) else: return form.save()