private DescribeSecurityGroupsResult getSecurityGroupsBy( String filterName, Set<String> filterValues, AmazonEC2 ec2) { DescribeSecurityGroupsRequest groupReq = new DescribeSecurityGroupsRequest(); groupReq.withFilters(new Filter(filterName).withValues(filterValues)); return ec2.describeSecurityGroups(groupReq); }
public static String setUpElbWithLoadGenerator(String loadGenerator) throws Exception { String loadBalancerName = "projecttwoelb"; // Create the ELB client. elbClient = new AmazonElasticLoadBalancingClient(awsCredentials()); Region usEast1 = Region.getRegion(Regions.US_EAST_1); elbClient.setRegion(usEast1); CreateLoadBalancerRequest elbRequest = new CreateLoadBalancerRequest(); elbRequest.setLoadBalancerName(loadBalancerName); // Security Group DescribeSecurityGroupsRequest sgRequest = new DescribeSecurityGroupsRequest(); ArrayList<String> list = new ArrayList<String>(); list.add("SecurityGroupForELBAndAutoscale"); sgRequest.setGroupNames(list); DescribeSecurityGroupsResult result = ec2.describeSecurityGroups(sgRequest); List<SecurityGroup> groups = result.getSecurityGroups(); ArrayList<String> securityGroups = new ArrayList<String>(); securityGroups.add(groups.get(0).getGroupId()); elbRequest.setSecurityGroups(securityGroups); // Adding tags Collection<com.amazonaws.services.elasticloadbalancing.model.Tag> tags = new ArrayList<com.amazonaws.services.elasticloadbalancing.model.Tag>(); com.amazonaws.services.elasticloadbalancing.model.Tag tag = new com.amazonaws.services.elasticloadbalancing.model.Tag(); tag.setKey("Project"); tag.setValue("2.1"); tags.add(tag); AddTagsRequest tagReq = new AddTagsRequest(); tagReq.withTags(tags); elbRequest.setTags(tags); // Specify ports List<Listener> listener = new ArrayList<Listener>(1); listener.add(new Listener("HTTP", 80, 80)); elbRequest.setListeners(listener); // Speciy availability zones List<String> zones = new ArrayList<String>(1); zones.add("us-east-1c"); elbRequest.setAvailabilityZones(zones); CreateLoadBalancerResult lbResult = elbClient.createLoadBalancer(elbRequest); // Disable Cross Zone ModifyLoadBalancerAttributesRequest attribRequest = new ModifyLoadBalancerAttributesRequest(); LoadBalancerAttributes attribs = new LoadBalancerAttributes(); CrossZoneLoadBalancing crossZone = new CrossZoneLoadBalancing(); crossZone.setEnabled(false); attribs.setCrossZoneLoadBalancing(crossZone); attribRequest.setLoadBalancerAttributes(attribs); attribRequest.setLoadBalancerName(loadBalancerName); elbClient.modifyLoadBalancerAttributes(attribRequest); // Specify Health Check String target = "HTTP:80/heartbeat?lg=" + URLEncoder.encode(loadGenerator, "UTF-8"); ConfigureHealthCheckRequest healthCheckRequest = new ConfigureHealthCheckRequest(); HealthCheck healthCheck = new HealthCheck(target, 5, 2, 2, 10); healthCheckRequest.setHealthCheck(healthCheck); healthCheckRequest.setLoadBalancerName(loadBalancerName); elbClient.configureHealthCheck(healthCheckRequest); System.out.println("Load balancer created with name: " + lbResult.getDNSName()); return lbResult.getDNSName(); }
public Request<DescribeSecurityGroupsRequest> marshall( DescribeSecurityGroupsRequest describeSecurityGroupsRequest) { if (describeSecurityGroupsRequest == null) { throw new SdkClientException("Invalid argument passed to marshall(...)"); } Request<DescribeSecurityGroupsRequest> request = new DefaultRequest<DescribeSecurityGroupsRequest>( describeSecurityGroupsRequest, "AmazonEC2"); request.addParameter("Action", "DescribeSecurityGroups"); request.addParameter("Version", "2016-11-15"); request.setHttpMethod(HttpMethodName.POST); com.amazonaws.internal.SdkInternalList<String> describeSecurityGroupsRequestGroupNamesList = (com.amazonaws.internal.SdkInternalList<String>) describeSecurityGroupsRequest.getGroupNames(); if (!describeSecurityGroupsRequestGroupNamesList.isEmpty() || !describeSecurityGroupsRequestGroupNamesList.isAutoConstruct()) { int groupNamesListIndex = 1; for (String describeSecurityGroupsRequestGroupNamesListValue : describeSecurityGroupsRequestGroupNamesList) { if (describeSecurityGroupsRequestGroupNamesListValue != null) { request.addParameter( "GroupName." + groupNamesListIndex, StringUtils.fromString(describeSecurityGroupsRequestGroupNamesListValue)); } groupNamesListIndex++; } } com.amazonaws.internal.SdkInternalList<String> describeSecurityGroupsRequestGroupIdsList = (com.amazonaws.internal.SdkInternalList<String>) describeSecurityGroupsRequest.getGroupIds(); if (!describeSecurityGroupsRequestGroupIdsList.isEmpty() || !describeSecurityGroupsRequestGroupIdsList.isAutoConstruct()) { int groupIdsListIndex = 1; for (String describeSecurityGroupsRequestGroupIdsListValue : describeSecurityGroupsRequestGroupIdsList) { if (describeSecurityGroupsRequestGroupIdsListValue != null) { request.addParameter( "GroupId." + groupIdsListIndex, StringUtils.fromString(describeSecurityGroupsRequestGroupIdsListValue)); } groupIdsListIndex++; } } com.amazonaws.internal.SdkInternalList<Filter> describeSecurityGroupsRequestFiltersList = (com.amazonaws.internal.SdkInternalList<Filter>) describeSecurityGroupsRequest.getFilters(); if (!describeSecurityGroupsRequestFiltersList.isEmpty() || !describeSecurityGroupsRequestFiltersList.isAutoConstruct()) { int filtersListIndex = 1; for (Filter describeSecurityGroupsRequestFiltersListValue : describeSecurityGroupsRequestFiltersList) { if (describeSecurityGroupsRequestFiltersListValue.getName() != null) { request.addParameter( "Filter." + filtersListIndex + ".Name", StringUtils.fromString(describeSecurityGroupsRequestFiltersListValue.getName())); } com.amazonaws.internal.SdkInternalList<String> filterValuesList = (com.amazonaws.internal.SdkInternalList<String>) describeSecurityGroupsRequestFiltersListValue.getValues(); if (!filterValuesList.isEmpty() || !filterValuesList.isAutoConstruct()) { int valuesListIndex = 1; for (String filterValuesListValue : filterValuesList) { if (filterValuesListValue != null) { request.addParameter( "Filter." + filtersListIndex + ".Value." + valuesListIndex, StringUtils.fromString(filterValuesListValue)); } valuesListIndex++; } } filtersListIndex++; } } return request; }