Beispiel #1
0
def create_vm_details(vm_name, mgmt_network, node_type, node_ifaces):
    """ Create the VM Details results used for both Deployment and Refresh VM Details """

    vm_instance_data = [
        VmDetailsProperty("Instance Type", node_type)  # IOSv, NX-OSv etc
    ]

    vm_network_data = []
    for iface in node_ifaces:
        network = iface.get("network")
        exclusion_network_name = f"{vm_name}-unconnected"
        if not network \
                or network == mgmt_network\
                or network.lower().startswith(exclusion_network_name.lower()):
            continue
        vm_nic = VmDetailsNetworkInterface()
        vm_nic.interfaceId = iface.get("port_id")
        vm_nic.networkId = network
        vm_nic.networkData.append(
            VmDetailsProperty("IP", iface.get("ipv4", "")))
        vm_nic.networkData.append(
            VmDetailsProperty("IPv6", iface.get("ipv6", "")))
        vm_nic.networkData.append(
            VmDetailsProperty("MAC Address", iface.get("mac", "")))
        vm_nic.isPrimary = iface.get("mgmt", False)
        vm_nic.isPredefined = iface.get("mgmt", False)

        vm_network_data.append(vm_nic)
    return VmDetailsData(vm_instance_data, vm_network_data, vm_name)
Beispiel #2
0
    def _get_vm_network_data(self, vm, reserved_networks, ip_regex,
                             wait_for_ip, logger):
        network_interfaces = []

        if wait_for_ip == 'True':
            primary_ip = self._get_primary_ip(vm, ip_regex, logger)
        else:
            primary_ip = None

        net_devices = [
            d for d in vm.config.hardware.device
            if isinstance(d, vim.vm.device.VirtualEthernetCard)
        ]

        for device in net_devices:
            network = VNicService.get_network_by_device(
                vm, device, self.pyvmomi_service, logger)
            vlan_id = self._convert_vlan_id_to_str(
                VNicService.get_network_vlan_id(network))
            private_ip = self._get_ip_by_device(vm, device)

            if vlan_id and (network.name.startswith('QS_')
                            or network.name in reserved_networks):
                is_primary = private_ip and primary_ip == private_ip
                is_predefined = network.name in reserved_networks

                network_data = [
                    VmDetailsProperty(key='IP', value=private_ip),
                    VmDetailsProperty(key='MAC Address',
                                      value=device.macAddress),
                    VmDetailsProperty(key='Network Adapter',
                                      value=device.deviceInfo.label),
                    VmDetailsProperty(key='Port Group Name',
                                      value=network.name)
                ]

                current_interface = VmDetailsNetworkInterface(
                    interfaceId=device.macAddress,
                    networkId=vlan_id,
                    isPrimary=is_primary,
                    isPredefined=is_predefined,
                    networkData=network_data,
                    privateIpAddress=private_ip)
                network_interfaces.append(current_interface)

        return network_interfaces
    def extract_vm_details(self, vm_uuid):
        '''vm_detail_url = self.nutanix_base_url + '/vms/' + vm_uuid + '?include_vm_nic_config=true'
        in_progress = True

        while in_progress:
            response = self.session.get(vm_detail_url)
            if response.status_code != 200:
                raise StandardError("Unable to extract VM details. uid: {}".format(vm_uuid))
            json_response = response.json()
            if 'vm_nics' in json_response and json_response['vm_nics'] != [] and 'ip_address' in json_response['vm_nics']:
                in_progress = False
            else:
                time.sleep(5)'''

        vm_detail_url = self.nutanix_base_url + '/vms/' + vm_uuid + '?include_vm_nic_config=true'
        response = self.session.get(vm_detail_url)

        if response.status_code != 200 and response.status_code != 201:
            raise StandardError(
                "Unable to extract VM details. uid: {}".format(vm_uuid))

        json_response = response.json()

        vm_name = json_response['name']

        vm_instance_data = [
            VmDetailsProperty(key='Instance Name', value=json_response['name'])
        ]
        vm_network_data = []

        i = 0
        for nic in json_response['vm_nics']:
            network_data = [
                VmDetailsProperty(key='MAC Address', value=nic['mac_address']),
            ]

            current_interface = VmDetailsNetworkInterface(
                interfaceId=i,
                networkId=nic['network_uuid'],
                isPredefined=True,
                networkData=network_data)
            i += 1
            vm_network_data.append(current_interface)

        return VmDetailsData(vmInstanceData=vm_instance_data,
                             vmNetworkData=vm_network_data)
    def _get_vm_network_data(self, instance):
        network_interfaces_results = []

        instance.reload()

        if not instance.network_interfaces:
            return network_interfaces_results

        network_interfaces = sorted(
            instance.network_interfaces,
            key=lambda x: x.attachment.get("DeviceIndex"))

        for network_interface in network_interfaces:
            is_attached_to_elastic_ip = self._has_elastic_ip(network_interface)
            is_primary = self._is_primary_interface(network_interface)
            public_ip = self._calculate_public_ip(network_interface)

            network_data = [
                VmDetailsProperty(key="IP",
                                  value=network_interface.private_ip_address),
                VmDetailsProperty(key="Public IP", value=public_ip),
                VmDetailsProperty(key="Elastic IP",
                                  value=is_attached_to_elastic_ip),
                VmDetailsProperty(key="MAC Address",
                                  value=network_interface.mac_address),
                VmDetailsProperty(
                    key="NIC", value=network_interface.network_interface_id),
                VmDetailsProperty(
                    key="Device Index",
                    value=network_interface.attachment.get("DeviceIndex"),
                ),
            ]

            current_interface = VmDetailsNetworkInterface(
                interfaceId=network_interface.network_interface_id,
                networkId=network_interface.subnet_id,
                isPrimary=is_primary,
                networkData=network_data,
                privateIpAddress=network_interface.private_ip_address,
                publicIpAddress=public_ip,
            )

            network_interfaces_results.append(current_interface)

        return network_interfaces_results
    def _get_vm_network_data(self, instance, network_client, group_name,
                             logger):

        nic = network_client.network_interfaces.get(group_name, instance.name)

        ip_configuration = nic.ip_configurations[0]
        private_ip = ip_configuration.private_ip_address
        public_ip = ''
        network_data = [
            VmDetailsProperty(key="IP",
                              value=ip_configuration.private_ip_address)
        ]

        if ip_configuration.public_ip_address:
            public_ip_object = self.network_service.get_public_ip(
                network_client=network_client,
                group_name=group_name,
                ip_name=instance.name)
            public_ip = public_ip_object.ip_address

            network_data.append(
                VmDetailsProperty(key="Public IP", value=public_ip))
            network_data.append(
                VmDetailsProperty(
                    key="Public IP Type",
                    value=public_ip_object.public_ip_allocation_method))
            # logger.info("VM {} was created with public IP '{}'.".format(instance.name,
            #                                                             ip_configuration.public_ip_address.ip_address))
            logger.info("VM {} was created with public IP '{}'.".format(
                instance.name, public_ip))

        network_data.append(
            VmDetailsProperty(key="MAC Address", value=nic.mac_address))

        current_interface = VmDetailsNetworkInterface(
            interfaceId=nic.resource_guid,
            networkId=nic.name,
            isPrimary=nic.primary,
            networkData=network_data,
            privateIpAddress=private_ip,
            publicIpAddress=public_ip)

        return [current_interface]
