@Test public void testApply() throws UnknownHostException { final CloudStackApi client = createMock(CloudStackApi.class); SecurityGroupApi secClient = createMock(SecurityGroupApi.class); ZoneApi zoneClient = createMock(ZoneApi.class); AsyncJobApi jobClient = createMock(AsyncJobApi.class); SecurityGroup group = createMock(SecurityGroup.class); Zone zone = createMock(Zone.class); expect(group.getIngressRules()).andReturn(ImmutableSet.<IngressRule> of()); expect(group.getId()).andReturn("sec-1234").anyTimes(); expect(zone.isSecurityGroupsEnabled()).andReturn(true); expect(client.getSecurityGroupApi()).andReturn(secClient) .anyTimes(); expect(client.getZoneApi()).andReturn(zoneClient); expect(client.getAsyncJobApi()).andReturn(jobClient).anyTimes(); expect(zoneClient.getZone("zone-abc1")).andReturn(zone); expect(secClient.createSecurityGroup("group-1")).andReturn(group); expect(secClient.authorizeIngressPortsToCIDRs("sec-1234", "TCP", 22, 22, ImmutableSet.of("0.0.0.0/0"))).andReturn("job-1234"); replay(client, secClient, zoneClient, zone, group); ZoneSecurityGroupNamePortsCidrs input = ZoneSecurityGroupNamePortsCidrs.builder() .zone("zone-abc1") .name("group-1") .ports(ImmutableSet.of(22)) .cidrs(ImmutableSet.<String> of()).build(); CreateSecurityGroupIfNeeded parser = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(new TypeLiteral<Supplier<String>>() { }).toInstance(Suppliers.ofInstance("1")); bind(CloudStackApi.class).toInstance(client); bind(new TypeLiteral<CacheLoader<String, Zone>>() {}). to(ZoneIdToZone.class); bind(new TypeLiteral<Supplier<LoadingCache<String, Zone>>>() {}). to(ZoneIdToZoneSupplier.class); bind(String.class).annotatedWith(Names.named(PROPERTY_SESSION_INTERVAL)).toInstance("60"); } @Provides @Singleton protected Predicate<String> jobComplete(JobComplete jobComplete) { return retry(jobComplete, 1200, 1, 5, SECONDS); } }).getInstance(CreateSecurityGroupIfNeeded.class); assertEquals(parser.apply(input), group); verify(client, secClient, zoneClient, zone, group); }
@Override public SecurityGroup apply(ZoneSecurityGroupNamePortsCidrs input) { checkNotNull(input, "input"); String zoneId = input.getZone(); Zone zone = zoneIdToZone.get().getUnchecked(zoneId); checkArgument(supportsSecurityGroups().apply(zone), "Security groups are required, but the zone %s does not support security groups", zoneId); logger.debug(">> creating securityGroup %s", input); try { SecurityGroup securityGroup = client.getSecurityGroupApi().createSecurityGroup(input.getName()); logger.debug("<< created securityGroup(%s)", securityGroup); ImmutableSet<String> cidrs; if (!input.getCidrs().isEmpty()) { cidrs = ImmutableSet.copyOf(input.getCidrs()); } else { cidrs = ImmutableSet.of("0.0.0.0/0"); } for (int port : input.getPorts()) { authorizeGroupToItselfAndToTCPPortAndCidr(client, securityGroup, port, cidrs); } return securityGroup; } catch (IllegalStateException e) { logger.trace("<< trying to find securityGroup(%s): %s", input, e.getMessage()); SecurityGroup group = client.getSecurityGroupApi().getSecurityGroupByName(input.getName()); logger.debug("<< reused securityGroup(%s)", group.getId()); return group; } }
CreateSecurityGroupIfNeeded implements Function<ZoneSecurityGroupNamePortsCidrs, SecurityGroup> { @Override public SecurityGroup apply(ZoneSecurityGroupNamePortsCidrs input) { checkNotNull(input, "input"); String zoneId = input.getZone(); Zone zone = zoneIdToZone.get().getUnchecked(zoneId); checkArgument(supportsSecurityGroups().apply(zone), "Security groups are required, but the zone %s does not support security groups", zoneId); logger.debug(">> creating securityGroup %s", input); try { SecurityGroup securityGroup = client.getSecurityGroupApi().createSecurityGroup(input.getName()); logger.debug("<< created securityGroup(%s)", securityGroup); ImmutableSet<String> cidrs; if (!input.getCidrs().isEmpty()) { cidrs = ImmutableSet.copyOf(input.getCidrs()); } else { cidrs = ImmutableSet.of("0.0.0.0/0"); } for (int port : input.getPorts()) { authorizeGroupToItselfAndToTCPPortAndCidr(client, securityGroup, port, cidrs); } return securityGroup; } catch (IllegalStateException e) { logger.trace("<< trying to find securityGroup(%s): %s", input, e.getMessage()); SecurityGroup group = client.getSecurityGroupApi().getSecurityGroupByName(input.getName()); logger.debug("<< reused securityGroup(%s)", group.getId()); return group; } } }
CreateSecurityGroupIfNeeded implements Function<ZoneSecurityGroupNamePortsCidrs, SecurityGroup> { @Override public SecurityGroup apply(ZoneSecurityGroupNamePortsCidrs input) { checkNotNull(input, "input"); String zoneId = input.getZone(); Zone zone = zoneIdToZone.get().getUnchecked(zoneId); checkArgument(supportsSecurityGroups().apply(zone), "Security groups are required, but the zone %s does not support security groups", zoneId); logger.debug(">> creating securityGroup %s", input); try { SecurityGroup securityGroup = client.getSecurityGroupApi().createSecurityGroup(input.getName()); logger.debug("<< created securityGroup(%s)", securityGroup); ImmutableSet<String> cidrs; if (!input.getCidrs().isEmpty()) { cidrs = ImmutableSet.copyOf(input.getCidrs()); } else { cidrs = ImmutableSet.of("0.0.0.0/0"); } for (int port : input.getPorts()) { authorizeGroupToItselfAndToTCPPortAndCidr(client, securityGroup, port, cidrs); } return securityGroup; } catch (IllegalStateException e) { logger.trace("<< trying to find securityGroup(%s): %s", input, e.getMessage()); SecurityGroup group = client.getSecurityGroupApi().getSecurityGroupByName(input.getName()); logger.debug("<< reused securityGroup(%s)", group.getId()); return group; } } @Inject  CreateSecurityGroupIfNeeded(CloudStackApi client,
                                      Predicate<String> jobComplete,
                                      Supplier<LoadingCache<String, Zone>> zoneIdToZone); }
