Beispiel #1
0
    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
Beispiel #2
0
    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
Beispiel #3
0
def vm_define_nic_list(request, hostname_or_uuid, data=None):
    """
    List (:http:get:`GET </vm/(hostname_or_uuid)/define/nic>`) VM NIC definitions.

    .. http:get:: /vm/(hostname_or_uuid)/define/nic

        :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 NIC definitions on compute node (default: false)
        :type data.active: boolean
        :status 200: 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 VmDefineNicView(request).get(vm, None, None, many=True)
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 vm_define_nic(request, hostname_or_uuid, nic_id=None, data=None):
    """
    Show (:http:get:`GET </vm/(hostname_or_uuid)/define/nic/(nic_id)>`),
    create (:http:post:`POST </vm/(hostname_or_uuid)/define/nic/(nic_id)>`),
    change (:http:put:`PUT </vm/(hostname_or_uuid)/define/nic/(nic_id)>`) or
    delete (:http:delete:`DELETE </vm/(hostname_or_uuid)/define/nic/(nic_id)>`)
    a VM NIC definition.

    .. http:get:: /vm/(hostname_or_uuid)/define/nic/(nic_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 nic_id: **required** - NIC number/ID (1 - 6)
        :type nic_id: integer
        :arg data.active: Display currently active VM NIC 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 NIC out of range

    .. http:post:: /vm/(hostname_or_uuid)/define/nic/(nic_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 nic_id: **required** - NIC number/ID (1 - 6)
        :type nic_id: integer
        :arg data.net: **required** - Name of a virtual network
        :type data.net: string
        :arg data.ip: Virtual NIC IPv4 address. Must be part of net (default: auto select)
        :type data.ip: string
        :arg data.model: Virtual NIC Model. One of virtio, e1000, rtl8139 (default: virtio)
        :type data.model: string
        :arg data.dns: Create a DNS A record for VM's FQDN? (default: true for first NIC, otherwise false)
        :type data.dns: boolean
        :arg data.use_net_dns: Inherit DNS resolvers from network's resolvers setting (default: false)
        :type data.use_net_dns: boolean
        :arg data.mac: Virtual NIC MAC address (default: auto-generated)
        :type data.mac: string
        :arg data.primary: Use this NICs gateway as VM default gateway (default: true for first NIC, otherwise false)
        :type data.primary: boolean
        :arg data.allow_dhcp_spoofing: Allow packets required for DHCP server (requires |SuperAdmin| permission) \
(default: false)
        :type data.allow_dhcp_spoofing: boolean
        :arg data.allow_ip_spoofing: Allow sending and receiving packets for IP addresses other \
than specified in ``ip`` (requires |SuperAdmin| permission) (default: false)
        :type data.allow_ip_spoofing: boolean
        :arg data.allow_mac_spoofing: Allow sending packets with MAC addresses other than specified in ``mac`` \
(requires |SuperAdmin| permission) (default: false)
        :type data.allow_mac_spoofing: boolean
        :arg data.allow_restricted_traffic: Allow sending packets that are not IPv4, IPv6, or ARP \
(requires |SuperAdmin| permission) (default: false)
        :type data.allow_restricted_traffic: boolean
        :arg data.allow_unfiltered_promisc: Allow VM to have multiple MAC addresses. Use with caution! \
(requires |SuperAdmin| permission) (default: false)
        :type data.allow_unfiltered_promisc: boolean
        :arg data.allowed_ips: List of additional IP addresses that can be used by this VM's NIC and also by \
other VMs. Useful for floating/shared IPs (default: [])
        :type data.allowed_ips: array
        :arg data.monitoring: Use this NIC's IP address for external monitoring \
(default: true for first NIC, otherwise false)
        :type data.monitoring: boolean
        :arg data.set_gateway: Whether to set gateway from network (``data.net``) settings (default: true)
        :type data.set_gateway: boolean
        :status 201: SUCCESS
        :status 400: FAILURE
        :status 403: Forbidden
        :status 404: VM not found
        :status 406: VM NIC out of range / VM NIC already exists
        :status 423: VM is not operational / VM is locked or has slave VMs

    .. http:put:: /vm/(hostname_or_uuid)/define/nic/(nic_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 nic_id: **required** - NIC number/ID (1 - 6)
        :type nic_id: integer
        :arg data.net: Name of a virtual network
        :type data.net: string
        :arg data.ip: Virtual NIC IPv4 address
        :type data.ip: string
        :arg data.model: Virtual NIC Model. One of virtio, e1000, rtl8139
        :type data.model: string
        :arg data.dns: Create a DNS A record for VM's FQDN?
        :type data.dns: boolean
        :arg data.use_net_dns: Inherit DNS resolvers from network's resolvers setting
        :type data.use_net_dns: boolean
        :arg data.mac: Virtual NIC MAC address
        :type data.mac: string
        :arg data.primary: Use this NICs gateway as VM default gateway
        :type data.primary: boolean
        :arg data.allow_dhcp_spoofing: Allow packets required for DHCP server \
(requires |SuperAdmin| permission)
        :type data.allow_dhcp_spoofing: boolean
        :arg data.allow_ip_spoofing: Allow sending and receiving packets for IP addresses other \
than specified in ``ip`` (requires |SuperAdmin| permission)
        :type data.allow_ip_spoofing: boolean
        :arg data.allow_mac_spoofing: Allow sending packets with MAC addresses other than specified in ``mac`` \
(requires |SuperAdmin| permission)
        :type data.allow_mac_spoofing: boolean
        :arg data.allow_restricted_traffic: Allow sending packets that are not IPv4, IPv6, or ARP \
(requires |SuperAdmin| permission)
        :type data.allow_restricted_traffic: boolean
        :arg data.allow_unfiltered_promisc: Allow VM to have multiple MAC addresses. Use with caution! \
(requires |SuperAdmin| permission)
        :type data.allow_unfiltered_promisc: boolean
        :arg data.allowed_ips: List of additional IP addresses that can be used by this VM's NIC and also by \
other VMs. Useful for floating/shared IPs
        :type data.allowed_ips: array
        :arg data.monitoring: Use this NIC's IP address for external monitoring
        :type data.monitoring: boolean
        :arg data.set_gateway: Whether to set gateway from network (``data.net``) settings
        :type data.set_gateway: boolean
        :status 200: SUCCESS
        :status 400: FAILURE
        :status 403: Forbidden
        :status 404: VM not found
        :status 406: VM NIC out of range
        :status 423: VM is not operational / VM is locked or has slave VMs

    .. http:delete:: /vm/(hostname_or_uuid)/define/nic/(nic_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 nic_id: **required** - NIC number/ID (1 - 6)
        :type nic_id: integer
        :status 200: SUCCESS
        :status 400: FAILURE
        :status 403: Forbidden
        :status 404: VM not found
        :status 406: VM NIC 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:
        nic_id = int(nic_id) - 1
    except ValueError:
        raise BadRequest

    return VmDefineNicView(request).response(vm, nic_id, data)