Beispiel #6
0
    def _get_vm_data(self, vm_uid, vm_name):
        """

        :param str vm_uid:
        :param str vm_name:
        :return:
        """
        machine = self._maas_client.machines.get(vm_uid)
        vm_network_data = []

        data = [
            VmDetailsProperty(key="Architecture", value=machine.architecture),
            VmDetailsProperty(key="HWE Kernel", value=machine.hwe_kernel),
            VmDetailsProperty(key="CPU Cores", value=machine.cpus),
            VmDetailsProperty(key="RAM GiB", value=machine.memory // 1024),
            VmDetailsProperty(key="Disks", value=len(machine.block_devices)),
            VmDetailsProperty(key="Distro Series",
                              value=machine.distro_series),
            VmDetailsProperty(key="Operation System", value=machine.osystem),
        ]

        for iface in machine.interfaces:
            network_data = [
                VmDetailsProperty(key="MAC Address", value=iface.mac_address),
            ]

            for link in iface.links:
                iface_id = hash(f"{iface.id }_{link.id}")
                interface = VmDetailsNetworkInterface(
                    interfaceId=iface_id,
                    networkId=iface_id,
                    isPredefined=True,
                    networkData=network_data,
                    privateIpAddress=link.ip_address,
                )
                vm_network_data.append(interface)

        vm_details_data = VmDetailsData(vmInstanceData=data,
                                        vmNetworkData=vm_network_data,
                                        appName=vm_name)

        return vm_details_data
    def extract_vm_instance_network_data(instance):
        network_interfaces = []

        # for each network interface
        for i in range(2):
            network_data = [
                VmDetailsProperty(key='MaxSpeed', value='1KB'),
                VmDetailsProperty(key='Network Type', value='Ethernet'),
            ]

            current_interface = VmDetailsNetworkInterface(
                interfaceId=i,
                networkId=i,
                isPrimary=i == 0,  # if nic is the primary interface
                isPredefined=False,
                # if the network existed before reservation
                networkData=network_data,
                privateIpAddress='10.0.0.' + str(i),
                publicIpAddress='8.8.8.' + str(i))
            network_interfaces.append(current_interface)

        return network_interfaces
    def get_vm_details(self, vm_name, vm_uuid):

        vm_detail_url = self.nutanix_base_url + '/vms/' + vm_uuid + '?include_vm_nic_config=true'
        response = self.session.get(vm_detail_url)

        if response.status_code != 200 and response.status_code != 201:
            raise StandardError(
                "Unable to get VM details. uid: {}".format(vm_uuid))

        json_response = response.json()

        vm_name = json_response['name']

        vm_instance_data = [
            VmDetailsProperty(key='Instance Name', value=json_response['name'])
        ]
        vm_network_data = []
        i = 0

        for nic in json_response['vm_nics']:
            network_data = [
                VmDetailsProperty(key='MAC Address', value=nic['mac_address']),
            ]

            current_interface = VmDetailsNetworkInterface(
                interfaceId=i,
                networkId=nic['network_uuid'],
                isPrimary=i == 0,
                isPredefined=True,
                networkData=network_data,
                privateIpAddress=nic['ip_address'])
            vm_network_data.append(current_interface)
            i += 1

        return VmDetailsData(vmInstanceData=vm_instance_data,
                             vmNetworkData=vm_network_data,
                             appName=vm_name)