예제 #1
0
  @Test
  public void test() throws ApiSenderException {
    EipInventory eip = deployer.eips.get("eip");
    VmInstanceInventory vm = deployer.vms.get("TestVm");
    VmNicInventory nic = vm.getVmNics().get(0);
    IpRangeInventory ipr = deployer.ipRanges.get("GuestIpRange");

    String newIp = null;
    long s = NetworkUtils.ipv4StringToLong(ipr.getStartIp());
    long e = NetworkUtils.ipv4StringToLong(ipr.getEndIp());

    for (long l = s; s < e; s++) {
      String ip = NetworkUtils.longToIpv4String(l);
      if (!ip.equals(nic.getIp())) {
        newIp = ip;
        break;
      }
    }
    Assert.assertNotNull(newIp);

    api.stopVmInstance(vm.getUuid());
    api.setStaticIp(vm.getUuid(), nic.getL3NetworkUuid(), newIp);
    EipVO eipvo = dbf.findByUuid(eip.getUuid(), EipVO.class);
    Assert.assertEquals(newIp, eipvo.getGuestIp());
  }
  private String allocateIp(IpRangeVO vo) {
    int total = vo.size();
    Random random = new Random();
    long s = random.nextInt(total) + NetworkUtils.ipv4StringToLong(vo.getStartIp());
    long e = NetworkUtils.ipv4StringToLong(vo.getEndIp());
    String ret = steppingAllocate(s, e, total, vo.getUuid());
    if (ret != null) {
      return ret;
    }

    e = s;
    s = NetworkUtils.ipv4StringToLong(vo.getStartIp());
    return steppingAllocate(s, e, total, vo.getUuid());
  }