com.amazonaws.services.ec2.model.AmazonEC2Exception Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.AmazonEC2Exception. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EC2Api.java    From ec2-spot-jenkins-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Auto handle instance not found exception if any and assume those instances as already terminated
 *
 * @param ec2
 * @param instanceIds
 */
public void terminateInstances(final AmazonEC2 ec2, final Set<String> instanceIds) {
    final List<String> temp = new ArrayList<>(instanceIds);
    while (temp.size() > 0) {
        // terminateInstances is idempotent so it can be called until it's successful
        try {
            ec2.terminateInstances(new TerminateInstancesRequest(temp));
            // clear as removed so we can finish
            temp.clear();
        } catch (AmazonEC2Exception exception) {
            // if we cannot find instance, that's fine assume them as terminated
            // remove from request and try again
            if (exception.getErrorCode().equals(NOT_FOUND_ERROR_CODE)) {
                final List<String> notFoundInstanceIds = parseInstanceIdsFromNotFoundException(exception.getMessage());
                if (notFoundInstanceIds.isEmpty()) {
                    // looks like we cannot parse correctly, rethrow
                    throw exception;
                }
                temp.removeAll(notFoundInstanceIds);
            }
        }
    }
}
 
Example #2
Source File: EC2ApiTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void describeInstances_shouldFailIfNotAbleToParseNotFoundExceptionFromEc2Api() {
    // given
    Set<String> instanceIds = new HashSet<>();
    instanceIds.add("i-1");
    instanceIds.add("i-f");
    instanceIds.add("i-3");

    AmazonEC2Exception notFoundException = new AmazonEC2Exception(
            "unparseable");
    notFoundException.setErrorCode("InvalidInstanceID.NotFound");

    when(amazonEC2.describeInstances(any(DescribeInstancesRequest.class)))
            .thenThrow(notFoundException);

    // when
    try {
        new EC2Api().describeInstances(amazonEC2, instanceIds);
        Assert.fail();
    } catch (AmazonEC2Exception exception) {
        Assert.assertSame(notFoundException, exception);
    }
}
 
Example #3
Source File: EC2InstanceProviderImpl.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Override
@Cacheable(cacheNames = "ec2-instance", cacheManager = "twoHoursTTLCacheManager")
public Optional<Instance> getById(final String accountId, final Region region, final String instanceId) {
    try {
        return clientProvider.getClient(AmazonEC2Client.class, accountId, region)
                .describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceId))
                .getReservations().stream()
                .flatMap(reservation -> reservation.getInstances().stream())
                .filter(instance -> Objects.equals(instance.getInstanceId(), instanceId))
                .findFirst();
    } catch (AmazonEC2Exception e) {
        if (Objects.equals(e.getErrorCode(), "InvalidInstanceID.NotFound")) {
            return Optional.empty();
        } else {
            throw e;
        }
    }
}
 
Example #4
Source File: AwsInstanceConnectorTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void mockDescribeInstancesException(String errorCode, String errorMessage) {
    when(amazonEC2Client.describeInstances(any(DescribeInstancesRequest.class))).then(invocation -> {
        AmazonEC2Exception exception = new AmazonEC2Exception("Sheep lost control");
        exception.setErrorCode(errorCode);
        exception.setErrorMessage(errorMessage);
        throw exception;
    });
}
 
Example #5
Source File: AwsInstanceConnectorTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testStopExceptionHandle() {
    mockDescribeInstancesException(INSTANCE_NOT_FOUND_ERROR_CODE, "i-1 is a sheep!");
    List<CloudInstance> mutableList = getCloudInstances().stream().collect(toCollection(ArrayList::new));
    Assertions.assertThrows(AmazonEC2Exception.class, () -> underTest.stop(authenticatedContext, List.of(), mutableList));
    Assert.assertThat(mutableList, hasSize(1));
}
 
Example #6
Source File: AwsInstanceConnectorTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartExceptionHandle() {
    mockDescribeInstancesException(INSTANCE_NOT_FOUND_ERROR_CODE, "i-1 is a sheep!");
    List<CloudInstance> mutableList = getCloudInstances().stream().collect(toCollection(ArrayList::new));
    Assertions.assertThrows(AmazonEC2Exception.class, () -> underTest.start(authenticatedContext, List.of(), mutableList));
    Assert.assertThat(mutableList, hasSize(1));
}
 
