Search in sources :

Example 1 with VMGuestMetrics

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);
}
Also used : Answer(com.cloud.agent.api.Answer) Types(com.xensource.xenapi.Types) VMGuestMetrics(com.xensource.xenapi.VMGuestMetrics) VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 2 with VMGuestMetrics

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());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) VMGuestMetrics(com.xensource.xenapi.VMGuestMetrics) HashMap(java.util.HashMap) GetVmIpAddressCommand(com.cloud.agent.api.GetVmIpAddressCommand) VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) Test(org.junit.Test)

Aggregations

Answer (com.cloud.agent.api.Answer)2 Connection (com.xensource.xenapi.Connection)2 VM (com.xensource.xenapi.VM)2 VMGuestMetrics (com.xensource.xenapi.VMGuestMetrics)2 GetVmIpAddressCommand (com.cloud.agent.api.GetVmIpAddressCommand)1 RebootAnswer (com.cloud.agent.api.RebootAnswer)1 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)1 Types (com.xensource.xenapi.Types)1 HashMap (java.util.HashMap)1 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)1 XmlRpcException (org.apache.xmlrpc.XmlRpcException)1 Test (org.junit.Test)1