Search in sources :

Example 1 with IpPortAndProto

use of com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto in project cloudstack by apache.

the class SecurityGroupManagerImpl method generateRulesetCmd.

protected SecurityGroupRulesCmd generateRulesetCmd(String vmName, String guestIp, String guestIp6, String guestMac, Long vmId, String signature, long seqnum, Map<PortAndProto, Set<String>> ingressRules, Map<PortAndProto, Set<String>> egressRules, List<String> secIps) {
    List<IpPortAndProto> ingressResult = new ArrayList<IpPortAndProto>();
    List<IpPortAndProto> egressResult = new ArrayList<IpPortAndProto>();
    for (PortAndProto pAp : ingressRules.keySet()) {
        Set<String> cidrs = ingressRules.get(pAp);
        if (cidrs.size() > 0) {
            IpPortAndProto ipPortAndProto = new SecurityGroupRulesCmd.IpPortAndProto(pAp.getProto(), pAp.getStartPort(), pAp.getEndPort(), cidrs.toArray(new String[cidrs.size()]));
            ingressResult.add(ipPortAndProto);
        }
    }
    for (PortAndProto pAp : egressRules.keySet()) {
        Set<String> cidrs = egressRules.get(pAp);
        if (cidrs.size() > 0) {
            IpPortAndProto ipPortAndProto = new SecurityGroupRulesCmd.IpPortAndProto(pAp.getProto(), pAp.getStartPort(), pAp.getEndPort(), cidrs.toArray(new String[cidrs.size()]));
            egressResult.add(ipPortAndProto);
        }
    }
    SecurityGroupRulesCmd cmd = new SecurityGroupRulesCmd(guestIp, guestIp6, guestMac, vmName, vmId, signature, seqnum, ingressResult.toArray(new IpPortAndProto[ingressResult.size()]), egressResult.toArray(new IpPortAndProto[egressResult.size()]), secIps);
    final VirtualMachineTO to = getVmTO(vmId);
    cmd.setVmTO(to);
    return cmd;
}
Also used : SecurityGroupRulesCmd(com.cloud.agent.api.SecurityGroupRulesCmd) ArrayList(java.util.ArrayList) IpPortAndProto(com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto) IpPortAndProto(com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO)

Example 2 with IpPortAndProto

use of com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto in project cloudstack by apache.

the class NotAValidCommand method testSecurityGroupRulesCommand.

@Test
public void testSecurityGroupRulesCommand() {
    final Connection conn = Mockito.mock(Connection.class);
    final XsHost xsHost = Mockito.mock(XsHost.class);
    final String guestIp = "127.0.0.1";
    final String guestIp6 = "2001:db8::cad:40ff:fefd:75c4";
    final String guestMac = "00:00:00:00";
    final String vmName = "Test";
    final Long vmId = 1l;
    final String signature = "signature";
    final Long seqNum = 1l;
    final IpPortAndProto[] ingressRuleSet = new IpPortAndProto[] { Mockito.mock(IpPortAndProto.class) };
    final IpPortAndProto[] egressRuleSet = new IpPortAndProto[] { Mockito.mock(IpPortAndProto.class) };
    final List<String> secIps = new Vector<String>();
    final SecurityGroupRulesCmd sshCommand = new SecurityGroupRulesCmd(guestIp, guestIp6, guestMac, vmName, vmId, signature, seqNum, ingressRuleSet, egressRuleSet, secIps);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.getConnection()).thenReturn(conn);
    when(citrixResourceBase.getHost()).thenReturn(xsHost);
    final Answer answer = wrapper.execute(sshCommand, citrixResourceBase);
    verify(citrixResourceBase, times(1)).getConnection();
    assertFalse(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) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) SecurityGroupRulesCmd(com.cloud.agent.api.SecurityGroupRulesCmd) Connection(com.xensource.xenapi.Connection) IpPortAndProto(com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto) Vector(java.util.Vector) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with IpPortAndProto

use of com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto in project cloudstack by apache.

the class SecurityGroupHttpClient method generateRules.

private List<SecurityGroupRule> generateRules(final List<IpPortAndProto> ipps) {
    List<SecurityGroupRule> rules = new ArrayList<SecurityGroupRule>(ipps.size());
    for (SecurityGroupRulesCmd.IpPortAndProto ipp : ipps) {
        SecurityGroupRule r = new SecurityGroupRule();
        r.setProtocol(ipp.getProto());
        r.setStartPort(ipp.getStartPort());
        r.setEndPort(ipp.getEndPort());
        for (String cidr : ipp.getAllowedCidrs()) {
            r.getIp().add(cidr);
        }
        rules.add(r);
    }
    return rules;
}
Also used : SecurityGroupRulesCmd(com.cloud.agent.api.SecurityGroupRulesCmd) IpPortAndProto(com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto) ArrayList(java.util.ArrayList) SecurityGroupRule(com.cloud.baremetal.networkservice.schema.SecurityGroupRule)