Example #7
Source File: AwsInstanceConnectorTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckExceptionHandle() {
    mockDescribeInstancesException(INSTANCE_NOT_FOUND_ERROR_CODE, "i-1 is a sheep!");
    List<CloudInstance> mutableList = getCloudInstances().stream().collect(toCollection(ArrayList::new));
    Assertions.assertThrows(AmazonEC2Exception.class, () -> underTest.check(authenticatedContext, mutableList));
    Assert.assertThat(mutableList, hasSize(1));
}
 
Example #8
Source File: AwsInstanceConnectorTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void testCheckException() {
    mockDescribeInstancesException("silence of the lambs", "would you ...");
    List<CloudInstance> list = getCloudInstances();
    Assertions.assertThrows(AmazonEC2Exception.class, () -> underTest.check(authenticatedContext, list));
    Assert.assertThat(list, hasSize(2));
}
 
Example #9
Source File: AwsInstanceConnector.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void handleEC2Exception(List<CloudInstance> vms, AmazonEC2Exception e) throws AmazonEC2Exception {
    LOGGER.debug("Exception received from AWS: ", e);
    if (e.getErrorCode().equalsIgnoreCase(INSTANCE_NOT_FOUND_ERROR_CODE)) {
        Pattern pattern = Pattern.compile("i-[a-z0-9]*");
        Matcher matcher = pattern.matcher(e.getErrorMessage());
        if (matcher.find()) {
            String doesNotExistInstanceId = matcher.group();
            LOGGER.debug("Remove instance from vms: {}", doesNotExistInstanceId);
            vms.removeIf(vm -> doesNotExistInstanceId.equals(vm.getInstanceId()));
        }
    }
    throw e;
}
 
Example #10
Source File: AwsInstanceConnector.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void doStart(List<CloudInstance> affectedVMs, AuthenticatedContext ac, List<CloudInstance> instances) {
    for (CloudInstance instance: instances) {
        try {
            start(ac, null, List.of(instance));
            affectedVMs.add(instance);
        } catch (AmazonEC2Exception e) {
            LOGGER.warn(String.format("Unable to start instance %s", instance), e);
        }
    }
}
 
Example #11
Source File: AwsInstanceConnector.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void doReboot(List<CloudInstance> affectedVMs, AmazonEC2Client amazonEC2Client, List<CloudInstance> instances) {
    for (CloudInstance instance: instances) {
        try {
            amazonEC2Client.rebootInstances(new RebootInstancesRequest().withInstanceIds(List.of(instance.getInstanceId())));
            affectedVMs.add(instance);
        } catch (AmazonEC2Exception e) {
            LOGGER.warn(String.format("Unable to reboot instance %s", instance), e);
        }
    }
}
 
Example #12
Source File: AwsPlatformResources.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private DescribeRegionsResult describeRegionsResult(AmazonEC2Client ec2Client) {
    LOGGER.debug("Getting regions");
    try {
        DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
        return ec2Client.describeRegions(describeRegionsRequest);
    } catch (AmazonEC2Exception e) {
        LOGGER.info("Failed to retrieve regions!", e);
    }
    return new DescribeRegionsResult();
}
 
Example #13
Source File: AwsPlatformResources.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private DescribeAvailabilityZonesResult describeAvailabilityZonesResult(AmazonEC2Client ec2Client, com.amazonaws.services.ec2.model.Region awsRegion) {
    try {
        DescribeAvailabilityZonesRequest describeAvailabilityZonesRequest = getDescribeAvailabilityZonesRequest(ec2Client, awsRegion);
        return ec2Client.describeAvailabilityZones(describeAvailabilityZonesRequest);
    } catch (AmazonEC2Exception e) {
        LOGGER.info("Failed to retrieve AZ from Region: {}!", awsRegion.getRegionName(), e);
    }
    return new DescribeAvailabilityZonesResult();
}
 
