@Test public void testGetElbDnsName() { final LoadBalancerDescription description = new LoadBalancerDescription(); description.setDNSName("testDnsName"); final List<LoadBalancerDescription> descriptions = new ArrayList<>(); descriptions.add(description); final DescribeLoadBalancersResult result = mock(DescribeLoadBalancersResult.class); when(result.getLoadBalancerDescriptions()).thenReturn(descriptions); when(amazonElbClient.describeLoadBalancers(any(DescribeLoadBalancersRequest.class))).thenReturn(result); final String elbName = "testElbName"; assertThat(awsHelperService.getElbDnsName(elbName), equalTo(description.getDNSName())); final ArgumentCaptor<DescribeLoadBalancersRequest> argumentCaptor = ArgumentCaptor.forClass(DescribeLoadBalancersRequest.class); verify(amazonElbClient).describeLoadBalancers(argumentCaptor.capture()); assertThat(argumentCaptor.getValue().getLoadBalancerNames().get(0), equalTo(elbName)); }
/** * Test helper - makes describe result with one or more named LBs. */ private DescribeLoadBalancersResult makeDescribeLoadBalancersResult(String... loadBalancerNames) { DescribeLoadBalancersResult result = new DescribeLoadBalancersResult(); if (ArrayUtils.isNotEmpty(loadBalancerNames)) { List<LoadBalancerDescription> list = new ArrayList<LoadBalancerDescription>(); for (String loadBalancerName : loadBalancerNames) { LoadBalancerDescription loadBalancerDescription = new LoadBalancerDescription(); loadBalancerDescription.setLoadBalancerName(loadBalancerName); list.add(loadBalancerDescription); } result.setLoadBalancerDescriptions(list); } return result; }
private LoadBalancerDescription getAwsLoadBalancer(String name) { DescribeLoadBalancersRequest describeRequest = new DescribeLoadBalancersRequest() .withLoadBalancerNames(name); DescribeLoadBalancersResult describeResult = null; try { describeResult = this.client.describeLoadBalancers(describeRequest); } catch (Exception e) { this.host.log("Exception describing load balancers with name '%s': %s", name, e.toString()); } Collection<LoadBalancerDescription> lbs = describeResult != null ? describeResult.getLoadBalancerDescriptions() : null; if (lbs == null || lbs.isEmpty()) { return null; } if (lbs.size() > 1) { throw new IllegalStateException( "More than one load balancers found with name '" + name + "'."); } return lbs.iterator().next(); }
public static String getLoadBalancerDNS(String name) { if (name == null || name.trim().length() == 0) throw new RuntimeException( "The name of the load balancer cannot be empty!"); connect(); ArrayList<String> names = new ArrayList<String>(); names.add(name); DescribeLoadBalancersRequest req = new DescribeLoadBalancersRequest( names); DescribeLoadBalancersResult res = client.describeLoadBalancers(req); List<LoadBalancerDescription> descs = res.getLoadBalancerDescriptions(); if (descs.size() == 0 || descs.get(0) == null) return null; return descs.get(0).getDNSName(); }
public LoadBalancerDescription describeLoadBalancer(AwsProcessClient awsProcessClient, String loadBalancerName) { // 単一ロードバランサの参照 DescribeLoadBalancersRequest request = new DescribeLoadBalancersRequest(); request.withLoadBalancerNames(loadBalancerName); DescribeLoadBalancersResult result = awsProcessClient.getElbClient().describeLoadBalancers(request); List<LoadBalancerDescription> descriptions = result.getLoadBalancerDescriptions(); // API実行結果チェック if (descriptions.size() == 0) { // アドレスが存在しない場合 throw new AutoException("EPROCESS-000131", loadBalancerName); } else if (descriptions.size() > 1) { // アドレスを複数参照できた場合 AutoException exception = new AutoException("EPROCESS-000132", loadBalancerName); exception.addDetailInfo("result=" + descriptions); throw exception; } return descriptions.get(0); }
@GET @NoAuth @Path("/{elbName}") public LoadBalancerDescription getElb(@PathParam("elbName") String elbName) { if (config.isPresent()) { try { DescribeLoadBalancersRequest request = new DescribeLoadBalancersRequest(Arrays.asList(elbName)); DescribeLoadBalancersResult result = elbClient.describeLoadBalancers(request); for (LoadBalancerDescription elb : result.getLoadBalancerDescriptions()) { if (elb.getLoadBalancerName().equals(elbName)) { return elb; } } } catch (AmazonClientException e) { throw new BaragonWebException(String.format("AWS Client Error: %s", e)); } throw new BaragonNotFoundException(String.format("ELB with name %s not found", elbName)); } else { throw new BaragonWebException("ElbSync and related actions are not currently enabled"); } }
/** * Returns a description of the named ELB. */ public LoadBalancerDescription describeLoadBalancer(String elbName) { LOGGER.debug("describeLoadBalancers(elbName: " + elbName + ")"); if (StringUtils.isBlank(elbName)) { throw new IllegalArgumentException("Blank elbName"); } StopWatch stopWatch = new StopWatch(); try { stopWatch.start(); DescribeLoadBalancersRequest request = new DescribeLoadBalancersRequest(); request.setLoadBalancerNames(Arrays.asList(elbName)); DescribeLoadBalancersResult result = awsElbClient.describeLoadBalancers(request); if (result == null || CollectionUtils.isEmpty(result.getLoadBalancerDescriptions())) { throw new RuntimeException("ELB '" + elbName + "' was not found"); } else if (result.getLoadBalancerDescriptions().size() > 1) { LOGGER.warn("Expected 1 ELB description for elb name '" + elbName + "', found " + result.getLoadBalancerDescriptions().size()); } return result.getLoadBalancerDescriptions().get(0); } finally { stopWatch.stop(); LOGGER.debug("describeLoadBalancers time elapsed " + stopWatch); } }
private DeferredResult<AWSLoadBalancerContext> getAWSLoadBalancer( AWSLoadBalancerContext context) { DescribeLoadBalancersRequest describeRequest = new DescribeLoadBalancersRequest() .withLoadBalancerNames(context.loadBalancerStateExpanded.name); String message = "Describing AWS load balancer [" + context.loadBalancerStateExpanded.name + "]."; AWSDeferredResultAsyncHandler<DescribeLoadBalancersRequest, DescribeLoadBalancersResult> handler = new AWSDeferredResultAsyncHandler<>(this, message); context.client.describeLoadBalancersAsync(describeRequest, handler); return handler.toDeferredResult().thenCompose(result -> { List<com.amazonaws.services.elasticloadbalancing.model.LoadBalancerDescription> lbs = result .getLoadBalancerDescriptions(); if (lbs != null && !lbs.isEmpty() && lbs.size() == 1) { context.registeredInstances = lbs.iterator().next().getInstances(); return DeferredResult.completed(context); } return DeferredResult.failed(new IllegalStateException( "Unable to describe load balancer with name '" + context.loadBalancerStateExpanded.name + "' for update")); }); }
@Override protected DeferredResult<RemoteResourcesPage> getExternalResources(String nextPageLink) { DescribeLoadBalancersRequest describeRequest = new DescribeLoadBalancersRequest() .withPageSize(getQueryPageSize()); if (nextPageLink != null) { describeRequest.setMarker(nextPageLink); } String msg = "Getting AWS Load Balancers [" + this.request.original.resourceReference + "]"; AWSDeferredResultAsyncHandler<DescribeLoadBalancersRequest, DescribeLoadBalancersResult> asyncHandler = new AWSDeferredResultAsyncHandler<>(this.service, msg); this.amazonLoadBalancerClient.describeLoadBalancersAsync(describeRequest, asyncHandler); return asyncHandler.toDeferredResult().thenApply(describeLoadBalancersResult -> { RemoteResourcesPage page = new RemoteResourcesPage(); page.nextPageLink = describeLoadBalancersResult.getNextMarker(); describeLoadBalancersResult.getLoadBalancerDescriptions().forEach( lbDescription -> page.resourcesPage .put(lbDescription.getLoadBalancerName(), lbDescription)); return page; }); }
public Collection<String> getLoadBalancerList(){ DescribeLoadBalancersResult loadBalancers = this.loadBalanceClient.describeLoadBalancers(); System.out.println(loadBalancers.toString()); Collection returnList = new ArrayList<String>(); List<LoadBalancerDescription> loadBalancerDescriptions = loadBalancers.getLoadBalancerDescriptions(); for( LoadBalancerDescription desc : loadBalancerDescriptions){ returnList.add(desc.getDNSName()); } return returnList; }
private List<ListenerDescription> describeMatchingElbListeners(String loadbalancerName, List<Integer> ports) { DescribeLoadBalancersResult loadbalancer = awsElbClient.describeLoadBalancers(new DescribeLoadBalancersRequest().withLoadBalancerNames(loadbalancerName)); LoadBalancerDescription description = loadbalancer.getLoadBalancerDescriptions().get(0); return description.getListenerDescriptions().stream() .filter(d -> ports.contains(d.getListener().getLoadBalancerPort())) .filter(d -> d.getListener().getProtocol().startsWith("HTTP")) .collect(Collectors.toList()); }
@Override public List<AbstractResource<?>> describeLoadBalancers(Account account, Region region, DateTime dt) { AmazonElasticLoadBalancing elb = findClient(account, region); DescribeLoadBalancersRequest req = new DescribeLoadBalancersRequest(); log.debug("start describing elb for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeLoadBalancersResult res = elb.describeLoadBalancers(req); return converter.toLoadBalancers(res.getLoadBalancerDescriptions(), account.getId(), region, dt); }
public List<LoadBalancerDescription> describeLoadBalancers() { DescribeLoadBalancersRequest request = new DescribeLoadBalancersRequest(); DescribeLoadBalancersResult result = elbClient.describeLoadBalancers(request); List<LoadBalancerDescription> descriptions = result.getLoadBalancerDescriptions(); logger.info(String.format("Found %s load balancers %s", descriptions.size(), descriptions)); return descriptions; }
private Optional<LoadBalancerDescription> getElb(String elbName) { DescribeLoadBalancersRequest request = new DescribeLoadBalancersRequest(Arrays.asList(elbName)); DescribeLoadBalancersResult result = elbClient.describeLoadBalancers(request); if (!result.getLoadBalancerDescriptions().isEmpty()) { return Optional.of(result.getLoadBalancerDescriptions().get(0)); } else { return Optional.absent(); } }
/** * Return the DNS name for a given AWS ELB group name * @param elbName the ELB group name * @return String DNS name */ public String getElbDnsName(String elbName) { DescribeLoadBalancersResult result = amazonElbClient.describeLoadBalancers(new DescribeLoadBalancersRequest() .withLoadBalancerNames(elbName)); return result.getLoadBalancerDescriptions().get(0).getDNSName(); }
private void setupMock(DescribeLoadBalancersResult fakeResult) { when(mockELBClient.describeLoadBalancers(any(DescribeLoadBalancersRequest.class))).thenReturn(fakeResult); }
@Before public void setUp() throws Exception { this.violationSinkMock = mock(ViolationSink.class); this.clientProviderMock = mock(ClientProvider.class); this.accountIdSupplierMock = mock(AccountIdSupplier.class); this.jobsPropertiesMock = mock(JobsProperties.class); this.portsChecker = mock(PortsChecker.class); this.securityGroupsChecker = mock(SecurityGroupsChecker.class); this.mockAwsELBClient = mock(AmazonElasticLoadBalancingClient.class); this.mockAwsApplications = mock(AwsApplications.class); this.mockViolationService = mock(ViolationService.class); this.fetchTaupageYamlMock = mock(FetchTaupageYaml.class); this.mockAmiDetailsProvider = mock(AmiDetailsProvider.class); this.mockEC2InstanceProvider = mock(EC2InstanceProvider.class); final Listener listener = new Listener("HTTPS", 80, 80); final ListenerDescription listenerDescription = new ListenerDescription(); listenerDescription.setListener(listener); final ArrayList<LoadBalancerDescription> elbs = newArrayList(); final ArrayList<TagDescription> tagDescriptions = newArrayList(); final LoadBalancerDescription publicELB = new LoadBalancerDescription(); publicELB.setScheme("internet-facing"); publicELB.setListenerDescriptions(newArrayList(listenerDescription)); publicELB.setCanonicalHostedZoneName("test.com"); publicELB.setInstances(asList(new Instance("i1"), new Instance("i2"))); publicELB.setLoadBalancerName("publicELB"); elbs.add(publicELB); tagDescriptions.add( new TagDescription() .withLoadBalancerName("publicELB") .withTags(newArrayList( new Tag().withKey("someTag").withValue("someValue")))); final LoadBalancerDescription privateELB = new LoadBalancerDescription(); privateELB.setScheme("internal"); privateELB.setCanonicalHostedZoneName("internal.org"); privateELB.setLoadBalancerName("privateELB"); elbs.add(privateELB); for (int i = 1; i <= 20; i++) { final String loadBalancerName = "kubeELB" + i; final LoadBalancerDescription kubeELB = new LoadBalancerDescription(); kubeELB.setScheme("internet-facing"); kubeELB.setCanonicalHostedZoneName("test" + i + ".com"); kubeELB.setLoadBalancerName(loadBalancerName); elbs.add(kubeELB); tagDescriptions.add( new TagDescription() .withLoadBalancerName(loadBalancerName) .withTags(newArrayList( new Tag().withKey("someTag").withValue("someValue"), new Tag().withKey("kubernetes.io/cluster/").withValue("owned")))); } mockDescribeELBResult = new DescribeLoadBalancersResult(); mockDescribeELBResult.setLoadBalancerDescriptions(elbs); mockDescribeTagsResult = new DescribeTagsResult(); mockDescribeTagsResult.setTagDescriptions(tagDescriptions); regions.add(REGION1); when(clientProviderMock.getClient(any(), any(String.class), any(Region.class))).thenReturn(mockAwsELBClient); when(mockEC2InstanceProvider.getById(anyString(), any(Region.class), anyString())) .thenReturn(Optional.of(new com.amazonaws.services.ec2.model.Instance().withInstanceId("foo").withImageId("bar"))); when(mockAmiDetailsProvider.getAmiDetails(anyString(), any(Region.class), anyString())) .thenReturn(ImmutableMap.of("ami_id", "bar")); }
public LoadBalancerDescription describeELB(String elbName) { logger.info("describe elb, elbName={}", elbName); DescribeLoadBalancersResult result = elb.describeLoadBalancers(new DescribeLoadBalancersRequest() .withLoadBalancerNames(elbName)); return result.getLoadBalancerDescriptions().get(0); }
public List<LoadBalancerDescription> listELBs() { logger.info("list all elbs"); DescribeLoadBalancersResult result = elb.describeLoadBalancers(); return result.getLoadBalancerDescriptions(); }