def update(self, request, system_id, id): """Read volume group on a machine. :param name: Name of the volume group. :param uuid: UUID of the volume group. :param add_block_devices: Block devices to add to the volume group. :param remove_block_devices: Block devices to remove from the volume group. :param add_partitions: Partitions to add to the volume group. :param remove_partitions: Partitions to remove from the volume group. 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 update volume group because the machine is not Ready.") form = UpdateVolumeGroupForm(volume_group, data=request.data) if not form.is_valid(): raise MAASAPIValidationError(form.errors) else: return form.save()
def test_is_not_valid_if_invalid_uuid(self): volume_group = factory.make_VolumeGroup() data = {"uuid": factory.make_string(size=32)} form = UpdateVolumeGroupForm(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_updates_uuid(self): volume_group = factory.make_VolumeGroup() vguuid = "%s" % uuid.uuid4() data = {"uuid": vguuid} form = UpdateVolumeGroupForm(volume_group, data=data) self.assertTrue(form.is_valid(), form._errors) volume_group = form.save() self.assertEqual(vguuid, volume_group.uuid)
def test_updates_name(self): volume_group = factory.make_VolumeGroup() name = factory.make_name("vg") data = {"name": name} form = UpdateVolumeGroupForm(volume_group, data=data) self.assertTrue(form.is_valid(), form._errors) volume_group = form.save() self.assertEqual(name, volume_group.name)
def test_adds_block_device_by_name(self): node = factory.make_Node() volume_group = factory.make_VolumeGroup(node=node) block_device = factory.make_PhysicalBlockDevice(node=node) data = {"add_block_devices": [block_device.name]} form = UpdateVolumeGroupForm(volume_group, data=data) self.assertTrue(form.is_valid(), form._errors) volume_group = form.save() self.assertEqual( volume_group.id, block_device.get_effective_filesystem().filesystem_group.id, )
def update(self, request, system_id, id): """@description-title Update a volume group @description Update a 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=false] Name of the volume group. @param (string) "uuid" [required=false] UUID of the volume group. @param (string) "add_block_devices" [required=false] Block devices to add to the volume group. @param (string) "remove_block_devices" [required=false] Block devices to remove from the volume group. @param (string) "add_partitions" [required=false] Partitions to add to the volume group. @param (string) "remove_partitions" [required=false] Partitions to remove from the volume group. @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-update] 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 update volume group because the machine is not Ready." ) form = UpdateVolumeGroupForm(volume_group, data=request.data) if not form.is_valid(): raise MAASAPIValidationError(form.errors) else: return form.save()
def test_adds_boot_disk(self): node = factory.make_Node(with_boot_disk=False) boot_disk = factory.make_PhysicalBlockDevice(node=node) volume_group = factory.make_VolumeGroup(node=node) data = {"add_block_devices": [boot_disk.id]} form = UpdateVolumeGroupForm(volume_group, data=data) self.assertTrue(form.is_valid(), form._errors) volume_group = form.save() boot_partition = boot_disk.get_partitiontable().partitions.first() self.assertEqual( boot_partition.get_effective_filesystem().filesystem_group.id, volume_group.id, )
def test_removes_block_device_by_name(self): node = factory.make_Node() volume_group = factory.make_VolumeGroup(node=node) block_device = factory.make_PhysicalBlockDevice(node=node) factory.make_Filesystem(fstype=FILESYSTEM_TYPE.LVM_PV, block_device=block_device, filesystem_group=volume_group) data = { 'remove_block_devices': [block_device.name], } form = UpdateVolumeGroupForm(volume_group, data=data) self.assertTrue(form.is_valid(), form._errors) volume_group = form.save() self.assertIsNone(block_device.get_effective_filesystem())
def test_adds_partition_by_name(self): node = factory.make_Node() volume_group = factory.make_VolumeGroup(node=node) block_device = factory.make_PhysicalBlockDevice(node=node) partition_table = factory.make_PartitionTable( block_device=block_device) partition = factory.make_Partition(partition_table=partition_table) data = {"add_partitions": [partition.name]} form = UpdateVolumeGroupForm(volume_group, data=data) self.assertTrue(form.is_valid(), form._errors) volume_group = form.save() self.assertEqual( volume_group.id, partition.get_effective_filesystem().filesystem_group.id, )
def test_removes_partition(self): node = factory.make_Node() volume_group = factory.make_VolumeGroup(node=node) block_device = factory.make_PhysicalBlockDevice(node=node) partition_table = factory.make_PartitionTable( block_device=block_device) partition = factory.make_Partition(partition_table=partition_table) factory.make_Filesystem(fstype=FILESYSTEM_TYPE.LVM_PV, partition=partition, filesystem_group=volume_group) data = { 'remove_partitions': [partition.id], } form = UpdateVolumeGroupForm(volume_group, data=data) self.assertTrue(form.is_valid(), form._errors) volume_group = form.save() self.assertIsNone(partition.get_effective_filesystem())
def test_requires_no_fields(self): volume_group = factory.make_VolumeGroup() form = UpdateVolumeGroupForm(volume_group, data={}) self.assertTrue(form.is_valid(), form.errors)