public Set<String> deleteIPForwardingRulesForVMAndReturnDistinctIPs(String virtualMachineId) { Builder<String> jobsToTrack = ImmutableSet.builder(); // immutable doesn't permit duplicates Set<String> ipAddresses = Sets.newLinkedHashSet(); Set<IPForwardingRule> forwardingRules = client.getNATApi().getIPForwardingRulesForVirtualMachine( virtualMachineId); for (IPForwardingRule rule : forwardingRules) { if (!"Deleting".equals(rule.getState())) { ipAddresses.add(rule.getIPAddressId()); String deleteForwardingRule = client.getNATApi().deleteIPForwardingRule(rule.getId()); if (deleteForwardingRule != null) { logger.debug(">> deleting IPForwardingRule(%s) job(%s)", rule.getId(), deleteForwardingRule); jobsToTrack.add(deleteForwardingRule); } } } awaitCompletion(jobsToTrack.build()); return ipAddresses; }
protected boolean systemEnableStaticNat(String publicIpId, Location targetVm) { String targetVmId = targetVm.getConfig(LegacyJcloudsCloudstackSubnetLocation.VM_IDENTIFIER); if (targetVmId==null) { log.warn("Skipping enabling of static nat for "+targetVm+" because location does not have an identifier set"); // throw? return false; } try { cloudstackClient.getNATClient().enableStaticNATForVirtualMachine(targetVmId, publicIpId); } catch (Exception e) { log.error("Failed creating firewall rule on "+this+" to "+targetVmId+": "+e); // it might already be created, so don't crash and burn too hard! return false; } return true; }
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.isEmpty(ports)) return ImmutableSet.<IPForwardingRule> of(); Builder<AsyncCreateResponse> responses = ImmutableSet.builder(); for (int port : ports) { AsyncCreateResponse response = client.getNATApi().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(); } }
/** * @throws org.jclouds.rest.ResourceNotFoundException * when there is no ip forwarding rule available for the VM */ @Override public Set<IPForwardingRule> load(String input) { return client.getNATApi().getIPForwardingRulesForVirtualMachine(input); } }
@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(); }
public void disableStaticNATOnIPAddresses(Set<String> ipAddresses) { Builder<String> jobsToTrack = ImmutableSet.builder(); for (String ipAddress : ipAddresses) { String disableStaticNAT = client.getNATApi().disableStaticNATOnPublicIP(ipAddress); if (disableStaticNAT != null) { logger.debug(">> disabling static NAT IPAddress(%s) job(%s)", ipAddress, disableStaticNAT); jobsToTrack.add(disableStaticNAT); } } awaitCompletion(jobsToTrack.build()); }
@Test(enabled = false) // takes too long public void testListIPForwardingRules() throws Exception { Set<IPForwardingRule> response = client.getNATApi().listIPForwardingRules(); assert null != response; assertTrue(response.size() > 0); for (IPForwardingRule rule : response) { IPForwardingRule newDetails = getOnlyElement(client.getNATApi().listIPForwardingRules( ListIPForwardingRulesOptions.Builder.id(rule.getId()))); assertEquals(rule.getId(), newDetails.getId()); checkRule(rule); } }
public void testCreateIPForwardingRule() throws Exception { if (networksDisabled) return; BlockUntilJobCompletesAndReturnResult blocker = new BlockUntilJobCompletesAndReturnResult(client, jobComplete); StaticNATVirtualMachineInNetwork fn = new StaticNATVirtualMachineInNetwork(client, reuseOrAssociate, network); CreatePortForwardingRulesForIP createPortForwardingRulesForIP = new CreatePortForwardingRulesForIP(client, blocker, CacheBuilder.newBuilder().<String, Set<IPForwardingRule>> build( new GetIPForwardingRulesByVirtualMachine(client))); // logger injector.injectMembers(blocker); injector.injectMembers(fn); injector.injectMembers(createPortForwardingRulesForIP); ip = fn.apply(vm); createPortForwardingRulesForIP.apply(ip, ImmutableSet.of(22)); rule = getOnlyElement(filter(client.getNATApi().getIPForwardingRulesForIPAddress(ip.getId()), new Predicate<IPForwardingRule>() { @Override public boolean apply(IPForwardingRule rule) { return rule != null && rule.getStartPort() == 22; } })); assertEquals(rule.getIPAddressId(), ip.getId()); assertEquals(rule.getVirtualMachineId(), vm.getId()); assertEquals(rule.getStartPort(), 22); assertEquals(rule.getProtocol(), "tcp"); checkRule(rule); HostAndPort socket = HostAndPort.fromParts(ip.getIPAddress(), 22); checkSSH(socket); }
protected void checkRule(IPForwardingRule rule) { assertEquals(rule.getId(), client.getNATApi().getIPForwardingRule(rule.getId()).getId()); assert rule.getId() != null : rule; assert rule.getIPAddress() != null : rule; assert rule.getIPAddressId() != null : rule; assert rule.getStartPort() > 0 : rule; assert rule.getProtocol() != null : rule; assert rule.getEndPort() > 0 : rule; assert rule.getState() != null : rule; assert rule.getVirtualMachineId() != null : rule; assert rule.getVirtualMachineName() != null : rule; } }
@Test public void testCreateNodeWithGroupEncodedIntoName() { String group = prefix + "-foo"; String name = group + "-node-" + new Random().nextInt(); Template template = view.getComputeService().templateBuilder().build(); if (!client .getTemplateApi() .getTemplateInZone(template.getImage().getId(), template.getLocation().getId()).isPasswordEnabled()) { // TODO: look at SecurityGroupApiLiveTest for how to do this template.getOptions().as(CloudStackTemplateOptions.class).keyPair(keyPairName); } vm = adapter.createNodeWithGroupEncodedIntoName(group, name, template); assertEquals(vm.getNode().getDisplayName(), name); // check to see if we setup a NAT rule (conceding we could check this from // cache) IPForwardingRule rule = getFirst( client.getNATApi().getIPForwardingRulesForVirtualMachine(vm.getNode().getId()), null); String address = rule != null ? rule.getIPAddress() : vm.getNode().getIPAddress(); loginCredentials = prioritizeCredentialsFromTemplate.apply(template, vm.getCredentials()); assert InetAddresses.isInetAddress(address) : vm; HostAndPort socket = HostAndPort.fromParts(address, 22); checkSSH(socket); }
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; } }