com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest Java Examples

The following examples show how to use com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AwsLoadBalancerConnector.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Override
public Single<LoadBalancer> getLoadBalancer(String loadBalancerId) {
    final DescribeTargetHealthRequest request = new DescribeTargetHealthRequest().withTargetGroupArn(loadBalancerId);

    long startTime = registry.clock().wallTime();
    Single<DescribeTargetHealthResult> asyncResult = AwsObservableExt.asyncActionSingle(
            factory -> getClient(loadBalancerId).describeTargetHealthAsync(request, factory.handler())
    );

    return asyncResult
            .observeOn(scheduler)
            .map(result -> {
                connectorMetrics.success(AwsLoadBalancerConnectorMetrics.AwsLoadBalancerMethods.DescribeTargetHealth, startTime);
                Set<String> ips = result.getTargetHealthDescriptions().stream()
                        .map(description -> description.getTarget().getId())
                        .collect(Collectors.toSet());
                return new LoadBalancer(loadBalancerId, LoadBalancer.State.ACTIVE, ips);
            })
            .onErrorResumeNext(throwable -> {
                connectorMetrics.failure(AwsLoadBalancerConnectorMetrics.AwsLoadBalancerMethods.DescribeTargetHealth, throwable, startTime);
                if (throwable instanceof TargetGroupNotFoundException) {
                    return Single.just(new LoadBalancer(loadBalancerId, LoadBalancer.State.REMOVED, Collections.emptySet()));
                }
                return Single.error(throwable);
            });
}
 
Example #2
Source File: ELBIsInstanceRegisteredStep.java    From pipeline-aws-plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected Boolean run() throws Exception {
	TaskListener listener = this.getContext().get(TaskListener.class);
	listener.getLogger().println("elbIsInstanceRegistered instanceID: " + this.step.instanceID + " port: " + this.step.port + " from targetGroupARN: " + this.step.targetGroupARN);
	
	Boolean rval = false;
	AmazonElasticLoadBalancing client = AWSClientFactory.create(AmazonElasticLoadBalancingClientBuilder.standard(), this.getEnvVars());
	DescribeTargetHealthRequest req = new DescribeTargetHealthRequest().withTargetGroupArn(this.step.targetGroupARN);
	DescribeTargetHealthResult res = client.describeTargetHealth(req);
	List<TargetHealthDescription> targets = res.getTargetHealthDescriptions();
	for (TargetHealthDescription target : targets) {
		if (target.getTarget().getId().equals(this.step.instanceID) && target.getTargetHealth().getState().equals("healthy") ) {
			rval = true;
			break;
		}
	}
	listener.getLogger().println(res.toString());
	
	return rval;
}
 
Example #3
Source File: ELBRegisterInstanceStep.java    From pipeline-aws-plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected String run() throws Exception {
	TaskListener listener = this.getContext().get(TaskListener.class);
	listener.getLogger().println("elbRegisterInstance instanceID: " + this.step.instanceID + " port: " + this.step.port + " from targetGroupARN: " + this.step.targetGroupARN);
	ArrayList<TargetDescription> arr = new ArrayList<TargetDescription>();
	arr.add(new TargetDescription().withId(this.step.instanceID).withPort(this.step.port));
	RegisterTargetsRequest request = new RegisterTargetsRequest().withTargetGroupArn(this.step.targetGroupARN).withTargets( arr );
	AmazonElasticLoadBalancing client = AWSClientFactory.create(AmazonElasticLoadBalancingClientBuilder.standard(), this.getEnvVars());
	client.registerTargets(request);
	
	DescribeTargetHealthRequest req = new DescribeTargetHealthRequest().withTargetGroupArn(this.step.targetGroupARN);
	DescribeTargetHealthResult res = client.describeTargetHealth(req);
	listener.getLogger().println(res.toString());
	
	return null;
}
 
