@Test public void testPopulatePrecomputedMaps() { AwsConfiguration c = new AwsConfiguration(); Account account = c.addOrGetAccount("123"); String vpcId = "vpc"; String noInstanceZoneName = "zone1"; String instanceZoneName = "zone2"; String noInstanceSubnetId = "subnet1"; String instanceSubnetId = "subnet2"; String instanceId = "instance"; String lbArn = "lbArn"; String tgArn = "tgArn"; Vpc vpc = getTestVpc(vpcId); AvailabilityZone noInstanceZone = new AvailabilityZone(noInstanceSubnetId, noInstanceZoneName); AvailabilityZone instanceZone = new AvailabilityZone(instanceSubnetId, instanceZoneName); Prefix prefix1 = Prefix.parse("1.1.1.0/24"); Prefix prefix2 = Prefix.parse("2.2.2.0/24"); Subnet noInstanceSubnet = getTestSubnet(prefix1, noInstanceSubnetId, vpcId, noInstanceZoneName); Subnet instanceSubnet = getTestSubnet(prefix2, instanceSubnetId, vpcId, instanceZoneName); Instance instance = new Instance( instanceId, vpcId, instanceSubnetId, ImmutableList.of(), ImmutableList.of(), Ip.parse("3.3.3.3"), ImmutableMap.of(), Status.RUNNING); TargetGroup targetGroup = new TargetGroup( tgArn, ImmutableList.of(lbArn), Protocol.TCP, 80, "tgName", TargetGroup.Type.INSTANCE); TargetHealthDescription targetHealthDescription = new TargetHealthDescription( new LoadBalancerTarget(lbArn, instanceId, 80), new TargetHealth(HealthState.HEALTHY)); LoadBalancerTargetHealth targetHealth = new LoadBalancerTargetHealth("tgArn", ImmutableList.of(targetHealthDescription)); Region.RegionBuilder regionBuilder = Region.builder("region") .setInstances(ImmutableMap.of(instanceId, instance)) .setSubnets( ImmutableMap.of( noInstanceSubnetId, noInstanceSubnet, instanceSubnetId, instanceSubnet)) .setVpcs(ImmutableMap.of(vpcId, vpc)) .setLoadBalancerTargetHealths(ImmutableMap.of(tgArn, targetHealth)) .setTargetGroups(ImmutableMap.of(tgArn, targetGroup)); { LoadBalancer nlb = new LoadBalancer( lbArn, ImmutableList.of(instanceZone, noInstanceZone), "lbDnsName", "lbName", Scheme.INTERNET_FACING, Type.NETWORK, "vpc"); account.addRegion(regionBuilder.setLoadBalancers(ImmutableMap.of(lbArn, nlb)).build()); c.populatePrecomputedMaps(); assertThat( c.getSubnetsToInstanceTargets(), equalTo(ImmutableMultimap.of(instanceSubnetId, instance))); assertThat( c.getSubnetsToNlbs(), equalTo(ImmutableMultimap.of(noInstanceSubnetId, nlb, instanceSubnetId, nlb))); assertThat(c.getNlbsToInstanceTargets(), equalTo(ImmutableMultimap.of(lbArn, instance))); } { LoadBalancer alb = new LoadBalancer( lbArn, ImmutableList.of(instanceZone, noInstanceZone), "lbDnsName", "lbName", Scheme.INTERNET_FACING, Type.APPLICATION, "vpc"); account.addRegion(regionBuilder.setLoadBalancers(ImmutableMap.of(lbArn, alb)).build()); c.populatePrecomputedMaps(); assertTrue(c.getSubnetsToInstanceTargets().isEmpty()); assertTrue(c.getSubnetsToNlbs().isEmpty()); assertTrue(c.getNlbsToInstanceTargets().isEmpty()); } }
@VisibleForTesting void populatePrecomputedMaps() { ImmutableMultimap.Builder<String, Instance> subnetsToInstanceTargets = ImmutableMultimap.builder(); ImmutableMultimap.Builder<String, LoadBalancer> subnetsToNlbs = ImmutableMultimap.builder(); ImmutableMultimap.Builder<String, Instance> nlbsToInstanceTargets = ImmutableMultimap.builder(); for (Account account : getAccounts()) { Collection<Region> regions = account.getRegions(); for (Region region : regions) { for (Entry<String, TargetGroup> targetEntry : region.getTargetGroups().entrySet()) { TargetGroup targetGroup = targetEntry.getValue(); if (targetGroup.getTargetType() != Type.INSTANCE) { continue; } String targetGroupArn = targetEntry.getKey(); LoadBalancerTargetHealth lbth = region.getLoadBalancerTargetHealth(targetGroupArn); if (lbth == null) { continue; } for (String lbArn : targetGroup.getLoadBalancerArns()) { LoadBalancer lb = region.getLoadBalancersMap().get(lbArn); if (lb.getType() == LoadBalancer.Type.APPLICATION) { continue; } Set<String> azNames = lb.getAvailabilityZones().stream() .map(AvailabilityZone::getZoneName) .collect(ImmutableSet.toImmutableSet()); AtomicBoolean hasInstanceTarget = new AtomicBoolean(false); getActiveTargets(lbth, targetGroup, azNames, region, false, null).stream() .filter(desc -> getTargetIp(desc.getTarget(), Type.INSTANCE, region) != null) .map(desc -> region.getInstances().get(desc.getTarget().getId())) .forEach( instance -> { assert instance.getSubnetId() != null; subnetsToInstanceTargets.put(instance.getSubnetId(), instance); nlbsToInstanceTargets.put(lbArn, instance); hasInstanceTarget.set(true); }); if (hasInstanceTarget.get()) { lb.getAvailabilityZones().stream() .map(AvailabilityZone::getSubnetId) .filter(region.getSubnets()::containsKey) .forEach(subnetId -> subnetsToNlbs.put(subnetId, lb)); } } } } } _subnetsToInstanceTargets = subnetsToInstanceTargets.build(); _subnetsToNlbs = subnetsToNlbs.build(); _nlbsToInstanceTargets = nlbsToInstanceTargets.build(); }
AwsConfiguration extends VendorConfiguration { @VisibleForTesting void populatePrecomputedMaps() { ImmutableMultimap.Builder<String, Instance> subnetsToInstanceTargets = ImmutableMultimap.builder(); ImmutableMultimap.Builder<String, LoadBalancer> subnetsToNlbs = ImmutableMultimap.builder(); ImmutableMultimap.Builder<String, Instance> nlbsToInstanceTargets = ImmutableMultimap.builder(); for (Account account : getAccounts()) { Collection<Region> regions = account.getRegions(); for (Region region : regions) { for (Entry<String, TargetGroup> targetEntry : region.getTargetGroups().entrySet()) { TargetGroup targetGroup = targetEntry.getValue(); if (targetGroup.getTargetType() != Type.INSTANCE) { continue; } String targetGroupArn = targetEntry.getKey(); LoadBalancerTargetHealth lbth = region.getLoadBalancerTargetHealth(targetGroupArn); if (lbth == null) { continue; } for (String lbArn : targetGroup.getLoadBalancerArns()) { LoadBalancer lb = region.getLoadBalancersMap().get(lbArn); if (lb.getType() == LoadBalancer.Type.APPLICATION) { continue; } Set<String> azNames = lb.getAvailabilityZones().stream() .map(AvailabilityZone::getZoneName) .collect(ImmutableSet.toImmutableSet()); AtomicBoolean hasInstanceTarget = new AtomicBoolean(false); getActiveTargets(lbth, targetGroup, azNames, region, false, null).stream() .filter(desc -> getTargetIp(desc.getTarget(), Type.INSTANCE, region) != null) .map(desc -> region.getInstances().get(desc.getTarget().getId())) .forEach( instance -> { assert instance.getSubnetId() != null; subnetsToInstanceTargets.put(instance.getSubnetId(), instance); nlbsToInstanceTargets.put(lbArn, instance); hasInstanceTarget.set(true); }); if (hasInstanceTarget.get()) { lb.getAvailabilityZones().stream() .map(AvailabilityZone::getSubnetId) .filter(region.getSubnets()::containsKey) .forEach(subnetId -> subnetsToNlbs.put(subnetId, lb)); } } } } } _subnetsToInstanceTargets = subnetsToInstanceTargets.build(); _subnetsToNlbs = subnetsToNlbs.build(); _nlbsToInstanceTargets = nlbsToInstanceTargets.build(); } }
AwsConfiguration extends VendorConfiguration { @VisibleForTesting void populatePrecomputedMaps() { ImmutableMultimap.Builder<String, Instance> subnetsToInstanceTargets = ImmutableMultimap.builder(); ImmutableMultimap.Builder<String, LoadBalancer> subnetsToNlbs = ImmutableMultimap.builder(); ImmutableMultimap.Builder<String, Instance> nlbsToInstanceTargets = ImmutableMultimap.builder(); for (Account account : getAccounts()) { Collection<Region> regions = account.getRegions(); for (Region region : regions) { for (Entry<String, TargetGroup> targetEntry : region.getTargetGroups().entrySet()) { TargetGroup targetGroup = targetEntry.getValue(); if (targetGroup.getTargetType() != Type.INSTANCE) { continue; } String targetGroupArn = targetEntry.getKey(); LoadBalancerTargetHealth lbth = region.getLoadBalancerTargetHealth(targetGroupArn); if (lbth == null) { continue; } for (String lbArn : targetGroup.getLoadBalancerArns()) { LoadBalancer lb = region.getLoadBalancersMap().get(lbArn); if (lb.getType() == LoadBalancer.Type.APPLICATION) { continue; } Set<String> azNames = lb.getAvailabilityZones().stream() .map(AvailabilityZone::getZoneName) .collect(ImmutableSet.toImmutableSet()); AtomicBoolean hasInstanceTarget = new AtomicBoolean(false); getActiveTargets(lbth, targetGroup, azNames, region, false, null).stream() .filter(desc -> getTargetIp(desc.getTarget(), Type.INSTANCE, region) != null) .map(desc -> region.getInstances().get(desc.getTarget().getId())) .forEach( instance -> { assert instance.getSubnetId() != null; subnetsToInstanceTargets.put(instance.getSubnetId(), instance); nlbsToInstanceTargets.put(lbArn, instance); hasInstanceTarget.set(true); }); if (hasInstanceTarget.get()) { lb.getAvailabilityZones().stream() .map(AvailabilityZone::getSubnetId) .filter(region.getSubnets()::containsKey) .forEach(subnetId -> subnetsToNlbs.put(subnetId, lb)); } } } } } _subnetsToInstanceTargets = subnetsToInstanceTargets.build(); _subnetsToNlbs = subnetsToNlbs.build(); _nlbsToInstanceTargets = nlbsToInstanceTargets.build(); } AwsConfiguration(); private  AwsConfiguration(Map<String, Account> accounts); }
AwsConfiguration extends VendorConfiguration { @VisibleForTesting void populatePrecomputedMaps() { ImmutableMultimap.Builder<String, Instance> subnetsToInstanceTargets = ImmutableMultimap.builder(); ImmutableMultimap.Builder<String, LoadBalancer> subnetsToNlbs = ImmutableMultimap.builder(); ImmutableMultimap.Builder<String, Instance> nlbsToInstanceTargets = ImmutableMultimap.builder(); for (Account account : getAccounts()) { Collection<Region> regions = account.getRegions(); for (Region region : regions) { for (Entry<String, TargetGroup> targetEntry : region.getTargetGroups().entrySet()) { TargetGroup targetGroup = targetEntry.getValue(); if (targetGroup.getTargetType() != Type.INSTANCE) { continue; } String targetGroupArn = targetEntry.getKey(); LoadBalancerTargetHealth lbth = region.getLoadBalancerTargetHealth(targetGroupArn); if (lbth == null) { continue; } for (String lbArn : targetGroup.getLoadBalancerArns()) { LoadBalancer lb = region.getLoadBalancersMap().get(lbArn); if (lb.getType() == LoadBalancer.Type.APPLICATION) { continue; } Set<String> azNames = lb.getAvailabilityZones().stream() .map(AvailabilityZone::getZoneName) .collect(ImmutableSet.toImmutableSet()); AtomicBoolean hasInstanceTarget = new AtomicBoolean(false); getActiveTargets(lbth, targetGroup, azNames, region, false, null).stream() .filter(desc -> getTargetIp(desc.getTarget(), Type.INSTANCE, region) != null) .map(desc -> region.getInstances().get(desc.getTarget().getId())) .forEach( instance -> { assert instance.getSubnetId() != null; subnetsToInstanceTargets.put(instance.getSubnetId(), instance); nlbsToInstanceTargets.put(lbArn, instance); hasInstanceTarget.set(true); }); if (hasInstanceTarget.get()) { lb.getAvailabilityZones().stream() .map(AvailabilityZone::getSubnetId) .filter(region.getSubnets()::containsKey) .forEach(subnetId -> subnetsToNlbs.put(subnetId, lb)); } } } } } _subnetsToInstanceTargets = subnetsToInstanceTargets.build(); _subnetsToNlbs = subnetsToNlbs.build(); _nlbsToInstanceTargets = nlbsToInstanceTargets.build(); } AwsConfiguration(); private  AwsConfiguration(Map<String, Account> accounts); Collection<Account> getAccounts(); @Nonnull Stream<Vpc> getAllVpc(); @Nullable Vpc getVpc(String vpcId); @Nonnull Account addOrGetAccount(String accountId); void addConfigElement(
      String region,
      JsonNode json,
      String sourceFileName,
      ParseVendorConfigurationAnswerElement pvcae,
      String account); @Nonnull @Override List<Configuration> toVendorIndependentConfigurations(); @Override @Nonnull IspConfiguration getIspConfiguration(); @Override String getFilename(); @Override String getHostname(); @Override @Nonnull Set<Layer1Edge> getLayer1Edges(); @Override void setHostname(String hostname); @Override void setVendor(ConfigurationFormat format); static String vpnExternalInterfaceName(String tunnelId); static String vpnTunnelId(String vpnConnectionId, int idNum); static String vpnInterfaceName(String tunnelId); }
