コード例 #1
0
    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()
コード例 #2
0
 def test_is_not_valid_if_invalid_uuid(self):
     fstype = factory.pick_filesystem_type()
     partition = factory.make_Partition()
     data = {"fstype": fstype, "uuid": factory.make_string(size=32)}
     form = FormatPartitionForm(partition, data=data)
     self.assertFalse(form.is_valid(),
                      "Should be invalid because of an invalid uuid.")
     self.assertEqual({"uuid": ["Enter a valid value."]}, form._errors)
コード例 #3
0
 def test_is_not_valid_if_vmfs_partition(self):
     node = factory.make_Node(with_boot_disk=False)
     bd = factory.make_PhysicalBlockDevice(node=node,
                                           size=LARGE_BLOCK_DEVICE)
     layout = VMFS6StorageLayout(node)
     layout.configure()
     pt = bd.get_partitiontable()
     partition = random.choice(list(pt.partitions.all()))
     form = FormatPartitionForm(partition, {"fstype": FILESYSTEM_TYPE.EXT4})
     self.assertFalse(form.is_valid())
コード例 #4
0
 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()
コード例 #5
0
 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)
コード例 #6
0
 def test_is_not_valid_if_invalid_format_fstype(self):
     partition = factory.make_Partition()
     data = {"fstype": FILESYSTEM_TYPE.LVM_PV}
     form = FormatPartitionForm(partition, data=data)
     self.assertFalse(form.is_valid(),
                      "Should be invalid because of an invalid fstype.")
     self.assertEqual(
         {
             "fstype": [
                 "Select a valid choice. lvm-pv is not one of the "
                 "available choices."
             ]
         },
         form._errors,
     )
コード例 #7
0
 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)
コード例 #8
0
 def test_is_not_valid_if_non_user_format_fstype(self):
     partition = factory.make_Partition()
     factory.make_Filesystem(fstype='bcache-backing', partition=partition)
     data = {
         'fstype': FILESYSTEM_TYPE.EXT4,
     }
     form = FormatPartitionForm(partition, data=data)
     self.assertRaises(ValidationError, form.save)
コード例 #9
0
ファイル: partitions.py プロジェクト: zhangrb/maas
    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()
コード例 #10
0
 def test_requires_fields(self):
     form = FormatPartitionForm(partition=factory.make_Partition(), data={})
     self.assertFalse(form.is_valid(), form.errors)
     self.assertItemsEqual(["fstype"], form.errors.keys())