Example #4
Source File: ELBDeregisterInstanceStep.java    From pipeline-aws-plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected String run() throws Exception {
	TaskListener listener = this.getContext().get(TaskListener.class);
	listener.getLogger().println("elbDeregisterInstance instanceID: " + this.step.instanceID + " port: " + this.step.port + " from targetGroupARN: " + this.step.targetGroupARN);
	ArrayList<TargetDescription> arr = new ArrayList<TargetDescription>();
	arr.add(new TargetDescription().withId(this.step.instanceID).withPort(this.step.port));
	DeregisterTargetsRequest request = new DeregisterTargetsRequest().withTargetGroupArn(this.step.targetGroupARN).withTargets( arr );
	AmazonElasticLoadBalancing client = AWSClientFactory.create(AmazonElasticLoadBalancingClientBuilder.standard(), this.getEnvVars());
	client.deregisterTargets(request);
	
	DescribeTargetHealthRequest req = new DescribeTargetHealthRequest().withTargetGroupArn(this.step.targetGroupARN);
	DescribeTargetHealthResult res = client.describeTargetHealth(req);
	listener.getLogger().println(res.toString());
	
	return null;
}
 
Example #5
Source File: ELBIsInstanceDeregisteredStep.java    From pipeline-aws-plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected Boolean run() throws Exception {
	TaskListener listener = this.getContext().get(TaskListener.class);
	listener.getLogger().println("elbIsInstanceDeregistered instanceID: " + this.step.instanceID + " port: " + this.step.port + " from targetGroupARN: " + this.step.targetGroupARN);
	
	Boolean rval = true;
	AmazonElasticLoadBalancing client = AWSClientFactory.create(AmazonElasticLoadBalancingClientBuilder.standard(), this.getEnvVars());
	DescribeTargetHealthRequest req = new DescribeTargetHealthRequest().withTargetGroupArn(this.step.targetGroupARN);
	DescribeTargetHealthResult res = client.describeTargetHealth(req);
	List<TargetHealthDescription> targets = res.getTargetHealthDescriptions();
	for (TargetHealthDescription target : targets) {
		if (target.getTarget().getId().equals(this.step.instanceID) ) {
			rval = false;
			break;
		}
	}
	listener.getLogger().println(res.toString());
	
	return rval;
}
 
Example #6
Source File: ApplicationLoadBalancer.java    From Baragon with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param target Target to check
 * @param targetGroup Group to check in
 * @return if the given target is the last healthy target in the given target group
 */
private boolean isLastHealthyInstance(TargetDescription target, TargetGroup targetGroup) {
  DescribeTargetHealthRequest targetHealthRequest = new DescribeTargetHealthRequest()
      .withTargetGroupArn(targetGroup.getTargetGroupArn());
  List<TargetHealthDescription> targetHealthDescriptions = elbClient
      .describeTargetHealth(targetHealthRequest)
      .getTargetHealthDescriptions();

  boolean instanceIsHealthy = false;
  int healthyCount = 0;

  for (TargetHealthDescription targetHealthDescription : targetHealthDescriptions) {
    if (targetHealthDescription.getTargetHealth().getState()
        .equals(TargetHealthStateEnum.Healthy.toString())) {
      healthyCount += 1;
      if (targetHealthDescription.getTarget().equals(target)) {
        instanceIsHealthy = true;
      }
    }
  }

  return instanceIsHealthy && healthyCount == 1;
}
 
Example #7
Source File: InventoryUtil.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch target groups.
 *
 * @param temporaryCredentials the temporary credentials
 * @param skipRegions the skip regions
 * @param accountId the accountId
 * @param accountName the account name
 * @return the map
 */
