Search in sources :

Example 76 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client 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 77 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client 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 78 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsContextBuilder method contextInit.

@Override
public AwsContext contextInit(CloudContext context, AuthenticatedContext auth, Network network, List<CloudResource> resources, boolean build) {
    Location location = context.getLocation();
    AuthenticatedContextView authenticatedContextView = new AuthenticatedContextView(auth);
    AmazonEc2Client amazonEC2Client = authenticatedContextView.getAmazonEC2Client();
    AmazonElasticLoadBalancingClient elasticLoadBalancingClient = authenticatedContextView.getElasticLoadBalancingClient();
    return new AwsContext(context.getName(), amazonEC2Client, location, PARALLEL_RESOURCE_REQUEST, build, elasticLoadBalancingClient);
}
Also used : AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) AuthenticatedContextView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Location(com.sequenceiq.cloudbreak.cloud.model.Location)

Example 79 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsTaggingService method tagRootVolumes.

public void tagRootVolumes(AuthenticatedContext ac, AmazonEc2Client ec2Client, List<CloudResource> instanceResources, Map<String, String> userDefinedTags) {
    String stackName = ac.getCloudContext().getName();
    LOGGER.debug("Fetch AWS instances to collect all root volume ids for stack: {}", stackName);
    List<String> instanceIds = instanceResources.stream().map(CloudResource::getInstanceId).collect(Collectors.toList());
    DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceIds));
    List<Instance> instances = describeInstancesResult.getReservations().stream().flatMap(res -> res.getInstances().stream()).collect(Collectors.toList());
    List<String> rootVolumeIds = instances.stream().map(this::getRootVolumeId).filter(Optional::isPresent).map(blockDeviceMapping -> blockDeviceMapping.get().getEbs().getVolumeId()).collect(Collectors.toList());
    int instanceCount = instances.size();
    int volumeCount = rootVolumeIds.size();
    if (instanceCount != volumeCount) {
        LOGGER.debug("Did not find all root volumes, instanceResources: {}, found root volumes: {} for stack: {}", instanceCount, volumeCount, stackName);
    } else {
        LOGGER.debug("Found all ({}) root volumes for stack: {}", volumeCount, stackName);
    }
    AtomicInteger counter = new AtomicInteger();
    Collection<List<String>> volumeIdChunks = rootVolumeIds.stream().collect(Collectors.groupingBy(it -> counter.getAndIncrement() / MAX_RESOURCE_PER_REQUEST)).values();
    Collection<Tag> tags = prepareEc2Tags(userDefinedTags);
    for (List<String> volumeIds : volumeIdChunks) {
        LOGGER.debug("Tag {} root volumes for stack: {}", volumeIds.size(), stackName);
        ec2Client.createTags(new CreateTagsRequest().withResources(volumeIds).withTags(tags));
    }
}
Also used : ResourceType(com.amazonaws.services.ec2.model.ResourceType) Logger(org.slf4j.Logger) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) HashMap(java.util.HashMap) TagSpecification(com.amazonaws.services.ec2.model.TagSpecification) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Service(org.springframework.stereotype.Service) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) Tag(com.amazonaws.services.ec2.model.Tag) Optional(java.util.Optional) Instance(com.amazonaws.services.ec2.model.Instance) Optional(java.util.Optional) Instance(com.amazonaws.services.ec2.model.Instance) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) ArrayList(java.util.ArrayList) List(java.util.List) Tag(com.amazonaws.services.ec2.model.Tag)

Example 80 with AmazonEc2Client

use of com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client in project cloudbreak by hortonworks.

the class AwsLaunchTest method launchStackWithEfs.