CreateSecurityGroupIfNeeded implements Function<ZoneSecurityGroupNamePortsCidrs, SecurityGroup> { @Override public SecurityGroup apply(ZoneSecurityGroupNamePortsCidrs input) { checkNotNull(input, "input"); String zoneId = input.getZone(); Zone zone = zoneIdToZone.get().getUnchecked(zoneId); checkArgument(supportsSecurityGroups().apply(zone), "Security groups are required, but the zone %s does not support security groups", zoneId); logger.debug(">> creating securityGroup %s", input); try { SecurityGroup securityGroup = client.getSecurityGroupApi().createSecurityGroup(input.getName()); logger.debug("<< created securityGroup(%s)", securityGroup); ImmutableSet<String> cidrs; if (!input.getCidrs().isEmpty()) { cidrs = ImmutableSet.copyOf(input.getCidrs()); } else { cidrs = ImmutableSet.of("0.0.0.0/0"); } for (int port : input.getPorts()) { authorizeGroupToItselfAndToTCPPortAndCidr(client, securityGroup, port, cidrs); } return securityGroup; } catch (IllegalStateException e) { logger.trace("<< trying to find securityGroup(%s): %s", input, e.getMessage()); SecurityGroup group = client.getSecurityGroupApi().getSecurityGroupByName(input.getName()); logger.debug("<< reused securityGroup(%s)", group.getId()); return group; } } @Inject  CreateSecurityGroupIfNeeded(CloudStackApi client,
                                      Predicate<String> jobComplete,
                                      Supplier<LoadingCache<String, Zone>> zoneIdToZone); @Override SecurityGroup apply(ZoneSecurityGroupNamePortsCidrs input); }
CreateSecurityGroupIfNeeded implements Function<ZoneSecurityGroupNamePortsCidrs, SecurityGroup> { @Override public SecurityGroup apply(ZoneSecurityGroupNamePortsCidrs input) { checkNotNull(input, "input"); String zoneId = input.getZone(); Zone zone = zoneIdToZone.get().getUnchecked(zoneId); checkArgument(supportsSecurityGroups().apply(zone), "Security groups are required, but the zone %s does not support security groups", zoneId); logger.debug(">> creating securityGroup %s", input); try { SecurityGroup securityGroup = client.getSecurityGroupApi().createSecurityGroup(input.getName()); logger.debug("<< created securityGroup(%s)", securityGroup); ImmutableSet<String> cidrs; if (!input.getCidrs().isEmpty()) { cidrs = ImmutableSet.copyOf(input.getCidrs()); } else { cidrs = ImmutableSet.of("0.0.0.0/0"); } for (int port : input.getPorts()) { authorizeGroupToItselfAndToTCPPortAndCidr(client, securityGroup, port, cidrs); } return securityGroup; } catch (IllegalStateException e) { logger.trace("<< trying to find securityGroup(%s): %s", input, e.getMessage()); SecurityGroup group = client.getSecurityGroupApi().getSecurityGroupByName(input.getName()); logger.debug("<< reused securityGroup(%s)", group.getId()); return group; } } @Inject  CreateSecurityGroupIfNeeded(CloudStackApi client,
                                      Predicate<String> jobComplete,
                                      Supplier<LoadingCache<String, Zone>> zoneIdToZone); @Override SecurityGroup apply(ZoneSecurityGroupNamePortsCidrs input);  }