AwsConfiguration extends VendorConfiguration { @VisibleForTesting void populatePrecomputedMaps() { ImmutableMultimap.Builder<String, Instance> subnetsToInstanceTargets = ImmutableMultimap.builder(); ImmutableMultimap.Builder<String, LoadBalancer> subnetsToNlbs = ImmutableMultimap.builder(); ImmutableMultimap.Builder<String, Instance> nlbsToInstanceTargets = ImmutableMultimap.builder(); for (Account account : getAccounts()) { Collection<Region> regions = account.getRegions(); for (Region region : regions) { for (Entry<String, TargetGroup> targetEntry : region.getTargetGroups().entrySet()) { TargetGroup targetGroup = targetEntry.getValue(); if (targetGroup.getTargetType() != Type.INSTANCE) { continue; } String targetGroupArn = targetEntry.getKey(); LoadBalancerTargetHealth lbth = region.getLoadBalancerTargetHealth(targetGroupArn); if (lbth == null) { continue; } for (String lbArn : targetGroup.getLoadBalancerArns()) { LoadBalancer lb = region.getLoadBalancersMap().get(lbArn); if (lb.getType() == LoadBalancer.Type.APPLICATION) { continue; } Set<String> azNames = lb.getAvailabilityZones().stream() .map(AvailabilityZone::getZoneName) .collect(ImmutableSet.toImmutableSet()); AtomicBoolean hasInstanceTarget = new AtomicBoolean(false); getActiveTargets(lbth, targetGroup, azNames, region, false, null).stream() .filter(desc -> getTargetIp(desc.getTarget(), Type.INSTANCE, region) != null) .map(desc -> region.getInstances().get(desc.getTarget().getId())) .forEach( instance -> { assert instance.getSubnetId() != null; subnetsToInstanceTargets.put(instance.getSubnetId(), instance); nlbsToInstanceTargets.put(lbArn, instance); hasInstanceTarget.set(true); }); if (hasInstanceTarget.get()) { lb.getAvailabilityZones().stream() .map(AvailabilityZone::getSubnetId) .filter(region.getSubnets()::containsKey) .forEach(subnetId -> subnetsToNlbs.put(subnetId, lb)); } } } } } _subnetsToInstanceTargets = subnetsToInstanceTargets.build(); _subnetsToNlbs = subnetsToNlbs.build(); _nlbsToInstanceTargets = nlbsToInstanceTargets.build(); } AwsConfiguration(); private  AwsConfiguration(Map<String, Account> accounts); Collection<Account> getAccounts(); @Nonnull Stream<Vpc> getAllVpc(); @Nullable Vpc getVpc(String vpcId); @Nonnull Account addOrGetAccount(String accountId); void addConfigElement(
      String region,
      JsonNode json,
      String sourceFileName,
      ParseVendorConfigurationAnswerElement pvcae,
      String account); @Nonnull @Override List<Configuration> toVendorIndependentConfigurations(); @Override @Nonnull IspConfiguration getIspConfiguration(); @Override String getFilename(); @Override String getHostname(); @Override @Nonnull Set<Layer1Edge> getLayer1Edges(); @Override void setHostname(String hostname); @Override void setVendor(ConfigurationFormat format); static String vpnExternalInterfaceName(String tunnelId); static String vpnTunnelId(String vpnConnectionId, int idNum); static String vpnInterfaceName(String tunnelId); static final String DEFAULT_REGION_NAME; static final String DEFAULT_ACCOUNT_NAME; static final String AWS_BACKBONE_HOSTNAME; }