use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsNetworkConnectorTest method testGetNetworkCidr.
@Test
public void testGetNetworkCidr() {
String existingVpc = "vpc-1";
String cidrBlock = "10.0.0.0/16";
Network network = new Network(null, Map.of(NetworkConstants.VPC_ID, existingVpc, "region", "us-west-2"));
CloudCredential credential = new CloudCredential();
AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
DescribeVpcsResult describeVpcsResult = describeVpcsResult(cidrBlock);
when(awsClient.createEc2Client(any(AwsCredentialView.class), eq("us-west-2"))).thenReturn(amazonEC2Client);
when(amazonEC2Client.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc))).thenReturn(describeVpcsResult);
NetworkCidr result = underTest.getNetworkCidr(network, credential);
assertEquals(cidrBlock, result.getCidr());
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsAutoScalingService method scheduleStatusChecks.
public void scheduleStatusChecks(List<Group> groups, AuthenticatedContext ac, AmazonCloudFormationClient cloudFormationClient, Date timeBeforeASUpdate, List<String> knownInstances) throws AmazonAutoscalingFailed {
AmazonEc2Client amClient = awsClient.createEc2Client(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
AmazonAutoScalingClient asClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
for (Group group : groups) {
String asGroupName = cfStackUtil.getAutoscalingGroupName(ac, cloudFormationClient, group.getName());
checkLastScalingActivity(asClient, asGroupName, timeBeforeASUpdate, group);
LOGGER.debug("Polling Auto Scaling group until new instances are ready. [stack: {}, asGroup: {}]", ac.getCloudContext().getId(), asGroupName);
waitForGroup(amClient, asClient, asGroupName, group.getInstancesSize(), ac.getCloudContext().getId(), knownInstances);
}
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsDownscaleService method downscale.
public List<CloudResourceStatus> downscale(AuthenticatedContext auth, CloudStack stack, List<CloudResource> resources, List<CloudInstance> vmsToDownscale) {
if (!vmsToDownscale.isEmpty()) {
List<String> instanceIdsToDownscale = new ArrayList<>();
for (CloudInstance vm : vmsToDownscale) {
instanceIdsToDownscale.add(vm.getInstanceId());
}
AwsCredentialView credentialView = new AwsCredentialView(auth.getCloudCredential());
AuthenticatedContextView authenticatedContextView = new AuthenticatedContextView(auth);
String regionName = authenticatedContextView.getRegion();
LOGGER.debug("Calling deleteCloudWatchAlarmsForSystemFailures from AwsDownscaleService");
awsCloudWatchService.deleteCloudWatchAlarmsForSystemFailures(stack, regionName, credentialView, instanceIdsToDownscale);
List<CloudResource> resourcesToDownscale = resources.stream().filter(resource -> instanceIdsToDownscale.contains(resource.getInstanceId())).collect(Collectors.toList());
awsComputeResourceService.deleteComputeResources(auth, stack, resourcesToDownscale);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, auth.getCloudContext().getLocation().getRegion().value());
AmazonEc2Client amazonEC2Client = awsClient.createEc2Client(credentialView, auth.getCloudContext().getLocation().getRegion().value());
Map<String, List<CloudInstance>> downscaledGroupsWithCloudInstances = vmsToDownscale.stream().collect(Collectors.groupingBy(cloudInstance -> cloudInstance.getTemplate().getGroupName()));
Long stackId = auth.getCloudContext().getId();
List<String> terminatedInstances = terminateInstances(auth, amazonASClient, amazonEC2Client, downscaledGroupsWithCloudInstances, stackId);
if (!terminatedInstances.isEmpty()) {
waitForTerminateInstances(stackId, terminatedInstances, amazonEC2Client);
}
updateAutoscalingGroups(auth, amazonASClient, downscaledGroupsWithCloudInstances);
List<String> targetGroupArns = getTargetGroupArns(stack.getLoadBalancers(), auth);
loadBalancerService.removeLoadBalancerTargets(auth, targetGroupArns, resourcesToDownscale);
}
return awsResourceConnector.check(auth, resources);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsLaunchService method createKeyPair.
private void createKeyPair(AuthenticatedContext ac, CloudStack stack) {
if (!awsClient.existingKeyPairNameSpecified(stack.getInstanceAuthentication())) {
AwsCredentialView awsCredential = new AwsCredentialView(ac.getCloudCredential());
try {
String region = ac.getCloudContext().getLocation().getRegion().value();
LOGGER.debug("Importing public key to {} region on AWS", region);
AmazonEc2Client client = awsClient.createEc2Client(awsCredential, region);
String keyPairName = awsClient.getKeyPairName(ac);
ImportKeyPairRequest importKeyPairRequest = new ImportKeyPairRequest(keyPairName, stack.getInstanceAuthentication().getPublicKey());
try {
client.describeKeyPairs(new DescribeKeyPairsRequest().withKeyNames(keyPairName));
LOGGER.debug("Key-pair already exists: {}", keyPairName);
} catch (AmazonServiceException e) {
client.importKeyPair(importKeyPairRequest);
}
} catch (Exception e) {
String errorMessage = String.format("Failed to import public key [roleArn:'%s'], detailed message: %s", awsCredential.getRoleArn(), e.getMessage());
LOGGER.info(errorMessage, e);
throw new CloudConnectorException(e.getMessage(), e);
}
}
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.
the class AwsMetadataCollector method collectInstancesForGroup.
public List<Instance> collectInstancesForGroup(AuthenticatedContext ac, AmazonAutoScalingClient amazonASClient, AmazonEc2Client amazonEC2Client, AmazonCloudFormationClient amazonCFClient, String group) {
LOGGER.debug("Collect aws instances for group: {}", group);
String asGroupName = cloudFormationStackUtil.getAutoscalingGroupName(ac, amazonCFClient, group);
List<String> instanceIds = cloudFormationStackUtil.getInstanceIds(amazonASClient, asGroupName);
DescribeInstancesRequest instancesRequest = cloudFormationStackUtil.createDescribeInstancesRequest(instanceIds);
DescribeInstancesResult instancesResult = amazonEC2Client.describeInstances(instancesRequest);
return instancesResult.getReservations().stream().flatMap(reservation -> reservation.getInstances().stream()).collect(Collectors.toList());
}
Aggregations