public static Map<String,List<TargetGroupVH>> fetchTargetGroups(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName){
	com.amazonaws.services.elasticloadbalancingv2.AmazonElasticLoadBalancing elbClient ;
	Map<String,List<TargetGroupVH>> targetGrpMap = new LinkedHashMap<>();
	String expPrefix = InventoryConstants.ERROR_PREFIX_CODE+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"Target Group\" , \"region\":\"" ;
	for(Region region : RegionUtils.getRegions()){
		try{
			if(!skipRegions.contains(region.getName())){
				elbClient = com.amazonaws.services.elasticloadbalancingv2.AmazonElasticLoadBalancingClientBuilder.standard().
					 	withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build();
				String nextMarker = null;
				List<TargetGroupVH> targetGrpList = new ArrayList<>();
				do{
					DescribeTargetGroupsResult  trgtGrpRslt =  elbClient.describeTargetGroups(new DescribeTargetGroupsRequest().withMarker(nextMarker));
					List<TargetGroup> targetGrpListTemp = trgtGrpRslt.getTargetGroups();
					for(TargetGroup tg : targetGrpListTemp) {
						DescribeTargetHealthResult rslt =  elbClient.describeTargetHealth(new DescribeTargetHealthRequest().withTargetGroupArn(tg.getTargetGroupArn()));
						targetGrpList.add(new TargetGroupVH(tg, rslt.getTargetHealthDescriptions()));
					}
					nextMarker = trgtGrpRslt.getNextMarker();
				}while(nextMarker!=null);

				if( !targetGrpList.isEmpty() ) {
					log.debug(InventoryConstants.ACCOUNT + accountId +" Type : Target Group " +region.getName() + "-"+targetGrpList.size());
					targetGrpMap.put(accountId+delimiter+accountName+delimiter+region.getName(), targetGrpList);
				}

			}
		}catch(Exception e){
			log.warn(expPrefix+ region.getName()+InventoryConstants.ERROR_CAUSE +e.getMessage()+"\"}");
			ErrorManageUtil.uploadError(accountId,region.getName(),"targetgroup",e.getMessage());
		}
	}
	return targetGrpMap;
}
 
Example #8
Source File: ApplicationLoadBalancer.java    From Baragon with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isInstanceHealthy(String instanceId, String elbName) {
  DescribeTargetHealthRequest healthRequest = new DescribeTargetHealthRequest()
      .withTargets(new TargetDescription().withId(instanceId));
  DescribeTargetHealthResult result = elbClient.describeTargetHealth(healthRequest);

  for (TargetHealthDescription health: result.getTargetHealthDescriptions()) {
    if (health.getTargetHealth().getState().equals(TargetHealthStateEnum.Healthy.toString())
        && health.getTarget().getId().equals(instanceId)) {
      return true;
    }
  }

  return false;
}
 
Example #9
Source File: ApplicationLoadBalancer.java    From Baragon with Apache License 2.0 5 votes vote down vote up
public Collection<TargetHealthDescription> getTargetsOn(TargetGroup targetGroup) {
  DescribeTargetHealthRequest targetHealthRequest = new DescribeTargetHealthRequest()
      .withTargetGroupArn(targetGroup.getTargetGroupArn());

  return elbClient
      .describeTargetHealth(targetHealthRequest)
      .getTargetHealthDescriptions();
}
 
Example #10
Source File: ApplicationLoadBalancer.java    From Baragon with Apache License 2.0 5 votes vote down vote up
/**
 * @param targetGroup Target group to check
 * @return the instance IDs of the targets in the given target group
 */
private Collection<String> targetsOn(TargetGroup targetGroup) {
  DescribeTargetHealthRequest targetHealthRequest = new DescribeTargetHealthRequest()
      .withTargetGroupArn(targetGroup.getTargetGroupArn());
  return elbClient
      .describeTargetHealth(targetHealthRequest)
      .getTargetHealthDescriptions().stream()
      .map((t) -> t.getTarget().getId())
      .collect(Collectors.toSet());
}
 
Example #11
Source File: ApplicationLoadBalancer.java    From Baragon with Apache License 2.0 5 votes vote down vote up
private Collection<TargetDescription> targetsInTargetGroup(TargetGroup targetGroup) {
  DescribeTargetHealthRequest targetHealthRequest = new DescribeTargetHealthRequest()
      .withTargetGroupArn(targetGroup.getTargetGroupArn());
  List<TargetHealthDescription> targetGroupsResult = elbClient
      .describeTargetHealth(targetHealthRequest)
      .getTargetHealthDescriptions();

  Collection<TargetDescription> targetDescriptions = new HashSet<>();
  for (TargetHealthDescription targetHealthDescription : targetGroupsResult) {
    targetDescriptions.add(targetHealthDescription.getTarget());
  }

  return targetDescriptions;
}