public Set<IPForwardingRule> apply(PublicIPAddress ip, String protocol, Iterable<Integer> ports) { checkState(ip.getVirtualMachineId() != null, "ip %s should be static NATed to a virtual machine before applying rules", ip); if (Iterables.size(ports) == 0) return ImmutableSet.<IPForwardingRule> of(); Builder<AsyncCreateResponse> responses = ImmutableSet.builder(); for (int port : ports) { AsyncCreateResponse response = client.getNATClient().createIPForwardingRule(ip.getId(), protocol, port); logger.debug(">> creating IP forwarding rule IPAddress(%s) for protocol(%s), port(%s); response(%s)", ip.getId(), protocol, port, response); responses.add(response); } Builder<IPForwardingRule> rules = ImmutableSet.builder(); for (AsyncCreateResponse response : responses.build()) { IPForwardingRule rule = blockUntilJobCompletesAndReturnResult.<IPForwardingRule> apply(response); rules.add(rule); getIPForwardingRulesByVirtualMachine.asMap().put(ip.getVirtualMachineId(), ImmutableSet.of(rule)); } return rules.build(); } }
public void apply(Entity source, Object valueIgnored) { Location targetVm = Iterables.getOnlyElement(serviceToOpen.getLocations(), null); if (targetVm==null) { log.warn("Skipping port forward rule for "+serviceToOpen+" because it does not have a location"); return; } if (isSubnetEnabled()) { PublicIPAddress ip = systemCreatePublicIpHostname(); serviceToOpen.sensors().set(sensorAdvertisingHostname, ip.getIPAddress()); boolean success = systemEnableStaticNat(ip.getId(), targetVm); if (success) { log.debug("Enabled static NAT via to {} via {} (VM {})", new Object[] {serviceToOpen, ip, targetVm}); } } } }
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 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); } }
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; } }
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; } }
@Override public PublicIPAddress apply(Network input) { try { logger.debug(">> looking for existing address in network(%s)", input.getId()); PublicIPAddress returnVal = findAvailableAndAssociatedWithNetwork(input.getId(), client.getAddressClient()); logger.debug("<< reused address(%s)", returnVal.getId()); return returnVal; } catch (NoSuchElementException e) { logger.debug(">> associating new address in network(%s)", input.getId()); PublicIPAddress returnVal = associateIPAddressInNetwork(input, client, blockUntilJobCompletesAndReturnResult); logger.debug("<< associated address(%s)", returnVal.getId()); return returnVal; } } }
@Override public PublicIPAddress apply(Network input) { try { logger.debug(">> looking for existing address in network(%s)", input.getId()); PublicIPAddress returnVal = findAvailableAndAssociatedWithNetwork(input.getId(), client.getAddressClient()); logger.debug("<< reused address(%s)", returnVal.getId()); return returnVal; } catch (NoSuchElementException e) { logger.debug(">> associating new address in network(%s)", input.getId()); PublicIPAddress returnVal = associateIPAddressInNetwork(input, client, blockUntilJobCompletesAndReturnResult); logger.debug("<< associated address(%s)", returnVal.getId()); return returnVal; } } }
@Override public PublicIPAddress apply(Network input) { try { logger.debug(">> looking for existing address in network(%s)", input.getId()); PublicIPAddress returnVal = findAvailableAndAssociatedWithNetwork(input.getId(), client.getAddressApi()); logger.debug("<< reused address(%s)", returnVal.getId()); return returnVal; } catch (NoSuchElementException e) { logger.debug(">> associating new address in network(%s)", input.getId()); PublicIPAddress returnVal = associateIPAddressInNetwork(input, client, blockUntilJobCompletesAndReturnResult); logger.debug("<< associated address(%s)", returnVal.getId()); return returnVal; } } }
protected void systemCreatePublicIpHostnameForForwarding() { PublicIPAddress ip = systemCreatePublicIpHostname(); sensors().set(PUBLIC_HOSTNAME, ip.getIPAddress()); sensors().set(PUBLIC_HOSTNAME_IP_ADDRESS_ID, ip.getId()); }
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(); }
@Test(dependsOnMethods = "testCreatePortForwardingRule") public void testCreateFirewallRule() { if (networksDisabled) return; AsyncCreateResponse job = client.getFirewallClient().createFirewallRuleForIpAndProtocol( ip.getId(), FirewallRule.Protocol.TCP, CreateFirewallRuleOptions.Builder.startPort(30).endPort(35)); assertTrue(jobComplete.apply(job.getJobId())); firewallRule = client.getFirewallClient().getFirewallRule(job.getId()); assertEquals(firewallRule.getStartPort(), 30); assertEquals(firewallRule.getEndPort(), 35); assertEquals(firewallRule.getProtocol(), FirewallRule.Protocol.TCP); checkFirewallRule(firewallRule); }
@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(); }
@AfterGroups(groups = "live") @Override protected void tearDownContext() { if (ip != null) { client.getAddressApi().disassociateIPAddress(ip.getId()); } super.tearDownContext(); }
@Test(dependsOnMethods = "testCreatePortForwardingRule") public void testCreateFirewallRule() { if (networksDisabled) return; AsyncCreateResponse job = client.getFirewallApi().createFirewallRuleForIpAndProtocol( ip.getId(), FirewallRule.Protocol.TCP, CreateFirewallRuleOptions.Builder.startPort(30).endPort(35)); assertTrue(jobComplete.apply(job.getJobId())); firewallRule = client.getFirewallApi().getFirewallRule(job.getId()); assertEquals(firewallRule.getStartPort(), 30); assertEquals(firewallRule.getEndPort(), 35); assertEquals(firewallRule.getProtocol(), FirewallRule.Protocol.TCP); checkFirewallRule(firewallRule); }
@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(); }
@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 (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(); }
@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(); }