Search in sources :

Example 11 with AwsCredentialView

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

the class AwsStackValidator method validate.

@Override
public void validate(AuthenticatedContext ac, CloudStack cloudStack) {
    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 {
        cfClient.describeStacks(new DescribeStacksRequest().withStackName(cFStackName));
        throw new CloudConnectorException(String.format("Stack is already exists with the given name: %s", cFStackName));
    } catch (AmazonServiceException ignored) {
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonCloudFormationClient(com.amazonaws.services.cloudformation.AmazonCloudFormationClient)

Example 12 with AwsCredentialView

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

the class AwsPlatformResources method accessConfigs.

@Override
public CloudAccessConfigs accessConfigs(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    String queryFailedMessage = "Could not get instance profile roles from Amazon: ";
    CloudAccessConfigs cloudAccessConfigs = new CloudAccessConfigs(new HashSet<>());
    AwsCredentialView awsCredentialView = new AwsCredentialView(cloudCredential);
    AmazonIdentityManagement client = awsClient.createAmazonIdentityManagement(awsCredentialView);
    try {
        ListInstanceProfilesResult listRolesResult = client.listInstanceProfiles();
        for (InstanceProfile instanceProfile : listRolesResult.getInstanceProfiles()) {
            Map<String, Object> properties = new HashMap<>();
            properties.put("arn", instanceProfile.getArn());
            properties.put("creationDate", instanceProfile.getCreateDate().toString());
            if (!instanceProfile.getRoles().isEmpty()) {
                String roleName = instanceProfile.getRoles().get(0).getArn();
                properties.put("roleArn", Strings.isNullOrEmpty(roleName) ? instanceProfile.getArn() : roleName);
            }
            cloudAccessConfigs.getCloudAccessConfigs().add(new CloudAccessConfig(instanceProfile.getInstanceProfileName(), instanceProfile.getInstanceProfileId(), properties));
        }
    } catch (AmazonServiceException ase) {
        if (ase.getStatusCode() == UNAUTHORIZED) {
            String policyMessage = "Could not get instance profile roles because the user does not have enough permission.";
            LOGGER.info(policyMessage + ase);
            throw new CloudConnectorException(policyMessage, ase);
        } else {
            LOGGER.error(queryFailedMessage, ase);
            throw new CloudConnectorException(queryFailedMessage + ase.getMessage(), ase);
        }
    } catch (Exception e) {
        LOGGER.error(queryFailedMessage, e);
        throw new CloudConnectorException(queryFailedMessage + e.getMessage(), e);
    }
    return cloudAccessConfigs;
}
Also used : CloudAccessConfig(com.sequenceiq.cloudbreak.cloud.model.CloudAccessConfig) HashMap(java.util.HashMap) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudAccessConfigs(com.sequenceiq.cloudbreak.cloud.model.CloudAccessConfigs) AmazonServiceException(com.amazonaws.AmazonServiceException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) IOException(java.io.IOException) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) InstanceProfile(com.amazonaws.services.identitymanagement.model.InstanceProfile) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement) ListInstanceProfilesResult(com.amazonaws.services.identitymanagement.model.ListInstanceProfilesResult)

Example 13 with AwsCredentialView

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

the class AwsPlatformResources method sshKeys.