@Test
public void launchStackWithEfs() throws Exception {
    setup();
    setupRetryService();
    setupFreemarkerTemplateProcessing();
    setupDescribeStacksResponses();
    setupDescribeImagesResponse();
    setupDescribeStackResourceResponse();
    setupAutoscalingResponses();
    setupDescribeInstancesResponse();
    setupCreateVolumeResponse();
    setupDescribeVolumeResponse();
    setupDescribeSubnetResponse();
    setupDescribePrefixListsResponse();
    setupCreateFileSystem();
    setupDescribeFileSystems();
    setupDescribeMountTargets();
    setupDeleteMountTarget();
    setupDeleteFileSystem();
    InMemoryStateStore.putStack(1L, PollGroup.POLLABLE);
    AuthenticatedContext authenticatedContext = componentTestUtil.getAuthenticatedContext();
    authenticatedContext.putParameter(AmazonEc2Client.class, amazonEc2Client);
    authenticatedContext.putParameter(AmazonElasticFileSystemClient.class, amazonElasticFileSystemClient);
    authenticatedContext.putParameter(AmazonEfsClient.class, amazonEfsClient);
    awsResourceConnector.launch(authenticatedContext, componentTestUtil.getStackForLaunch(InstanceStatus.CREATE_REQUESTED, InstanceStatus.CREATE_REQUESTED), persistenceNotifier, new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, Long.MAX_VALUE));
    // assert
    verify(persistenceNotifier).notifyAllocation(argThat(cloudResource -> ResourceType.AWS_VPC.equals(cloudResource.getType())), any());
    verify(persistenceNotifier, times(2)).notifyAllocation(argThat(cloudResource -> ResourceType.AWS_VOLUMESET.equals(cloudResource.getType())), any());
    verify(persistenceNotifier).notifyAllocation(argThat(cloudResource -> ResourceType.AWS_SUBNET.equals(cloudResource.getType())), any());
    verify(persistenceNotifier).notifyAllocation(argThat(cloudResource -> ResourceType.CLOUDFORMATION_STACK.equals(cloudResource.getType())), any());
    InOrder inOrder = inOrder(amazonElasticFileSystemClient, amazonEfsClient, amazonCloudFormationClient, amazonEc2Client);
    inOrder.verify(amazonEc2Client).describeImages(any());
    inOrder.verify(amazonCloudFormationClient).createStack(any());
    inOrder.verify(amazonEc2Client, times(2)).createVolume(any());
    inOrder.verify(amazonEc2Client, times(2)).attachVolume(any());
    inOrder.verify(amazonEc2Client, never()).describePrefixLists();
}
Also used : DescribeMountTargetsResult(com.amazonaws.services.elasticfilesystem.model.DescribeMountTargetsResult) Arrays(java.util.Arrays) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) EntitlementService(com.sequenceiq.cloudbreak.auth.altus.EntitlementService) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) ActiveProfiles(org.springframework.test.context.ActiveProfiles) AmazonCloudWatchClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonCloudWatchClient) DescribeStacksResult(com.amazonaws.services.cloudformation.model.DescribeStacksResult) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) AmazonEC2Waiters(com.amazonaws.services.ec2.waiters.AmazonEC2Waiters) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) DescribeScalingActivitiesRequest(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesRequest) SpringRunner(org.springframework.test.context.junit4.SpringRunner) AmazonAutoScalingWaiters(com.amazonaws.services.autoscaling.waiters.AmazonAutoScalingWaiters) Waiter(com.amazonaws.waiters.Waiter) MockBean(org.springframework.boot.test.mock.mockito.MockBean) Reservation(com.amazonaws.services.ec2.model.Reservation) LifeCycleState(com.amazonaws.services.elasticfilesystem.model.LifeCycleState) DescribeFileSystemsResult(com.amazonaws.services.elasticfilesystem.model.DescribeFileSystemsResult) AmazonServiceException(com.amazonaws.AmazonServiceException) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Output(com.amazonaws.services.cloudformation.model.Output) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) TestPropertySource(org.springframework.test.context.TestPropertySource) AmazonCloudFormationWaiters(com.amazonaws.services.cloudformation.waiters.AmazonCloudFormationWaiters) Retry(com.sequenceiq.cloudbreak.service.Retry) FreeMarkerTemplateUtils(com.sequenceiq.cloudbreak.util.FreeMarkerTemplateUtils) AwsResourceConnector(com.sequenceiq.cloudbreak.cloud.aws.connector.resource.AwsResourceConnector) CreateFileSystemResult(com.amazonaws.services.elasticfilesystem.model.CreateFileSystemResult) AdjustmentType(com.sequenceiq.common.api.type.AdjustmentType) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) VolumeState(com.amazonaws.services.ec2.model.VolumeState) Mockito.inOrder(org.mockito.Mockito.inOrder) DeleteFileSystemResult(com.amazonaws.services.elasticfilesystem.model.DeleteFileSystemResult) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LocationHelper(com.sequenceiq.cloudbreak.cloud.storage.LocationHelper) Mock(org.mockito.Mock) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) AVAILABILITY_ZONE(com.sequenceiq.cloudbreak.cloud.aws.component.ComponentTestUtil.AVAILABILITY_ZONE) RunWith(org.junit.runner.RunWith) AmazonEfsClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEfsClient) ResourceRetriever(com.sequenceiq.cloudbreak.cloud.service.ResourceRetriever) Supplier(java.util.function.Supplier) StackResourceDetail(com.amazonaws.services.cloudformation.model.StackResourceDetail) Inject(javax.inject.Inject) CreateVolumeResult(com.amazonaws.services.ec2.model.CreateVolumeResult) Answer(org.mockito.stubbing.Answer) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) DeleteMountTargetResult(com.amazonaws.services.elasticfilesystem.model.DeleteMountTargetResult) DescribeImagesResult(com.amazonaws.services.ec2.model.DescribeImagesResult) ResourceType(com.sequenceiq.common.api.type.ResourceType) InOrder(org.mockito.InOrder) AmazonElasticFileSystemClient(com.amazonaws.services.elasticfilesystem.AmazonElasticFileSystemClient) FileSystemDescription(com.amazonaws.services.elasticfilesystem.model.FileSystemDescription) DescribeScalingActivitiesResult(com.amazonaws.services.autoscaling.model.DescribeScalingActivitiesResult) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) AdjustmentTypeWithThreshold(com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold) DescribePrefixListsResult(com.amazonaws.services.ec2.model.DescribePrefixListsResult) InMemoryStateStore(com.sequenceiq.cloudbreak.cloud.store.InMemoryStateStore) Import(org.springframework.context.annotation.Import) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Stack(com.amazonaws.services.cloudformation.model.Stack) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) LifecycleState(com.amazonaws.services.autoscaling.model.LifecycleState) Mockito.verify(org.mockito.Mockito.verify) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) Mockito.never(org.mockito.Mockito.never) MountTargetDescription(com.amazonaws.services.elasticfilesystem.model.MountTargetDescription) CustomAmazonWaiterProvider(com.sequenceiq.cloudbreak.cloud.aws.scheduler.CustomAmazonWaiterProvider) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) Instance(com.amazonaws.services.autoscaling.model.Instance) DescribeStackResourceResult(com.amazonaws.services.cloudformation.model.DescribeStackResourceResult) CommonAwsClient(com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient) PollGroup(com.sequenceiq.cloudbreak.cloud.scheduler.PollGroup) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InOrder(org.mockito.InOrder) AdjustmentTypeWithThreshold(com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Test(org.junit.Test)

Aggregations

AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)97 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)44 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)41 Test (org.junit.Test)31 ArrayList (java.util.ArrayList)30 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)29 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)29 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)28 HashMap (java.util.HashMap)28 Group (com.sequenceiq.cloudbreak.cloud.model.Group)24 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)24 DescribeSubnetsResult (com.amazonaws.services.ec2.model.DescribeSubnetsResult)23 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)23 Network (com.sequenceiq.cloudbreak.cloud.model.Network)23 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)22 AmazonServiceException (com.amazonaws.AmazonServiceException)21 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)21 List (java.util.List)21 Vpc (com.amazonaws.services.ec2.model.Vpc)20 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)20