Search in sources :

Example 1 with ValidationError

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

the class AwsGatewaySubnetMultiAzValidator method validate.

@Override
public void validate(AuthenticatedContext ac, CloudStack cloudStack) {
    LOGGER.debug("Validating that only one subnet per availability zone have been provided for the gateway group.");
    AwsCredentialView awsCredential = new AwsCredentialView(ac.getCloudCredential());
    String region = ac.getCloudContext().getLocation().getRegion().value();
    Optional<Group> gatewayGroupOptional = getGatewayGroup(cloudStack);
    if (gatewayGroupOptional.isPresent()) {
        Group group = gatewayGroupOptional.get();
        Set<String> subnetIds = getInstanceGroupNetworSubnetIds(group);
        if (!subnetIds.isEmpty()) {
            Map<String, Set<String>> subnetIdSetsByAvailabilityZone = fetchingSubnetsAndGroupingByAvailabilityZone(awsCredential, subnetIds, region);
            Set<String> availabilityZonesWithMultipleSubnets = subnetIdSetsByAvailabilityZone.entrySet().stream().filter(entry -> entry.getValue().size() > 1).map(e -> String.format("subnets: '%s' from availability zone: '%s'", String.join(",", e.getValue()), e.getKey())).collect(toSet());
            if (!availabilityZonesWithMultipleSubnets.isEmpty()) {
                LOGGER.debug("Validation error found there are multiple subnets associated with an availability zone");
                String validationError = String.format("Only one subnet per Availability Zone can be specified for a Gateway group. The request contains %s", String.join(", ", availabilityZonesWithMultipleSubnets));
                LOGGER.info(validationError);
                throw new CloudConnectorException(validationError);
            }
        } else {
            LOGGER.debug("No group network subnet specified, there is no info to be validated");
        }
        LOGGER.debug("There is no validation error found for gateway's group network's subnets");
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) LoggerFactory(org.slf4j.LoggerFactory) Validator(com.sequenceiq.cloudbreak.cloud.Validator) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) HashSet(java.util.HashSet) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) GroupSubnet(com.sequenceiq.cloudbreak.cloud.model.GroupSubnet) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) GroupNetwork(com.sequenceiq.cloudbreak.cloud.model.GroupNetwork) Collectors.mapping(java.util.stream.Collectors.mapping) Map(java.util.Map) Subnet(com.amazonaws.services.ec2.model.Subnet) Collectors.toSet(java.util.stream.Collectors.toSet) Logger(org.slf4j.Logger) AmazonServiceException(com.amazonaws.AmazonServiceException) Set(java.util.Set) Collectors(java.util.stream.Collectors) InstanceGroupType(com.sequenceiq.common.api.type.InstanceGroupType) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Component(org.springframework.stereotype.Component) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Optional(java.util.Optional) CommonAwsClient(com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient) Group(com.sequenceiq.cloudbreak.cloud.model.Group) HashSet(java.util.HashSet) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)

Example 2 with ValidationError

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

the class AwsResourceConnector method downscale.

