Search in sources :

Example 1 with EnableStaticNatCmd

use of org.apache.cloudstack.api.command.user.nat.EnableStaticNatCmd in project cloudstack by apache.

the class NetworkProviderTest method createFloatingIp.

public IPAddressVO createFloatingIp(Network network, UserVm vm) throws Exception {
    BaseCmd cmd = new AssociateIPAddrCmd();
    BaseCmd proxy = ComponentContext.inject(cmd);
    Account system = _accountMgr.getSystemAccount();
    DataCenter zone = _server.getZone();
    ManagementServerMock.setParameter(proxy, "accountName", BaseCmd.CommandType.STRING, system.getAccountName());
    ManagementServerMock.setParameter(proxy, "domainId", BaseCmd.CommandType.LONG, Domain.ROOT_DOMAIN);
    ManagementServerMock.setParameter(proxy, "zoneId", BaseCmd.CommandType.LONG, zone.getId());
    ManagementServerMock.setParameter(proxy, "networkId", BaseCmd.CommandType.LONG, network.getId());
    try {
        ((AssociateIPAddrCmd) cmd).create();
        ((AssociateIPAddrCmd) cmd).execute();
    } catch (Exception e) {
        s_logger.debug("AssociateIPAddrCmd exception: " + e);
        e.printStackTrace();
        throw e;
    }
    SearchBuilder<IPAddressVO> searchBuilder = _ipAddressDao.createSearchBuilder();
    searchBuilder.and("sourceNat", searchBuilder.entity().isSourceNat(), Op.EQ);
    searchBuilder.and("network", searchBuilder.entity().getAssociatedWithNetworkId(), Op.EQ);
    searchBuilder.and("dataCenterId", searchBuilder.entity().getDataCenterId(), Op.EQ);
    searchBuilder.and("associatedWithVmId", searchBuilder.entity().getAssociatedWithVmId(), Op.NULL);
    SearchCriteria<IPAddressVO> sc = searchBuilder.create();
    sc.setParameters("sourceNat", false);
    sc.setParameters("network", network.getId());
    List<IPAddressVO> publicIps = _ipAddressDao.search(sc, null);
    assertNotNull(publicIps);
    cmd = new EnableStaticNatCmd();
    proxy = ComponentContext.inject(cmd);
    ManagementServerMock.setParameter(proxy, "ipAddressId", BaseCmd.CommandType.LONG, publicIps.get(0).getId());
    ManagementServerMock.setParameter(proxy, "networkId", BaseCmd.CommandType.LONG, network.getId());
    ManagementServerMock.setParameter(proxy, "virtualMachineId", BaseCmd.CommandType.LONG, vm.getId());
    try {
        proxy.execute();
    } catch (Exception e) {
        s_logger.debug("EnableStaticNatCmd exception: " + e);
        e.printStackTrace();
        throw e;
    }
    return publicIps.get(0);
}
Also used : Account(com.cloud.user.Account) DataCenter(com.cloud.dc.DataCenter) AssociateIPAddrCmd(org.apache.cloudstack.api.command.user.address.AssociateIPAddrCmd) IPAddressVO(com.cloud.network.dao.IPAddressVO) EnableStaticNatCmd(org.apache.cloudstack.api.command.user.nat.EnableStaticNatCmd) BaseCmd(org.apache.cloudstack.api.BaseCmd) CloudException(com.cloud.exception.CloudException) IOException(java.io.IOException)

Aggregations

DataCenter (com.cloud.dc.DataCenter)1 CloudException (com.cloud.exception.CloudException)1 IPAddressVO (com.cloud.network.dao.IPAddressVO)1 Account (com.cloud.user.Account)1 IOException (java.io.IOException)1 BaseCmd (org.apache.cloudstack.api.BaseCmd)1 AssociateIPAddrCmd (org.apache.cloudstack.api.command.user.address.AssociateIPAddrCmd)1 EnableStaticNatCmd (org.apache.cloudstack.api.command.user.nat.EnableStaticNatCmd)1