Example #1
0
 def update_blockdevice_filesystem(self, blockdevice, fstype, mount_point,
                                   mount_options):
     fs = blockdevice.get_effective_filesystem()
     if not fstype:
         if fs:
             fs.delete()
         return
     if fs is None or fstype != fs.fstype:
         form = FormatBlockDeviceForm(blockdevice, {'fstype': fstype})
         if not form.is_valid():
             raise HandlerError(form.errors)
         form.save()
         fs = blockdevice.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(
                 blockdevice.get_effective_filesystem(), {
                     'mount_point': mount_point,
                     'mount_options': mount_options
                 })
             if not form.is_valid():
                 raise HandlerError(form.errors)
             else:
                 form.save()
Example #2
0
 def test_creates_filesystem(self):
     fsuuid = "%s" % uuid.uuid4()
     fstype = factory.pick_filesystem_type()
     block_device = factory.make_PhysicalBlockDevice()
     data = {"uuid": fsuuid, "fstype": fstype}
     form = FormatBlockDeviceForm(block_device, data=data)
     self.assertTrue(form.is_valid(), form._errors)
     form.save()
     filesystem = get_one(
         Filesystem.objects.filter(block_device=block_device))
     self.assertIsNotNone(filesystem)
     self.assertEqual(fstype, filesystem.fstype)
     self.assertEqual(fsuuid, filesystem.uuid)
Example #3
0
 def test_deletes_old_filesystem_and_creates_new_one(self):
     fstype = factory.pick_filesystem_type()
     block_device = factory.make_PhysicalBlockDevice()
     prev_filesystem = factory.make_Filesystem(block_device=block_device)
     data = {"fstype": fstype}
     form = FormatBlockDeviceForm(block_device, data=data)
     self.assertTrue(form.is_valid(), form._errors)
     form.save()
     self.assertEqual(
         1,
         Filesystem.objects.filter(block_device=block_device).count(),
         "Should only be one filesystem that exists for block device.",
     )
     self.assertIsNone(reload_object(prev_filesystem))
     filesystem = get_one(
         Filesystem.objects.filter(block_device=block_device))
     self.assertIsNotNone(filesystem)
     self.assertEqual(fstype, filesystem.fstype)
Example #4
0
    def format(self, request, system_id, id):
        """Format block device with filesystem.

        :param fstype: Type of filesystem.
        :param uuid: UUID of the filesystem.

        Returns 403 when the user doesn't have the ability to format the \
            block device.
        Returns 404 if the machine or block device is not found.
        Returns 409 if the machine is not Ready or Allocated.
        """
        device = BlockDevice.objects.get_block_device_or_404(
            system_id, id, request.user, NODE_PERMISSION.EDIT)
        node = device.get_node()
        raise_error_for_invalid_state_on_allocated_operations(
            node, request.user, "format")
        form = FormatBlockDeviceForm(device, data=request.data)
        if form.is_valid():
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)
Example #5
0
    def format(self, request, system_id, id):
        """@description-title Format block device
        @description Format block device with filesystem.

        @param (string) "{system_id}" [required=true] The machine system_id.
        @param (string) "{id}" [required=true] The block device's id.

        @param (string) "fstype" [required=true] Type of filesystem.

        @param (string) "uuid" [required=false] UUID of the filesystem.

        @success (http-status-code) "server-success" 200
        @success (json) "success-json" A JSON object containing the updated
        block device.
        @success-example "success-json" [exkey=block-devs-format] placeholder
        text

        @error (http-status-code) "403" 403
        @error (content) "no-perms" The user does not have permissions to
        format the block device.

        @error (http-status-code) "404" 404
        @error (content) "not-found" The requested machine or block device 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.
        """
        device = BlockDevice.objects.get_block_device_or_404(
            system_id, id, request.user, NodePermission.edit)
        node = device.get_node()
        raise_error_for_invalid_state_on_allocated_operations(
            node, request.user, "format")
        form = FormatBlockDeviceForm(device, data=request.data)
        if form.is_valid():
            return form.save()
        else:
            raise MAASAPIValidationError(form.errors)