use of com.xensource.xenapi.VMGuestMetrics in project cloudstack by apache.
the class CitrixGetVmIpAddressCommandWrapper method execute.
@Override
public Answer execute(final GetVmIpAddressCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
String vmName = command.getVmName();
String networkCidr = command.getVmNetworkCidr();
boolean result = false;
String errorMsg = null;
String vmIp = null;
try {
VM vm = citrixResourceBase.getVM(conn, vmName);
VMGuestMetrics mtr = vm.getGuestMetrics(conn);
VMGuestMetrics.Record rec = mtr.getRecord(conn);
Map<String, String> vmIpsMap = rec.networks;
for (String ipAddr : vmIpsMap.values()) {
if (NetUtils.isIpWithtInCidrRange(ipAddr, networkCidr)) {
vmIp = ipAddr;
break;
}
}
if (vmIp != null) {
s_logger.debug("VM " + vmName + " ip address got retrieved " + vmIp);
result = true;
return new Answer(command, result, vmIp);
}
} catch (Types.XenAPIException e) {
s_logger.debug("Got exception in GetVmIpAddressCommand " + e.getMessage());
errorMsg = "Failed to retrived vm ip addr, exception: " + e.getMessage();
} catch (XmlRpcException e) {
s_logger.debug("Got exception in GetVmIpAddressCommand " + e.getMessage());
errorMsg = "Failed to retrived vm ip addr, exception: " + e.getMessage();
}
return new Answer(command, result, errorMsg);
}
use of com.xensource.xenapi.VMGuestMetrics in project cloudstack by apache.
the class NotAValidCommand method testGetVmIpAddressCommand.
@Test
public void testGetVmIpAddressCommand() throws XenAPIException, XmlRpcException {
final Connection conn = Mockito.mock(Connection.class);
final VM vm = Mockito.mock(VM.class);
final VMGuestMetrics mtr = Mockito.mock(VMGuestMetrics.class);
final VMGuestMetrics.Record rec = Mockito.mock(VMGuestMetrics.Record.class);
final Map<String, String> vmIpsMap = new HashMap<>();
vmIpsMap.put("Test", "127.0.0.1");
rec.networks = vmIpsMap;
final GetVmIpAddressCommand getVmIpAddrCmd = new GetVmIpAddressCommand("Test", "127.0.0.1/24", false);
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(citrixResourceBase.getConnection()).thenReturn(conn);
when(citrixResourceBase.getVM(conn, getVmIpAddrCmd.getVmName())).thenReturn(vm);
when(vm.getGuestMetrics(conn)).thenReturn(mtr);
when(mtr.getRecord(conn)).thenReturn(rec);
final Answer answer = wrapper.execute(getVmIpAddrCmd, citrixResourceBase);
verify(citrixResourceBase, times(1)).getConnection();
assertTrue(answer.getResult());
}
Aggregations