use of com.cloud.agent.api.UnPlugNicCommand in project cloudstack by apache.
the class LibvirtComputingResourceTest method testUnPlugNicCommandLibvirtException.
@SuppressWarnings("unchecked")
@Test
public void testUnPlugNicCommandLibvirtException() {
final NicTO nic = Mockito.mock(NicTO.class);
final String instanceName = "Test";
final UnPlugNicCommand command = new UnPlugNicCommand(nic, instanceName);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenThrow(LibvirtException.class);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertFalse(answer.getResult());
verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
use of com.cloud.agent.api.UnPlugNicCommand in project cloudstack by apache.
the class HypervDirectConnectResource method executeRequest.
// TODO: Is it valid to return NULL, or should we throw on error?
@Override
public final Answer executeRequest(final Command cmd) {
// Set HTTP POST destination URI
// Using java.net.URI, see
// http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URI.html
URI agentUri = null;
final Class<? extends Command> clazz = cmd.getClass();
Answer answer = null;
try {
final String cmdName = cmd.getClass().getName();
agentUri = new URI("https", null, _agentIp, _port, "/api/HypervResource/" + cmdName, null, null);
} catch (final URISyntaxException e) {
// TODO add proper logging
final String errMsg = "Could not generate URI for Hyper-V agent";
s_logger.error(errMsg, e);
return null;
}
if (cmd instanceof NetworkElementCommand) {
return _vrResource.executeRequest((NetworkElementCommand) cmd);
}
if (clazz == CheckSshCommand.class) {
answer = execute((CheckSshCommand) cmd);
} else if (cmd instanceof NetworkUsageCommand) {
answer = execute((NetworkUsageCommand) cmd);
} else if (clazz == PingTestCommand.class) {
answer = execute((PingTestCommand) cmd);
} else if (clazz == PlugNicCommand.class) {
answer = execute((PlugNicCommand) cmd);
} else if (clazz == UnPlugNicCommand.class) {
answer = execute((UnPlugNicCommand) cmd);
} else if (clazz == CopyCommand.class) {
answer = execute((CopyCommand) cmd);
} else {
if (clazz == StartCommand.class) {
final VirtualMachineTO vmSpec = ((StartCommand) cmd).getVirtualMachine();
if (vmSpec.getType() != VirtualMachine.Type.User) {
if (s_hypervMgr != null) {
final String secondary = s_hypervMgr.prepareSecondaryStorageStore(Long.parseLong(_zoneId));
if (secondary != null) {
((StartCommand) cmd).setSecondaryStorage(secondary);
}
} else {
s_logger.error("Hyperv manager isn't available. Couldn't check and copy the systemvm iso.");
}
}
}
// Send the cmd to hyperv agent.
final String ansStr = postHttpRequest(s_gson.toJson(cmd), agentUri);
if (ansStr == null) {
return Answer.createUnsupportedCommandAnswer(cmd);
}
// Only Answer instances are returned by remote agents.
// E.g. see Response.getAnswers()
final Answer[] result = s_gson.fromJson(ansStr, Answer[].class);
final String logResult = cleanPassword(s_gson.toJson(result));
s_logger.debug("executeRequest received response " + logResult);
if (result.length > 0) {
return result[0];
}
}
return answer;
}
use of com.cloud.agent.api.UnPlugNicCommand in project cloudstack by apache.
the class Ovm3VmSupportTest method unPlugNicTest.
@Test
public void unPlugNicTest() throws ConfigurationException, URISyntaxException {
hypervisor = support.prepare(configTest.getParams());
NicTO nic = prepNic(xen.getVmNicMac(), 200, TrafficType.Guest);
UnPlugNicCommand plug = new UnPlugNicCommand(nic, xen.getVmName());
Answer ra = hypervisor.executeRequest(plug);
results.basicBooleanTest(ra.getResult());
}
use of com.cloud.agent.api.UnPlugNicCommand in project cosmic by MissionCriticalCloud.
the class VirtualMachineManagerImpl method unplugNic.
public boolean unplugNic(final Network network, final NicTO nic, final VirtualMachineTO vm, final ReservationContext context, final DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException {
boolean result = true;
final VMInstanceVO router = _vmDao.findById(vm.getId());
if (router.getState() == State.Running) {
try {
final Commands cmds = new Commands(Command.OnError.Stop);
final UnPlugNicCommand unplugNicCmd = new UnPlugNicCommand(nic, vm.getName());
cmds.addCommand("unplugnic", unplugNicCmd);
_agentMgr.send(dest.getHost().getId(), cmds);
final UnPlugNicAnswer unplugNicAnswer = cmds.getAnswer(UnPlugNicAnswer.class);
if (!(unplugNicAnswer != null && unplugNicAnswer.getResult())) {
s_logger.warn("Unable to unplug nic from router " + router);
result = false;
}
} catch (final OperationTimedoutException e) {
throw new AgentUnavailableException("Unable to unplug nic from rotuer " + router + " from network " + network, dest.getHost().getId(), e);
}
} else if (router.getState() == State.Stopped || router.getState() == State.Stopping) {
s_logger.debug("Vm " + router.getInstanceName() + " is in " + router.getState() + ", so not sending unplug nic command to the backend");
} else {
s_logger.warn("Unable to apply unplug nic, Vm " + router + " is not in the right state " + router.getState());
throw new ResourceUnavailableException("Unable to apply unplug nic on the backend," + " vm " + router + " is not in the right state", DataCenter.class, router.getDataCenterId());
}
return result;
}
use of com.cloud.agent.api.UnPlugNicCommand in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResourceTest method testUnPlugNicCommandNoNics.
@Test
public void testUnPlugNicCommandNoNics() {
final NicTO nic = Mockito.mock(NicTO.class);
final String instanceName = "Test";
final UnPlugNicCommand command = new UnPlugNicCommand(nic, instanceName);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Connect conn = Mockito.mock(Connect.class);
final Domain vm = Mockito.mock(Domain.class);
final List<InterfaceDef> nics = new ArrayList<>();
final VifDriver vifDriver = Mockito.mock(VifDriver.class);
final List<VifDriver> drivers = new ArrayList<>();
drivers.add(vifDriver);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
when(libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertTrue(answer.getResult());
verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
Aggregations