Search in sources :

Example 61 with AwsCredentialView

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;
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) ModelContext(com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AwsEfsFileSystem(com.sequenceiq.cloudbreak.cloud.aws.common.efs.AwsEfsFileSystem) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AwsInstanceProfileView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsInstanceProfileView)

Example 62 with AwsCredentialView

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;
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Subnet(com.amazonaws.services.ec2.model.Subnet) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest)

Example 63 with AwsCredentialView

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);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)

Example 64 with AwsCredentialView

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");
            }
        }
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) Logger(org.slf4j.Logger) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) DeregisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.DeregisterTargetsRequest) Set(java.util.Set) Collectors(java.util.stream.Collectors) Inject(javax.inject.Inject) List(java.util.List) Component(org.springframework.stereotype.Component) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) InvalidTargetException(com.amazonaws.services.elasticloadbalancingv2.model.InvalidTargetException) CommonAwsClient(com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) InvalidTargetException(com.amazonaws.services.elasticloadbalancingv2.model.InvalidTargetException) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) DeregisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.DeregisterTargetsRequest)

Example 65 with AwsCredentialView

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()));
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DescribeAlarmsRequest(com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) LinkedList(java.util.LinkedList) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) MetricAlarm(com.amazonaws.services.cloudwatch.model.MetricAlarm) DescribeAlarmsResult(com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult) DeleteAlarmsRequest(com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest) Test(org.junit.jupiter.api.Test)

Aggregations

AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)94 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)32 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)32 List (java.util.List)25 AmazonServiceException (com.amazonaws.AmazonServiceException)22 AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)21 Logger (org.slf4j.Logger)21 Inject (javax.inject.Inject)20 ArrayList (java.util.ArrayList)19 Collectors (java.util.stream.Collectors)19 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)18 Group (com.sequenceiq.cloudbreak.cloud.model.Group)18 Set (java.util.Set)18 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)17 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)17 Map (java.util.Map)16 LoggerFactory (org.slf4j.LoggerFactory)16 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)15 Service (org.springframework.stereotype.Service)15 AmazonAutoScalingClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient)14