@GET @NoAuth @Path("/{elbName}/instances") public List<InstanceState> getInstancesByElb(@PathParam("elbName") String elbName) { if (config.isPresent()) { try { DescribeInstanceHealthRequest request = new DescribeInstanceHealthRequest(elbName); DescribeInstanceHealthResult result = elbClient.describeInstanceHealth(request); return result.getInstanceStates(); } catch (AmazonClientException exn) { throw new BaragonWebException(String.format("AWS Client Error %s", exn)); } } else { throw new BaragonWebException("ElbSync and related actions are not currently enabled"); } }
/** * Checks the instance health of the ec2 instance in the given ELB. */ public InstanceState describeInstanceHealth(String elbName, String ec2InstanceId) { LOGGER.debug("describeInstanceHealth(elbName: " + elbName + ", ec2InstanceId: " + ec2InstanceId + ")"); assertNonBlankArgs(elbName, ec2InstanceId); StopWatch stopWatch = new StopWatch(); try { stopWatch.start(); DescribeInstanceHealthRequest request = new DescribeInstanceHealthRequest(); request.setLoadBalancerName(elbName); request.setInstances(Arrays.asList(new Instance(ec2InstanceId))); DescribeInstanceHealthResult result = awsElbClient.describeInstanceHealth(request); if (result == null || CollectionUtils.isEmpty(result.getInstanceStates())) { throw new RuntimeException("ELB '" + elbName + "' didn't match instance id '" + ec2InstanceId + "'"); } else if (result.getInstanceStates().size() > 1) { LOGGER.warn("Expected 1 instance state for instance id '" + ec2InstanceId + "' in elb '" + elbName + "', found " + result.getInstanceStates().size()); } return result.getInstanceStates().get(0); } finally { stopWatch.stop(); LOGGER.debug("describeInstanceHealth time elapsed " + stopWatch); } }
public List<InstanceState> describeInstanceHealth(String elbName, List<String> instanceIds) { logger.info("describe elb instance health, instanceIds={}", instanceIds); List<com.amazonaws.services.elasticloadbalancing.model.Instance> instances = instanceIds.stream() .map(com.amazonaws.services.elasticloadbalancing.model.Instance::new) .collect(Collectors.toList()); DescribeInstanceHealthResult result = elb.describeInstanceHealth(new DescribeInstanceHealthRequest(elbName) .withInstances(instances)); return result.getInstanceStates(); }
public boolean isInstanceHealthy(String instanceId, String elbName) { DescribeInstanceHealthRequest describeRequest = new DescribeInstanceHealthRequest(elbName); DescribeInstanceHealthResult result = elbClient.describeInstanceHealth(describeRequest); boolean instanceIsHealthy = false; for (InstanceState instanceState : result.getInstanceStates()) { if (instanceState.getState().equals("InService")) { if (instanceState.getInstanceId().equals(instanceId)) { instanceIsHealthy = true; } } } return instanceIsHealthy; }
private boolean isLastHealthyInstance(DeregisterInstancesFromLoadBalancerRequest request) throws AmazonClientException { DescribeInstanceHealthRequest describeRequest = new DescribeInstanceHealthRequest(request.getLoadBalancerName()); DescribeInstanceHealthResult result = elbClient.describeInstanceHealth(describeRequest); boolean instanceIsHealthy = false; int healthyCount = 0; for (InstanceState instanceState : result.getInstanceStates()) { if (instanceState.getState().equals("InService")) { healthyCount++; if (instanceState.getInstanceId().equals(request.getInstances().get(0).getInstanceId())) { //Will only ever be one instance per request instanceIsHealthy = true; } } } return (instanceIsHealthy && healthyCount == 1); }
/** * Sets up the mock elb client to return a fakeResult for the describe-instance-health call. */ private void setupMock(DescribeInstanceHealthResult fakeResult) { when(mockELBClient.describeInstanceHealth(any(DescribeInstanceHealthRequest.class))).thenReturn(fakeResult); }