Example #14
Source File: AwsPlatformResources.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Override
@Cacheable(cacheNames = "cloudResourceRegionCache", key = "{ #cloudCredential?.id, #availabilityZonesNeeded }")
public CloudRegions regions(CloudCredential cloudCredential, Region region, Map<String, String> filters, boolean availabilityZonesNeeded) {
    AmazonEC2Client ec2Client = awsClient.createAccess(cloudCredential);
    Map<Region, List<AvailabilityZone>> regionListMap = new HashMap<>();
    Map<Region, String> displayNames = new HashMap<>();
    Map<Region, Coordinate> coordinates = new HashMap<>();

    DescribeRegionsResult describeRegionsResult = describeRegionsResult(ec2Client);
    String defaultRegion = awsDefaultZoneProvider.getDefaultZone(cloudCredential);

    for (com.amazonaws.services.ec2.model.Region awsRegion : describeRegionsResult.getRegions()) {
        if (region == null || Strings.isNullOrEmpty(region.value()) || awsRegion.getRegionName().equals(region.value())) {
            try {
                fetchAZsIfNeeded(availabilityZonesNeeded, ec2Client, regionListMap, awsRegion, cloudCredential);
            } catch (AmazonEC2Exception e) {
                LOGGER.info("Failed to retrieve AZ from Region: {}!", awsRegion.getRegionName(), e);
            }
            addDisplayName(displayNames, awsRegion);
            addCoordinate(coordinates, awsRegion);
        }
    }
    if (region != null && !Strings.isNullOrEmpty(region.value())) {
        defaultRegion = region.value();
    }
    return new CloudRegions(regionListMap, displayNames, coordinates, defaultRegion, true);
}
 
Example #15
Source File: AwsPlatformResources.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private List<SecurityGroup> fetchSecurityGroups(AmazonEC2Client ec2Client, DescribeSecurityGroupsRequest describeSecurityGroupsRequest) {
    try {
        return ec2Client.describeSecurityGroups(describeSecurityGroupsRequest).getSecurityGroups();
    } catch (AmazonEC2Exception e) {
        if (e.getStatusCode() == HttpStatus.BAD_REQUEST.value() || e.getStatusCode() == HttpStatus.NOT_FOUND.value()) {
            throw new PermanentlyFailedException(e.getErrorMessage(), e);
        } else {
            throw e;
        }
    }
}
 
Example #16
Source File: AwsVolumeResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private ResourceStatus getResourceStatus(DescribeVolumesResult result) {
    try {
        return result.getVolumes().stream()
                .peek(volume -> LOGGER.debug("State of volume {} is {}", volume.getVolumeId(), volume.getState()))
                .map(com.amazonaws.services.ec2.model.Volume::getState)
                .map(toResourceStatus())
                .reduce(ResourceStatus.ATTACHED, resourceStatusReducer());
    } catch (AmazonEC2Exception e) {
        if ("InvalidVolume.NotFound".equals(e.getErrorCode())) {
            return ResourceStatus.DELETED;
        }
        return ResourceStatus.FAILED;
    }
}
 
Example #17
Source File: EC2ApiTest.java    From ec2-spot-jenkins-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * NotFound exception example data
 * <p>
 * <code>
 * Single instance
 * requestId = "0fd56c54-e11a-4928-843c-9a80a24bedd1"
 * errorCode = "InvalidInstanceID.NotFound"
 * errorType = {AmazonServiceException$ErrorType@11247} "Unknown"
 * errorMessage = "The instance ID 'i-1233f' does not exist"
 * </code>
 * <p>
 * Multiple instances
 * <code>
 * ex = {AmazonEC2Exception@11233} "com.amazonaws.services.ec2.model.AmazonEC2Exception: The instance IDs 'i-1233f, i-ffffff' do not exist (Service: AmazonEC2; Status Code: 400; Error Code: InvalidInstanceID.NotFound; Request ID:)"
 * requestId = "1a353313-ef52-4626-b87b-fd828db6343f"
 * errorCode = "InvalidInstanceID.NotFound"
 * errorType = {AmazonServiceException$ErrorType@11251} "Unknown"
 * errorMessage = "The instance IDs 'i-1233f, i-ffffff' do not exist"
 * </code>
 */
