Beispiel #1
0
    def put(self, vm, data, task_id=None, **kwargs):
        """Common code for updating VM properties used in vm_define and gui.vm.forms.ServerSettingsForm"""
        ser = VmDefineSerializer(self.request, vm, data=data, partial=True)

        if ser.is_valid():
            ser.object.save(
                sync_json=True,
                update_hostname=ser.hostname_changed,
                update_node_resources=ser.update_node_resources,
                update_storage_resources=ser.update_storage_resources)

            if ser.hostname_changed:
                # Task event for GUI
                VmDefineHostnameChanged(self.request, vm,
                                        ser.old_hostname).send()

            return SuccessTaskResponse(self.request,
                                       ser.data,
                                       vm=vm,
                                       task_id=task_id,
                                       msg=LOG_DEF_UPDATE,
                                       detail_dict=ser.detail_dict())

        return FailureTaskResponse(self.request,
                                   ser.errors,
                                   vm=vm,
                                   task_id=task_id)
Beispiel #2
0
    def post(self, vm, data, hostname_or_uuid=None):
        """
        Create VM definition
        In this case, hostname_or_uuid parameter has to be only hostname, never uuid
        """
        ser = VmDefineSerializer(self.request,
                                 data=data,
                                 hostname=hostname_or_uuid)

        if ser.is_valid():
            ser.object.save(sync_json=True,
                            update_node_resources=ser.update_node_resources)
            vm = ser.object

            try:
                res = SuccessTaskResponse(self.request,
                                          ser.data,
                                          status=scode.HTTP_201_CREATED,
                                          vm=vm,
                                          msg=LOG_DEF_CREATE,
                                          detail_dict=ser.detail_dict())
                vm_defined.send(TaskID(res.data.get('task_id'),
                                       request=self.request),
                                vm=vm)  # Signal!

                return res
            finally:
                # Create disk/nics if defined in template
                if vm.template:
                    self._create_disks_and_nics(vm)

        return FailureTaskResponse(self.request, ser.errors)
Beispiel #3
0
def get_vm_define(request, vm, many=False):
    """
    Like GET api.vm.define.vm_define.
    """
    request.method = 'GET'

    if many:
        return VmDefineSerializer(request, vm, many=True).data

    return VmDefineSerializer(request, vm).data
Beispiel #4
0
    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
Beispiel #5
0
    def _get_vm_define(self, vm):
        """Get one VM definition"""
        if self.active:
            vm.revert_active()

        res = VmDefineSerializer(self.request, vm).data

        if self.full:
            res['disks'] = VmDefineDiskSerializer(self.request, vm, vm.json_get_disks(), many=True).data
            res['nics'] = VmDefineNicSerializer(self.request, vm, vm.json_get_nics(), many=True).data

        return res
Beispiel #6
0
    def delete(self, vm, data, **kwargs):
        """Delete VM definition"""
        if vm.is_deployed():
            raise VmIsNotOperational(_('VM is not notcreated'))

        # noinspection PyUnusedLocal
        ser = VmDefineSerializer(self.request, vm)
        owner = vm.owner
        dead_vm = vm.log_list
        uuid = vm.uuid
        hostname = vm.hostname
        alias = vm.alias
        zabbix_sync = vm.is_zabbix_sync_active()
        external_zabbix_sync = vm.is_external_zabbix_sync_active()
        task_id = SuccessTaskResponse.gen_task_id(self.request, vm=dead_vm, owner=owner)

        # Every VM NIC could have an association to other tables. Cleanup first:
        for nic in vm.json_get_nics():
            # noinspection PyBroadException
            try:
                nic_ser = VmDefineNicSerializer(self.request, vm, nic)
                nic_ser.delete_ip(task_id)
            except Exception as ex:
                logger.exception(ex)
                continue

        # Finally delete VM
        logger.debug('Deleting VM %s from DB', vm)
        vm.delete()

        try:
            return SuccessTaskResponse(self.request, None, vm=dead_vm, owner=owner, task_id=task_id, msg=LOG_DEF_DELETE)
        finally:
            # Signal!
            vm_undefined.send(TaskID(task_id, request=self.request), vm_uuid=uuid, vm_hostname=hostname, vm_alias=alias,
                              dc=self.request.dc, zabbix_sync=zabbix_sync, external_zabbix_sync=external_zabbix_sync)