/** * Retrieves IP addresses on the VM from vCenter. The VI Java API's method for returning * the IP addresses can be unreliable - it depends on when the VM is polled by Terraform. * Only IPv4 addresses will be added - IPv6 addresses are ignored right now. * Sets the IP addresses as a comma-separated list. This will be written out to XML. */ public void setIpListFromVmInfo() { ipList.clear(); GuestNicInfo[] nicInfos = vm.getGuest().getNet(); if (nicInfos == null) { log.warn("problem retrieving network info from VM"); } else { for (int i=0; i<nicInfos.length; i++) { String[] nicInfoIpList = nicInfos[i].getIpAddress(); for (int j=0; j<nicInfoIpList.length; j++) { //ip4 addresses only; vsphere 5 likes to return ip4 and ip6 String unparsedIp = nicInfoIpList[j]; if (!unparsedIp.contains(":")) { Ip4 newIp = new Ip4(unparsedIp); ipList.add(newIp); } } } } }
/** * Searches for all ip addresses of a virtual machine * * @param virtualMachine the virtual machine to query * @return the ip addresses of the virtual machine, the first one is the primary */ public TreeSet<String> getVirtualMachineIpAddresses(VirtualMachine virtualMachine) { TreeSet<String> ipAddresses = new TreeSet<String>(); // add the Ip address reported by VMware tools, this should be primary if (virtualMachine.getGuest().getIpAddress() != null) { ipAddresses.add(virtualMachine.getGuest().getIpAddress()); } // if possible, iterate over all virtual networks networks and add interface Ip addresses if (virtualMachine.getGuest().getNet() != null) { for (GuestNicInfo guestNicInfo : virtualMachine.getGuest().getNet()) { if (guestNicInfo.getIpAddress() != null) { for (String ipAddress : guestNicInfo.getIpAddress()) { ipAddresses.add(ipAddress); } } } } return ipAddresses; }
/** * Searches for all ip addresses of a virtual machine * * @param virtualMachine the virtual machine to query * @return the ip addresses of the virtual machine, the first one is the primary */ public TreeSet<String> getVirtualMachineIpAddresses(VirtualMachine virtualMachine) { TreeSet<String> ipAddresses = new TreeSet<String>(); // add the Ip address reported by VMware tools, this should be primary if (virtualMachine.getGuest().getIpAddress() != null) { ipAddresses.add(virtualMachine.getGuest().getIpAddress()); } // if possible, iterate over all virtual networks networks and add interface Ip addresses if (virtualMachine.getGuest().getNet() != null) { for (GuestNicInfo guestNicInfo : virtualMachine.getGuest().getNet()) { if (guestNicInfo.getIpAddress() != null) { for (String ipAddress : guestNicInfo.getIpAddress()) { ipAddresses.add(ipAddress); } } } } return ipAddresses; }