public static PublicIPAddress associateIPAddressInNetwork(Network network, CloudStackApi client, BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult) { AsyncCreateResponse job = client.getAddressApi().associateIPAddressInZone(network.getZoneId(), networkId(network.getId())); PublicIPAddress ip = blockUntilJobCompletesAndReturnResult.<PublicIPAddress> apply(job); assert ip.getZoneId().equals(network.getZoneId()); return ip; }
public void deleteIpsAtVpc(String vpcId) { JsonElement je = listPublicIpAddressesAtVpc(vpcId); LOG.debug(pretty(je)); int i = 0; for (JsonElement jei : je.getAsJsonObject().get("publicipaddress").getAsJsonArray()) { String id = jei.getAsJsonObject().get("id").getAsString(); LOG.debug("deleting IP " + id); getCloudstackGlobalClient().getAddressApi().disassociateIPAddress(id); i++; } if (i > 0) LOG.info("deleted " + i + " IP's at VPC " + vpcId); }
public PublicIPAddress createIpAddressForNetwork(String networkId) { Multimap<String, String> params = ArrayListMultimap.create(); params.put("command", "associateIpAddress"); params.put("networkid", networkId); if (accAndDomain.isPresent()) { params.put("account", accAndDomain.get().account); params.put("domainid", accAndDomain.get().domainId); } params.put("apiKey", this.apiKey); params.put("response", "json"); LOG.debug("associateIpAddress GET " + params); HttpRequest request = HttpRequest.builder() .method("GET") .endpoint(this.endpoint) .addQueryParams(params) .addHeader("Accept", "application/json") .build(); request = getQuerySigner().filter(request); HttpToolResponse response = HttpUtil.invoke(request); // TODO does non-2xx response need to be handled separately ? try { String result = waitForJobCompletion(response, "createIpAddressForNetwork("+networkId+")"); return getCloudstackGlobalClient().getAddressApi().getPublicIPAddress(result); } catch (InterruptedException e) { throw Exceptions.propagate(e); } }
protected PublicIPAddress systemCreatePublicIpHostname() { PublicIPAddress ip; Location l = Iterables.getOnlyElement(getLocations()); String vpcId = getVpcId(l); if (vpcId==null) { String zoneId = getZoneId(l); String networkId = getAttribute(NETWORK_ID); AsyncCreateResponse response = cloudstackClient.getCloudstackGlobalClient().getAddressApi().associateIPAddressInZone(zoneId, AssociateIPAddressOptions.Builder.networkId(networkId)); cloudstackClient.waitForJobSuccess(response.getJobId()); ip = cloudstackClient.getCloudstackGlobalClient().getAddressApi().getPublicIPAddress(response.getId()); } else { ip = cloudstackClient.createIpAddressForVpc(vpcId); } synchronized (this) { Map<String, String> ips = getAttribute(PUBLIC_HOSTNAME_IP_IDS); if (ips == null) { ips = ImmutableMap.of(); } Map<String,String> newips = MutableMap.<String,String>builder().putAll(ips).put(ip.getIPAddress(), ip.getId()).build(); sensors().set(PUBLIC_HOSTNAME_IP_IDS, newips); } return ip; }
public void testAssociateWorks() throws SecurityException, NoSuchMethodException { // create mocks CloudStackApi client = createMock(CloudStackApi.class); BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult = createMock(BlockUntilJobCompletesAndReturnResult.class); AddressApi addressClient = createMock(AddressApi.class); expect(client.getAddressApi()).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); }
/** * 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, AddressApi client) { return find(client.listPublicIPAddresses(allocatedOnly(true).networkId(networkId)), and(associatedWithNetwork(networkId), available())); }
public void testListPublicIPAddresss() throws Exception { if (!networksEnabled) return; Set<PublicIPAddress> response = client.getAddressApi().listPublicIPAddresses(); assert null != response; assertTrue(response.size() > 0); for (PublicIPAddress ip : response) { PublicIPAddress newDetails = getOnlyElement(client.getAddressApi().listPublicIPAddresses( ListPublicIPAddressesOptions.Builder.id(ip.getId()))); assertEquals(ip.getId(), newDetails.getId()); checkIP(ip); } }
public void disassociateIPAddresses(Set<String> ipAddresses) { for (String ipAddress : ipAddresses) { logger.debug(">> disassociating IPAddress(%s)", ipAddress); client.getAddressApi().disassociateIPAddress(ipAddress); } }
public PublicIPAddress createIpAddressForVpc(String vpcId) { Multimap<String, String> params = ArrayListMultimap.create(); params.put("command", "associateIpAddress"); params.put("vpcid", vpcId); if (accAndDomain.isPresent()) { params.put("account", accAndDomain.get().account); params.put("domainid", accAndDomain.get().domainId); } params.put("apiKey", this.apiKey); params.put("response", "json"); LOG.debug("associateIpAddress GET " + params); HttpRequest request = HttpRequest.builder() .method("GET") .endpoint(this.endpoint) .addQueryParams(params) .addHeader("Accept", "application/json") .build(); request = getQuerySigner().filter(request); HttpToolResponse response = HttpUtil.invoke(request); // TODO does non-2xx response need to be handled separately ? try { String result = waitForJobCompletion(response, "createIpAddressForVpc("+vpcId+")"); return getCloudstackGlobalClient().getAddressApi().getPublicIPAddress(result); } catch (InterruptedException e) { throw Exceptions.propagate(e); } }
public void testAssociateDisassociatePublicIPAddress() throws Exception { if (!networksEnabled) return; AsyncCreateResponse job = client.getAddressApi().associateIPAddressInZone( Iterables.get(client.getNetworkApi().listNetworks(), 0).getZoneId()); checkState(jobComplete.apply(job.getJobId()), "job %s failed to complete", job.getJobId()); ip = client.getAsyncJobApi().<PublicIPAddress> getAsyncJob(job.getJobId()).getResult(); checkIP(ip); }
public void testReuseWorks() throws SecurityException, NoSuchMethodException { // create mocks CloudStackApi client = createMock(CloudStackApi.class); BlockUntilJobCompletesAndReturnResult blockUntilJobCompletesAndReturnResult = createMock(BlockUntilJobCompletesAndReturnResult.class); AddressApi addressClient = createMock(AddressApi.class); expect(client.getAddressApi()).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 (ip != null) { client.getAddressApi().disassociateIPAddress(ip.getId()); } super.tearDownContext(); }
public Maybe<PublicIPAddress> findPublicIpAddressByVmId(final String vmId) { Set<PortForwardingRule> portForwardingRules = getCloudstackGlobalClient().getFirewallApi().listPortForwardingRules(); Optional<PortForwardingRule> pfr = Iterables.tryFind(portForwardingRules, new Predicate<PortForwardingRule>() { @Override public boolean apply(PortForwardingRule portForwardingRule) { return portForwardingRule.getVirtualMachineId().equals(vmId); } }); if (pfr.isPresent()) { return Maybe.of(getCloudstackGlobalClient().getAddressApi().getPublicIPAddress(pfr.get().getIPAddressId())); } else { return Maybe.absent(); } } }
@AfterGroups(groups = "live") @Override protected void tearDownContext() { if (rule != null) { assertTrue(jobComplete.apply(client.getLoadBalancerApi().deleteLoadBalancerRule(rule.getId()))); } if (vm != null) { assertTrue(jobComplete.apply(client.getVirtualMachineApi().destroyVirtualMachine(vm.getId()))); } if (ip != null) { client.getAddressApi().disassociateIPAddress(ip.getId()); } super.tearDownContext(); }
public PublicIPAddress apply(VirtualMachine vm) { PublicIPAddress ip; for (ip = reuseOrAssociate.apply(network); !ip.isStaticNAT() || !Objects.equal(ip.getVirtualMachineId(), vm.getId()); ip = reuseOrAssociate .apply(network)) { // check to see if someone already grabbed this ip if (ip.getVirtualMachineId() != null && !ip.getVirtualMachineId().equals(vm.getId())) continue; try { logger.debug(">> static NATing IPAddress(%s) to virtualMachine(%s)", ip.getId(), vm.getId()); client.getNATApi().enableStaticNATForVirtualMachine(vm.getId(), ip.getId()); ip = client.getAddressApi().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 (rule != null) { client.getNATApi().deleteIPForwardingRule(rule.getId()); } if (vm != null) { jobComplete.apply(client.getVirtualMachineApi().destroyVirtualMachine(vm.getId())); } if (ip != null) { client.getAddressApi().disassociateIPAddress(ip.getId()); } super.tearDownContext(); }
protected void checkIP(PublicIPAddress ip) { assertEquals(ip.getId(), client.getAddressApi().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; } }
@AfterGroups(groups = "live") @Override protected void tearDownContext() { if (firewallRule != null) { client.getFirewallApi().deleteFirewallRule(firewallRule.getId()); } if (egressFirewallRule != null) { client.getFirewallApi().deleteEgressFirewallRule(egressFirewallRule.getId()); } if (portForwardingRule != null) { client.getFirewallApi().deletePortForwardingRule(portForwardingRule.getId()); } if (vm != null) { jobComplete.apply(client.getVirtualMachineApi().destroyVirtualMachine(vm.getId())); } if (ip != null) { client.getAddressApi().disassociateIPAddress(ip.getId()); } super.tearDownContext(); }
String accountInDomain = getConfig(ACCOUNT_IN_DOMAIN); Boolean openFirewall = getConfig(OPEN_FIREWALL); PublicIPAddress ip = client.getCloudstackGlobalClient().getAddressApi().getPublicIPAddress(publicIPId);