use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsModelService method buildDefaultModelContext.
public ModelContext buildDefaultModelContext(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier) {
Network network = stack.getNetwork();
AwsNetworkView awsNetworkView = new AwsNetworkView(network);
AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
String regionName = ac.getCloudContext().getLocation().getRegion().value();
AmazonEc2Client amazonEC2Client = awsClient.createEc2Client(credentialView, regionName);
boolean mapPublicIpOnLaunch = awsNetworkService.isMapPublicOnLaunch(awsNetworkView, amazonEC2Client);
boolean existingVPC = awsNetworkView.isExistingVPC();
boolean existingSubnet = awsNetworkView.isExistingSubnet();
String cidr = network.getSubnet().getCidr();
String subnet = isNoCIDRProvided(existingVPC, existingSubnet, cidr) ? awsNetworkService.findNonOverLappingCIDR(ac, stack) : cidr;
AwsInstanceProfileView awsInstanceProfileView = new AwsInstanceProfileView(stack);
ModelContext modelContext = new ModelContext().withAuthenticatedContext(ac).withStack(stack).withExistingVpc(existingVPC).withExistingIGW(awsNetworkView.isExistingIGW()).withExistingSubnetCidr(existingSubnet ? awsNetworkService.getExistingSubnetCidr(ac, stack) : null).withExistinVpcCidr(awsNetworkService.getVpcCidrs(ac, awsNetworkView)).withExistingSubnetIds(existingSubnet ? awsNetworkView.getSubnetList() : null).mapPublicIpOnLaunch(mapPublicIpOnLaunch).withEnableInstanceProfile(awsInstanceProfileView.isInstanceProfileAvailable()).withInstanceProfileAvailable(awsInstanceProfileView.isInstanceProfileAvailable()).withTemplate(stack.getTemplate()).withDefaultSubnet(subnet).withOutboundInternetTraffic(network.getOutboundInternetTraffic()).withVpcCidrs(network.getNetworkCidrs()).withPrefixListIds(awsNetworkService.getPrefixListIds(amazonEC2Client, regionName, network.getOutboundInternetTraffic()));
AwsEfsFileSystem efsFileSystem = getAwsEfsFileSystem(stack);
if (efsFileSystem != null) {
modelContext.withEnableEfs(true);
modelContext.withEfsFileSystem(efsFileSystem);
} else {
modelContext.withEnableEfs(false);
}
return modelContext;
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsNetworkService method getExistingSubnetCidr.
public List<String> getExistingSubnetCidr(AuthenticatedContext ac, CloudStack stack) {
AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
String region = ac.getCloudContext().getLocation().getRegion().value();
AmazonEc2Client ec2Client = awsClient.createEc2Client(new AwsCredentialView(ac.getCloudCredential()), region);
DescribeSubnetsRequest subnetsRequest = new DescribeSubnetsRequest().withSubnetIds(awsNetworkView.getSubnetList());
List<Subnet> subnets = ec2Client.describeSubnets(subnetsRequest).getSubnets();
if (subnets.isEmpty()) {
throw new CloudConnectorException("The specified subnet does not exist (maybe it's in a different region).");
}
List<String> cidrs = Lists.newArrayList();
for (Subnet subnet : subnets) {
cidrs.add(subnet.getCidrBlock());
}
return cidrs;
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsVolumeResourceBuilder method getAmazonEC2Client.
private AmazonEc2Client getAmazonEC2Client(AuthenticatedContext auth) {
AwsCredentialView credentialView = new AwsCredentialView(auth.getCloudCredential());
String regionName = auth.getCloudContext().getLocation().getRegion().value();
return awsClient.createEc2Client(credentialView, regionName);
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class LoadBalancerService method removeLoadBalancerTargets.
public void removeLoadBalancerTargets(AuthenticatedContext ac, List<String> targetGroupArns, List<CloudResource> resourcesToRemove) {
if (targetGroupArns.isEmpty()) {
LOGGER.info("Cannot find target group for the stack, skip the removing of the targets");
return;
}
for (String targetGroupArn : targetGroupArns) {
String region = ac.getCloudContext().getLocation().getRegion().value();
AmazonElasticLoadBalancingClient amazonElbClient = awsClient.createElasticLoadBalancingClient(new AwsCredentialView(ac.getCloudCredential()), region);
LOGGER.debug("Get a list of the instance ids to remove");
Set<String> instancesToRemove = getInstanceIdsForGroups(resourcesToRemove);
LOGGER.debug("Deregister any instances that no longer exist");
if (!instancesToRemove.isEmpty()) {
try {
List<TargetDescription> targetsToRemove = instancesToRemove.stream().map(instanceId -> new TargetDescription().withId(instanceId)).collect(Collectors.toList());
amazonElbClient.deregisterTargets(new DeregisterTargetsRequest().withTargetGroupArn(targetGroupArn).withTargets(targetsToRemove));
LOGGER.debug("Targets deregistered: {}", targetsToRemove);
} catch (InvalidTargetException ignored) {
LOGGER.debug("no-op - we tried to remove a target that wasn't in the target group, which is fine");
}
}
}
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.
the class AwsCloudWatchServiceTest method testDeleteCloudWatchAlarmsForSystemFailuresBatchesDeletion.
@Test
void testDeleteCloudWatchAlarmsForSystemFailuresBatchesDeletion() {
List<CloudInstance> cloudInstances = new LinkedList<>();
List<String> alarmNames1 = new LinkedList<>();
List<String> alarmNames2 = new LinkedList<>();
List<String> alarmNames3 = new LinkedList<>();
List<MetricAlarm> alarms1 = new LinkedList<>();
List<MetricAlarm> alarms2 = new LinkedList<>();
List<MetricAlarm> alarms3 = new LinkedList<>();
for (int i = 1; i <= 100; i++) {
String alarmName = "i-" + i + "-Status-Check-Failed-System";
alarmNames1.add(alarmName);
MetricAlarm alarm = mock(MetricAlarm.class);
when(alarm.getAlarmName()).thenReturn(alarmName);
alarms1.add(alarm);
}
for (int i = 101; i <= 200; i++) {
String alarmName = "i-" + i + "-Status-Check-Failed-System";
alarmNames2.add(alarmName);
MetricAlarm alarm = mock(MetricAlarm.class);
when(alarm.getAlarmName()).thenReturn(alarmName);
alarms2.add(alarm);
}
for (int i = 201; i <= 210; i++) {
String alarmName = "i-" + i + "-Status-Check-Failed-System";
alarmNames3.add(alarmName);
MetricAlarm alarm = mock(MetricAlarm.class);
when(alarm.getAlarmName()).thenReturn(alarmName);
alarms3.add(alarm);
}
for (int i = 1; i <= 210; i++) {
String instanceId = "i-" + i;
CloudInstance cloudInstance = mock(CloudInstance.class);
when(cloudInstance.getInstanceId()).thenReturn(instanceId);
cloudInstances.add(cloudInstance);
}
DescribeAlarmsResult describeAlarmsResult1 = mock(DescribeAlarmsResult.class);
DescribeAlarmsResult describeAlarmsResult2 = mock(DescribeAlarmsResult.class);
DescribeAlarmsResult describeAlarmsResult3 = mock(DescribeAlarmsResult.class);
when(describeAlarmsResult1.getMetricAlarms()).thenReturn(alarms1);
when(describeAlarmsResult2.getMetricAlarms()).thenReturn(alarms2);
when(describeAlarmsResult3.getMetricAlarms()).thenReturn(alarms3);
AmazonCloudWatchClient cloudWatchClient = mock(AmazonCloudWatchClient.class);
AwsCredentialView credentialView = mock(AwsCredentialView.class);
CloudStack stack = mock(CloudStack.class);
Group group = mock(Group.class);
List<Group> groups = List.of(group);
when(stack.getGroups()).thenReturn(groups);
when(group.getInstances()).thenReturn(cloudInstances);
when(awsClient.createCloudWatchClient(credentialView, REGION)).thenReturn(cloudWatchClient);
when(cloudWatchClient.describeAlarms(any())).thenReturn(describeAlarmsResult1, describeAlarmsResult2, describeAlarmsResult3);
underTest.deleteAllCloudWatchAlarmsForSystemFailures(stack, REGION, credentialView);
ArgumentCaptor<DescribeAlarmsRequest> captorDescribe = ArgumentCaptor.forClass(DescribeAlarmsRequest.class);
ArgumentCaptor<DeleteAlarmsRequest> captorDelete = ArgumentCaptor.forClass(DeleteAlarmsRequest.class);
verify(cloudWatchClient, times(3)).describeAlarms(captorDescribe.capture());
verify(cloudWatchClient, times(3)).deleteAlarms(captorDelete.capture());
assertEquals(List.of(alarmNames1, alarmNames2, alarmNames3), captorDescribe.getAllValues().stream().map(DescribeAlarmsRequest::getAlarmNames).collect(Collectors.toList()));
assertEquals(List.of(alarmNames1, alarmNames2, alarmNames3), captorDelete.getAllValues().stream().map(DeleteAlarmsRequest::getAlarmNames).collect(Collectors.toList()));
}
Aggregations