Skip to content

Commit

Permalink
Merge pull request #457 from perfectsense/bugfix/add-back-target-heal…
Browse files Browse the repository at this point in the history
…th-method-in-target-group-resource

Add back the target health method
  • Loading branch information
deepanjan90 authored Apr 27, 2021
2 parents 1a5fbe3 + b095d50 commit 31fe83e
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/main/java/gyro/aws/elbv2/TargetGroupResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

package gyro.aws.elbv2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.psddev.dari.util.CompactMap;
import gyro.aws.AwsResource;
import gyro.aws.Copyable;
import gyro.aws.ec2.VpcResource;
Expand All @@ -26,8 +33,6 @@
import gyro.core.resource.Output;
import gyro.core.resource.Resource;
import gyro.core.resource.Updatable;

import com.psddev.dari.util.CompactMap;
import gyro.core.scope.State;
import gyro.core.validation.Required;
import gyro.core.validation.ValidStrings;
Expand All @@ -41,12 +46,6 @@
import software.amazon.awssdk.services.elasticloadbalancingv2.model.TargetGroup;
import software.amazon.awssdk.services.elasticloadbalancingv2.model.TargetHealthDescription;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
*
* Example
Expand Down Expand Up @@ -209,6 +208,31 @@ public void setVpc(VpcResource vpc) {
this.vpc = vpc;
}

/**
* Helper method that maps Health State to instances in that state.
*
* @return A map of health state to number of targets in that state
*/
public Map<String, Integer> targetHealth() {
Map<String, Integer> healthMap = new HashMap<>();

ElasticLoadBalancingV2Client client = createClient(ElasticLoadBalancingV2Client.class);
List<TargetHealthDescription> targetHealthDescriptions = client.describeTargetHealth(
DescribeTargetHealthRequest.builder()
.targetGroupArn(getArn())
.build())
.targetHealthDescriptions();

for (TargetHealthDescription thd : targetHealthDescriptions) {
String state = thd.targetHealth().stateAsString();
int count = healthMap.getOrDefault(state, 0);
healthMap.put(state, count + 1);
}

healthMap.put("Total", targetHealthDescriptions.size());
return healthMap;
}

@Override
public void copyFrom(TargetGroup targetGroup) {
if (getHealthCheck() != null) {
Expand Down

0 comments on commit 31fe83e

Please sign in to comment.