def get_vm_networks(self, vm_id):
     network_info = []
     for net in self._client.GetVmNetworks(self._session, vm_id):
         info = VmNetworkInfo(network=net.name, mac_address=net.macAddress, is_connected=net.connected)
         if net.ipAddress:
             netmask = self._prefix_len_to_mask(net.prefixLength)
             info.ip_address = Ipv4Address(ip_address=net.ipAddress, netmask=netmask)
         network_info.append(info)
     return network_info
 def get_vm_networks(self, vm_id):
     network_info = []
     for net in self._client.GetVmNetworks(self._session, vm_id):
         info = VmNetworkInfo(mac_address=net.macAddress,
                              is_connected=net.connected)
         if net.name:
             # only set network if it is neither None nor empty string
             info.network = net.name
         if net.ipAddress:
             netmask = self._prefix_len_to_mask(net.prefixLength)
             info.ip_address = Ipv4Address(ip_address=net.ipAddress,
                                           netmask=netmask)
         network_info.append(info)
     return network_info
    def get_vm_networks(self, vm_id):
        """ Get the vm's network information
        We only report ip info if vmware tools is running within the guest.
        If tools are not running we can only report back the mac address
        assigned by the vmx, the connected status of the device and the network
        attached to the device.
        The information for mac, networkname and connected status is available
        through two places, the ethernetCards backing info and through the
        guestInfo. Both of these codepaths are not using VimVigor and seem to
        be implemented in a similar manner in hostd, so they should agree with
        each other. Just read this from the guestInfo as well.
        """
        network_info = []

        vm = self.get_vm(vm_id)

        if vm.guest and vm.guest.net:
            for guest_nic_info in vm.guest.net:
                if guest_nic_info.macAddress is None:
                    # No mac address no real guest info. Not possible to have mac
                    # address not reported but ip stack info available.
                    continue

                info = VmNetworkInfo(mac_address=guest_nic_info.macAddress)
                # Fill in the connected status.
                if guest_nic_info.connected:
                    info.is_connected = ConnectedStatus.CONNECTED
                else:
                    info.is_connected = ConnectedStatus.DISCONNECTED

                # Fill in the network binding info
                if guest_nic_info.network is not None:
                    info.network = guest_nic_info.network

                # See if the ip information is available.
                if guest_nic_info.ipConfig is not None:
                    ip_addresses = guest_nic_info.ipConfig.ipAddress
                    for ip_address in ip_addresses:
                        if self._is_ipv4_address(ip_address.ipAddress):
                            netmask = self._prefix_len_to_mask(
                                ip_address.prefixLength)
                            info.ip_address = Ipv4Address(
                                ip_address=ip_address.ipAddress,
                                netmask=netmask)
                            break
                network_info.append(info)

        elif vm.config and vm.config.hardware.device:
            for device in vm.config.hardware.device:
                if (isinstance(device, vim.vm.device.VirtualEthernetCard)
                        and isinstance(
                            device.backing, vim.vm.device.VirtualEthernetCard.
                            NetworkBackingInfo)):
                    info = VmNetworkInfo(mac_address=device.macAddress,
                                         network=device.backing.deviceName)
                    network_info.append(info)
        return network_info
    def get_vm_networks(self, vm_id):
        network_info = []
        for net in self._client.GetVmNetworks(self._session, vm_id):
            info = VmNetworkInfo(mac_address=net.macAddress)

            # Fill in the connected status.
            if net.connected:
                info.is_connected = ConnectedStatus.CONNECTED
            else:
                info.is_connected = ConnectedStatus.DISCONNECTED

            if net.name:
                # only set network if it is neither None nor empty string
                info.network = net.name
            if net.ipAddress:
                netmask = self._prefix_len_to_mask(net.prefixLength)
                info.ip_address = Ipv4Address(ip_address=net.ipAddress, netmask=netmask)
            network_info.append(info)
        return network_info
Beispiel #5
0
 def get_vm_network(self, vm_id):
     vm = self._get_vm(vm_id)
     network_info = []
     for nic in vm.nics:
         ip = Ipv4Address(ip_address=nic.ip_address, netmask=nic.netmask)
         info = VmNetworkInfo(mac_address=nic.mac_address,
                              is_connected=nic.connected,
                              network=nic.network_name,
                              ip_address=ip)
         network_info.append(info)
     return network_info
