Search in sources :

Example 1 with AwsCredentialView

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

the class AwsCredentialConnector method verifyAccessKeySecretKeyIsAssumable.

private CloudCredentialStatus verifyAccessKeySecretKeyIsAssumable(CloudCredential cloudCredential) {
    AwsCredentialView awsCredential = new AwsCredentialView(cloudCredential);
    try {
        AmazonEC2Client access = awsClient.createAccess(cloudCredential);
        DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest();
        access.describeRegions(describeRegionsRequest);
    } catch (AmazonClientException ae) {
        String errorMessage = "Unable to verify AWS credentials: please make sure the access key and secret key is correct";
        LOGGER.error(errorMessage, ae);
        return new CloudCredentialStatus(cloudCredential, CredentialStatus.FAILED, ae, errorMessage);
    } catch (RuntimeException e) {
        String errorMessage = String.format("Could not verify keys '%s': check if the keys exists and if it's created with the correct external ID", awsCredential.getAccessKey());
        LOGGER.error(errorMessage, e);
        return new CloudCredentialStatus(cloudCredential, CredentialStatus.FAILED, e, errorMessage);
    }
    return new CloudCredentialStatus(cloudCredential, CredentialStatus.CREATED);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) AmazonClientException(com.amazonaws.AmazonClientException) DescribeRegionsRequest(com.amazonaws.services.ec2.model.DescribeRegionsRequest) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus)

Example 2 with AwsCredentialView

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

the class AwsCredentialConnector method verify.

@Override
public CloudCredentialStatus verify(AuthenticatedContext authenticatedContext) {
    CloudCredential credential = authenticatedContext.getCloudCredential();
    LOGGER.info("Create credential: {}", credential);
    AwsCredentialView awsCredential = new AwsCredentialView(credential);
    String roleArn = awsCredential.getRoleArn();
    String accessKey = awsCredential.getAccessKey();
    String secretKey = awsCredential.getSecretKey();
    String smartSenseId = smartSenseIdGenerator.getSmartSenseId(awsCredential);
    if (isNoneEmpty(smartSenseId)) {
        credential.putParameter(SMART_SENSE_ID, smartSenseId);
    }
    if (isNoneEmpty(roleArn) && isNoneEmpty(accessKey) && isNoneEmpty(secretKey)) {
        String message = "Please only provide the 'role arn' or the 'access' and 'secret key'";
        return new CloudCredentialStatus(credential, CredentialStatus.FAILED, new Exception(message), message);
    }
    if (isNoneEmpty(roleArn)) {
        return verifyIamRoleIsAssumable(credential);
    }
    if (isEmpty(accessKey) || isEmpty(secretKey)) {
        String message = "Please provide both the 'access' and 'secret key'";
        return new CloudCredentialStatus(credential, CredentialStatus.FAILED, new Exception(message), message);
    } else {
        return verifyAccessKeySecretKeyIsAssumable(credential);
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) ExtendedCloudCredential(com.sequenceiq.cloudbreak.cloud.model.ExtendedCloudCredential) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) AmazonClientException(com.amazonaws.AmazonClientException) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus)

Example 3 with AwsCredentialView

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

the class AwsInstanceConnector method check.

@Override
public List<CloudVmInstanceStatus> check(AuthenticatedContext ac, List<CloudInstance> vms) {
    List<CloudVmInstanceStatus> cloudVmInstanceStatuses = new ArrayList<>();
    for (CloudInstance vm : vms) {
        try {
            String region = ac.getCloudContext().getLocation().getRegion().value();
            DescribeInstancesResult result = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value()).describeInstances(new DescribeInstancesRequest().withInstanceIds(vm.getInstanceId()));
            for (Reservation reservation : result.getReservations()) {
                for (Instance instance : reservation.getInstances()) {
                    if ("Stopped".equalsIgnoreCase(instance.getState().getName())) {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.STOPPED));
                    } else if ("Running".equalsIgnoreCase(instance.getState().getName())) {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.STARTED));
                    } else if ("Terminated".equalsIgnoreCase(instance.getState().getName())) {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.TERMINATED));
                    } else {
                        LOGGER.info("AWS instance [{}] is in {} state, region: {}, stack: {}", instance.getInstanceId(), instance.getState().getName(), region, ac.getCloudContext().getId());
                        cloudVmInstanceStatuses.add(new CloudVmInstanceStatus(vm, InstanceStatus.IN_PROGRESS));
                    }
                }
            }
        } catch (AmazonEC2Exception e) {
            LOGGER.warn("Instance does not exist with this id: {}, original message: {}", vm.getInstanceId(), e.getMessage());
        }
    }
    return cloudVmInstanceStatuses;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) Reservation(com.amazonaws.services.ec2.model.Reservation) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.amazonaws.services.ec2.model.Instance) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 4 with AwsCredentialView

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