@Test
public void describeInstances_shouldHandleAmazonEc2NotFoundErrorAsTerminatedInstancesAndRetry() {
    // given
    Set<String> instanceIds = new HashSet<>();
    instanceIds.add("i-1");
    instanceIds.add("i-f");
    instanceIds.add("i-3");

    AmazonEC2Exception notFoundException = new AmazonEC2Exception(
            "The instance IDs 'i-1, i-f' do not exist");
    notFoundException.setErrorCode("InvalidInstanceID.NotFound");

    final Instance instance3 = new Instance().withInstanceId("i-3")
            .withState(new InstanceState().withName(InstanceStateName.Running));
    DescribeInstancesResult describeInstancesResult2 = new DescribeInstancesResult()
            .withReservations(new Reservation().withInstances(
                    instance3));

    when(amazonEC2.describeInstances(any(DescribeInstancesRequest.class)))
            .thenThrow(notFoundException)
            .thenReturn(describeInstancesResult2);

    // when
    final Map<String, Instance> described = new EC2Api().describeInstances(amazonEC2, instanceIds);

    // then
    Assert.assertEquals(ImmutableMap.of("i-3", instance3), described);
    verify(amazonEC2).describeInstances(new DescribeInstancesRequest().withInstanceIds(Arrays.asList("i-1", "i-3", "i-f")));
    verify(amazonEC2).describeInstances(new DescribeInstancesRequest().withInstanceIds(Arrays.asList("i-3")));
    verifyNoMoreInteractions(amazonEC2);
}
 
Example #18
Source File: AwsInstanceConnectorTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testStartException() {
    mockDescribeInstancesException("silence of the lambs", "would you ...");
    Assertions.assertThrows(AmazonEC2Exception.class, () -> underTest.start(authenticatedContext, List.of(), inputList));
    Assert.assertThat(inputList, hasSize(2));
}
 
Example #19
Source File: AwsInstanceConnectorTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Test
public void testStopException() {
    mockDescribeInstancesException("silence of the lambs", "would you ...");
    Assertions.assertThrows(AmazonEC2Exception.class, () -> underTest.stop(authenticatedContext, List.of(), inputList));
    Assert.assertThat(inputList, hasSize(2));
}
 
Example #20
Source File: EC2Api.java    From ec2-spot-jenkins-plugin with Apache License 2.0 4 votes vote down vote up
private static void describeInstancesBatch(
        final AmazonEC2 ec2, final Map<String, Instance> described, final List<String> batch) {
    // we are going to modify list, so copy
    final List<String> copy = new ArrayList<>(batch);

    // just to simplify debug by having consist order
    Collections.sort(copy);

    // because instances could be terminated at any time we do multiple
    // retry to get status and all time remove from request all non found instances if any
    while (copy.size() > 0) {
        try {
            final DescribeInstancesRequest request = new DescribeInstancesRequest().withInstanceIds(copy);

            DescribeInstancesResult result;
            do {
                result = ec2.describeInstances(request);
                request.setNextToken(result.getNextToken());

                for (final Reservation r : result.getReservations()) {
                    for (final Instance instance : r.getInstances()) {
                        // if instance not in terminated state, add it to described
                        if (!TERMINATED_STATES.contains(instance.getState().getName())) {
                            described.put(instance.getInstanceId(), instance);
                        }
                    }
                }
            } while (result.getNextToken() != null);

            // all good, clear request batch to stop
            copy.clear();
        } catch (final AmazonEC2Exception exception) {
            // if we cannot find instance, that's fine assume them as terminated
            // remove from request and try again
            if (exception.getErrorCode().equals(NOT_FOUND_ERROR_CODE)) {
                final List<String> notFoundInstanceIds = parseInstanceIdsFromNotFoundException(exception.getMessage());
                if (notFoundInstanceIds.isEmpty()) {
                    // looks like we cannot parse correctly, rethrow
                    throw exception;
                }
                copy.removeAll(notFoundInstanceIds);
            }
        }
    }
}