@Test public void testGetActiveTargets() { TargetHealthDescription unhealthyTarget = new TargetHealthDescription( new LoadBalancerTarget("zone1", "6.6.6.6", 80), new TargetHealth(HealthState.UNHEALTHY)); TargetHealthDescription healthyTarget1 = new TargetHealthDescription( new LoadBalancerTarget("zone1", "1.1.1.1", 80), new TargetHealth(HealthState.HEALTHY)); TargetHealthDescription healthyTarget2 = new TargetHealthDescription( new LoadBalancerTarget("zone2", "2.2.2.2", 80), new TargetHealth(HealthState.HEALTHY)); TargetHealthDescription healthyTargetAll = new TargetHealthDescription( new LoadBalancerTarget("all", "3.3.3.3", 80), new TargetHealth(HealthState.HEALTHY)); String targetGroupArn = "tgArn"; TargetGroup targetGroup = new TargetGroup( targetGroupArn, ImmutableList.of(), Protocol.TCP, 80, "tgName", TargetGroup.Type.IP); Region.RegionBuilder rb = Region.builder("r1").setTargetGroups(ImmutableMap.of(targetGroupArn, targetGroup)); { LoadBalancerTargetHealth targetHealth = new LoadBalancerTargetHealth( targetGroupArn, ImmutableList.of(unhealthyTarget, healthyTarget1, healthyTarget2, healthyTargetAll)); rb.setLoadBalancerTargetHealths(ImmutableMap.of(targetGroupArn, targetHealth)); assertThat( getActiveTargets( targetHealth, targetGroup, ImmutableSet.of("zone1", "zone2"), rb.build(), false, null), containsInAnyOrder(healthyTarget1, healthyTarget2, healthyTargetAll)); } { LoadBalancerTargetHealth targetHealth = new LoadBalancerTargetHealth( targetGroupArn, ImmutableList.of(unhealthyTarget, healthyTarget1, healthyTarget2, healthyTargetAll)); rb.setLoadBalancerTargetHealths(ImmutableMap.of(targetGroupArn, targetHealth)); assertThat( getActiveTargets( targetHealth, targetGroup, ImmutableSet.of("zone1"), rb.build(), false, null), containsInAnyOrder(healthyTarget1, healthyTargetAll)); } { LoadBalancerTargetHealth targetHealth = new LoadBalancerTargetHealth( targetGroupArn, ImmutableList.of(unhealthyTarget, healthyTarget2, healthyTargetAll)); rb.setLoadBalancerTargetHealths(ImmutableMap.of(targetGroupArn, targetHealth)); assertThat( getActiveTargets( targetHealth, targetGroup, ImmutableSet.of("zone1"), rb.build(), false, null), containsInAnyOrder(healthyTargetAll)); } { LoadBalancerTargetHealth targetHealth = new LoadBalancerTargetHealth( targetGroupArn, ImmutableList.of(unhealthyTarget, healthyTarget2)); rb.setLoadBalancerTargetHealths(ImmutableMap.of(targetGroupArn, targetHealth)); assertThat( getActiveTargets( targetHealth, targetGroup, ImmutableSet.of("zone1"), rb.build(), false, null), containsInAnyOrder(unhealthyTarget)); } }
@Nonnull static Set<TargetHealthDescription> getActiveTargets( LoadBalancerTargetHealth targetHealth, TargetGroup targetGroup, Set<String> enabledTargetZones, Region region, boolean fileWarnings, @Nullable Warnings warnings) { checkArgument( !fileWarnings || warnings != null, "Can't file warnings because warnings is null"); Set<TargetHealthDescription> enabledTargets = targetHealth.getTargetHealthDescriptions().stream() .filter( desc -> isTargetInValidAvailabilityZone( desc, targetGroup.getTargetType(), enabledTargetZones, region)) .collect(ImmutableSet.toImmutableSet()); if (enabledTargets.isEmpty()) { if (fileWarnings) { warnings.redFlag( String.format( "No targets found in enabled availability zone(s) for target group ARN %s", targetGroup.getId())); } return ImmutableSet.of(); } Set<TargetHealthDescription> healthyTargets = enabledTargets.stream() .filter(desc -> desc.getTargetHealth().getState() == HealthState.HEALTHY) .collect(ImmutableSet.toImmutableSet()); return healthyTargets.isEmpty() ? enabledTargets : healthyTargets; }
LoadBalancer implements AwsVpcEntity, Serializable { @Nonnull static Set<TargetHealthDescription> getActiveTargets( LoadBalancerTargetHealth targetHealth, TargetGroup targetGroup, Set<String> enabledTargetZones, Region region, boolean fileWarnings, @Nullable Warnings warnings) { checkArgument( !fileWarnings || warnings != null, "Can't file warnings because warnings is null"); Set<TargetHealthDescription> enabledTargets = targetHealth.getTargetHealthDescriptions().stream() .filter( desc -> isTargetInValidAvailabilityZone( desc, targetGroup.getTargetType(), enabledTargetZones, region)) .collect(ImmutableSet.toImmutableSet()); if (enabledTargets.isEmpty()) { if (fileWarnings) { warnings.redFlag( String.format( "No targets found in enabled availability zone(s) for target group ARN %s", targetGroup.getId())); } return ImmutableSet.of(); } Set<TargetHealthDescription> healthyTargets = enabledTargets.stream() .filter(desc -> desc.getTargetHealth().getState() == HealthState.HEALTHY) .collect(ImmutableSet.toImmutableSet()); return healthyTargets.isEmpty() ? enabledTargets : healthyTargets; } }
LoadBalancer implements AwsVpcEntity, Serializable { @Nonnull static Set<TargetHealthDescription> getActiveTargets( LoadBalancerTargetHealth targetHealth, TargetGroup targetGroup, Set<String> enabledTargetZones, Region region, boolean fileWarnings, @Nullable Warnings warnings) { checkArgument( !fileWarnings || warnings != null, "Can't file warnings because warnings is null"); Set<TargetHealthDescription> enabledTargets = targetHealth.getTargetHealthDescriptions().stream() .filter( desc -> isTargetInValidAvailabilityZone( desc, targetGroup.getTargetType(), enabledTargetZones, region)) .collect(ImmutableSet.toImmutableSet()); if (enabledTargets.isEmpty()) { if (fileWarnings) { warnings.redFlag( String.format( "No targets found in enabled availability zone(s) for target group ARN %s", targetGroup.getId())); } return ImmutableSet.of(); } Set<TargetHealthDescription> healthyTargets = enabledTargets.stream() .filter(desc -> desc.getTargetHealth().getState() == HealthState.HEALTHY) .collect(ImmutableSet.toImmutableSet()); return healthyTargets.isEmpty() ? enabledTargets : healthyTargets; } LoadBalancer(
      String arn,
      List<AvailabilityZone> availabilityZones,
      String dnsName,
      String name,
      Scheme scheme,
      Type type,
      String vpcId); }
