Search in sources :

Example 1 with SpotInstanceRequest

use of com.amazonaws.services.ec2.model.SpotInstanceRequest in project cloudbreak by hortonworks.

the class AwsCheckSpotInstance method checkSpotInstance.

@Parameters({ "region", "hostGroupToCheck", "scalingAdjustment" })
@Test
public void checkSpotInstance(Regions region, String hostGroupToCheck, @Optional Integer scalingAdjustment) {
    // GIVEN
    Integer spotInstanceCount = 0;
    IntegrationTestContext itContext = getItContext();
    String stackId = itContext.getContextParam(CloudbreakITContextConstants.STACK_ID);
    StackV1Endpoint stackV1Endpoint = getCloudbreakClient().stackV1Endpoint();
    StackResponse stackResponse = stackV1Endpoint.get(Long.valueOf(stackId), new HashSet<>());
    List<InstanceGroupResponse> instanceGroups = stackResponse.getInstanceGroups();
    Collection<String> instanceIdList = new ArrayList<>();
    List<String> hostGroupList = Arrays.asList(hostGroupToCheck.split(","));
    for (InstanceGroupResponse instanceGroup : instanceGroups) {
        if (hostGroupList.contains(instanceGroup.getGroup())) {
            Set<InstanceMetaDataJson> instanceMetaData = instanceGroup.getMetadata();
            for (InstanceMetaDataJson metaData : instanceMetaData) {
                instanceIdList.add(metaData.getInstanceId());
            }
        }
    }
    // WHEN
    AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withRegion(region).build();
    DescribeSpotInstanceRequestsResult describeSpotInstanceRequestsResult = ec2.describeSpotInstanceRequests();
    List<SpotInstanceRequest> spotInstanceRequests = describeSpotInstanceRequestsResult.getSpotInstanceRequests();
    // THEN
    Assert.assertFalse(spotInstanceRequests.isEmpty());
    Collection<String> spotInstanceIdList = new ArrayList<>();
    for (SpotInstanceRequest request : spotInstanceRequests) {
        spotInstanceIdList.add(request.getInstanceId());
    }
    for (String id : instanceIdList) {
        Assert.assertTrue(spotInstanceIdList.contains(id));
        if (spotInstanceIdList.contains(id)) {
            spotInstanceCount += 1;
        }
    }
    if (scalingAdjustment != null) {
        Assert.assertNotNull(itContext.getContextParam(CloudbreakITContextConstants.INSTANCE_COUNT, List.class));
        Integer instanceCountPrev = 0;
        for (String hostGroup : hostGroupList) {
            List<Map<String, Integer>> instanceList = itContext.getContextParam(CloudbreakITContextConstants.INSTANCE_COUNT, List.class);
            Assert.assertTrue(instanceList.size() >= 2);
            instanceCountPrev += instanceList.get(instanceList.size() - 2).get(hostGroup);
        }
        Assert.assertEquals(Integer.valueOf(instanceCountPrev + scalingAdjustment), spotInstanceCount);
    }
}
Also used : StackV1Endpoint(com.sequenceiq.cloudbreak.api.endpoint.v1.StackV1Endpoint) IntegrationTestContext(com.sequenceiq.it.IntegrationTestContext) ArrayList(java.util.ArrayList) InstanceGroupResponse(com.sequenceiq.cloudbreak.api.model.InstanceGroupResponse) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) SpotInstanceRequest(com.amazonaws.services.ec2.model.SpotInstanceRequest) DescribeSpotInstanceRequestsResult(com.amazonaws.services.ec2.model.DescribeSpotInstanceRequestsResult) ArrayList(java.util.ArrayList) List(java.util.List) InstanceMetaDataJson(com.sequenceiq.cloudbreak.api.model.InstanceMetaDataJson) StackResponse(com.sequenceiq.cloudbreak.api.model.StackResponse) Map(java.util.Map) Parameters(org.testng.annotations.Parameters) Test(org.testng.annotations.Test)

Example 2 with SpotInstanceRequest

use of com.amazonaws.services.ec2.model.SpotInstanceRequest in project cloudbreak by hortonworks.

the class ASGroupStatusCheckerTask method checkForSpotRequest.

private void checkForSpotRequest(Activity activity, AmazonEC2 amazonEC2Client) {
    if (WAIT_FOR_SPOT_INSTANCES_STATUS_CODE.equals(activity.getStatusCode())) {
        Pattern pattern = Pattern.compile(SPOT_ID_PATTERN);
        Matcher matcher = pattern.matcher(activity.getStatusMessage());
        if (matcher.find()) {
            String spotId = matcher.group(0);
            DescribeSpotInstanceRequestsResult spotResult = amazonEC2Client.describeSpotInstanceRequests(new DescribeSpotInstanceRequestsRequest().withSpotInstanceRequestIds(spotId));
            Optional<SpotInstanceRequest> request = spotResult.getSpotInstanceRequests().stream().findFirst();
            if (request.isPresent()) {
                if (LOW_SPOT_PRICE_STATUS_CODE.equals(request.get().getStatus().getCode())) {
                    throw new CloudConnectorException(request.get().getStatus().getMessage());
                }
            }
        }
    }
}
Also used : DescribeSpotInstanceRequestsResult(com.amazonaws.services.ec2.model.DescribeSpotInstanceRequestsResult) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) DescribeSpotInstanceRequestsRequest(com.amazonaws.services.ec2.model.DescribeSpotInstanceRequestsRequest) SpotInstanceRequest(com.amazonaws.services.ec2.model.SpotInstanceRequest)

Aggregations

DescribeSpotInstanceRequestsResult (com.amazonaws.services.ec2.model.DescribeSpotInstanceRequestsResult)2 SpotInstanceRequest (com.amazonaws.services.ec2.model.SpotInstanceRequest)2 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)1 DescribeSpotInstanceRequestsRequest (com.amazonaws.services.ec2.model.DescribeSpotInstanceRequestsRequest)1 StackV1Endpoint (com.sequenceiq.cloudbreak.api.endpoint.v1.StackV1Endpoint)1 InstanceGroupResponse (com.sequenceiq.cloudbreak.api.model.InstanceGroupResponse)1 InstanceMetaDataJson (com.sequenceiq.cloudbreak.api.model.InstanceMetaDataJson)1 StackResponse (com.sequenceiq.cloudbreak.api.model.StackResponse)1 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 IntegrationTestContext (com.sequenceiq.it.IntegrationTestContext)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Parameters (org.testng.annotations.Parameters)1 Test (org.testng.annotations.Test)1