@Override public Map<AvailabilityZone, List<Instance>> getInstancesMapForZone( AvailabilityZone zone, AmazonEC2Client client) throws Exception { OperationStats op = new OperationStats("ec2InstanceStore", "getInstancesMapForZone"); try { Map<AvailabilityZone, List<Instance>> ret = new HashMap<>(); ret.put(zone, getInstancesForZone(zone, client)); op.succeed(); return ret; } catch (Exception e) { op.failed(); logger.error(ExceptionUtils.getRootCauseMessage(e)); throw e; } }
@Override public Map<AvailabilityZone, List<ReservedInstances>> getReservedInstancesForZone( AvailabilityZone zone, AmazonEC2Client client) throws Exception { OperationStats op = new OperationStats("ec2InstanceStore", "getReservedInstancesForZone"); try { Map<AvailabilityZone, List<ReservedInstances>> ret = new HashMap<>(); DescribeReservedInstancesRequest request = new DescribeReservedInstancesRequest() .withFilters(new Filter("availability-zone", Arrays.asList(zone.getZoneName()))) .withSdkClientExecutionTimeout( 600 * 1000) //10 minutes time out for total execution including retries .withSdkRequestTimeout(300 * 1000); //5 minutes time out for a single request DescribeReservedInstancesResult result = client.describeReservedInstances(request); ret.put(zone, result.getReservedInstances()); op.succeed(); return ret; } catch (Exception e) { op.failed(); logger.error(ExceptionUtils.getRootCauseMessage(e)); throw e; } }
@Override public void handleSuccess(DescribeAvailabilityZonesRequest request, DescribeAvailabilityZonesResult result) { List<AvailabilityZone> zones = result.getAvailabilityZones(); if (zones == null || zones.isEmpty()) { this.service .logFine(() -> "No AvailabilityZones found. Nothing to be created locally"); this.context.refreshSubStage = this.next; this.service.processRefreshSubStages(this.context); return; } loadLocalResources(this.service, this.context, zones.stream() .map(AvailabilityZone::getZoneName) .collect(Collectors.toList()), cm -> createMissingLocalInstances(zones, cm), cm -> { this.service.logFine(() -> "No AvailabilityZones found. Nothing to be" + " created locally"); this.context.refreshSubStage = this.next; this.service.processRefreshSubStages(this.context); }); }
private ComputeDescription createComputeDescription(AvailabilityZone z) { ComputeDescriptionService.ComputeDescription cd = Utils .clone(this.context.parentCompute.description); cd.supportedChildren = new ArrayList<>(); cd.supportedChildren.add(ComputeType.VM_GUEST.toString()); cd.documentSelfLink = null; cd.id = z.getZoneName(); cd.zoneId = z.getZoneName(); cd.name = z.getZoneName(); cd.regionId = z.getRegionName(); cd.endpointLink = this.context.request.original.endpointLink; if (cd.endpointLinks == null) { cd.endpointLinks = new HashSet<>(); } cd.computeHostLink = this.context.request.parentCompute.documentSelfLink; cd.endpointLinks.add(this.context.request.original.endpointLink); // Book keeping information about the creation of the compute description in the system. if (cd.customProperties == null) { cd.customProperties = new HashMap<>(); } cd.customProperties.put(SOURCE_TASK_LINK, ResourceEnumerationTaskService.FACTORY_LINK); return cd; }
@Test public void testGetAvailabilityZones() { String zoneName = "zone-name"; when(ec2Client.describeAvailabilityZones()).thenReturn( new DescribeAvailabilityZonesResult() .withAvailabilityZones( new AvailabilityZone() .withZoneName(zoneName) .withState(AvailabilityZoneState.Available), new AvailabilityZone() .withZoneName("not-available-zone") .withState(AvailabilityZoneState.Unavailable) ) ); // invoke method under test List<String> results = ec2Service.getAvailabilityZones(); assertEquals(1, results.size()); assertEquals(zoneName, results.get(0)); }
/** * Returns a mapping of instance types to on-demand prices for the given AZ and instance types. The on-demand prices are retrieved from database * configurations. The on-demand prices are looked up by the AZ's region name. This method also validates that the given instance types are real instance * types supported by AWS. * * @param availabilityZone the availability zone of the on-demand instances * @param instanceTypes the sizes of the on-demand instances * * @return the map of instance type to on-demand price * @throws ObjectNotFoundException when any of the instance type was not found in the given region */ private Map<String, BigDecimal> getInstanceTypeOnDemandPrices(AvailabilityZone availabilityZone, Set<String> instanceTypes) { Map<String, BigDecimal> instanceTypeOnDemandPrices = new HashMap<>(); for (String instanceType : instanceTypes) { Ec2OnDemandPricingEntity onDemandPrice = ec2OnDemandPricingDao.getEc2OnDemandPricing(availabilityZone.getRegionName(), instanceType); if (onDemandPrice == null) { throw new ObjectNotFoundException( "On-demand price for region '" + availabilityZone.getRegionName() + "' and instance type '" + instanceType + "' not found."); } instanceTypeOnDemandPrices.put(instanceType, onDemandPrice.getHourlyPrice()); } return instanceTypeOnDemandPrices; }
/** * This implementation uses the DescribeAvailabilityZones API to get the list of AZs. */ @Override public List<AvailabilityZone> getAvailabilityZonesForSubnetIds(Collection<Subnet> subnets, AwsParamsDto awsParamsDto) { Set<String> zoneNames = new HashSet<>(); for (Subnet subnet : subnets) { zoneNames.add(subnet.getAvailabilityZone()); } AmazonEC2Client ec2Client = getEc2Client(awsParamsDto); DescribeAvailabilityZonesRequest describeAvailabilityZonesRequest = new DescribeAvailabilityZonesRequest(); describeAvailabilityZonesRequest.setZoneNames(zoneNames); DescribeAvailabilityZonesResult describeAvailabilityZonesResult = ec2Operations.describeAvailabilityZones(ec2Client, describeAvailabilityZonesRequest); return describeAvailabilityZonesResult.getAvailabilityZones(); }
public static List<String> getAllAvailabilityZones() { connect(); DescribeAvailabilityZonesRequest req = new DescribeAvailabilityZonesRequest(); ArrayList<Filter> filters = new ArrayList<Filter>(); ArrayList<String> regions = new ArrayList<String>(); regions.add(Configuration.REGION); filters.add(new Filter("region-name", regions)); req.setFilters(filters); DescribeAvailabilityZonesResult res = client.describeAvailabilityZones(req); List<AvailabilityZone> zones = res.getAvailabilityZones(); ArrayList<String> zonesStr = new ArrayList<String>(); for (AvailabilityZone zone : zones) zonesStr.add(zone.getZoneName()); return zonesStr; }
private Zone getZoneByName(String zoneName) { checkState(!isNullOrEmpty(zoneName)); try { DescribeAvailabilityZonesResult zones = ec2_ .describeAvailabilityZones(new DescribeAvailabilityZonesRequest() .withZoneNames(zoneName) .withFilters(new Filter().withName("region-name").withValues(credentials_.getRegion().getName()))); if (zones != null && zones.getAvailabilityZones().size() == 1) { //available | impaired | unavailable AvailabilityZone availabilityZone = zones.getAvailabilityZones().get(0); return new Zone().setName(availabilityZone.getZoneName()).setRegion(credentials_.getRegion()).setStatus(availabilityZone.getState()); } } catch (AmazonClientException exception) { LOG.debug("Invalid zone [{}]! Error message: [{}]", zoneName, exception.getMessage(), exception); } return null; }
private List<Parameter> fetchAutopopulateParametersFor(ProjectAndEnv projectAndEnv, List<TemplateParameter> declaredParameters, Map<String, AvailabilityZone> zones) throws IOException, InvalidStackParameterException, CannotFindVpcException { logger.info(String.format("Discover and populate parameters for %s and %s", templateFile.getAbsolutePath(), projectAndEnv)); List<Parameter> matches = new LinkedList<>(); for(TemplateParameter templateParam : declaredParameters) { String name = templateParam.getParameterKey(); if (isBuiltInParamater(name)) { continue; } logger.info("Checking if parameter should be auto-populated from an existing resource, param name is " + name); String description = templateParam.getDescription(); if (shouldPopulateFor(description)) { populateParameter(projectAndEnv, matches, name, description, declaredParameters, zones); } } return matches; }
private void populateParamForZone(Collection<Parameter> results, List<TemplateParameter> declaredParameters, Map<String, AvailabilityZone> zones, String parameterName, String parameterDescription) { logger.info(String.format("Check parameter for zone %s and target %s", parameterName, parameterDescription)); String target = parameterDescription.replaceFirst(PopulatesParameters.CFN_TAG_ZONE, "").toLowerCase(); logger.debug("Check for zone " + target); if (zones.containsKey(target)) { String zoneName = zones.get(target).getZoneName(); declaredParameters.stream().filter(declaredParameter -> declaredParameter.getParameterKey().equals(parameterName)). forEach(declaredParameter -> { addParameterTo(results, declaredParameters, parameterName, zoneName); logger.info(String.format("Adding zone parameter %s with value %s", parameterName, zoneName)); }); } else { logger.error("Could not find matching zone for target " + target); } }
@Test public void shouldApplySimpleTemplateNoParameters() throws CfnAssistException, IOException, InterruptedException { String filename = FilesForTesting.SIMPLE_STACK; String stackName = "CfnAssistTestsimpleStack"; String contents = EnvironmentSetupForTests.loadFile(filename); List<TemplateParameter> templateParameters = new LinkedList<>(); Collection<Parameter> creationParameters = new LinkedList<>(); Map<String, AvailabilityZone> zones = new HashMap<>(); StackNameAndId stackNameAndId = SetCreateExpectations(stackName, contents, templateParameters, creationParameters, zones); replayAll(); StackNameAndId result = aws.applyTemplate(filename, projectAndEnv); assertEquals(result, stackNameAndId); verifyAll(); }
@Test public void shouldApplySimpleTemplateParametersWithOutDescriptions() throws CfnAssistException, IOException, InterruptedException { String filename = FilesForTesting.SIMPLE_STACK; String stackName = "CfnAssistTestsimpleStack"; String contents = EnvironmentSetupForTests.loadFile(filename); List<TemplateParameter> templateParameters = new LinkedList<>(); templateParameters.add(new TemplateParameter().withParameterKey("noDescription").withDefaultValue("defaultValue")); Collection<Parameter> creationParameters = new LinkedList<>(); Map<String, AvailabilityZone> zones = new HashMap<>(); StackNameAndId stackNameAndId = SetCreateExpectations(stackName, contents, templateParameters, creationParameters, zones); replayAll(); StackNameAndId result = aws.applyTemplate(filename, projectAndEnv); assertEquals(result, stackNameAndId); verifyAll(); }
@Test public void shouldApplySimpleTemplateOutputParameters() throws CfnAssistException, IOException, InterruptedException { String filename = FilesForTesting.SIMPLE_STACK; String stackName = "CfnAssistTestsimpleStack"; String contents = EnvironmentSetupForTests.loadFile(filename); List<TemplateParameter> templateParameters = new LinkedList<>(); Collection<Parameter> creationParameters = new LinkedList<>(); Collection<Output> outputs = new LinkedList<>(); outputs.add(new Output().withDescription("::CFN_TAG").withOutputKey("outputKey").withOutputValue("outputValue")); outputs.add(new Output().withDescription("something").withOutputKey("ignored").withOutputValue("noThanks")); Map<String, AvailabilityZone> zones = new HashMap<>(); StackNameAndId stackNameAndId = SetCreateExpectations(stackName, contents, templateParameters, creationParameters, "", outputs, zones); vpcRepository.setVpcTag(projectAndEnv, "outputKey", "outputValue"); EasyMock.expectLastCall(); replayAll(); StackNameAndId result = aws.applyTemplate(filename, projectAndEnv); assertEquals(result, stackNameAndId); verifyAll(); }
@Test public void shouldApplySimpleTemplateNoParametersWithComment() throws CfnAssistException, IOException, InterruptedException { String filename = FilesForTesting.SIMPLE_STACK; String stackName = "CfnAssistTestsimpleStack"; String contents = EnvironmentSetupForTests.loadFile(filename); List<TemplateParameter> templateParameters = new LinkedList<>(); Collection<Parameter> creationParameters = new LinkedList<>(); Map<String, AvailabilityZone> zones = new HashMap<>(); StackNameAndId stackNameAndId = SetCreateExpectations(stackName, contents, templateParameters, creationParameters, "aComment", zones); projectAndEnv.setComment("aComment"); replayAll(); StackNameAndId result = aws.applyTemplate(filename, projectAndEnv); assertEquals(result, stackNameAndId); verifyAll(); }
@Test public void shouldApplySimpleTemplateEnvAndVpcBuiltInParamsWithBuild() throws CfnAssistException, IOException, InterruptedException { String filename = FilesForTesting.SIMPLE_STACK; String stackName = "CfnAssist43TestsimpleStack"; String contents = EnvironmentSetupForTests.loadFile(filename); List<TemplateParameter> templateParameters = new LinkedList<>(); templateParameters.add(new TemplateParameter().withParameterKey("vpc")); templateParameters.add(new TemplateParameter().withParameterKey("env")); templateParameters.add(new TemplateParameter().withParameterKey("build")); Collection<Parameter> creationParameters = new LinkedList<>(); projectAndEnv.addBuildNumber(43); addParam(creationParameters, "env", projectAndEnv.getEnv()); addParam(creationParameters, "vpc", VPC_ID); addParam(creationParameters, "build", "43"); Map<String, AvailabilityZone> zones = new HashMap<>(); StackNameAndId stackNameAndId = SetCreateExpectations(stackName, contents, templateParameters, creationParameters, zones); replayAll(); StackNameAndId result = aws.applyTemplate(filename, projectAndEnv); assertEquals(result, stackNameAndId); verifyAll(); }
@Test public void shouldApplySimpleTemplateInputParameters() throws CfnAssistException, IOException, InterruptedException { String filename = FilesForTesting.SIMPLE_STACK; String stackName = "CfnAssistTestsimpleStack"; String contents = EnvironmentSetupForTests.loadFile(filename); List<TemplateParameter> templateParameters = new LinkedList<>(); Collection<Parameter> creationParameters = new LinkedList<>(); addParam(creationParameters, "subnet", "subnetValue"); Map<String, AvailabilityZone> zones = new HashMap<>(); StackNameAndId stackNameAndId = SetCreateExpectations(stackName, contents, templateParameters, creationParameters, zones); replayAll(); List<Parameter> userParams = new LinkedList<>(); addParam(userParams, "subnet", "subnetValue"); StackNameAndId result = aws.applyTemplate(new File(filename), projectAndEnv, userParams); assertEquals(result, stackNameAndId); verifyAll(); }
@Test public void shouldApplySimpleTemplateEnvVarParameters() throws CfnAssistException, IOException, InterruptedException { String filename = FilesForTesting.SIMPLE_STACK; String stackName = "CfnAssistTestsimpleStack"; String contents = EnvironmentSetupForTests.loadFile(filename); List<TemplateParameter> templateParameters = new LinkedList<>(); templateParameters.add(new TemplateParameter().withParameterKey("testEnvVar").withDescription("::ENV")); Collection<Parameter> creationParameters = new LinkedList<>(); addParam(creationParameters, "testEnvVar", "testValue"); Map<String, AvailabilityZone> zones = new HashMap<>(); StackNameAndId stackNameAndId = SetCreateExpectations(stackName, contents, templateParameters, creationParameters, zones); replayAll(); List<Parameter> userParams = new LinkedList<>(); StackNameAndId result = aws.applyTemplate(new File(filename), projectAndEnv, userParams); assertEquals(result, stackNameAndId); verifyAll(); }
@Test public void shouldApplySimpleTemplateEnvVarParametersNoEchoSet() throws CfnAssistException, IOException, InterruptedException { String filename = FilesForTesting.SIMPLE_STACK; String stackName = "CfnAssistTestsimpleStack"; String contents = EnvironmentSetupForTests.loadFile(filename); List<TemplateParameter> templateParameters = new LinkedList<>(); templateParameters.add(new TemplateParameter().withParameterKey("testEnvVar"). withDescription("::ENV").withNoEcho(true)); Collection<Parameter> creationParameters = new LinkedList<>(); addParam(creationParameters, "testEnvVar", "testValue"); Map<String, AvailabilityZone> zones = new HashMap<>(); StackNameAndId stackNameAndId = SetCreateExpectations(stackName, contents, templateParameters, creationParameters, zones); replayAll(); List<Parameter> userParams = new LinkedList<>(); StackNameAndId result = aws.applyTemplate(new File(filename), projectAndEnv, userParams); assertEquals(result, stackNameAndId); verifyAll(); }
@Test public void shouldApplySimpleTemplateInputParametersNotPassBuild() throws CfnAssistException, IOException, InterruptedException { String filename = FilesForTesting.SIMPLE_STACK; String stackName = "CfnAssist56TestsimpleStack"; String contents = EnvironmentSetupForTests.loadFile(filename); List<TemplateParameter> templateParameters = new LinkedList<>(); Collection<Parameter> creationParameters = new LinkedList<>(); addParam(creationParameters, "subnet", "subnetValue"); projectAndEnv.addBuildNumber(56); Map<String, AvailabilityZone> zones = new HashMap<>(); StackNameAndId stackNameAndId = SetCreateExpectations(stackName, contents, templateParameters, creationParameters, zones); replayAll(); List<Parameter> userParams = new LinkedList<>(); addParam(userParams, "subnet", "subnetValue"); StackNameAndId result = aws.applyTemplate(new File(filename), projectAndEnv, userParams); assertEquals(result, stackNameAndId); verifyAll(); }
@Test public void shouldApplySimpleTemplateEnvAndVpcBuiltInAndUserParams() throws CfnAssistException, IOException, InterruptedException { String filename = FilesForTesting.SIMPLE_STACK; String stackName = "CfnAssistTestsimpleStack"; String contents = EnvironmentSetupForTests.loadFile(filename); List<TemplateParameter> templateParameters = new LinkedList<>(); templateParameters.add(new TemplateParameter().withParameterKey("vpc")); templateParameters.add(new TemplateParameter().withParameterKey("env")); Collection<Parameter> creationParameters = new LinkedList<>(); addParam(creationParameters, "subnet", "subnetValue"); addParam(creationParameters, "env", projectAndEnv.getEnv()); addParam(creationParameters, "vpc", VPC_ID); Map<String, AvailabilityZone> zones = new HashMap<>(); StackNameAndId stackNameAndId = SetCreateExpectations(stackName, contents, templateParameters, creationParameters, zones); List<Parameter> userParams = new LinkedList<>(); addParam(userParams, "subnet", "subnetValue"); replayAll(); StackNameAndId result = aws.applyTemplate(new File(filename), projectAndEnv, userParams); assertEquals(result, stackNameAndId); verifyAll(); }
@Test public void shouldApplySimpleTemplateEnvAndVpcBuiltInParams() throws CfnAssistException, IOException, InterruptedException { String filename = FilesForTesting.SIMPLE_STACK; String stackName = "CfnAssistTestsimpleStack"; String contents = EnvironmentSetupForTests.loadFile(filename); List<TemplateParameter> templateParameters = new LinkedList<>(); templateParameters.add(new TemplateParameter().withParameterKey("vpc")); templateParameters.add(new TemplateParameter().withParameterKey("env")); Collection<Parameter> creationParameters = new LinkedList<>(); addParam(creationParameters, "env", projectAndEnv.getEnv()); addParam(creationParameters, "vpc", VPC_ID); Map<String, AvailabilityZone> zones = new HashMap<>(); StackNameAndId stackNameAndId = SetCreateExpectations(stackName, contents, templateParameters, creationParameters, zones); replayAll(); StackNameAndId result = aws.applyTemplate(filename, projectAndEnv); assertEquals(result, stackNameAndId); verifyAll(); }
private StackNameAndId SetCreateExpectations(String stackName, String contents, List<TemplateParameter> templateParameters, Collection<Parameter> creationParameters, String comment, Collection<Output> outputs, Map<String, AvailabilityZone> zones) throws CfnAssistException, InterruptedException { StackNameAndId stackNameAndId = new StackNameAndId(stackName, "stackId"); Stack stack = new Stack().withStackId("stackId"); if (outputs.size()>0) { stack.setOutputs(outputs); } EasyMock.expect(vpcRepository.getCopyOfVpc(projectAndEnv)).andReturn(new Vpc().withVpcId(VPC_ID)); EasyMock.expect(cfnRepository.validateStackTemplate(contents)).andReturn(templateParameters); EasyMock.expect(cfnRepository.getStackStatus(stackName)).andReturn(""); Tagging tagging = new Tagging(); tagging.setCommentTag(comment); EasyMock.expect(cfnRepository.createStack(projectAndEnv, contents, stackName, creationParameters, monitor, tagging)). andReturn(stackNameAndId); EasyMock.expect(cloudRepository.getZones()).andReturn(zones); EasyMock.expect(monitor.waitForCreateFinished(stackNameAndId)).andReturn(CREATE_COMP_STATUS); EasyMock.expect(identityProvider.getUserId()).andReturn(user); CFNAssistNotification notification = new CFNAssistNotification(stackName, CREATE_COMP_STATUS, user); EasyMock.expect(notificationSender.sendNotification(notification)).andReturn("sendMessageID"); EasyMock.expect(cfnRepository.createSuccess(stackNameAndId)).andReturn(stack); return stackNameAndId; }
@Override public List<Instance> getInstances(Region region) throws Exception { List<Instance> ret = new ArrayList<>(); List<AvailabilityZone> zones = getAvailabilityZones(region); AmazonEC2Client client = getClient(region); ExecutorService executor = Executors.newFixedThreadPool(zones.size()); try { List<Callable<List<Instance>>> retrieveFunction = new ArrayList<>(); for (AvailabilityZone zone : zones) { retrieveFunction.add(new Callable<List<Instance>>() { @Override public List<Instance> call() throws Exception { return getInstancesForZone(zone, client); } }); } List<Future<List<Instance>>> futures = executor.invokeAll(retrieveFunction); for (Future<List<Instance>> future : futures) { ret.addAll(future.get()); } } finally { executor.shutdown(); } return ret; }
@Override public List<AvailabilityZone> getAvailabilityZones(Region region) throws Exception { OperationStats op = new OperationStats("ec2InstanceStore", "getAvailabilityZones"); try { DescribeAvailabilityZonesRequest request = new DescribeAvailabilityZonesRequest(); DescribeAvailabilityZonesResult result = getClient(region).describeAvailabilityZones(); List<AvailabilityZone> ret = result.getAvailabilityZones(); op.succeed(); return ret; } catch (Exception ex) { op.failed(); throw ex; } }
@Override public List<Instance> getInstancesForZone(AvailabilityZone zone, AmazonEC2Client client) throws Exception { OperationStats op = new OperationStats("ec2InstanceStore", "getInstancesForZone"); try { List<Instance> ret = new ArrayList<>(); DescribeInstancesRequest request = new DescribeInstancesRequest() .withMaxResults(1000) .withFilters(new Filter("availability-zone", Arrays.asList(zone.getZoneName()))) .withSdkClientExecutionTimeout( 600 * 1000) //10 minutes time out for total execution including retries .withSdkRequestTimeout(300 * 1000); //5 minutes time out for a single request List<Reservation> reservations = new ArrayList<>(); DescribeInstancesResult result = client.describeInstances(request); while (result != null) { reservations.addAll(result.getReservations()); if (result.getNextToken() != null) { request.setNextToken(result.getNextToken()); result = client.describeInstances(request); } else { result = null; } } for (Reservation reservation : reservations) { //Reservation refers to one launch command in EC2. Most time it should //only contains one instance for (Instance inst : reservation.getInstances()) { ret.add(inst); } } op.succeed(); return ret; } catch (Exception ex) { op.failed(); throw ex; } }
/** * This function is a wrapper to the ec2 describeInstanceStatus function. * * @param region * @return List of InstanceStatus */ public List<InstanceStatus> describeInstancesStatusAsync(Region region) throws Exception { Preconditions.checkNotNull(region); List<InstanceStatus> statusList = new ArrayList<>(); List<AvailabilityZone> zones = getAvailabilityZones(region); AmazonEC2Client client = getClient(region); ExecutorService executor = Executors.newFixedThreadPool(zones.size()); OperationStats op = new OperationStats("ec2InstanceStore", "describeInstancesStatusAsync"); try { List<Callable<List<InstanceStatus>>> retrieveFunction = new ArrayList<>(zones.size()); for (AvailabilityZone zone : zones) { retrieveFunction.add(new Callable<List<InstanceStatus>>() { @Override public List<InstanceStatus> call() throws Exception { return getInstancesStatusByZone(zone, client); } }); } List<Future<List<InstanceStatus>>> futures = executor.invokeAll(retrieveFunction); for (Future<List<InstanceStatus>> future : futures) { statusList.addAll(future.get()); } op.succeed(); } catch (Exception ex) { op.failed(); throw ex; } finally { executor.shutdown(); } return statusList; }
private List<InstanceStatus> getInstancesStatusByZone(AvailabilityZone zone, AmazonEC2Client client) { // Create this list to capture paginated async results from aws sdk List<InstanceStatus> statusList = new ArrayList<>(); // Create an initial request object DescribeInstanceStatusRequest statusRequest = new DescribeInstanceStatusRequest() .withMaxResults(1000) .withFilters(new Filter("availability-zone", Arrays.asList(zone.getZoneName()))) .withSdkClientExecutionTimeout(600 * 1000) .withSdkRequestTimeout(300 * 1000); // Make the request for instanceStatus DescribeInstanceStatusResult result = client.describeInstanceStatus(statusRequest); // Until more results are available we loop through this code while (result != null) { statusList.addAll(result.getInstanceStatuses()); if (result.getNextToken() != null) { statusRequest.setNextToken(result.getNextToken()); result = client.describeInstanceStatus(statusRequest); } else { result = null; } } // Return all statuses as a list of InstanceStatus objects return statusList; }
public static void main(String[] args) { final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); DescribeRegionsResult regions_response = ec2.describeRegions(); for(Region region : regions_response.getRegions()) { System.out.printf( "Found region %s " + "with endpoint %s", region.getRegionName(), region.getEndpoint()); } DescribeAvailabilityZonesResult zones_response = ec2.describeAvailabilityZones(); for(AvailabilityZone zone : zones_response.getAvailabilityZones()) { System.out.printf( "Found availability zone %s " + "with status %s " + "in region %s", zone.getZoneName(), zone.getState(), zone.getRegionName()); } }
/** * Determines all availability zones for a region that are marked as available. * * @return List of availability zones */ public List<String> getAvailabilityZones() { final DescribeAvailabilityZonesResult result = ec2Client.describeAvailabilityZones(); return result.getAvailabilityZones() .stream() .filter(az -> AvailabilityZoneState.Available == AvailabilityZoneState.fromValue(az.getState())) .map(AvailabilityZone::getZoneName).collect(Collectors.toList()); }
/** * Returns a mapping of instance types to spot prices for the given AZ and instance types. The spot prices are retrieved from EC2 API. * <p/> * This method also validates that the given instance types are real instance types supported by AWS. * * @param availabilityZone the AZ of the spot instances * @param instanceTypes the size of the spot instances * @param awsParamsDto the AWS related parameters for access/secret keys and proxy details * * @return the mapping of instance type to spot prices * @throws ObjectNotFoundException when any of the instance type does not exist in AWS */ private Map<String, BigDecimal> getInstanceTypeSpotPrices(AvailabilityZone availabilityZone, Set<String> instanceTypes, AwsParamsDto awsParamsDto) { List<String> productDescriptions = herdStringHelper.getDelimitedConfigurationValue(ConfigurationValue.EMR_SPOT_PRICE_HISTORY_PRODUCT_DESCRIPTIONS); List<SpotPrice> spotPrices = ec2Dao.getLatestSpotPrices(availabilityZone.getZoneName(), instanceTypes, productDescriptions, awsParamsDto); Map<String, BigDecimal> instanceTypeSpotPrices = new HashMap<>(); for (SpotPrice spotPrice : spotPrices) { instanceTypeSpotPrices.put(spotPrice.getInstanceType(), new BigDecimal(spotPrice.getSpotPrice())); } return instanceTypeSpotPrices; }
/** * Converts this object into an AWS equivalent object. * * @return A new equivalent AWS object */ public AvailabilityZone toAwsObject() { AvailabilityZone availabilityZone = new AvailabilityZone(); availabilityZone.setRegionName(regionName); availabilityZone.setZoneName(zoneName); return availabilityZone; }
@Override public DescribeAvailabilityZonesResult describeAvailabilityZones(AmazonEC2Client ec2Client, DescribeAvailabilityZonesRequest describeAvailabilityZonesRequest) { List<AvailabilityZone> availabilityZones = new ArrayList<>(); List<String> requestedZoneNames = describeAvailabilityZonesRequest.getZoneNames(); // add all AZ if request is empty (this is AWS behavior) if (requestedZoneNames.isEmpty()) { requestedZoneNames.addAll(mockAvailabilityZones.keySet()); } for (String requestedZoneName : requestedZoneNames) { // ignore AZ name which does not exist (this is AWS behavior) MockAvailabilityZone mockAvailabilityZone = mockAvailabilityZones.get(requestedZoneName); if (mockAvailabilityZone != null) { availabilityZones.add(mockAvailabilityZone.toAwsObject()); } } DescribeAvailabilityZonesResult describeAvailabilityZonesResult = new DescribeAvailabilityZonesResult(); describeAvailabilityZonesResult.setAvailabilityZones(availabilityZones); return describeAvailabilityZonesResult; }
@RolesAllowed({"ROLE_ADMIN", "ROLE_USER"}) @RequestMapping( value = "/zones", method = RequestMethod.GET) public ResponseEntity getAvailabilityZones() { try { HashSet<String> availabilityZones = new HashSet<>(); for (AvailabilityZone zone : communicationService.describeAvailabilityZonesForCurrentRegion()) { availabilityZones.add(zone.getZoneName()); } return new ResponseEntity<>(availabilityZones, HttpStatus.OK); } catch (Exception e) { return new ResponseEntity<>("Failed to get list of availability zones.", HttpStatus.INTERNAL_SERVER_ERROR); } }
public synchronized List<String> availabilityZones() { if (availabilityZones == null) { DescribeAvailabilityZonesResult result = ec2.describeAvailabilityZones(); availabilityZones = result.getAvailabilityZones().stream() .filter(zone -> "available".equals(zone.getState())) .map(AvailabilityZone::getZoneName) .collect(Collectors.toList()); logger.info("availability zones => {}", availabilityZones); } return availabilityZones; }
private List<Zone> listAvailableZonesOfRegion(Region region) { checkNotNull(region); checkState(!isNullOrEmpty(region.getName())); checkState(!isNullOrEmpty(region.getEndpoint())); List<Zone> zones = newArrayList(); AmazonEC2Client client = new AmazonEC2Client(awsCredentials_); client.setEndpoint(region.getEndpoint()); try { DescribeAvailabilityZonesResult availabilityZonesResult = client.describeAvailabilityZones(new DescribeAvailabilityZonesRequest() .withFilters(new com.amazonaws.services.ec2.model.Filter().withName("region-name").withValues(region.getName()), new com.amazonaws.services.ec2.model.Filter().withName("state").withValues("available")) ); for (AvailabilityZone zone : availabilityZonesResult.getAvailabilityZones()) { zones.add(new Zone().setName(zone.getZoneName()).setRegion(region)); } Collections.sort(zones); } catch (AmazonClientException exception) { LOG.warn("Error on listing the available zones of region [name:{}, endpoint:{}]. Error message: [{}]", region.getName(), region.getEndpoint(), exception.getMessage(), exception); } return zones; }
@Override public List<AvailabilityZone> getAvailabilityZones(Long userNo, Long platformNo) { List<AvailabilityZone> zones = new ArrayList<AvailabilityZone>(); zones.add(new AvailabilityZone().withZoneName("ap-northeast-1a")); zones.add(new AvailabilityZone().withZoneName("ap-northeast-1b")); zones.add(new AvailabilityZone().withZoneName("ap-northeast-1c")); return zones; }
@Override protected AvailabilityZone convertObject(com.xerox.amazonws.ec2.AvailabilityZone from) { AvailabilityZone to = new AvailabilityZone(); to.setZoneName(from.getName()); to.setState(from.getState()); // 未実装 to.setRegionName(null); return to; }
/** * {@inheritDoc} */ @Override public List<AvailabilityZone> getAvailabilityZones(Long userNo, Long platformNo) { // アベイラビリティゾーンを取得 AwsProcessClient awsProcessClient = awsProcessClientFactory.createAwsProcessClient(userNo, platformNo); DescribeAvailabilityZonesRequest request = new DescribeAvailabilityZonesRequest(); DescribeAvailabilityZonesResult result = awsProcessClient.getEc2Client().describeAvailabilityZones(request); List<AvailabilityZone> availabilityZones = result.getAvailabilityZones(); // ソート Collections.sort(availabilityZones, Comparators.COMPARATOR_AVAILABILITY_ZONE); return availabilityZones; }
public List<AvailabilityZone> describeAvailabilityZones(AwsProcessClient awsProcessClient) { DescribeAvailabilityZonesRequest request = new DescribeAvailabilityZonesRequest(); DescribeAvailabilityZonesResult result = awsProcessClient.getEc2Client().describeAvailabilityZones(request); List<AvailabilityZone> availabilityZones = result.getAvailabilityZones(); return availabilityZones; }