def _create_disks_and_nics(self, vm): """Try to create disks and nics defined by template""" # WARNING: This will temporary change the request.method to POST request = set_request_method(self.request, 'POST') if not vm.json_get_disks(): vm_define_disk = VmDefineDiskView(request) for i, data in enumerate(vm.template.vm_define_disk): if data: if i == 0 and not vm.is_kvm(): # Non-global zone's 1st disk can be only modified logger.info('Updating disk_id=%d for vm %s defined by template %s', i, vm, vm.template) res = vm_define_disk.put(vm, i, data) if res.status_code != scode.HTTP_200_OK: logger.warn('Failed (%s) to modify disk_id=%s in vm %s defined by template %s. ' 'Error: %s', res.status_code, i, vm, vm.template, res.data) else: logger.info('Creating disk_id=%d for vm %s defined by template %s', i, vm, vm.template) res = vm_define_disk.post(vm, i, data) if res.status_code != scode.HTTP_201_CREATED: logger.warn('Failed (%s) to add disk_id=%s into vm %s defined by template %s. ' 'Error: %s', res.status_code, i, vm, vm.template, res.data) break if not vm.json_get_nics(): vm_define_nic = VmDefineNicView(request) for i, data in enumerate(vm.template.vm_define_nic): if data: logger.info('Creating nic_id=%d for vm %s defined by template %s', i, vm, vm.template) res = vm_define_nic.post(vm, i, data) if res.status_code != scode.HTTP_201_CREATED: logger.warn('Failed (%s) to add nic_id=%s into vm %s defined by template %s. ' 'Error: %s', res.status_code, i, vm, vm.template, res.data) break
def vm_define_disk_list(request, hostname_or_uuid, data=None): """ List (:http:get:`GET </vm/(hostname_or_uuid)/define/disk>`) VM disk definitions. .. http:get:: /vm/(hostname_or_uuid)/define/disk :DC-bound?: * |dc-yes| :Permissions: * |VmOwner| :Asynchronous?: * |async-no| :arg hostname_or_uuid: **required** - Server hostname or uuid :type hostname_or_uuid: string :arg data.active: Display currently active VM disk definitions on compute node (default: false) :type data.active: boolean :status 201: SUCCESS :status 403: Forbidden :status 404: VM not found """ vm = get_vm(request, hostname_or_uuid, exists_ok=True, noexists_fail=True, check_node_status=None) return VmDefineDiskView(request).get(vm, None, None, many=True)
def _create_disks_and_nics(self, vm): """Try to create disks and nics defined by template""" for i, data in enumerate(vm.template.vm_define_disk): if data: if i == 0 and not vm.is_hvm(): # Disk representation within a zone is created together with VM request = set_request_method(self.request, 'PUT') vm_define_disk = VmDefineDiskView(request) logger.info('Updating disk_id=%d for vm %s defined by template %s', i, vm, vm.template) res = vm_define_disk.put(vm, i, data) if res.status_code != scode.HTTP_200_OK: logger.warn('Failed (%s) to modify disk_id=%s in vm %s defined by template %s. ' 'Error: %s', res.status_code, i, vm, vm.template, res.data) else: request = set_request_method(self.request, 'POST') vm_define_disk = VmDefineDiskView(request) logger.info('Creating disk_id=%d for vm %s defined by template %s', i, vm, vm.template) res = vm_define_disk.post(vm, i, data) if res.status_code != scode.HTTP_201_CREATED: logger.warn('Failed (%s) to add disk_id=%s into vm %s defined by template %s. ' 'Error: %s', res.status_code, i, vm, vm.template, res.data) break request = set_request_method(self.request, 'POST') vm_define_nic = VmDefineNicView(request) for i, data in enumerate(vm.template.vm_define_nic): if data: logger.info('Creating nic_id=%d for vm %s defined by template %s', i, vm, vm.template) res = vm_define_nic.post(vm, i, data) if res.status_code != scode.HTTP_201_CREATED: logger.warn('Failed (%s) to add nic_id=%s into vm %s defined by template %s. ' 'Error: %s', res.status_code, i, vm, vm.template, res.data) break
def get_diff(self, vm, full=False): """Show differences between active and in db json vm_define.""" def_current = VmDefineSerializer(self.request, vm).data if full: res = { 'disks': VmDefineDiskView(self.request).get_diff(vm), 'nics': VmDefineNicView(self.request).get_diff(vm), } else: res = {} vm.revert_active() def_active = VmDefineSerializer(self.request, vm).data vm_diff = self._diff_dicts(def_active, def_current) if vm_diff.get('change', False): res.update(vm_diff) return res
def vm_define_disk(request, hostname_or_uuid, disk_id=None, data=None): """ Show (:http:get:`GET </vm/(hostname_or_uuid)/define/disk/(disk_id)>`), create (:http:post:`POST </vm/(hostname_or_uuid)/define/disk/(disk_id)>`), change (:http:put:`PUT </vm/(hostname_or_uuid)/define/disk/(disk_id)>`) or delete (:http:delete:`DELETE </vm/(hostname_or_uuid)/define/disk/(disk_id)>`) a VM disk definition. .. http:get:: /vm/(hostname_or_uuid)/define/disk/(disk_id) :DC-bound?: * |dc-yes| :Permissions: * |VmOwner| :Asynchronous?: * |async-no| :arg hostname_or_uuid: **required** - Server hostname or uuid :type hostname_or_uuid: string :arg disk_id: **required** - Disk number/ID (1 - 2) :type disk_id: integer :arg data.active: Display currently active VM disk definition on compute node (default: false) :type data.active: boolean :arg data.diff: Display differences between active VM definition on compute node and current configuration \ (default: false) :type data.diff: boolean :status 200: SUCCESS :status 403: Forbidden :status 404: VM not found :status 406: VM disk out of range .. http:post:: /vm/(hostname_or_uuid)/define/disk/(disk_id) :DC-bound?: * |dc-yes| :Permissions: * |Admin| :Asynchronous?: * |async-no| :arg hostname_or_uuid: **required** - Server hostname or uuid :type hostname_or_uuid: string :arg disk_id: **required** - Disk number/ID (1 - 2) :type disk_id: integer :arg data.size: **required** (if not specified in image) - Disk size (1 - 268435456 MB) :type data.size: integer :arg data.image: **required** (if size is not specified) - Disk image name :type data.image: string :arg data.model: Disk driver. One of virtio, ide, scsi (default: virtio) :type data.model: string :arg data.block_size: Block size for this disk (default: depends on OS Type) :type data.block_size: integer :arg data.compression: Disk compression algorithm. One of off, lzjb, gzip, gzip-N, zle, lz4 (default: off) :type data.compression: string :arg data.zpool: The zpool in which to create the disk (default: ``vm.zpool`` [zones]) :type data.zpool: string :arg data.boot: Whether this disk should be bootable (default: true for first disk, otherwise false) :type data.boot: boolean :arg data.refreservation: Minimum amount of space in MB reserved for this disk (KVM only, default: ``size``) :type data.refreservation: integer :arg data.image_tags_inherit: Whether to update VM tags from image tags (default: true) :type data.image_tags_inherit: boolean :status 201: SUCCESS :status 400: FAILURE :status 403: Forbidden :status 404: VM not found :status 406: VM disk out of range / VM disk already exists :status 423: VM is not operational / VM is locked or has slave VMs .. http:put:: /vm/(hostname_or_uuid)/define/disk/(disk_id) :DC-bound?: * |dc-yes| :Permissions: * |Admin| :Asynchronous?: * |async-no| :arg hostname_or_uuid: **required** - Server hostname or uuid :type hostname_or_uuid: string :arg disk_id: **required** - Disk number/ID (1 - 2) :type disk_id: integer :arg data.size: Disk size (1 - 268435456 MB) :type data.size: integer :arg data.model: Disk driver. One of virtio, ide, scsi :type data.model: string :arg data.block_size: Block size for this disk :type data.block_size: integer :arg data.compression: Disk compression algorithm. One of off, lzjb, gzip, gzip-N, zle, lz4 :type data.compression: string :arg data.zpool: The zpool in which to create the disk :type data.zpool: string :arg data.boot: Whether this disk should be bootable :type data.boot: boolean :arg data.refreservation: Minimum amount of space in MB reserved for this disk (KVM only) :type data.refreservation: integer :arg data.image_tags_inherit: Whether to update VM tags from image tags (default: true) :type data.image_tags_inherit: boolean :status 200: SUCCESS :status 400: FAILURE :status 403: Forbidden :status 404: VM not found :status 406: VM disk out of range :status 423: VM is not operational / VM is locked or has slave VMs .. http:delete:: /vm/(hostname_or_uuid)/define/disk/(disk_id) :DC-bound?: * |dc-yes| :Permissions: * |Admin| :Asynchronous?: * |async-no| :arg hostname_or_uuid: **required** - Server hostname or uuid :type hostname_or_uuid: string :arg disk_id: **required** - Disk number/ID (1 - 2) :type disk_id: integer :arg data.image_tags_inherit: Whether to update VM tags from image tags (default: true) :type data.image_tags_inherit: boolean :status 200: SUCCESS :status 400: FAILURE :status 403: Forbidden :status 404: VM not found :status 406: VM disk out of range :status 423: VM is not operational / VM is locked or has slave VMs """ vm = get_vm(request, hostname_or_uuid, exists_ok=True, noexists_fail=True, sr=('node', 'owner', 'template', 'slavevm'), check_node_status=None) try: disk_id = int(disk_id) - 1 except ValueError: raise BadRequest return VmDefineDiskView(request).response(vm, disk_id, data)