the class AwsPlatformResources method securityGroups.

@Override
public CloudSecurityGroups securityGroups(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    Map<String, Set<CloudSecurityGroup>> result = new HashMap<>();
    Set<CloudSecurityGroup> cloudSecurityGroups = new HashSet<>();
    AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(cloudCredential), region.value());
    // create securitygroup filter view
    PlatformResourceSecurityGroupFilterView filter = new PlatformResourceSecurityGroupFilterView(filters);
    DescribeSecurityGroupsRequest describeSecurityGroupsRequest = new DescribeSecurityGroupsRequest();
    // If the filtervalue is provided then we should filter only for those securitygroups
    if (!Strings.isNullOrEmpty(filter.getVpcId())) {
        describeSecurityGroupsRequest.withFilters(new Filter("vpc-id", singletonList(filter.getVpcId())));
    }
    if (!Strings.isNullOrEmpty(filter.getGroupId())) {
        describeSecurityGroupsRequest.withGroupIds(filter.getGroupId());
    }
    if (!Strings.isNullOrEmpty(filter.getGroupName())) {
        describeSecurityGroupsRequest.withGroupNames(filter.getGroupName());
    }
    for (SecurityGroup securityGroup : ec2Client.describeSecurityGroups(describeSecurityGroupsRequest).getSecurityGroups()) {
        Map<String, Object> properties = new HashMap<>();
        properties.put("vpcId", securityGroup.getVpcId());
        properties.put("description", securityGroup.getDescription());
        properties.put("ipPermissions", securityGroup.getIpPermissions());
        properties.put("ipPermissionsEgress", securityGroup.getIpPermissionsEgress());
        cloudSecurityGroups.add(new CloudSecurityGroup(securityGroup.getGroupName(), securityGroup.getGroupId(), properties));
    }
    result.put(region.value(), cloudSecurityGroups);
    return new CloudSecurityGroups(result);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) PlatformResourceSecurityGroupFilterView(com.sequenceiq.cloudbreak.cloud.model.view.PlatformResourceSecurityGroupFilterView) DescribeSecurityGroupsRequest(com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup) CloudSecurityGroup(com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroup) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) Filter(com.amazonaws.services.ec2.model.Filter) CloudSecurityGroups(com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroups) CloudSecurityGroup(com.sequenceiq.cloudbreak.cloud.model.CloudSecurityGroup) HashSet(java.util.HashSet)

Example 5 with AwsCredentialView

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

the class AwsResourceConnector method suspendAutoScaling.

private void suspendAutoScaling(AuthenticatedContext ac, CloudStack stack) {
    AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(new AwsCredentialView(ac.getCloudCredential()), ac.getCloudContext().getLocation().getRegion().value());
    for (Group group : stack.getGroups()) {
        String asGroupName = cfStackUtil.getAutoscalingGroupName(ac, group.getName(), ac.getCloudContext().getLocation().getRegion().value());
        amazonASClient.suspendProcesses(new SuspendProcessesRequest().withAutoScalingGroupName(asGroupName).withScalingProcesses(SUSPENDED_PROCESSES));
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) AmazonAutoScalingClient(com.amazonaws.services.autoscaling.AmazonAutoScalingClient) SuspendProcessesRequest(com.amazonaws.services.autoscaling.model.SuspendProcessesRequest)

Aggregations

AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)28 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)20 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)12 AmazonServiceException (com.amazonaws.AmazonServiceException)10 AmazonAutoScalingClient (com.amazonaws.services.autoscaling.AmazonAutoScalingClient)8 ArrayList (java.util.ArrayList)8 AmazonCloudFormationClient (com.amazonaws.services.cloudformation.AmazonCloudFormationClient)6 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)6 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)5 AwsNetworkView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsNetworkView)5 Group (com.sequenceiq.cloudbreak.cloud.model.Group)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)4 Subnet (com.amazonaws.services.ec2.model.Subnet)4 ActionWentFailException (com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)4 AmazonClientException (com.amazonaws.AmazonClientException)3 ResumeProcessesRequest (com.amazonaws.services.autoscaling.model.ResumeProcessesRequest)3 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)3 DescribeSubnetsRequest (com.amazonaws.services.ec2.model.DescribeSubnetsRequest)3