Example 4 with IpPortAndProto

use of com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto in project cloudstack by apache.

the class LibvirtComputingResourceTest method testSecurityGroupRulesCmdFalse.

@Test
public void testSecurityGroupRulesCmdFalse() {
    final String guestIp = "127.0.0.1";
    final String guestIp6 = "2001:db8::cad:40ff:fefd:75c4";
    final String guestMac = "00:00:00:00";
    final String vmName = "Test";
    final Long vmId = 1l;
    final String signature = "signature";
    final Long seqNum = 1l;
    final IpPortAndProto[] ingressRuleSet = new IpPortAndProto[] { Mockito.mock(IpPortAndProto.class) };
    final IpPortAndProto[] egressRuleSet = new IpPortAndProto[] { Mockito.mock(IpPortAndProto.class) };
    final List<String> secIps = new Vector<String>();
    final List<String> cidrs = new Vector<String>();
    cidrs.add("0.0.0.0/0");
    final SecurityGroupRulesCmd command = new SecurityGroupRulesCmd(guestIp, guestIp6, guestMac, vmName, vmId, signature, seqNum, ingressRuleSet, egressRuleSet, secIps);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
    final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class);
    nics.add(interfaceDef);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    when(ingressRuleSet[0].getProto()).thenReturn("tcp");
    when(ingressRuleSet[0].getStartPort()).thenReturn(22);
    when(ingressRuleSet[0].getEndPort()).thenReturn(22);
    when(ingressRuleSet[0].getAllowedCidrs()).thenReturn(cidrs);
    when(egressRuleSet[0].getProto()).thenReturn("tcp");
    when(egressRuleSet[0].getStartPort()).thenReturn(22);
    when(egressRuleSet[0].getEndPort()).thenReturn(22);
    when(egressRuleSet[0].getAllowedCidrs()).thenReturn(cidrs);
    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());
    }
}
Also used : SecurityGroupRulesCmd(com.cloud.agent.api.SecurityGroupRulesCmd) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) IpPortAndProto(com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) Vector(java.util.Vector) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with IpPortAndProto

use of com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto in project cloudstack by apache.

the class LibvirtComputingResourceTest method testSecurityGroupRulesCmdException.

@SuppressWarnings("unchecked")
@Test
public void testSecurityGroupRulesCmdException() {
    final String guestIp = "127.0.0.1";
    final String guestIp6 = "2001:db8::cad:40ff:fefd:75c4";
    final String guestMac = "00:00:00:00";
    final String vmName = "Test";
    final Long vmId = 1l;
    final String signature = "signature";
    final Long seqNum = 1l;
    final IpPortAndProto[] ingressRuleSet = new IpPortAndProto[] { Mockito.mock(IpPortAndProto.class) };
    final IpPortAndProto[] egressRuleSet = new IpPortAndProto[] { Mockito.mock(IpPortAndProto.class) };
    final List<String> secIps = new Vector<String>();
    final SecurityGroupRulesCmd command = new SecurityGroupRulesCmd(guestIp, guestIp6, guestMac, vmName, vmId, signature, seqNum, ingressRuleSet, egressRuleSet, secIps);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
    final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class);
    nics.add(interfaceDef);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
    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());
    }
}
Also used : SecurityGroupRulesCmd(com.cloud.agent.api.SecurityGroupRulesCmd) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) IpPortAndProto(com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) Vector(java.util.Vector) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

SecurityGroupRulesCmd (com.cloud.agent.api.SecurityGroupRulesCmd)6 IpPortAndProto (com.cloud.agent.api.SecurityGroupRulesCmd.IpPortAndProto)6 ArrayList (java.util.ArrayList)5 Answer (com.cloud.agent.api.Answer)4 Vector (java.util.Vector)4 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)3 UnsupportedAnswer (com.cloud.agent.api.UnsupportedAnswer)3 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)3 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)3 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)3 Connect (org.libvirt.Connect)3 LibvirtException (org.libvirt.LibvirtException)3 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)2 RebootAnswer (com.cloud.agent.api.RebootAnswer)1 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)1 SecurityGroupRule (com.cloud.baremetal.networkservice.schema.SecurityGroupRule)1 XsHost (com.cloud.hypervisor.xenserver.resource.XsHost)1