LoadBalancer implements AwsVpcEntity, Serializable { @Nonnull static Set<TargetHealthDescription> getActiveTargets( LoadBalancerTargetHealth targetHealth, TargetGroup targetGroup, Set<String> enabledTargetZones, Region region, boolean fileWarnings, @Nullable Warnings warnings) { checkArgument( !fileWarnings || warnings != null, "Can't file warnings because warnings is null"); Set<TargetHealthDescription> enabledTargets = targetHealth.getTargetHealthDescriptions().stream() .filter( desc -> isTargetInValidAvailabilityZone( desc, targetGroup.getTargetType(), enabledTargetZones, region)) .collect(ImmutableSet.toImmutableSet()); if (enabledTargets.isEmpty()) { if (fileWarnings) { warnings.redFlag( String.format( "No targets found in enabled availability zone(s) for target group ARN %s", targetGroup.getId())); } return ImmutableSet.of(); } Set<TargetHealthDescription> healthyTargets = enabledTargets.stream() .filter(desc -> desc.getTargetHealth().getState() == HealthState.HEALTHY) .collect(ImmutableSet.toImmutableSet()); return healthyTargets.isEmpty() ? enabledTargets : healthyTargets; } LoadBalancer(
      String arn,
      List<AvailabilityZone> availabilityZones,
      String dnsName,
      String name,
      Scheme scheme,
      Type type,
      String vpcId); @Override String getId(); @Nonnull List<AvailabilityZone> getAvailabilityZones(); @Nonnull String getDnsName(); @Nonnull String getName(); @Nonnull Scheme getScheme(); @Nonnull Type getType(); @Nonnull String getVpcId(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); }
LoadBalancer implements AwsVpcEntity, Serializable { @Nonnull static Set<TargetHealthDescription> getActiveTargets( LoadBalancerTargetHealth targetHealth, TargetGroup targetGroup, Set<String> enabledTargetZones, Region region, boolean fileWarnings, @Nullable Warnings warnings) { checkArgument( !fileWarnings || warnings != null, "Can't file warnings because warnings is null"); Set<TargetHealthDescription> enabledTargets = targetHealth.getTargetHealthDescriptions().stream() .filter( desc -> isTargetInValidAvailabilityZone( desc, targetGroup.getTargetType(), enabledTargetZones, region)) .collect(ImmutableSet.toImmutableSet()); if (enabledTargets.isEmpty()) { if (fileWarnings) { warnings.redFlag( String.format( "No targets found in enabled availability zone(s) for target group ARN %s", targetGroup.getId())); } return ImmutableSet.of(); } Set<TargetHealthDescription> healthyTargets = enabledTargets.stream() .filter(desc -> desc.getTargetHealth().getState() == HealthState.HEALTHY) .collect(ImmutableSet.toImmutableSet()); return healthyTargets.isEmpty() ? enabledTargets : healthyTargets; } LoadBalancer(
      String arn,
      List<AvailabilityZone> availabilityZones,
      String dnsName,
      String name,
      Scheme scheme,
      Type type,
      String vpcId); @Override String getId(); @Nonnull List<AvailabilityZone> getAvailabilityZones(); @Nonnull String getDnsName(); @Nonnull String getName(); @Nonnull Scheme getScheme(); @Nonnull Type getType(); @Nonnull String getVpcId(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString();  }