Beispiel #6
0
    def get_vm_network(self, vm_id):
        """ Get the vm's network information
        We only report ip info if vmware tools is running within the guest.
        If tools are not running we can only report back the mac address
        assigned by the vmx, the connected status of the device and the network
        attached to the device.
        The information for mac, networkname and connected status is available
        through two places, the ethernetCards backing info and through the
        guestInfo. Both of these codepaths are not using VimVigor and seem to
        be implemented in a similar manner in hostd, so they should agree with
        each other. Just read this from the guestInfo as well.

        :param vm_id: Name of the VM
        :rtype: VmNetworkInfo
        """
        network_info = []

        # Throws when VM is not found.
        vm = self.vim_client.get_vm(vm_id)

        if (vm.guest is None or not vm.guest.net):
            # No guest info so return the info from the config file
            return self.get_network_config(vm_id)

        guest_nic_info_list = vm.guest.net

        # vmomi list attrs are never None could be an empty list
        for guest_nic_info in guest_nic_info_list:
            if (guest_nic_info.macAddress is None):
                # No mac address no real guest info. Not possible to have mac
                # address not reporte but ip stack info available.
                continue
            info = VmNetworkInfo(mac_address=guest_nic_info.macAddress)

            # Fill in the connected status.
            if guest_nic_info.connected:
                info.is_connected = ConnectedStatus.CONNECTED
            else:
                info.is_connected = ConnectedStatus.DISCONNECTED

            # Fill in the network binding info
            if guest_nic_info.network is not None:
                info.network = guest_nic_info.network

            # See if the ip information is available.
            if guest_nic_info.ipConfig is not None:
                ip_addresses = guest_nic_info.ipConfig.ipAddress
                # This is an array due to ipv6 support
                for ip_address in ip_addresses:
                    if (NetUtil.is_ipv4_address(ip_address.ipAddress)):
                        ip = Ipv4Address(
                            ip_address=ip_address.ipAddress,
                            netmask=NetUtil.prefix_len_to_mask(
                                ip_address.prefixLength))
                        info.ip_address = ip
                        break
            network_info.append(info)

        return network_info
    def get_vm_networks(self, vm_id):
        """ Get the vm's network information
        We only report ip info if vmware tools is running within the guest.
        If tools are not running we can only report back the mac address
        assigned by the vmx, the connected status of the device and the network
        attached to the device.
        The information for mac, networkname and connected status is available
        through two places, the ethernetCards backing info and through the
        guestInfo. Both of these codepaths are not using VimVigor and seem to
        be implemented in a similar manner in hostd, so they should agree with
        each other. Just read this from the guestInfo as well.
        """
        network_info = []

        vm = self.get_vm(vm_id)

        if vm.guest and vm.guest.net:
            for guest_nic_info in vm.guest.net:
                if guest_nic_info.macAddress is None:
                    # No mac address no real guest info. Not possible to have mac
                    # address not reported but ip stack info available.
                    continue

                info = VmNetworkInfo(mac_address=guest_nic_info.macAddress)
                # Fill in the connected status.
                if guest_nic_info.connected:
                    info.is_connected = ConnectedStatus.CONNECTED
                else:
                    info.is_connected = ConnectedStatus.DISCONNECTED

                # Fill in the network binding info
                if guest_nic_info.network is not None:
                    info.network = guest_nic_info.network

                # See if the ip information is available.
                if guest_nic_info.ipConfig is not None:
                    ip_addresses = guest_nic_info.ipConfig.ipAddress
                    for ip_address in ip_addresses:
                        if self._is_ipv4_address(ip_address.ipAddress):
                            netmask = self._prefix_len_to_mask(ip_address.prefixLength)
                            info.ip_address = Ipv4Address(ip_address=ip_address.ipAddress, netmask=netmask)
                            break
                network_info.append(info)

        elif vm.config and vm.config.hardware.device:
            for device in vm.config.hardware.device:
                if (isinstance(device, vim.vm.device.VirtualEthernetCard) and
                        isinstance(device.backing, vim.vm.device.VirtualEthernetCard.NetworkBackingInfo)):
                    info = VmNetworkInfo(mac_address=device.macAddress, network=device.backing.deviceName)
                    network_info.append(info)
        return network_info
Beispiel #8
0
    def get_network_config(self, vm_id):
        """ Get the network backing of a VM by reading its configuration.

        This is different from the get_vm_network above which gets the network
        information from tools.
        Only the mac address and the corresponding network name is going to be
        populated in this model.
        :type vm_id: VM str
        :rtype VMNetworkInfo list.
        """

        network_info = []
        vm = self.vim_client.get_vm(vm_id)
        networks = self._get_network_config_int(vm.config)

        for idx, mac, network, _ in networks:
            # We don't set MAC address when VM gets created, so MAC address
            # won't be set until the VM gets powered on.
            info = VmNetworkInfo(mac_address=mac, network=network)
            network_info.append(info)
        return network_info
