22
22
from maasserver.forms import (
23
23
CreatePhysicalBlockDeviceForm,
24
24
FormatBlockDeviceForm,
26
25
UpdatePhysicalBlockDeviceForm,
27
26
UpdateVirtualBlockDeviceForm,
28
from maasserver.forms_filesystem import MountFilesystemForm
29
29
from maasserver.models import (
61
61
node, user, operation):
62
62
if node.status not in [NODE_STATUS.READY, NODE_STATUS.ALLOCATED]:
63
63
raise NodeStateViolation(
64
"Cannot %s block device because the node is not Ready "
64
"Cannot %s block device because the machine is not Ready "
65
65
"or Allocated." % operation)
66
66
if node.status == NODE_STATUS.READY and not user.is_superuser:
67
67
raise PermissionDenied(
68
68
"Cannot %s block device because you don't have the "
69
"permissions on a Ready node." % operation)
69
"permissions on a Ready machine." % operation)
72
72
class BlockDevicesHandler(OperationsHandler):
73
"""Manage block devices on a node."""
73
"""Manage block devices on a machine."""
74
74
api_doc_section_name = "Block devices"
75
75
replace = update = delete = None
76
76
fields = DISPLAYED_BLOCKDEVICE_FIELDS
80
80
return ('blockdevices_handler', ["system_id"])
82
82
def read(self, request, system_id):
83
"""List all block devices belonging to node.
83
"""List all block devices belonging to a machine.
85
85
Returns 404 if the machine is not found.
203
203
def read(self, request, system_id, device_id):
204
204
"""Read block device on node.
206
Returns 404 if the node or block device is not found.
206
Returns 404 if the machine or block device is not found.
208
208
return BlockDevice.objects.get_block_device_or_404(
209
209
system_id, device_id, request.user, NODE_PERMISSION.VIEW)
211
211
def delete(self, request, system_id, device_id):
212
"""Delete block device on node.
212
"""Delete block device on a machine.
214
Returns 404 if the node or block device is not found.
214
Returns 404 if the machine or block device is not found.
215
215
Returns 403 if the user is not allowed to delete the block device.
216
Returns 409 if the node is not Ready.
216
Returns 409 if the machine is not Ready.
218
218
device = BlockDevice.objects.get_block_device_or_404(
219
219
system_id, device_id, request.user, NODE_PERMISSION.ADMIN)
220
220
node = device.get_node()
221
221
if node.status != NODE_STATUS.READY:
222
222
raise NodeStateViolation(
223
"Cannot delete block device because the node is not Ready.")
223
"Cannot delete block device because the machine is not Ready.")
225
225
return rc.DELETED
227
227
def update(self, request, system_id, device_id):
228
"""Update block device on node.
228
"""Update block device on a machine.
230
230
Fields for physical block device:
231
231
:param name: Name of the block device.
243
243
:param size: Size of the block device. (Only allowed for logical \
246
Returns 404 if the node or block device is not found.
246
Returns 404 if the machine or block device is not found.
247
247
Returns 403 if the user is not allowed to update the block device.
248
Returns 409 if the node is not Ready.
248
Returns 409 if the machine is not Ready.
250
250
device = BlockDevice.objects.get_block_device_or_404(
251
251
system_id, device_id, request.user, NODE_PERMISSION.ADMIN)
252
252
node = device.get_node()
253
253
if node.status != NODE_STATUS.READY:
254
254
raise NodeStateViolation(
255
"Cannot update block device because the node is not Ready.")
255
"Cannot update block device because the machine is not Ready.")
256
256
if device.type == 'physical':
257
257
form = UpdatePhysicalBlockDeviceForm(
258
258
instance=device, data=request.data)
270
270
@operation(idempotent=True)
271
271
def add_tag(self, request, system_id, device_id):
272
"""Add a tag to block device on node.
272
"""Add a tag to block device on a machine.
274
274
:param tag: The tag being added.
276
Returns 404 if the node or block device is not found.
276
Returns 404 if the machine or block device is not found.
277
277
Returns 403 if the user is not allowed to update the block device.
278
Returns 409 if the node is not Ready.
278
Returns 409 if the machine is not Ready.
280
280
device = BlockDevice.objects.get_block_device_or_404(
281
281
system_id, device_id, request.user, NODE_PERMISSION.ADMIN)
282
282
node = device.get_node()
283
283
if node.status != NODE_STATUS.READY:
284
284
raise NodeStateViolation(
285
"Cannot update block device because the node is not Ready.")
285
"Cannot update block device because the machine is not Ready.")
286
286
device.add_tag(get_mandatory_param(request.GET, 'tag'))
290
290
@operation(idempotent=True)
291
291
def remove_tag(self, request, system_id, device_id):
292
"""Remove a tag from block device on node.
292
"""Remove a tag from block device on a machine.
294
294
:param tag: The tag being removed.
296
Returns 404 if the node or block device is not found.
296
Returns 404 if the machine or block device is not found.
297
297
Returns 403 if the user is not allowed to update the block device.
298
Returns 409 if the node is not Ready.
298
Returns 409 if the machine is not Ready.
300
300
device = BlockDevice.objects.get_block_device_or_404(
301
301
system_id, device_id, request.user, NODE_PERMISSION.ADMIN)
302
302
node = device.get_node()
303
303
if node.status != NODE_STATUS.READY:
304
304
raise NodeStateViolation(
305
"Cannot update block device because the node is not Ready.")
305
"Cannot update block device because the machine is not Ready.")
306
306
device.remove_tag(get_mandatory_param(request.GET, 'tag'))
317
317
Returns 403 when the user doesn't have the ability to format the \
319
Returns 404 if the node or block device is not found.
320
Returns 409 if the node is not Ready or Allocated.
319
Returns 404 if the machine or block device is not found.
320
Returns 409 if the machine is not Ready or Allocated.
322
322
device = BlockDevice.objects.get_block_device_or_404(
323
323
system_id, device_id, request.user, NODE_PERMISSION.EDIT)
338
338
or part of a filesystem group.
339
339
Returns 403 when the user doesn't have the ability to unformat the \
341
Returns 404 if the node or block device is not found.
342
Returns 409 if the node is not Ready or Allocated.
341
Returns 404 if the machine or block device is not found.
342
Returns 409 if the machine is not Ready or Allocated.
344
344
device = BlockDevice.objects.get_block_device_or_404(
345
345
system_id, device_id, request.user, NODE_PERMISSION.EDIT)
349
349
filesystem = device.get_effective_filesystem()
350
350
if filesystem is None:
351
351
raise MAASAPIBadRequest("Block device is not formatted.")
352
if filesystem.mount_point:
352
if filesystem.is_mounted:
353
353
raise MAASAPIBadRequest(
354
354
"Filesystem is mounted and cannot be unformatted. Unmount the "
355
355
"filesystem before unformatting the block device.")
368
368
"""Mount the filesystem on block device.
370
370
:param mount_point: Path on the filesystem to mount.
371
:param mount_options: Options to pass to mount(8).
372
373
Returns 403 when the user doesn't have the ability to mount the \
374
Returns 404 if the node or block device is not found.
375
Returns 409 if the node is not Ready or Allocated.
375
Returns 404 if the machine or block device is not found.
376
Returns 409 if the machine is not Ready or Allocated.
377
378
device = BlockDevice.objects.get_block_device_or_404(
378
379
system_id, device_id, request.user, NODE_PERMISSION.EDIT)
395
396
Returns 403 when the user doesn't have the ability to unmount the \
397
Returns 404 if the node or block device is not found.
398
Returns 409 if the node is not Ready or Allocated.
398
Returns 404 if the machine or block device is not found.
399
Returns 409 if the machine is not Ready or Allocated.
400
401
device = BlockDevice.objects.get_block_device_or_404(
401
402
system_id, device_id, request.user, NODE_PERMISSION.EDIT)
405
406
filesystem = device.get_effective_filesystem()
406
407
if filesystem is None:
407
408
raise MAASAPIBadRequest("Block device is not formatted.")
408
if not filesystem.mount_point:
409
if not filesystem.is_mounted:
409
410
raise MAASAPIBadRequest("Filesystem is already unmounted.")
410
411
filesystem.mount_point = None
411
412
filesystem.mount_options = None
415
416
@operation(idempotent=False)
416
417
def set_boot_disk(self, request, system_id, device_id):
417
"""Set this block device as the boot disk for the node.
418
"""Set this block device as the boot disk for the machine.
419
420
Returns 400 if the block device is a virtual block device.
420
Returns 404 if the node or block device is not found.
421
Returns 404 if the machine or block device is not found.
421
422
Returns 403 if the user is not allowed to update the block device.
422
Returns 409 if the node is not Ready or Allocated.
423
Returns 409 if the machine is not Ready or Allocated.
424
425
device = BlockDevice.objects.get_block_device_or_404(
425
426
system_id, device_id, request.user, NODE_PERMISSION.ADMIN)
426
427
node = device.get_node()
427
428
if node.status != NODE_STATUS.READY:
428
429
raise NodeStateViolation(
429
"Cannot set as boot disk because the node is not Ready.")
430
"Cannot set as boot disk because the machine is not Ready.")
430
431
if not isinstance(device, PhysicalBlockDevice):
431
432
raise MAASAPIBadRequest(
432
433
"Cannot set a %s block device as the boot disk." % device.type)