@Override
public List<CloudResourceStatus> downscale(AuthenticatedContext auth, CloudStack stack, List<CloudResource> resources, List<CloudInstance> vms, Object resourcesToRemove) {
    Collection<String> instanceIds = new ArrayList<>();
    for (CloudInstance vm : vms) {
        instanceIds.add(vm.getInstanceId());
    }
    String asGroupName = cfStackUtil.getAutoscalingGroupName(auth, vms.get(0).getTemplate().getGroupName(), auth.getCloudContext().getLocation().getRegion().value());
    DetachInstancesRequest detachInstancesRequest = new DetachInstancesRequest().withAutoScalingGroupName(asGroupName).withInstanceIds(instanceIds).withShouldDecrementDesiredCapacity(true);
    AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(auth.getCloudCredential()), auth.getCloudContext().getLocation().getRegion().value());
    try {
        amazonASClient.detachInstances(detachInstancesRequest);
    } catch (AmazonServiceException e) {
        if (!"ValidationError".equals(e.getErrorCode()) || !e.getErrorMessage().contains("not part of Auto Scaling") || instanceIds.stream().anyMatch(id -> !e.getErrorMessage().contains(id))) {
            throw e;
        }
        LOGGER.info(e.getErrorMessage());
    }
    AmazonEC2Client amazonEC2Client = awsClient.createAccess(new AwsCredentialView(auth.getCloudCredential()), auth.getCloudContext().getLocation().getRegion().value());
    try {
        amazonEC2Client.terminateInstances(new TerminateInstancesRequest().withInstanceIds(instanceIds));
    } catch (AmazonServiceException e) {
        if (!"InvalidInstanceID.NotFound".equals(e.getErrorCode())) {
            throw e;
        }
        LOGGER.info(e.getErrorMessage());
    }
    LOGGER.info("Terminated instances in stack '{}': '{}'", auth.getCloudContext().getId(), instanceIds);
    return check(auth, resources);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DetachInstancesRequest(com.amazonaws.services.autoscaling.model.DetachInstancesRequest) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest)

Example 3 with ValidationError

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

the class AwsLaunchTemplateUpdateServiceTest method testUpdateImageWithWrongTemplateParams.

@Test
public void testUpdateImageWithWrongTemplateParams() throws IOException {
    // GIVEN
    String cfStackName = "cf";
    CloudResource cfResource = CloudResource.builder().type(ResourceType.CLOUDFORMATION_STACK).name(cfStackName).build();
    String template = FileReaderUtils.readFileFromClasspath("json/aws-cf-template.json");
    String cfTemplateBody = JsonUtil.minify(String.format(template, "{\"Ref\":\"AMI\"}"));
    when(cloudFormationClient.getTemplate(any())).thenReturn(new GetTemplateResult().withTemplateBody(cfTemplateBody));
    Map<AutoScalingGroup, String> autoScalingGroupsResult = createAutoScalingGroupHandler();
    when(autoScalingGroupHandler.getAutoScalingGroups(cloudFormationClient, autoScalingClient, cfResource.getName())).thenReturn(autoScalingGroupsResult);
    when(ec2Client.createLaunchTemplateVersion(any(CreateLaunchTemplateVersionRequest.class))).thenReturn(new CreateLaunchTemplateVersionResult().withWarning(new ValidationWarning().withErrors(new ValidationError().withCode("1").withMessage("error"))));
    // WHEN and THEN exception
    Assert.assertThrows(CloudConnectorException.class, () -> underTest.updateFields(ac, cfResource.getName(), Map.of(LaunchTemplateField.IMAGE_ID, stack.getImage().getImageName())));
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) GetTemplateResult(com.amazonaws.services.cloudformation.model.GetTemplateResult) CreateLaunchTemplateVersionResult(com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult) CreateLaunchTemplateVersionRequest(com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ValidationWarning(com.amazonaws.services.ec2.model.ValidationWarning) ValidationError(com.amazonaws.services.ec2.model.ValidationError) Test(org.junit.Test)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)2 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)1 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)1 DetachInstancesRequest (com.amazonaws.services.autoscaling.model.DetachInstancesRequest)1 GetTemplateResult (com.amazonaws.services.cloudformation.model.GetTemplateResult)1 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 CreateLaunchTemplateVersionRequest (com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionRequest)1 CreateLaunchTemplateVersionResult (com.amazonaws.services.ec2.model.CreateLaunchTemplateVersionResult)1 DescribeSubnetsRequest (com.amazonaws.services.ec2.model.DescribeSubnetsRequest)1 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)1 Subnet (com.amazonaws.services.ec2.model.Subnet)1 TerminateInstancesRequest (com.amazonaws.services.ec2.model.TerminateInstancesRequest)1 ValidationError (com.amazonaws.services.ec2.model.ValidationError)1 ValidationWarning (com.amazonaws.services.ec2.model.ValidationWarning)1 Validator (com.sequenceiq.cloudbreak.cloud.Validator)1 CommonAwsClient (com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient)1 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)1 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)1 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)1 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)1