Beispiel #9
0
    def test_get_vm_network_guest_info(self):
        """
        Tests the guest vm network info, without the vmx returned info.
        Test 1: Only mac address info available.
        Test 2: Only mac + ipv4 address available.
        Test 3: Only mac + ipv6 address available.
        Test 4: Only mac + ipv6, ipv4 address available.
        Test 5: No mac or ipv4 address available
        """

        sample_mac_address = "00:0c:29:00:00:01"
        sample_ip_address = "127.0.0.2"
        sample_prefix_length = 24
        sample_netmask = "255.255.255.0"
        sample_ipv6_address = "FE80:0000:0000:0000:0202:B3FF:FE1E:8329"
        sample_network = "VM Network"

        def _get_v4_address():
            ip_address = MagicMock(name="ipv4address")
            ip_address.ipAddress = sample_ip_address
            ip_address.prefixLength = sample_prefix_length
            return ip_address

        def _get_v6_address():
            ip_address = MagicMock(name="ipv6address")
            ip_address.ipAddress = sample_ipv6_address
            ip_address.prefixLength = sample_prefix_length
            return ip_address

        def _guest_info_1():
            """
            Only have the mac address.
            """
            net = MagicMock(name="guest_info_1")
            net.macAddress = sample_mac_address
            net.connected = True
            net.network = None
            return net

        def _guest_info_2():
            """
            Have mac and ipv4 address
            """
            net = MagicMock(name="guest_info_2")
            net.macAddress = sample_mac_address
            net.ipConfig.ipAddress = [_get_v4_address()]
            net.network = sample_network
            net.connected = False
            return net

        def _guest_info_3():
            """
            Have mac and ipv6 address
            """
            net = MagicMock(name="guest_info_3")
            net.macAddress = sample_mac_address
            net.ipConfig.ipAddress = [_get_v6_address()]
            net.connected = False
            net.network = sample_network
            return net

        def _guest_info_4():
            """
            Have a mac and an ipv4 and an ipv6 address
            """
            net = MagicMock(name="guest_info_4")
            net.macAddress = sample_mac_address
            net.network = None
            net.ipConfig.ipAddress = [_get_v6_address(), _get_v4_address()]
            net.connected = True

            return net

        def _get_vm_no_net_info(vm_id):
            """
            Return empty guest_info
            """
            f = MagicMock(name="get_vm")
            f.config.uuid = str(uuid.uuid4())
            g = MagicMock(name="guest_info")
            f.guest = g
            g.net = []
            return f

        def _get_vm(vm_id):
            """
            Return a mocked up guest info object
            """
            f = MagicMock(name="get_vm")
            g = MagicMock(name="guest_info")
            f.guest = g
            net = _guest_info()
            g.net = [net]
            return f

        def _get_vm_vim_guest_info(vm_id):
            """
            Return a real Vim object with reasonable values to validate
            python typing
            """
            f = MagicMock(name="get_vm")
            f.config.uuid = str(uuid.uuid4())
            g = MagicMock(name="guest_info")
            f.guest = g
            net = vim.vm.GuestInfo.NicInfo()
            ip_config_info = vim.net.IpConfigInfo()
            net.ipConfig = ip_config_info
            net.macAddress = sample_mac_address
            net.network = sample_network
            net.connected = True
            ipAddress = vim.net.IpConfigInfo.IpAddress()
            ipAddress.ipAddress = sample_ip_address
            ipAddress.prefixLength = sample_prefix_length
            ip_config_info.ipAddress.append(ipAddress)
            g.net = [net]
            return f

        # Test 1
        _guest_info = _guest_info_1
        self.vm_manager.vim_client.get_vm = _get_vm
        self.vm_manager._get_mac_network_mapping = MagicMock(return_value={})
        network_info = self.vm_manager.get_vm_network("vm_foo1")
        expected_1 = VmNetworkInfo(mac_address=sample_mac_address,
                                   is_connected=ConnectedStatus.CONNECTED)
        self.assertEqual(network_info, [expected_1])

        # Test 2
        _guest_info = _guest_info_2
        network_info = self.vm_manager.get_vm_network("vm_foo2")
        ip_address = Ipv4Address(ip_address=sample_ip_address,
                                 netmask=sample_netmask)
        expected_2 = VmNetworkInfo(mac_address=sample_mac_address,
                                   ip_address=ip_address,
                                   network=sample_network,
                                   is_connected=ConnectedStatus.DISCONNECTED)
        self.assertEqual(network_info, [expected_2])

        # Test 3
        _guest_info = _guest_info_3
        network_info = self.vm_manager.get_vm_network("vm_foo3")
        expected_3 = VmNetworkInfo(mac_address=sample_mac_address,
                                   network=sample_network,
                                   is_connected=ConnectedStatus.DISCONNECTED)
        self.assertEqual(network_info, [expected_3])

        # Test 4
        _guest_info = _guest_info_4
        network_info = self.vm_manager.get_vm_network("vm_foo4")
        expected_4 = VmNetworkInfo(mac_address=sample_mac_address,
                                   ip_address=ip_address,
                                   is_connected=ConnectedStatus.CONNECTED)
        self.assertEqual(network_info, [expected_4])

        # Test 5
        self.vm_manager.vim_client.get_vm = _get_vm_no_net_info
        network_info = self.vm_manager.get_vm_network("vm_foo5")
        self.assertEqual(network_info, [])

        # Test 6
        self.vm_manager.vim_client.get_vm = _get_vm_vim_guest_info
        network_info = self.vm_manager.get_vm_network("vm_foo5")
        expected_6 = VmNetworkInfo(mac_address=sample_mac_address,
                                   ip_address=ip_address,
                                   network=sample_network,
                                   is_connected=ConnectedStatus.CONNECTED)
        self.assertEqual(network_info, [expected_6])