Search in sources :

Example 81 with AwsCredentialView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.

the class AwsRdsStopService method stop.

public void stop(AuthenticatedContext ac, DatabaseStack dbStack) throws ExecutionException, TimeoutException, InterruptedException {
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AmazonRdsClient rdsClient = awsClient.createRdsClient(credentialView, regionName);
    String dbInstanceIdentifier = dbStack.getDatabaseServer().getServerId();
    StopDBInstanceRequest stopDBInstanceRequest = new StopDBInstanceRequest();
    stopDBInstanceRequest.setDBInstanceIdentifier(dbInstanceIdentifier);
    LOGGER.debug("RDS stop request");
    try {
        rdsClient.stopDBInstance(stopDBInstanceRequest);
    } catch (RuntimeException ex) {
        throw new CloudConnectorException(ex.getMessage(), ex);
    }
    Waiter<DescribeDBInstancesRequest> rdsWaiter = customAmazonWaiterProvider.getDbInstanceStopWaiter(rdsClient);
    DescribeDBInstancesRequest describeDBInstancesRequest = new DescribeDBInstancesRequest().withDBInstanceIdentifier(dbInstanceIdentifier);
    StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
    run(rdsWaiter, describeDBInstancesRequest, stackCancellationCheck, "RDS stop failed");
    LOGGER.debug("RDS stop process finished. DB Instance ID: {}", dbInstanceIdentifier);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) StopDBInstanceRequest(com.amazonaws.services.rds.model.StopDBInstanceRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) AmazonRdsClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonRdsClient) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest)

Example 82 with AwsCredentialView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.

the class AwsRdsTerminateService method terminate.

/**
 * Terminates a database server (stack).
 *
 * @param ac                  authenticated cloud context
 * @param stack               database stack to delete
 * @param force               whether to continue even if stack termination fails in AWS
 * @param persistenceNotifier notifies Resources table of resource deletion
 * @param resources           list of resources tracked in DB
 * @return list of affected cloud resources (not yet implemented)
 * @throws AmazonServiceException  if the search for the stack fails
 * @throws ExecutionException      if stack deletion fails (and force is false)
 * @throws TimeoutException        if stack deletion times out
 * @throws InterruptedException    if the wait for stack deletion is interrupted
 * @throws CloudConnectorException if stack deletion fails due to a runtime exception
 */
public List<CloudResourceStatus> terminate(AuthenticatedContext ac, DatabaseStack stack, boolean force, PersistenceNotifier persistenceNotifier, List<CloudResource> resources) throws Exception {
    String cFStackName = cfStackUtil.getCfStackName(ac);
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    try {
        initiateCFTemplateDeletion(ac, cFStackName, credentialView, regionName);
    } catch (AmazonServiceException e) {
        return getAmazonServiceException(force, cFStackName, e);
    } catch (Exception ex) {
        Exception runtimeException = getRuntimeException(force, cFStackName, ex);
        if (runtimeException != null) {
            throw runtimeException;
        }
    }
    CloudContext cloudContext = ac.getCloudContext();
    resources.forEach(r -> persistenceNotifier.notifyDeletion(r, cloudContext));
    // FIXME
    return List.of();
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) AmazonServiceException(com.amazonaws.AmazonServiceException) TimeoutException(java.util.concurrent.TimeoutException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonServiceException(com.amazonaws.AmazonServiceException) ExecutionException(java.util.concurrent.ExecutionException)

Example 83 with AwsCredentialView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.

the class AwsNetworkCfTemplateProvider method describeVpcServiceDetails.

private List<ServiceDetail> describeVpcServiceDetails(NetworkCreationRequest networkCreationRequest, Map<String, String> endpointNameMappings) {
    AwsCredentialView awsCredential = new AwsCredentialView(networkCreationRequest.getCloudCredential());
    AmazonEc2Client awsClientAccess = awsClient.createEc2Client(awsCredential, networkCreationRequest.getRegion().value());
    return awsClientAccess.describeVpcEndpointServices().getServiceDetails().stream().filter(sd -> endpointNameMappings.containsKey(sd.getServiceName())).collect(Collectors.toList());
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) TemplateException(freemarker.template.TemplateException) LoggerFactory(org.slf4j.LoggerFactory) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Value(org.springframework.beans.factory.annotation.Value) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Strings(com.google.common.base.Strings) SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) USE_VPC_ENDPOINT(com.sequenceiq.common.model.PrivateEndpointType.USE_VPC_ENDPOINT) Map(java.util.Map) Template(freemarker.template.Template) Logger(org.slf4j.Logger) IOException(java.io.IOException) PrivateEndpointType(com.sequenceiq.common.model.PrivateEndpointType) Collectors(java.util.stream.Collectors) ServiceDetail(com.amazonaws.services.ec2.model.ServiceDetail) FreeMarkerTemplateUtils(com.sequenceiq.cloudbreak.util.FreeMarkerTemplateUtils) List(java.util.List) Component(org.springframework.stereotype.Component) AwsServiceEndpointView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsServiceEndpointView) Configuration(freemarker.template.Configuration) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)

Example 84 with AwsCredentialView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.

the class AwsNetworkConnector method getNetworkCidr.

