public static PublicIPAddress associateIPAddressInNetwork(Network network, CloudStackClient client, BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult) { AsyncCreateResponse job = client.getAddressClient().associateIPAddressInZone(network.getZoneId(), networkId(network.getId())); PublicIPAddress ip = blockUntilJobCompletesAndReturnResult.<PublicIPAddress> apply(job); assert ip.getZoneId().equals(network.getZoneId()); return ip; }
public void disassociateIPAddresses(Set<String> ipAddresses) { for (String ipAddress : ipAddresses) { logger.debug(">> disassociating IPAddress(%s)", ipAddress); client.getAddressClient().disassociateIPAddress(ipAddress); } }
/** * Finds existing addresses who are ready for use and not assigned to a * machine. * * @param networkId * network id to search * @param client * address client * @return address to use * @throws NoSuchElementException * if there's no existing ip address that is free for use */ public static PublicIPAddress findAvailableAndAssociatedWithNetwork(String networkId, AddressClient client) { return find(client.listPublicIPAddresses(allocatedOnly(true).networkId(networkId)), and(associatedWithNetwork(networkId), available())); }
public void testAssociateWorks() throws SecurityException, NoSuchMethodException { // create mocks CloudStackClient client = createMock(CloudStackClient.class); BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult = createMock(BlockUntilJobCompletesAndReturnResult.class); AddressClient addressClient = createMock(AddressClient.class); expect(client.getAddressClient()).andReturn(addressClient).atLeastOnce(); // no ip addresses available expect(addressClient.listPublicIPAddresses(allocatedOnly(true).networkId(networkId))).andReturn( ImmutableSet.<PublicIPAddress> of()); AsyncCreateResponse job = AsyncCreateResponse.builder().id("1").jobId("2").build(); // make sure we created the job relating to a new ip expect(addressClient.associateIPAddressInZone(zoneId, networkId(networkId))).andReturn(job); expect(blockUntilJobCompletesAndReturnResult.apply(job)).andReturn(address); replay(client); replay(addressClient); replay(blockUntilJobCompletesAndReturnResult); assertEquals( new ReuseOrAssociateNewPublicIPAddress(client, blockUntilJobCompletesAndReturnResult).apply(Network .builder().id(networkId).zoneId(zoneId).build()), address); verify(client); verify(addressClient); verify(blockUntilJobCompletesAndReturnResult); }
protected void checkIP(PublicIPAddress ip) { assertEquals(ip.getId(), client.getAddressClient().getPublicIPAddress(ip.getId()).getId()); assert ip.getId() != null : ip; assert ip.getAccount() != null : ip; assert ip.getDomain() != null : ip; assert ip.getDomainId() != null : ip; assert ip.getState() != null : ip; assert ip.getZoneId() != null : ip; assert ip.getZoneName() != null : ip; } }
public PublicIPAddress apply(VirtualMachine vm) { PublicIPAddress ip; for (ip = reuseOrAssociate.apply(network); !ip.isStaticNAT() || ip.getVirtualMachineId() != vm.getId(); ip = reuseOrAssociate .apply(network)) { // check to see if someone already grabbed this ip if (ip.getVirtualMachineId() != null && ip.getVirtualMachineId() != vm.getId()) continue; try { logger.debug(">> static NATing IPAddress(%s) to virtualMachine(%s)", ip.getId(), vm.getId()); client.getNATClient().enableStaticNATForVirtualMachine(vm.getId(), ip.getId()); ip = client.getAddressClient().getPublicIPAddress(ip.getId()); if (ip.isStaticNAT() && ip.getVirtualMachineId().equals(vm.getId())) break; } catch (IllegalStateException e) { // very likely an ip conflict, so retry; } return ip; } return ip; } }
public void disassociateIPAddresses(Set<String> ipAddresses) { for (String ipAddress : ipAddresses) { logger.debug(">> disassociating IPAddress(%s)", ipAddress); client.getAddressClient().disassociateIPAddress(ipAddress); } }
/** * Finds existing addresses who are ready for use and not assigned to a * machine. * * @param networkId * network id to search * @param client * address client * @return address to use * @throws NoSuchElementException * if there's no existing ip address that is free for use */ public static PublicIPAddress findAvailableAndAssociatedWithNetwork(String networkId, AddressClient client) { return find(client.listPublicIPAddresses(allocatedOnly(true).networkId(networkId)), and(associatedWithNetwork(networkId), available())); }
public static PublicIPAddress associateIPAddressInNetwork(Network network, CloudStackClient client, BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult) { AsyncCreateResponse job = client.getAddressClient().associateIPAddressInZone(network.getZoneId(), networkId(network.getId())); PublicIPAddress ip = blockUntilJobCompletesAndReturnResult.<PublicIPAddress> apply(job); assert ip.getZoneId().equals(network.getZoneId()); return ip; }
public PublicIPAddress apply(VirtualMachine vm) { PublicIPAddress ip; for (ip = reuseOrAssociate.apply(network); !ip.isStaticNAT() || ip.getVirtualMachineId() != vm.getId(); ip = reuseOrAssociate .apply(network)) { // check to see if someone already grabbed this ip if (ip.getVirtualMachineId() != null && ip.getVirtualMachineId() != vm.getId()) continue; try { logger.debug(">> static NATing IPAddress(%s) to virtualMachine(%s)", ip.getId(), vm.getId()); client.getNATClient().enableStaticNATForVirtualMachine(vm.getId(), ip.getId()); ip = client.getAddressClient().getPublicIPAddress(ip.getId()); if (ip.isStaticNAT() && ip.getVirtualMachineId().equals(vm.getId())) break; } catch (IllegalStateException e) { // very likely an ip conflict, so retry; } return ip; } return ip; } }
@AfterGroups(groups = "live") @Override protected void tearDownContext() { if (ip != null) { client.getAddressClient().disassociateIPAddress(ip.getId()); } super.tearDownContext(); }
public void testListPublicIPAddresss() throws Exception { if (!networksEnabled) return; Set<PublicIPAddress> response = client.getAddressClient().listPublicIPAddresses(); assert null != response; assertTrue(response.size() >= 0); for (PublicIPAddress ip : response) { PublicIPAddress newDetails = getOnlyElement(client.getAddressClient().listPublicIPAddresses( ListPublicIPAddressesOptions.Builder.id(ip.getId()))); assertEquals(ip.getId(), newDetails.getId()); checkIP(ip); } }
public void testAssociateDisassociatePublicIPAddress() throws Exception { if (!networksEnabled) return; AsyncCreateResponse job = client.getAddressClient().associateIPAddressInZone( Iterables.get(client.getNetworkClient().listNetworks(), 0).getZoneId()); checkState(jobComplete.apply(job.getJobId()), "job %s failed to complete", job.getJobId()); ip = client.getAsyncJobClient().<PublicIPAddress> getAsyncJob(job.getJobId()).getResult(); checkIP(ip); }
@AfterGroups(groups = "live") @Override protected void tearDownContext() { if (rule != null) { assertTrue(jobComplete.apply(client.getLoadBalancerClient().deleteLoadBalancerRule(rule.getId()))); } if (vm != null) { assertTrue(jobComplete.apply(client.getVirtualMachineClient().destroyVirtualMachine(vm.getId()))); } if (ip != null) { client.getAddressClient().disassociateIPAddress(ip.getId()); } super.tearDownContext(); }
public void testReuseWorks() throws SecurityException, NoSuchMethodException { // create mocks CloudStackClient client = createMock(CloudStackClient.class); BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult = createMock(BlockUntilJobCompletesAndReturnResult.class); AddressClient addressClient = createMock(AddressClient.class); expect(client.getAddressClient()).andReturn(addressClient).atLeastOnce(); // an address is available expect(addressClient.listPublicIPAddresses(allocatedOnly(true).networkId(networkId))).andReturn( ImmutableSet.<PublicIPAddress> of(address)); replay(client); replay(blockUntilJobCompletesAndReturnResult); replay(addressClient); assertEquals( new ReuseOrAssociateNewPublicIPAddress(client, blockUntilJobCompletesAndReturnResult).apply(Network .builder().id(networkId).zoneId(zoneId).build()), address); verify(client); verify(blockUntilJobCompletesAndReturnResult); verify(addressClient); }
@AfterGroups(groups = "live") @Override protected void tearDownContext() { if (rule != null) { client.getNATClient().deleteIPForwardingRule(rule.getId()); } if (vm != null) { jobComplete.apply(client.getVirtualMachineClient().destroyVirtualMachine(vm.getId())); } if (ip != null) { client.getAddressClient().disassociateIPAddress(ip.getId()); } super.tearDownContext(); }
@AfterGroups(groups = "live") @Override protected void tearDownContext() { if (firewallRule != null) { client.getFirewallClient().deleteFirewallRule(firewallRule.getId()); } if (portForwardingRule != null) { client.getFirewallClient().deletePortForwardingRule(portForwardingRule.getId()); } if (vm != null) { jobComplete.apply(client.getVirtualMachineClient().destroyVirtualMachine(vm.getId())); } if (ip != null) { client.getAddressClient().disassociateIPAddress(ip.getId()); } super.tearDownContext(); }