/** * True if ec2InstanceId is not in the input list. */ private boolean instanceIsGoneFromList(List<Instance> instances) { if (instances == null) { throw new IllegalArgumentException(); } for (Instance instance : instances) { if (StringUtils.equals(instance.getInstanceId(), ec2InstanceId)) { return false; } } return true; }
/** * Requests registration of the ec2 instance with the ELB. * <p/> * After calling here, you need to call DescribeLoadBalancers or DescribeInstanceHealth to see if registration is * complete. */ public void registerInstance(String elbName, String ec2InstanceId) { LOGGER.debug("registerInstancesWithLoadBalancer(elbName: " + elbName + ", ec2InstanceId: " + ec2InstanceId + ")"); assertNonBlankArgs(elbName, ec2InstanceId); StopWatch stopWatch = new StopWatch(); try { stopWatch.start(); RegisterInstancesWithLoadBalancerRequest request = new RegisterInstancesWithLoadBalancerRequest(); request.setLoadBalancerName(elbName); request.setInstances(Arrays.asList(new Instance(ec2InstanceId))); awsElbClient.registerInstancesWithLoadBalancer(request); //Currently not doing anything with the RegisterInstancesWithLoadBalancerResult } finally { stopWatch.stop(); LOGGER.debug("registerInstancesWithLoadBalancer time elapsed " + stopWatch); } }
/** * Requests deregistration of the ec2 instance from the ELB. * <p/> * After calling here, you need to call DescribeLoadBalancers to see if registration is complete. */ public void deregisterInstance(String elbName, String ec2InstanceId) { LOGGER.debug("deregisterInstancesFromLoadBalancer(elbName: " + elbName + ", ec2InstanceId: " + ec2InstanceId + ")"); assertNonBlankArgs(elbName, ec2InstanceId); StopWatch stopWatch = new StopWatch(); try { stopWatch.start(); DeregisterInstancesFromLoadBalancerRequest request = new DeregisterInstancesFromLoadBalancerRequest(); request.setLoadBalancerName(elbName); request.setInstances(Arrays.asList(new Instance(ec2InstanceId))); awsElbClient.deregisterInstancesFromLoadBalancer(request); //Currently not doing anything with the DeregisterInstancesFromLoadBalancerResult } finally { stopWatch.stop(); LOGGER.debug("deregisterInstancesFromLoadBalancer time elapsed " + stopWatch); } }
/** * Test helper - makes a fake LB description. */ private LoadBalancerDescription makeLoadBalancerDescription(String elbName, String... instanceIds) { LoadBalancerDescription loadBalancerDescription = new LoadBalancerDescription(); loadBalancerDescription.setLoadBalancerName(elbName); List<Instance> instances = new ArrayList<Instance>(); if (instanceIds != null) { for (String instanceId : instanceIds) { Instance instance = new Instance(); instance.setInstanceId(instanceId); instances.add(instance); } } loadBalancerDescription.setInstances(instances); return loadBalancerDescription; }
private Boolean updateExistingLoadBalancer(Cluster cluster) { Boolean isUpdated=false; LoadBalancerInfo loadBalancerInfo = clusterIdToLoadBalancerMap.get(cluster.getClusterId()); String loadBalancerName = loadBalancerInfo.getName(); String region = loadBalancerInfo.getRegion(); // Get all the instances attached - Attach newly added instances to load balancer // attachedInstances list is useful in finding out what are the new instances which // should be attached to this load balancer. List<Instance> attachedInstances = awsHelper.getAttachedInstances(loadBalancerName, region); // clusterMembers stores all the members of a cluster. Collection<Member> clusterMembers = cluster.getMembers(); isUpdated= addClusterMembersInfo(clusterMembers, loadBalancerName, region); return isUpdated; }
/** * Detaches provided instances from the load balancer, associated with some * cluster. Useful when instances are removed from the cluster with which * this load balancer is associated. * * @param loadBalancerName * @param instances to be de-registered from load balancer * @param region of the load balancer */ public void deregisterInstancesFromLoadBalancer(String loadBalancerName, List<Instance> instances, String region) { log.info("De-registering following instance(s) from load balancer " + loadBalancerName); for (Instance instance : instances) { log.info(instance.getInstanceId()); } DeregisterInstancesFromLoadBalancerRequest deregisterInstancesFromLoadBalancerRequest = new DeregisterInstancesFromLoadBalancerRequest( loadBalancerName, instances); try { elbClient.setEndpoint(String.format( Constants.ELB_ENDPOINT_URL_FORMAT, region)); elbClient .deregisterInstancesFromLoadBalancer(deregisterInstancesFromLoadBalancerRequest); } catch (AmazonClientException e) { log.error("Could not de-register instances from load balancer " + loadBalancerName, e); } }
/** * Returns instances attached to the load balancer. Useful when deciding if * all attached instances are required or some should be detached. * * @param loadBalancerName * @param region * @return list of instances attached */ public List<Instance> getAttachedInstances(String loadBalancerName, String region) { try { LoadBalancerDescription lbDescription = getLoadBalancerDescription(loadBalancerName, region); if (lbDescription == null) { log.warn("Could not find description of load balancer "+ loadBalancerName); return null; } return lbDescription.getInstances(); } catch (AmazonClientException e) { log.error("Could not find instances attached load balancer "+ loadBalancerName, e); } return null; }
@Override public List<Instance> getAllInstancesMatchingType(SearchCriteria criteria, String typeTag) throws CfnAssistException { Collection<String> instancesIds = getAllInstancesFor(criteria); List<Instance> instances = new LinkedList<Instance>(); for (String id : instancesIds) { if (instanceHasCorrectType(typeTag, id)) { logger.info(String.format("Adding instance %s as it matched %s %s",id, AwsFacade.TYPE_TAG, typeTag)); instances.add(new Instance(id)); } else { logger.info(String.format("Not adding instance %s as did not match %s %s",id, AwsFacade.TYPE_TAG, typeTag)); } } logger.info(String.format("Found %s instances matching %s and type: %s", instances.size(), criteria, typeTag)); return instances; }
private List<Instance> addInstancesThatMatchBuildAndType(ProjectAndEnv projAndEnv, String typeTag) throws CfnAssistException { if (!projAndEnv.hasBuildNumber()) { throw new MustHaveBuildNumber(); } LoadBalancerDescription elb = findELBFor(projAndEnv, typeTag); List<Instance> currentInstances = elb.getInstances(); String lbName = elb.getLoadBalancerName(); SearchCriteria criteria = new SearchCriteria(projAndEnv); List<Instance> allMatchingInstances = cfnRepository.getAllInstancesMatchingType(criteria, typeTag); List<Instance> instancesToAdd = filterBy(currentInstances, allMatchingInstances); if (allMatchingInstances.size()==0) { logger.warn(String.format("No instances matched %s and type tag %s (%s)", projAndEnv, typeTag, AwsFacade.TYPE_TAG)); } else { logger.info(String.format("Regsister matching %s instances with the LB %s ", instancesToAdd.size(),lbName)); elbClient.registerInstances(instancesToAdd, lbName); } return instancesToAdd; }
private List<Instance> removeInstancesNotMatching(ProjectAndEnv projAndEnv, List<Instance> matchingInstances, String typeTag) throws MustHaveBuildNumber, TooManyELBException { LoadBalancerDescription elb = findELBFor(projAndEnv, typeTag); logger.info("Checking if instances should be removed from ELB " + elb.getLoadBalancerName()); List<Instance> currentInstances = elb.getInstances(); List<Instance> toRemove = new LinkedList<Instance>(); for(Instance current : currentInstances) { String instanceId = current.getInstanceId(); if (matchingInstances.contains(current)) { logger.info("Instance matched project/env/build/type, will not be removed " + instanceId); } else { logger.info("Instance did not match, will be removed from ELB " +instanceId); toRemove.add(new Instance(instanceId)); } } if (toRemove.isEmpty()) { logger.info("No instances to remove from ELB " + elb.getLoadBalancerName()); return new LinkedList<Instance>(); } return removeInstances(elb,toRemove); }
@Test public void shouldGetInstancesForTheLB() throws TooManyELBException { String vpcId = "myVPC"; Instance insA = new Instance().withInstanceId("instanceA"); // associated List<LoadBalancerDescription> theLB = new LinkedList<>(); theLB.add(new LoadBalancerDescription().withVPCId(vpcId). withInstances(insA). withLoadBalancerName("lbName").withDNSName("dnsName")); EasyMock.expect(vpcRepository.getCopyOfVpc(projAndEnv)).andStubReturn(new Vpc().withVpcId(vpcId)); EasyMock.expect(elbClient.describeLoadBalancers()).andReturn(theLB); replayAll(); List<Instance> result = elbRepository.findInstancesAssociatedWithLB(projAndEnv,"typeNotUsedWhenOneMatchingLB"); verifyAll(); assertEquals(1, result.size()); assertEquals("instanceA", result.get(0).getInstanceId()); }
public AgentCheckInResponse attemptRemoveAgent(BaragonAgentMetadata agent, Optional<BaragonGroup> group, String groupName, boolean isStatusCheck) throws AmazonClientException { TrafficSourceState state = TrafficSourceState.DONE; long maxWaitTime = 0L; Optional<String> maybeExceptions = Optional.absent(); if (isElbEnabledAgent(agent, group, groupName)) { for (TrafficSource source : group.get().getTrafficSources()) { Instance instance = new Instance(agent.getEc2().getInstanceId().get()); AgentCheckInResponse response = isStatusCheck ? getLoadBalancer(source.getType()).checkRemovedInstance(instance, source.getName(), agent.getAgentId()) : getLoadBalancer(source.getType()).removeInstance(instance, source.getName(), agent.getAgentId()); if (response.getState().ordinal() > state.ordinal()) { state = response.getState(); } if (response.getExceptionMessage().isPresent()) { maybeExceptions = Optional.of(maybeExceptions.or("") + response.getExceptionMessage().get() + "\n"); } if (response.getWaitTime() > maxWaitTime) { maxWaitTime = response.getWaitTime(); } } } return new AgentCheckInResponse(state, maybeExceptions, maxWaitTime); }
public AgentCheckInResponse attemptAddAgent(BaragonAgentMetadata agent, Optional<BaragonGroup> group, String groupName, boolean isStatusCheck) throws AmazonClientException, NoMatchingElbForVpcException { TrafficSourceState state = TrafficSourceState.DONE; Optional<String> maybeVpcException = Optional.absent(); long maxWaitTime = 0L; if (isElbEnabledAgent(agent, group, groupName)) { for (TrafficSource source : group.get().getTrafficSources()) { Instance instance = new Instance(agent.getEc2().getInstanceId().get()); AgentCheckInResponse response = isStatusCheck ? getLoadBalancer(source.getType()).checkRegisteredInstance(instance, source.getName(), agent) : getLoadBalancer(source.getType()).registerInstance(instance, source.getName(), agent); if (response.getExceptionMessage().isPresent()) { maybeVpcException = Optional.of(maybeVpcException.or("") + response.getExceptionMessage().get() + "\n"); } if (response.getState().ordinal() > state.ordinal()) { state = response.getState(); } if (response.getWaitTime() > maxWaitTime) { maxWaitTime = response.getWaitTime(); } } if (maybeVpcException.isPresent() && configuration.get().isFailWhenNoElbForVpc()) { throw new NoMatchingElbForVpcException(maybeVpcException.get()); } } return new AgentCheckInResponse(state, maybeVpcException, maxWaitTime); }
public AgentCheckInResponse registerInstance(Instance instance, String elbName, BaragonAgentMetadata agent) { Optional<String> maybeException = Optional.absent(); Optional<LoadBalancerDescription> elb = getElb(elbName); if (elb.isPresent()) { if (isVpcOk(agent, elb.get())) { if (!elb.get().getInstances().contains(instance)) { checkAZEnabled(agent, elbName, elb.get()); RegisterInstancesWithLoadBalancerRequest request = new RegisterInstancesWithLoadBalancerRequest(elbName, Arrays.asList(instance)); elbClient.registerInstancesWithLoadBalancer(request); LOG.info("Registered instances {} with ELB {}", request.getInstances(), request.getLoadBalancerName()); } else { LOG.debug("Agent {} already registered with ELB {}", agent.getAgentId(), elbName); } } else { maybeException = Optional.of(String.format("No ELB found for vpc %s", agent.getEc2().getVpcId())); } } return new AgentCheckInResponse(TrafficSourceState.DONE, maybeException, 0L); }
private boolean shouldRegister(BaragonAgentMetadata agent, String elbName, List<LoadBalancerDescription> elbs) { Optional<LoadBalancerDescription> matchingElb = Optional.absent(); for (LoadBalancerDescription elb : elbs) { if (elbName.equals(elb.getLoadBalancerName())) { matchingElb = Optional.of(elb); } } if (!matchingElb.isPresent()) { return false; } boolean alreadyRegistered = false; for (Instance instance : matchingElb.get().getInstances()) { if (agent.getEc2().getInstanceId().get().equals(instance.getInstanceId())) { alreadyRegistered = true; } } return !alreadyRegistered && (isVpcOk(agent, matchingElb.get()) || !configuration.get().isCheckForCorrectVpc()); }
private List<DeregisterInstancesFromLoadBalancerRequest> deregisterRequests(BaragonGroup group, Collection<BaragonAgentMetadata> agents, List<LoadBalancerDescription> elbs) { List<String> agentInstanceIds = agentInstanceIds(agents); List<DeregisterInstancesFromLoadBalancerRequest> requests = new ArrayList<>(); for (LoadBalancerDescription elb : elbs) { if (group.getTrafficSources().contains(new TrafficSource(elb.getLoadBalancerName(), TrafficSourceType.CLASSIC))) { for (Instance instance : elb.getInstances()) { if (!agentInstanceIds.contains(instance.getInstanceId()) && canDeregisterAgent(group, instance.getInstanceId())) { List<Instance> instanceList = new ArrayList<>(1); instanceList.add(instance); requests.add(new DeregisterInstancesFromLoadBalancerRequest(elb.getLoadBalancerName(), instanceList)); LOG.info("Will deregister instance {} from ELB {}", instance.getInstanceId(), elb.getLoadBalancerName()); } } } } return requests; }
/** * 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); } }
private DeferredResult<AWSLoadBalancerContext> assignInstances(AWSLoadBalancerContext context) { // If the registered instances are null this is a newly provisioned load balancer // so add all instances from the load balancer state to the registration request if (context.registeredInstances == null) { context.instanceIdsToRegister = context.loadBalancerStateExpanded.computes.stream() .map(computeState -> computeState.id) .collect(Collectors.toList()); context.instanceIdsToDeregister = Collections.emptyList(); } else { context.instanceIdsToRegister = context.loadBalancerStateExpanded.computes.stream() .map(computeState -> computeState.id) .filter(csId -> context.registeredInstances.stream() .noneMatch(i -> i.getInstanceId().equals(csId)) ) .collect(Collectors.toList()); context.instanceIdsToDeregister = context.registeredInstances.stream() .map(Instance::getInstanceId) .filter(instanceId -> context.loadBalancerStateExpanded.computes.stream() .noneMatch(computeState -> computeState.id.equals(instanceId)) ) .collect(Collectors.toList()); } return DeferredResult.completed(context) .thenCompose(this::registerInstances) .thenCompose(this::deregisterInstances); }
private RegisterInstancesWithLoadBalancerRequest buildInstanceRegistrationRequest( AWSLoadBalancerContext context) { return new RegisterInstancesWithLoadBalancerRequest() .withLoadBalancerName(context.loadBalancerStateExpanded.name) .withInstances(context.instanceIdsToRegister.stream() .map(Instance::new) .collect(Collectors.toList()) ); }
private DeregisterInstancesFromLoadBalancerRequest buildInstanceDeregistrationRequest( AWSLoadBalancerContext context) { return new DeregisterInstancesFromLoadBalancerRequest() .withLoadBalancerName(context.loadBalancerStateExpanded.name) .withInstances(context.instanceIdsToDeregister.stream() .map(Instance::new) .collect(Collectors.toList()) ); }
private DeferredResult<LoadBalancerEnumContext> getComputeStates( LoadBalancerEnumContext context) { if (context.remoteResources.values().isEmpty()) { return DeferredResult.completed(context); } List<String> instanceIds = context.remoteResources.values().stream() .flatMap(lb -> lb.getInstances().stream()) .map(Instance::getInstanceId) .collect(Collectors.toList()); if (instanceIds.isEmpty()) { return DeferredResult.completed(context); } Query.Builder qBuilder = Builder.create() .addKindFieldClause(ComputeState.class) .addInClause(ResourceState.FIELD_NAME_ID, instanceIds); QueryByPages<ComputeState> queryByPages = new QueryByPages<>( this.service.getHost(), qBuilder.build(), ComputeState.class, context.request.parentCompute.tenantLinks, context.request.original.endpointLink); queryByPages.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE); return queryByPages.queryDocuments( computeState -> this.localComputeStates .put(computeState.id, computeState.documentSelfLink)) .thenApply(ignore -> context); }
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 void detachInstances(String elbName, List<String> instanceIds) { logger.info("detach instances from elb, elb={}, instances={}", elbName, instanceIds); List<Instance> instances = instanceIds.stream().map(Instance::new).collect(Collectors.toList()); elb.deregisterInstancesFromLoadBalancer(new DeregisterInstancesFromLoadBalancerRequest() .withLoadBalancerName(elbName) .withInstances(instances)); }
public void attachInstances(String elbName, List<String> instanceIds, boolean waitUntilInService) throws InterruptedException { logger.info("attach instances to elb, elb={}, instances={}", elbName, instanceIds); String expectedState = waitUntilInService ? "InService" : "Service"; // if not waitUntilInService, state can be InService or OutOfService List<Instance> instances = instanceIds.stream().map(Instance::new).collect(Collectors.toList()); elb.registerInstancesWithLoadBalancer(new RegisterInstancesWithLoadBalancerRequest() .withLoadBalancerName(elbName) .withInstances(instances)); int attempts = 0; while (true) { attempts++; Threads.sleepRoughly(Duration.ofSeconds(15)); List<InstanceState> states = describeInstanceHealth(elbName, instanceIds); for (InstanceState state : states) { logger.info("instance elb state {} => {}", state.getInstanceId(), state.getState()); } boolean allAttached = states.stream().allMatch(state -> state.getState().contains(expectedState)); if (allAttached) { logger.info("all instances are attached to elb"); break; } else if (attempts >= 30) { throw new Error("failed to wait all instances to be attached to elb, please check aws console for more details"); } else { logger.info("continue to wait, not all new instances are attached"); } } }
/** * Attaches provided instances to the load balancer. Useful when new * instances get added to the cluster with which this load balancer is * associated. * * @param loadBalancerName * @param instances to attached to the load balancer * @param region of the load balancer */ public void registerInstancesToLoadBalancer(String loadBalancerName, List<Instance> instances, String region) { log.info("Registering following instance(s) to load balancer " + loadBalancerName); for (Instance instance : instances) { log.info(instance.getInstanceId()); } RegisterInstancesWithLoadBalancerRequest registerInstancesWithLoadBalancerRequest = new RegisterInstancesWithLoadBalancerRequest( loadBalancerName, instances); RegisterInstancesWithLoadBalancerResult registerInstancesWithLBRes = null; try { elbClient.setEndpoint(String.format( Constants.ELB_ENDPOINT_URL_FORMAT, region)); registerInstancesWithLBRes = elbClient .registerInstancesWithLoadBalancer(registerInstancesWithLoadBalancerRequest); } catch (AmazonClientException e) { log.error("Could not register instances to load balancer " + loadBalancerName, e); } if (registerInstancesWithLBRes != null && registerInstancesWithLBRes.getInstances().size() > 0) { log.info("Total instances attached to the LB " + loadBalancerName + " : " + registerInstancesWithLBRes.getInstances().size()); } else { log.warn("No instances attached to the LB " + loadBalancerName); } }
public void registerInstances(List<Instance> instances, String lbName) { logger.info(String.format("Registering instances %s with loadbalancer %s", instances, lbName)); RegisterInstancesWithLoadBalancerRequest regInstances = new RegisterInstancesWithLoadBalancerRequest(); regInstances.setInstances(instances); regInstances.setLoadBalancerName(lbName); RegisterInstancesWithLoadBalancerResult result = elbClient.registerInstancesWithLoadBalancer(regInstances); logger.info("ELB Add instance call result: " + result.toString()); }
public List<Instance> degisterInstancesFromLB(List<Instance> toRemove, String loadBalancerName) { DeregisterInstancesFromLoadBalancerRequest request= new DeregisterInstancesFromLoadBalancerRequest(); request.setInstances(toRemove); request.setLoadBalancerName(loadBalancerName); DeregisterInstancesFromLoadBalancerResult result = elbClient.deregisterInstancesFromLoadBalancer(request); List<Instance> remaining = result.getInstances(); logger.info(String.format("ELB %s now has %s instances registered", loadBalancerName, remaining.size())); return remaining; }
private List<Instance> filterBy(List<Instance> currentInstances, List<Instance> allMatchingInstances) { List<Instance> result = new LinkedList<Instance>(); for(Instance candidate : allMatchingInstances) { if (!currentInstances.contains(candidate)) { result.add(candidate); } } return result; }
private List<Instance> removeInstances(LoadBalancerDescription elb, List<Instance> toRemove) { String loadBalancerName = elb.getLoadBalancerName(); logger.info("Removing instances from ELB " + loadBalancerName); return elbClient.degisterInstancesFromLB(toRemove,loadBalancerName); }
private List<String> currentlyRegisteredInstanceIDs( ProjectAndEnv projectAndEnv, String typeTag) throws TooManyELBException { List<Instance> registeredInstances = elbRepository.findInstancesAssociatedWithLB(projectAndEnv, typeTag); List<String> regInstanceIds = new LinkedList<>(); if (registeredInstances.isEmpty()) { logger.warn("No instances associated with ELB"); } else { for(Instance ins : registeredInstances) { regInstanceIds.add(ins.getInstanceId()); } } return regInstanceIds; }
public List<InstanceSummary> listInstances(SearchCriteria searchCriteria) throws CfnAssistException { List<InstanceSummary> result = new LinkedList<>(); List<String> instanceIds = cfnRepository.getAllInstancesFor(searchCriteria); for(String id: instanceIds) { com.amazonaws.services.ec2.model.Instance instance = cloudRepository.getInstanceById(id); InstanceSummary summary = new InstanceSummary(id, instance.getPrivateIpAddress(), instance.getTags()); result.add(summary); } return result; }
@Test public void shouldFindInstancesForProjAndEnvMakingTypeTag() throws CfnAssistException { String stackName = "testStack"; String stackId = "theIdOfTheStack"; String instanceIdA = "InstanceIdA"; String instanceIdB = "InstanceIdB"; SearchCriteria criteria = new SearchCriteria(EnvironmentSetupForTests.getMainProjectAndEnv()); List<StackResource> resources = new LinkedList<>(); resources.add(new StackResource().withResourceType("AWS::EC2::Instance").withPhysicalResourceId(instanceIdA)); resources.add(new StackResource().withResourceType("AWS::EC2::Instance").withPhysicalResourceId(instanceIdB)); Stack stack = new Stack().withTags(EnvironmentSetupForTests.createExpectedStackTags("",noBuildNumber, "CfnAssist")). withStackName(stackName). withStackId(stackId). withStackStatus(StackStatus.CREATE_COMPLETE); List<Stack> stacks = new LinkedList<>(); stacks.add(stack); EasyMock.expect(formationClient.describeAllStacks()).andReturn(stacks); EasyMock.expect(formationClient.describeStackResources(stackName)).andReturn(resources); String typeTag = "theTypeTag"; EasyMock.expect(cloudRepository.getTagsForInstance(instanceIdA)).andStubReturn(withTags("0042", typeTag)); EasyMock.expect(cloudRepository.getTagsForInstance(instanceIdB)).andStubReturn(withTags("0042", "wrongTypeTag")); replayAll(); List<Instance> result = repository.getAllInstancesMatchingType(criteria,typeTag); assertEquals(result.size(),1); result = repository.getAllInstancesMatchingType(criteria, typeTag); // cached call assertEquals(result.size(),1); assertEquals(instanceIdA, result.get(0).getInstanceId()); verifyAll(); }
@Test public void shouldUpdateELB() throws MissingArgumentException, CfnAssistException, InterruptedException, IOException { setFactoryExpectations(); Integer buildNumber = 42; String typeTag = "web"; List<Instance> instances = new LinkedList<>(); projectAndEnv.addBuildNumber(buildNumber); EasyMock.expect(facade.updateELBToInstancesMatchingBuild(projectAndEnv, typeTag)).andReturn(instances); validate(CLIArgBuilder.updateELB(typeTag, buildNumber)); }
@Test public void shouldDeleteNamedStacksNotAssociatedWithLB() throws InterruptedException, CfnAssistException { String filename = FilesForTesting.SIMPLE_STACK; File file = new File(filename); Stack stackA = new Stack().withStackName("CfnAssist0057TestsimpleStack").withStackId("idA"); Stack stackB = new Stack().withStackName("CfnAssist0058TestsimpleStack").withStackId("idB"); Stack stackC = new Stack().withStackName("CfnAssist0059TestsimpleStack").withStackId("idC"); // only this one associated with LB List<StackEntry> stacksForProj = new LinkedList<>(); stacksForProj.add(new StackEntry(project, environmentTag, stackA)); stacksForProj.add(new StackEntry(project, environmentTag, stackB)); stacksForProj.add(new StackEntry(project, environmentTag, stackC)); List<Instance> elbInstances = new LinkedList<>(); elbInstances.add(new Instance().withInstanceId("matchingInstanceId")); EasyMock.expect(elbRepository.findInstancesAssociatedWithLB(projectAndEnv,"typeTag")).andReturn(elbInstances); EasyMock.expect(cfnRepository.getStacksMatching(environmentTag,"simpleStack")).andReturn(stacksForProj); EasyMock.expect(cfnRepository.getInstancesFor(stackA.getStackName())).andReturn(createInstancesFor("123")); EasyMock.expect(cfnRepository.getInstancesFor(stackB.getStackName())).andReturn(createInstancesFor("567")); EasyMock.expect(cfnRepository.getInstancesFor(stackC.getStackName())).andReturn(createInstancesFor("matchingInstanceId")); setDeleteExpectations(stackA.getStackName(), createNameAndId(stackA)); setDeleteExpectations(stackB.getStackName(), createNameAndId(stackB)); replayAll(); aws.tidyNonLBAssocStacks(file, projectAndEnv,"typeTag"); verifyAll(); }
@Test public void shouldDeleteNamedStacksNotAssociatedWithLBWhileIgnoringStacksWithNoInstances() throws InterruptedException, CfnAssistException { String filename = FilesForTesting.SIMPLE_STACK; File file = new File(filename); Stack stackA = new Stack().withStackName("CfnAssist0057TestsimpleStack").withStackId("idA"); // this one has no instances Stack stackB = new Stack().withStackName("CfnAssist0058TestsimpleStack").withStackId("idB"); Stack stackC = new Stack().withStackName("CfnAssist0059TestsimpleStack").withStackId("idC"); // only this one associated with LB List<StackEntry> stacksForProj = new LinkedList<>(); stacksForProj.add(new StackEntry(project, environmentTag, stackA)); stacksForProj.add(new StackEntry(project, environmentTag, stackB)); stacksForProj.add(new StackEntry(project, environmentTag, stackC)); List<Instance> elbInstances = new LinkedList<>(); elbInstances.add(new Instance().withInstanceId("matchingInstanceId")); EasyMock.expect(elbRepository.findInstancesAssociatedWithLB(projectAndEnv,"typeTag")).andReturn(elbInstances); EasyMock.expect(cfnRepository.getStacksMatching(environmentTag,"simpleStack")).andReturn(stacksForProj); EasyMock.expect(cfnRepository.getInstancesFor(stackA.getStackName())).andReturn(new LinkedList<>()); EasyMock.expect(cfnRepository.getInstancesFor(stackB.getStackName())).andReturn(createInstancesFor("567")); EasyMock.expect(cfnRepository.getInstancesFor(stackC.getStackName())).andReturn(createInstancesFor("matchingInstanceId")); setDeleteExpectations(stackB.getStackName(), createNameAndId(stackB)); replayAll(); aws.tidyNonLBAssocStacks(file, projectAndEnv,"typeTag"); verifyAll(); }
@POST @Path("/{elbName}/update") public RegisterInstancesWithLoadBalancerResult addToElb(@PathParam("elbName") String elbName, @QueryParam("instanceId") String instanceId) { if (config.isPresent()) { RegisterInstancesWithLoadBalancerRequest request = new RegisterInstancesWithLoadBalancerRequest(elbName, Arrays.asList(new Instance(instanceId))); return elbClient.registerInstancesWithLoadBalancer(request); } else { throw new BaragonWebException("ElbSync and related actions are not currently enabled"); } }
@DELETE @Path("/{elbName}/update") public DeregisterInstancesFromLoadBalancerResult removeFromElb(@PathParam("elbName") String elbName, @QueryParam("instanceId") String instanceId) { if (config.isPresent()) { DeregisterInstancesFromLoadBalancerRequest request = new DeregisterInstancesFromLoadBalancerRequest(elbName, Arrays.asList(new Instance(instanceId))); return elbClient.deregisterInstancesFromLoadBalancer(request); } else { throw new BaragonWebException("ElbSync and related actions are not currently enabled"); } }
@Override public AgentCheckInResponse checkRemovedInstance(Instance instance, String trafficSourceName, String agentId) { Optional<TargetGroup> maybeTargetGroup = getTargetGroup(trafficSourceName); if (maybeTargetGroup.isPresent()) { return getShutdownResponse(maybeTargetGroup.get().getTargetGroupArn(), new TargetDescription().withId(instance.getInstanceId()), false); } else { String message = String.format("Could not find target group %s", trafficSourceName); LOG.error(message); return new AgentCheckInResponse(TrafficSourceState.ERROR, Optional.of(message), 0L); } }
@Override public AgentCheckInResponse checkRegisteredInstance(Instance instance, String trafficSourceName, BaragonAgentMetadata agent) { Optional<TargetGroup> maybeTargetGroup = getTargetGroup(trafficSourceName); if (maybeTargetGroup.isPresent()) { return instanceHealthResponse(new TargetDescription().withId(instance.getInstanceId()), maybeTargetGroup.get(), instance.getInstanceId()); } else { String message = String.format("Could not find target group %s", trafficSourceName); LOG.error(message); return new AgentCheckInResponse(TrafficSourceState.ERROR, Optional.of(message), 0L); } }
public AgentCheckInResponse removeInstance(Instance instance, String elbName, String agentId) { Optional<LoadBalancerDescription> elb = getElb(elbName); if (elb.isPresent()) { if (elb.get().getInstances().contains(instance)) { DeregisterInstancesFromLoadBalancerRequest request = new DeregisterInstancesFromLoadBalancerRequest(elbName, Arrays.asList(instance)); elbClient.deregisterInstancesFromLoadBalancer(request); LOG.info("Deregistered instance {} from ELB {}", request.getInstances(), request.getLoadBalancerName()); } else { LOG.debug("Agent {} already de-registered from ELB {}", agentId, elbName); } } return new AgentCheckInResponse(TrafficSourceState.DONE, Optional.absent(), 0L); }