private DeferredResult<AWSLoadBalancerContext> createLoadBalancer( AWSLoadBalancerContext context) { CreateLoadBalancerRequest request = buildCreationRequest(context); String message = "Create a new AWS Load Balancer with name [" + context.loadBalancerStateExpanded.name + "]."; AWSDeferredResultAsyncHandler<CreateLoadBalancerRequest, CreateLoadBalancerResult> handler = new AWSDeferredResultAsyncHandler<>(this, message); context.client.createLoadBalancerAsync(request, handler); return handler.toDeferredResult().thenApply(result -> { context.loadBalancerStateExpanded.address = result.getDNSName(); return context; }); }
public static String provisionAWSLoadBalancerWithEC2Client(VerificationHost host, AmazonElasticLoadBalancingAsyncClient client, String name, String subnetId, String securityGroupId, List<String> instanceIds) { if (name == null) { name = LOAD_BALANCER_NAME_PREFIX + System.currentTimeMillis(); } Listener defaultListener = new Listener().withInstancePort(80) .withInstanceProtocol("HTTP") .withLoadBalancerPort(80) .withProtocol("HTTP"); CreateLoadBalancerRequest request = new CreateLoadBalancerRequest() .withLoadBalancerName(name) .withSubnets(Collections.singletonList(subnetId)) .withSecurityGroups(Collections.singleton(securityGroupId)) .withListeners(Collections.singleton(defaultListener)); CreateLoadBalancerResult result = null; try { result = client.createLoadBalancer(request); } catch (Exception e) { host.log(Level.SEVERE, "Error encountered in provisioning load balancer on AWS: %s", Utils.toString(e)); } assertNotNull(result); assertNotNull(result.getDNSName()); if (instanceIds != null) { registerAWSInstancesToLoadBalancer(host, client, name, instanceIds); } return name; }
public static String createNewLoadBalancer(String name, Listener... listeners) { if (name == null || name.trim().length() == 0) throw new RuntimeException( "The name of the load balancer cannot be empty!"); connect(); CreateLoadBalancerRequest req = new CreateLoadBalancerRequest(); req.setLoadBalancerName(name); if (listeners != null && listeners.length > 0) { ArrayList<com.amazonaws.services.elasticloadbalancing.model.Listener> actualListeners = new ArrayList<com.amazonaws.services.elasticloadbalancing.model.Listener>(); for (Listener listener : listeners) actualListeners .add(new com.amazonaws.services.elasticloadbalancing.model.Listener( listener.protocol, listener.port, listener.port)); req.setListeners(actualListeners); } ArrayList<String> securityGroups = new ArrayList<String>(); securityGroups.add(AmazonEC2.getSecurityGroupId()); req.setSecurityGroups(securityGroups); req.setAvailabilityZones(AmazonEC2.getAllAvailabilityZones()); CreateLoadBalancerResult res = client.createLoadBalancer(req); return res.getDNSName(); }
CreateLoadBalancerResult createLoadBalancer(AWSCredentials credentials, String name, String... zones);