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 testApply() { String id = "1"; String jobId = "2"; CloudStackClient client = createMock(CloudStackClient.class); Predicate<String> jobComplete = Predicates.alwaysTrue(); AsyncJobClient jobClient = createMock(AsyncJobClient.class); expect(client.getAsyncJobClient()).andReturn(jobClient).atLeastOnce(); expect(jobClient.getAsyncJob(jobId)).andReturn(AsyncJob.builder().id(jobId).result("foo").build()).atLeastOnce(); replay(client); replay(jobClient); assertEquals( new BlockUntilJobCompletesAndReturnResult(client, jobComplete).<String>apply(AsyncCreateResponse.builder().id(id).jobId( jobId).build()), "foo"); verify(client); verify(jobClient); }
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); }
public void testApply() { String id = "1"; String jobId = "2"; CloudStackApi client = createMock(CloudStackApi.class); Predicate<String> jobComplete = Predicates.alwaysTrue(); AsyncJobApi jobClient = createMock(AsyncJobApi.class); expect(client.getAsyncJobApi()).andReturn(jobClient).atLeastOnce(); expect(jobClient.getAsyncJob(jobId)).andReturn(AsyncJob.builder().id(jobId).result("foo").build()).atLeastOnce(); replay(client); replay(jobClient); assertEquals( new BlockUntilJobCompletesAndReturnResult(client, jobComplete).<String>apply(AsyncCreateResponse.builder().id(id).jobId( jobId).build()), "foo"); verify(client); verify(jobClient); }
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 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.getNATClient().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); }
@Test(expectedExceptions = IllegalStateException.class) public void testJobDoesntCompleteThrowsIllegalStateException() { String id = "1"; String jobId = "2"; CloudStackClient client = createMock(CloudStackClient.class); // the alwaysfalse predicate should blow up with IllegalStateException Predicate<String> jobComplete = Predicates.alwaysFalse(); AsyncJobClient jobClient = createMock(AsyncJobClient.class); expect(client.getAsyncJobClient()).andReturn(jobClient).atLeastOnce(); expect(jobClient.getAsyncJob(jobId)).andReturn(AsyncJob.builder().id(jobId).result("foo").build()).atLeastOnce(); replay(client); replay(jobClient); assertEquals( new BlockUntilJobCompletesAndReturnResult(client, jobComplete).<String>apply( AsyncCreateResponse.builder().id(id).jobId(jobId).build()), "foo"); verify(client); verify(jobClient); }
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(); } }
adminVirtualMachineDestroyed = retry(new VirtualMachineDestroyed(adminClient), 600, 5, 5, SECONDS); injector.injectMembers(adminVirtualMachineDestroyed); reuseOrAssociate = new ReuseOrAssociateNewPublicIPAddress(client, new BlockUntilJobCompletesAndReturnResult( client, jobComplete)); injector.injectMembers(reuseOrAssociate);
@Test(expectedExceptions = IllegalStateException.class) public void testJobDoesntCompleteThrowsIllegalStateException() { String id = "1"; String jobId = "2"; CloudStackApi client = createMock(CloudStackApi.class); // the alwaysfalse predicate should blow up with IllegalStateException Predicate<String> jobComplete = Predicates.alwaysFalse(); AsyncJobApi jobClient = createMock(AsyncJobApi.class); expect(client.getAsyncJobApi()).andReturn(jobClient).atLeastOnce(); expect(jobClient.getAsyncJob(jobId)).andReturn(AsyncJob.builder().id(jobId).result("foo").build()).atLeastOnce(); replay(client); replay(jobClient); assertEquals( new BlockUntilJobCompletesAndReturnResult(client, jobComplete).<String>apply( AsyncCreateResponse.builder().id(id).jobId(jobId).build()), "foo"); verify(client); verify(jobClient); }
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; }
@Test(expectedExceptions = UncheckedExecutionException.class) public void testJobWithErrorThrowsUncheckedExecutionException() { String id = "1"; String jobId = "2"; CloudStackClient client = createMock(CloudStackClient.class); Predicate<String> jobComplete = Predicates.alwaysTrue(); AsyncJobClient jobClient = createMock(AsyncJobClient.class); expect(client.getAsyncJobClient()).andReturn(jobClient).atLeastOnce(); expect(jobClient.getAsyncJob(jobId)).andReturn( AsyncJob.builder().id(jobId) .error(AsyncJobError.builder().errorCode(ErrorCode.INTERNAL_ERROR).errorText("ERRROR").build()) .result("foo").build()) .atLeastOnce(); replay(client); replay(jobClient); assertEquals( new BlockUntilJobCompletesAndReturnResult(client, jobComplete).<String>apply( AsyncCreateResponse.builder().id(id).jobId(jobId).build()), "foo"); verify(client); verify(jobClient); } }
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; }
@Test(expectedExceptions = UncheckedExecutionException.class) public void testJobWithErrorThrowsUncheckedExecutionException() { String id = "1"; String jobId = "2"; CloudStackApi client = createMock(CloudStackApi.class); Predicate<String> jobComplete = Predicates.alwaysTrue(); AsyncJobApi jobClient = createMock(AsyncJobApi.class); expect(client.getAsyncJobApi()).andReturn(jobClient).atLeastOnce(); expect(jobClient.getAsyncJob(jobId)).andReturn( AsyncJob.builder().id(jobId) .error(AsyncJobError.builder().errorCode(ErrorCode.INTERNAL_ERROR).errorText("ERRROR").build()) .result("foo").build()) .atLeastOnce(); replay(client); replay(jobClient); assertEquals( new BlockUntilJobCompletesAndReturnResult(client, jobComplete).<String>apply( AsyncCreateResponse.builder().id(id).jobId(jobId).build()), "foo"); verify(client); verify(jobClient); } }
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 Set<FirewallRule> 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.<FirewallRule> of(); Builder<AsyncCreateResponse> responses = ImmutableSet.builder(); for (int port : ports) { AsyncCreateResponse response = client.getFirewallClient().createFirewallRuleForIpAndProtocol(ip.getId(), FirewallRule.Protocol.fromValue(protocol), CreateFirewallRuleOptions.Builder.startPort(port).endPort(port)); logger.debug(">> creating firewall rule IPAddress(%s) for protocol(%s), port(%s); response(%s)", ip.getId(), protocol, port, response); responses.add(response); } Builder<FirewallRule> rules = ImmutableSet.builder(); for (AsyncCreateResponse response : responses.build()) { FirewallRule rule = blockUntilJobCompletesAndReturnResult.<FirewallRule> apply(response); rules.add(rule); getFirewallRulesByVirtualMachine.asMap().put(ip.getVirtualMachineId(), ImmutableSet.of(rule)); } return rules.build(); } }
public Set<FirewallRule> 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.<FirewallRule> of(); Builder<AsyncCreateResponse> responses = ImmutableSet.builder(); for (int port : ports) { AsyncCreateResponse response = client.getFirewallClient().createFirewallRuleForIpAndProtocol(ip.getId(), FirewallRule.Protocol.fromValue(protocol), CreateFirewallRuleOptions.Builder.startPort(port).endPort(port)); logger.debug(">> creating firewall rule IPAddress(%s) for protocol(%s), port(%s); response(%s)", ip.getId(), protocol, port, response); responses.add(response); } Builder<FirewallRule> rules = ImmutableSet.builder(); for (AsyncCreateResponse response : responses.build()) { FirewallRule rule = blockUntilJobCompletesAndReturnResult.<FirewallRule> apply(response); rules.add(rule); getFirewallRulesByVirtualMachine.asMap().put(ip.getVirtualMachineId(), ImmutableSet.of(rule)); } return rules.build(); } }
public Set<FirewallRule> 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.<FirewallRule> of(); Builder<AsyncCreateResponse> responses = ImmutableSet.builder(); for (int port : ports) { AsyncCreateResponse response = client.getFirewallApi().createFirewallRuleForIpAndProtocol(ip.getId(), FirewallRule.Protocol.fromValue(protocol), CreateFirewallRuleOptions.Builder.startPort(port).endPort(port)); logger.debug(">> creating firewall rule IPAddress(%s) for protocol(%s), port(%s); response(%s)", ip.getId(), protocol, port, response); responses.add(response); } Builder<FirewallRule> rules = ImmutableSet.builder(); for (AsyncCreateResponse response : responses.build()) { FirewallRule rule = blockUntilJobCompletesAndReturnResult.<FirewallRule> apply(response); rules.add(rule); getFirewallRulesByVirtualMachine.asMap().put(ip.getVirtualMachineId(), ImmutableSet.of(rule)); } return rules.build(); } }
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); }
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); }