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");
}
}
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);
}
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())));
}
Aggregations