def test_save_updates_the_cache_set_with_partition(self): node = factory.make_Node() cache_device = factory.make_PhysicalBlockDevice(node=node) cache_set = factory.make_CacheSet(block_device=cache_device) new_partition = factory.make_Partition(node=node) form = UpdateCacheSetForm(cache_set=cache_set, data={"cache_partition": new_partition.id}) self.assertTrue(form.is_valid(), form.errors) cache_set = form.save() self.assertEqual(new_partition, cache_set.get_device()) self.assertIsNone(cache_device.get_effective_filesystem())
def test_save_updates_the_cache_set_with_boot_disk(self): node = factory.make_Node(with_boot_disk=False) boot_disk = factory.make_PhysicalBlockDevice(node=node) partition = factory.make_Partition(node=node) cache_set = factory.make_CacheSet(partition=partition) form = UpdateCacheSetForm(cache_set=cache_set, data={"cache_device": boot_disk.id}) self.assertTrue(form.is_valid(), form.errors) cache_set = form.save() boot_partition = boot_disk.get_partitiontable().partitions.first() self.assertEqual(boot_partition, cache_set.get_device()) self.assertIsNone(partition.get_effective_filesystem())
def test_choices_are_being_populated_correctly(self): node = factory.make_Node(with_boot_disk=False) # Make 10 block devices. bds = [factory.make_PhysicalBlockDevice(node=node) for _ in range(10)] # Partition the last 5 devices with a single partition. partitions = [ factory.make_PartitionTable(block_device=bd).add_partition() for bd in bds[5:] ] partition_choices = [p.id for p in partitions ] + [p.name for p in partitions] # Get the chocies of the non-partitioned devices. block_device_choices = [ bd.id for bd in bds if bd.get_partitiontable() is None ] + [bd.name for bd in bds if bd.get_partitiontable() is None] cache_set = factory.make_CacheSet(block_device=bds[1]) form = UpdateCacheSetForm(cache_set=cache_set, data={}) # Should allow all devices and partitions, including the one currently # in use on the cache set. self.assertItemsEqual( block_device_choices, [k for (k, v) in form.fields["cache_device"].choices], ) self.assertItemsEqual( partition_choices, [k for (k, v) in form.fields["cache_partition"].choices], )
def update(self, request, system_id, id): """@description-title Update a bcache set @description Update bcache cache set on a machine. Note: specifying both a cache_device and a cache_partition is not allowed. @param (string) "{system_id}" [required=true] A machine system_id. @param (string) "{id}" [required=true] A cache_set_id. @param (string) "cache_device" [required=false] Cache block device to replace current one. @param (string) "cache_partition" [required=false] Cache partition to replace current one. @success (http-status-code) "server-success" 200 @success (json) "success-json" A JSON object containing a bcache set. @success-example "success-json" [exkey=bcache-placeholder] 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. """ cache_set = CacheSet.objects.get_cache_set_or_404( system_id, id, request.user, NodePermission.admin) node = cache_set.get_node() if node.status != NODE_STATUS.READY: raise NodeStateViolation( "Cannot update cache set because the machine is not Ready.") form = UpdateCacheSetForm(cache_set, data=request.data) if form.is_valid(): create_audit_event( EVENT_TYPES.NODE, ENDPOINT.API, request, system_id, "Updated bcache cache set.", ) return form.save() else: raise MAASAPIValidationError(form.errors)
def update(self, request, system_id, id): """Delete bcache on a machine. :param cache_device: Cache block device to replace current one. :param cache_partition: Cache partition to replace current one. Specifying both a cache_device and a cache_partition is not allowed. Returns 404 if the machine or the cache set is not found. Returns 409 if the machine is not Ready. """ cache_set = CacheSet.objects.get_cache_set_or_404( system_id, id, request.user, NODE_PERMISSION.ADMIN) node = cache_set.get_node() if node.status != NODE_STATUS.READY: raise NodeStateViolation( "Cannot update cache set because the machine is not Ready.") form = UpdateCacheSetForm(cache_set, data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def update(self, request, system_id, id): """Update bcache cache set on a machine. :param cache_device: Cache block device to replace current one. :param cache_partition: Cache partition to replace current one. Specifying both a cache_device and a cache_partition is not allowed. Returns 404 if the machine or the cache set is not found. Returns 409 if the machine is not Ready. """ cache_set = CacheSet.objects.get_cache_set_or_404( system_id, id, request.user, NodePermission.admin) node = cache_set.get_node() if node.status != NODE_STATUS.READY: raise NodeStateViolation( "Cannot update cache set because the machine is not Ready.") form = UpdateCacheSetForm(cache_set, data=request.data) if form.is_valid(): create_audit_event(EVENT_TYPES.NODE, ENDPOINT.API, request, system_id, "Updated bcache cache set.") return form.save() else: raise MAASAPIValidationError(form.errors)