@Test(expectedExceptions = IllegalArgumentException.class) public void testLoadZoneNoSecurityGroups() 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(zone.isSecurityGroupsEnabled()).andReturn(false); expect(client.getSecurityGroupApi()).andReturn(secClient) .anyTimes(); expect(client.getZoneApi()).andReturn(zoneClient); expect(client.getAsyncJobApi()).andReturn(jobClient).anyTimes(); expect(zoneClient.getZone("zone-1")).andReturn(zone); expect(secClient.getSecurityGroupByName("group-1")).andReturn(null); replay(client, secClient, zoneClient, zone, group); ZoneSecurityGroupNamePortsCidrs input = ZoneSecurityGroupNamePortsCidrs.builder() .zone("zone-1") .name("group-1") .ports(ImmutableSet.of(22)) .cidrs(ImmutableSet.<String> of()).build(); FindSecurityGroupOrCreate 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"); bind(new TypeLiteral<Function<ZoneSecurityGroupNamePortsCidrs, SecurityGroup>>() { }).to(CreateSecurityGroupIfNeeded.class); bind(new TypeLiteral<CacheLoader<ZoneAndName, SecurityGroup>>() { }).to(FindSecurityGroupOrCreate.class); } @Provides @Singleton protected Predicate<String> jobComplete(JobComplete jobComplete) { return retry(jobComplete, 1200, 1, 5, SECONDS); } }).getInstance(FindSecurityGroupOrCreate.class); assertEquals(parser.load(input), group); verify(client, secClient, zoneClient, zone, group); }
@Override public SecurityGroup load(ZoneAndName in) { SecurityGroup group = client.getSecurityGroupApi().getSecurityGroupByName(in.getName()); if (group != null) { return group; } else { return createNewSecurityGroup(in); } }
FindSecurityGroupOrCreate extends CacheLoader<ZoneAndName, SecurityGroup> { @Override public SecurityGroup load(ZoneAndName in) { SecurityGroup group = client.getSecurityGroupApi().getSecurityGroupByName(in.getName()); if (group != null) { return group; } else { return createNewSecurityGroup(in); } } }
FindSecurityGroupOrCreate extends CacheLoader<ZoneAndName, SecurityGroup> { @Override public SecurityGroup load(ZoneAndName in) { SecurityGroup group = client.getSecurityGroupApi().getSecurityGroupByName(in.getName()); if (group != null) { return group; } else { return createNewSecurityGroup(in); } } @Inject  FindSecurityGroupOrCreate(CloudStackApi client,
                                    Function<ZoneSecurityGroupNamePortsCidrs, SecurityGroup> groupCreator); }
FindSecurityGroupOrCreate extends CacheLoader<ZoneAndName, SecurityGroup> { @Override public SecurityGroup load(ZoneAndName in) { SecurityGroup group = client.getSecurityGroupApi().getSecurityGroupByName(in.getName()); if (group != null) { return group; } else { return createNewSecurityGroup(in); } } @Inject  FindSecurityGroupOrCreate(CloudStackApi client,
                                    Function<ZoneSecurityGroupNamePortsCidrs, SecurityGroup> groupCreator); @Override SecurityGroup load(ZoneAndName in); @Override String toString(); }
FindSecurityGroupOrCreate extends CacheLoader<ZoneAndName, SecurityGroup> { @Override public SecurityGroup load(ZoneAndName in) { SecurityGroup group = client.getSecurityGroupApi().getSecurityGroupByName(in.getName()); if (group != null) { return group; } else { return createNewSecurityGroup(in); } } @Inject  FindSecurityGroupOrCreate(CloudStackApi client,
                                    Function<ZoneSecurityGroupNamePortsCidrs, SecurityGroup> groupCreator); @Override SecurityGroup load(ZoneAndName in); @Override String toString();  }