@Override
public CloudSshKeys sshKeys(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    Map<String, Set<CloudSshKey>> result = new HashMap<>();
    for (Region actualRegion : regions(cloudCredential, region, new HashMap<>()).getCloudRegions().keySet()) {
        // If region is provided then should filter for those region
        if (regionMatch(actualRegion, region)) {
            Set<CloudSshKey> cloudSshKeys = new HashSet<>();
            AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(cloudCredential), actualRegion.value());
            // create sshkey filter view
            PlatformResourceSshKeyFilterView filter = new PlatformResourceSshKeyFilterView(filters);
            DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
            // If the filtervalue is provided then we should filter only for those securitygroups
            if (!Strings.isNullOrEmpty(filter.getKeyName())) {
                describeKeyPairsRequest.withKeyNames(filter.getKeyName());
            }
            for (KeyPairInfo keyPairInfo : ec2Client.describeKeyPairs(describeKeyPairsRequest).getKeyPairs()) {
                Map<String, Object> properties = new HashMap<>();
                properties.put("fingerPrint", keyPairInfo.getKeyFingerprint());
                cloudSshKeys.add(new CloudSshKey(keyPairInfo.getKeyName(), properties));
            }
            result.put(actualRegion.value(), cloudSshKeys);
        }
    }
    return new CloudSshKeys(result);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) PlatformResourceSshKeyFilterView(com.sequenceiq.cloudbreak.cloud.model.view.PlatformResourceSshKeyFilterView) Set(java.util.Set) HashSet(java.util.HashSet) DescribeKeyPairsRequest(com.amazonaws.services.ec2.model.DescribeKeyPairsRequest) KeyPairInfo(com.amazonaws.services.ec2.model.KeyPairInfo) HashMap(java.util.HashMap) CloudSshKeys(com.sequenceiq.cloudbreak.cloud.model.CloudSshKeys) CloudSshKey(com.sequenceiq.cloudbreak.cloud.model.CloudSshKey) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) Region(com.sequenceiq.cloudbreak.cloud.model.Region) HashSet(java.util.HashSet)

Example 14 with AwsCredentialView

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

the class AwsPlatformResources method networks.

@Override
public CloudNetworks networks(CloudCredential cloudCredential, Region region, Map<String, String> filters) {
    Map<String, Set<CloudNetwork>> result = new HashMap<>();
    Set<CloudNetwork> cloudNetworks = new HashSet<>();
    AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(cloudCredential), region.value());
    // create vpc filter view
    PlatformResourceVpcFilterView filter = new PlatformResourceVpcFilterView(filters);
    DescribeVpcsRequest describeVpcsRequest = new DescribeVpcsRequest();
    // If the filtervalue is provided then we should filter only for those vpc
    if (!Strings.isNullOrEmpty(filter.getVpcId())) {
        describeVpcsRequest.withVpcIds(filter.getVpcId());
    }
    for (Vpc vpc : ec2Client.describeVpcs(describeVpcsRequest).getVpcs()) {
        Map<String, String> subnetMap = new HashMap<>();
        List<Subnet> subnets = ec2Client.describeSubnets(createVpcDescribeRequest(vpc)).getSubnets();
        Map<String, Object> properties = new HashMap<>();
        properties.put("cidrBlock", vpc.getCidrBlock());
        properties.put("default", vpc.getIsDefault());
        properties.put("dhcpOptionsId", vpc.getDhcpOptionsId());
        properties.put("instanceTenancy", vpc.getInstanceTenancy());
        properties.put("state", vpc.getState());
        for (Subnet subnet : subnets) {
            subnetMap.put(subnet.getSubnetId(), subnet.getSubnetId());
        }
        cloudNetworks.add(new CloudNetwork(vpc.getVpcId(), vpc.getVpcId(), subnetMap, properties));
    }
    result.put(region.value(), cloudNetworks);
    return new CloudNetworks(result);
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) PlatformResourceVpcFilterView(com.sequenceiq.cloudbreak.cloud.model.view.PlatformResourceVpcFilterView) Vpc(com.amazonaws.services.ec2.model.Vpc) CloudNetworks(com.sequenceiq.cloudbreak.cloud.model.CloudNetworks) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) Subnet(com.amazonaws.services.ec2.model.Subnet) CloudNetwork(com.sequenceiq.cloudbreak.cloud.model.CloudNetwork) HashSet(java.util.HashSet)

Example 15 with AwsCredentialView

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

the class AwsResourceConnector method resumeAutoScaling.

private void resumeAutoScaling(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.resumeProcesses(new ResumeProcessesRequest().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) ResumeProcessesRequest(com.amazonaws.services.autoscaling.model.ResumeProcessesRequest)

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