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

The following examples show how to use com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthResult. 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: 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 #7
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch target groups test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchTargetGroupsTest() throws Exception {
    
    mockStatic(com.amazonaws.services.elasticloadbalancingv2.AmazonElasticLoadBalancingClientBuilder.class);
    com.amazonaws.services.elasticloadbalancingv2.AmazonElasticLoadBalancing elbClient = PowerMockito.mock(com.amazonaws.services.elasticloadbalancingv2.AmazonElasticLoadBalancing.class);
    com.amazonaws.services.elasticloadbalancingv2.AmazonElasticLoadBalancingClientBuilder amazonElasticLoadBalancingClientBuilder = PowerMockito.mock(com.amazonaws.services.elasticloadbalancingv2.AmazonElasticLoadBalancingClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonElasticLoadBalancingClientBuilder.standard()).thenReturn(amazonElasticLoadBalancingClientBuilder);
    when(amazonElasticLoadBalancingClientBuilder.withCredentials(anyObject())).thenReturn(amazonElasticLoadBalancingClientBuilder);
    when(amazonElasticLoadBalancingClientBuilder.withRegion(anyString())).thenReturn(amazonElasticLoadBalancingClientBuilder);
    when(amazonElasticLoadBalancingClientBuilder.build()).thenReturn(elbClient);
    
    DescribeTargetGroupsResult trgtGrpRslt = new DescribeTargetGroupsResult();
    List<TargetGroup> targetGrpList = new ArrayList<>();
    TargetGroup targetGroup = new TargetGroup();
    targetGroup.setTargetGroupArn("targetGroupArn");
    targetGrpList.add(targetGroup);
    trgtGrpRslt.setTargetGroups(targetGrpList);
    when(elbClient.describeTargetGroups(anyObject())).thenReturn(trgtGrpRslt);
    
    DescribeTargetHealthResult describeTargetHealthResult = new DescribeTargetHealthResult();
    describeTargetHealthResult.setTargetHealthDescriptions(new ArrayList<>());
    when(elbClient.describeTargetHealth(anyObject())).thenReturn(describeTargetHealthResult);
    assertThat(inventoryUtil.fetchTargetGroups(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), 
            "skipRegions", "account","accountName").size(), is(1));
}
 
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;
}