@Override
public NetworkCidr getNetworkCidr(Network network, CloudCredential credential) {
    AwsCredentialView awsCredentialView = new AwsCredentialView(credential);
    AmazonEc2Client awsClientAccess = awsClient.createEc2Client(awsCredentialView, network.getStringParameter(AwsNetworkView.REGION));
    AwsNetworkView awsNetworkView = new AwsNetworkView(network);
    String existingVpc = awsNetworkView.getExistingVpc();
    DescribeVpcsResult describeVpcsResult = awsClientAccess.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc));
    List<String> vpcCidrs = new ArrayList<>();
    for (Vpc vpc : describeVpcsResult.getVpcs()) {
        if (vpc.getCidrBlockAssociationSet() != null) {
            LOGGER.info("The VPC {} has associated CIDR block so using the CIDR blocks in the VPC.", vpc.getVpcId());
            List<String> cidrs = vpc.getCidrBlockAssociationSet().stream().map(VpcCidrBlockAssociation::getCidrBlock).distinct().filter(e -> !vpcCidrs.contains(e)).collect(Collectors.toList());
            LOGGER.info("The VPC {} CIDRs block are {}.", vpc.getVpcId(), cidrs);
            vpcCidrs.addAll(cidrs);
        } else {
            LOGGER.info("The VPC {} has no associated CIDR block so using the CIDR block in the VPC.", vpc.getVpcId());
            vpcCidrs.add(vpc.getCidrBlock());
        }
    }
    if (vpcCidrs.isEmpty()) {
        throw new BadRequestException("VPC cidr could not fetch from AWS: " + existingVpc);
    }
    if (vpcCidrs.size() > 1) {
        LOGGER.info("More than one vpc cidrs for VPC {}. We will use the first one: {}", existingVpc, vpcCidrs.get(0));
    }
    return new NetworkCidr(vpcCidrs.get(0), vpcCidrs);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) CreateStackRequest(com.amazonaws.services.cloudformation.model.CreateStackRequest) SubnetFilterStrategyType(com.sequenceiq.cloudbreak.cloud.aws.common.subnetselector.SubnetFilterStrategyType) NetworkDeletionRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkDeletionRequest) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) AwsCloudFormationErrorMessageProvider(com.sequenceiq.cloudbreak.cloud.aws.util.AwsCloudFormationErrorMessageProvider) LoggerFactory(org.slf4j.LoggerFactory) VpcCidrBlockAssociation(com.amazonaws.services.ec2.model.VpcCidrBlockAssociation) OnFailure(com.amazonaws.services.cloudformation.model.OnFailure) EnvironmentCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.EnvironmentCancellationCheck) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) Map(java.util.Map) BadRequestException(javax.ws.rs.BadRequestException) Waiter(com.amazonaws.waiters.Waiter) AwsConstants(com.sequenceiq.cloudbreak.cloud.aws.common.AwsConstants) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) Platform(com.sequenceiq.cloudbreak.cloud.model.Platform) Set(java.util.Set) Retry(com.sequenceiq.cloudbreak.service.Retry) CAPABILITY_IAM(com.amazonaws.services.cloudformation.model.Capability.CAPABILITY_IAM) Collectors(java.util.stream.Collectors) List(java.util.List) Tag(com.amazonaws.services.cloudformation.model.Tag) SubnetFilterStrategy(com.sequenceiq.cloudbreak.cloud.aws.common.subnetselector.SubnetFilterStrategy) NetworkCidr(com.sequenceiq.cloudbreak.cloud.network.NetworkCidr) SubnetSelectionParameters(com.sequenceiq.cloudbreak.cloud.model.SubnetSelectionParameters) Variant(com.sequenceiq.cloudbreak.cloud.model.Variant) DefaultNetworkConnector(com.sequenceiq.cloudbreak.cloud.DefaultNetworkConnector) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Value(org.springframework.beans.factory.annotation.Value) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Network(com.sequenceiq.cloudbreak.cloud.model.Network) DeleteStackRequest(com.amazonaws.services.cloudformation.model.DeleteStackRequest) CloudSubnet(com.sequenceiq.cloudbreak.cloud.model.CloudSubnet) Logger(org.slf4j.Logger) SubnetSelectionResult(com.sequenceiq.cloudbreak.cloud.model.SubnetSelectionResult) Vpc(com.amazonaws.services.ec2.model.Vpc) ResourceStatus(com.amazonaws.services.cloudformation.model.ResourceStatus) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) WaiterRunner.run(com.sequenceiq.cloudbreak.cloud.aws.scheduler.WaiterRunner.run) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet) AwsSubnetRequestProvider(com.sequenceiq.cloudbreak.cloud.aws.common.AwsSubnetRequestProvider) AwsTaggingService(com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) NetworkCidr(com.sequenceiq.cloudbreak.cloud.network.NetworkCidr) ArrayList(java.util.ArrayList) Vpc(com.amazonaws.services.ec2.model.Vpc) BadRequestException(javax.ws.rs.BadRequestException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)

Example 85 with AwsCredentialView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView in project cloudbreak by hortonworks.

the class AwsStackValidator method validateStackNameAvailability.

private void validateStackNameAvailability(AuthenticatedContext ac) {
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
    String cFStackName = cfStackUtil.getCfStackName(ac);
    try {
        LOGGER.debug("Checking stack name availability. [{}]", cFStackName);
        cfClient.describeStacks(new DescribeStacksRequest().withStackName(cFStackName));
        throw new CloudConnectorException(String.format("Stack already exists with the given name: %s", cFStackName));
    } catch (AmazonServiceException e) {
        if (e.getErrorMessage().contains(cFStackName + " does not exist")) {
            LOGGER.info("Stack name is available, CF stack not found by name {}", cFStackName);
        } else {
            LOGGER.warn("Exception while checking stack name availability.", e);
        }
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

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