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

The following examples show how to use com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest. 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: 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 #2
Source File: ApplicationLoadBalancer.java    From Baragon with Apache License 2.0 6 votes vote down vote up
private AgentCheckInResponse registerInstance(String id, TargetGroup targetGroup) {
  TargetDescription newTarget = new TargetDescription()
      .withId(id);

  if (!targetsOn(targetGroup).contains(newTarget.getId())) {
    try {
      RegisterTargetsRequest registerTargetsRequest = new RegisterTargetsRequest()
          .withTargets(newTarget)
          .withTargetGroupArn(targetGroup.getTargetGroupArn());
      elbClient.registerTargets(registerTargetsRequest);
      LOG.info("Registered instance {} with target group {}", newTarget.getId(), targetGroup);
    } catch (AmazonServiceException exn) {
      LOG.warn("Failed to register instance {} with target group {}", id, targetGroup);
      throw Throwables.propagate(exn);
    }

    return instanceHealthResponse(newTarget, targetGroup, id);
  } else {
    LOG.debug("Instance {} already registered with target group {}", id, targetGroup);
    return new AgentCheckInResponse(TrafficSourceState.DONE, Optional.absent(), 0L);
  }
}
 
Example #3
Source File: AwsLoadBalancerConnector.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
public Completable registerAll(String loadBalancerId, Set<String> ipAddresses) {
    if (CollectionsExt.isNullOrEmpty(ipAddresses)) {
        return Completable.complete();
    }

    // TODO: retry logic
    // TODO: handle partial failures in the batch
    // TODO: timeouts

    final Set<TargetDescription> targetDescriptions = ipAddresses.stream().map(
            ipAddress -> new TargetDescription().withId(ipAddress)
    ).collect(Collectors.toSet());
    final RegisterTargetsRequest request = new RegisterTargetsRequest()
            .withTargetGroupArn(loadBalancerId)
            .withTargets(targetDescriptions);

    long startTime = registry.clock().wallTime();
    // force observeOn(scheduler) since the callback will be called from the AWS SDK threadpool
    return AwsObservableExt.asyncActionCompletable(factory -> getClient(loadBalancerId).registerTargetsAsync(request, factory.handler(
            (req, resp) -> {
                logger.debug("Registered targets {}", resp);
                connectorMetrics.success(AwsLoadBalancerConnectorMetrics.AwsLoadBalancerMethods.RegisterTargets, startTime);
            },
            (t) -> {
                logger.error("Error registering targets on " + loadBalancerId, t);
                connectorMetrics.failure(AwsLoadBalancerConnectorMetrics.AwsLoadBalancerMethods.RegisterTargets, t, startTime);
            }
    ))).observeOn(scheduler);
}
 
Example #4
Source File: ApplicationLoadBalancer.java    From Baragon with Apache License 2.0 4 votes vote down vote up
/**
 * After this method completes, every agent in baragonAgents should be associated with
 * a target in the given target group.
 *
 * @param targetGroup group to register in
 * @param baragonAgents agents to be registered
 */
private void guaranteeHasAllTargets(TrafficSource trafficSource,
                                    TargetGroup targetGroup,
                                    Collection<TargetDescription> targets,
                                    Collection<BaragonAgentMetadata> baragonAgents) {
  Collection<TargetDescription> targetDescriptions = new HashSet<>();
  for (BaragonAgentMetadata agent : baragonAgents) {
    try {
      if ((trafficSource.getRegisterBy() == RegisterBy.INSTANCE_ID && agent.getEc2().getInstanceId().isPresent())
          || (trafficSource.getRegisterBy() == RegisterBy.PRIVATE_IP && agent.getEc2().getPrivateIp().isPresent())) {
        String id = trafficSource.getRegisterBy() == RegisterBy.INSTANCE_ID ? agent.getEc2().getInstanceId().get() : agent.getEc2().getPrivateIp().get();
        if (agentShouldRegisterInTargetGroup(id, targets)) {
          targetDescriptions.add(new TargetDescription()
              .withId(id));
          LOG.info("Will register agent {} to target in group {}", agent, targetGroup);
        } else {
          LOG.debug("Agent {} is already registered", agent);
        }
      }
    } catch (Exception exn) {
      LOG.error("Could not create request to register agent {} due to error", agent, exn);
      exceptionNotifier.notify(exn, ImmutableMap.of("agent", agent.toString()));
    }
  }

  if (targetDescriptions.isEmpty()) {
    LOG.debug("No new instances to register with target group");
  } else {
    try {
      RegisterTargetsRequest registerTargetsRequest = new RegisterTargetsRequest()
          .withTargetGroupArn(targetGroup.getTargetGroupArn())
          .withTargets(targetDescriptions);
      elbClient.registerTargets(registerTargetsRequest);
      LOG.info("Registered targets {} onto target group {}", targetDescriptions, targetGroup);
    } catch (AmazonClientException acexn) {
      LOG.error("Failed to register targets {} onto target group {}", targetDescriptions, targetGroup);
      exceptionNotifier.notify(acexn, ImmutableMap.of(
          "targets", targetDescriptions.toString(),
          "targetGroup", targetGroup.toString()));
    }
  }
}