Example usage for com.amazonaws.services.redshift.model DescribeClustersRequest DescribeClustersRequest

List of usage examples for com.amazonaws.services.redshift.model DescribeClustersRequest DescribeClustersRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.redshift.model DescribeClustersRequest DescribeClustersRequest.

Prototype

DescribeClustersRequest

Source Link

Usage

From source file:com.amazon.services.awsrum.utils.RedshiftUtils.java

License:Open Source License

/**
 * Gets the JDBC URL associated with an active Redshift cluster.
 * //w  w w .j  av  a2s.  c  o m
 * @param client
 *            The {@link AmazonRedshiftClient} with read permissions
 * @param clusterIdentifier
 *            The unique Redshift cluster identifier
 * @return JDBC URL for the Redshift cluster
 */
public static String getClusterURL(AmazonRedshiftClient client, String clusterIdentifier) {
    DescribeClustersRequest describeClustersRequest = new DescribeClustersRequest();
    describeClustersRequest.setClusterIdentifier(clusterIdentifier);
    DescribeClustersResult describeClustersResult = client.describeClusters(describeClustersRequest);
    List<Cluster> clusters = describeClustersResult.getClusters();
    if (!clusters.isEmpty()) {
        return toJDBC(clusters.get(0).getEndpoint(), clusters.get(0).getDBName());
    }
    return null;
}

From source file:com.amazon.services.awsrum.utils.RedshiftUtils.java

License:Open Source License

/**
 * Helper method to determine if a Redshift cluster exists
 * //w  w w .  j  ava  2  s  . co m
 * @param client
 *            The {@link AmazonRedshiftClient} with read permissions
 * @param clusterIdentifier
 *            The Redshift cluster to check
 * @return true if the Redshift cluster exists, otherwise return false
 */
private static boolean clusterExists(AmazonRedshiftClient client, String clusterIdentifier) {
    DescribeClustersRequest describeClustersRequest = new DescribeClustersRequest();
    describeClustersRequest.setClusterIdentifier(clusterIdentifier);
    try {
        client.describeClusters(describeClustersRequest);
        return true;
    } catch (ClusterNotFoundException e) {
        return false;
    }

}

From source file:com.amazon.services.awsrum.utils.RedshiftUtils.java

License:Open Source License

/**
 * Helper method to determine the Redshift cluster state
 * //from w  ww.j a va2 s.c o  m
 * @param client
 *            The {@link AmazonRedshiftClient} with read permissions
 * @param clusterIdentifier
 *            The Redshift cluster to get the state of
 * @return The String representation of the Redshift cluster state
 */
public static String clusterState(AmazonRedshiftClient client, String clusterIdentifier) {
    DescribeClustersRequest describeClustersRequest = new DescribeClustersRequest();
    describeClustersRequest.setClusterIdentifier(clusterIdentifier);
    List<Cluster> clusters = client.describeClusters(describeClustersRequest).getClusters();
    if (clusters.size() == 1) {
        return clusters.get(0).getClusterStatus();
    }
    throw new ClusterNotFoundException(clusterIdentifier);

}

From source file:com.gs.obevo.amazon.RedshiftInstanceUtil.java

License:Apache License

private void describe(String dbInstanceIdentifier) throws Exception {
    while (true) {
        DescribeDBInstancesRequest request = new DescribeDBInstancesRequest()
                .withDBInstanceIdentifier(dbInstanceIdentifier);

        DescribeClustersResult response = client
                .describeClusters(new DescribeClustersRequest().withClusterIdentifier(INSTANCE_ID));
        Cluster dbInstance = response.getClusters().get(0);
        if (!dbInstance.getClusterStatus().equalsIgnoreCase("creating")) {
            System.out.println("Done! " + response);
            System.out.println(dbInstance.getEndpoint().getAddress());
            System.out.println(dbInstance.getEndpoint().getPort());
            break;
        }/*w ww  .  j av a  2 s .c  om*/

        System.out.println("Not done - will wait 10s: " + response);
        Thread.sleep(10000L);
    }
}

From source file:com.jaspersoft.jasperserver.api.engine.jasperreports.util.AwsDataSourceRecovery.java

License:Open Source License

private void createRedshiftSecurityGroup(AwsReportDataSource awsReportDataSource) throws Exception {

    AWSCredentials awsCredentials = AwsCredentialUtil.getAWSCredentials(awsReportDataSource.getAWSAccessKey(),
            awsReportDataSource.getAWSSecretKey(), awsReportDataSource.getRoleARN());

    AmazonRedshiftClient redshiftClient = new AmazonRedshiftClient(awsCredentials);
    DescribeClustersRequest describeClustersRequest = new DescribeClustersRequest()
            .withClusterIdentifier(awsReportDataSource.getDbInstanceIdentifier());
    String endpoint = awsReportDataSource.getAWSRegion();
    if (endpoint != null) {
        redshiftClient.setEndpoint(Redshift + "." + endpoint);
    }/*ww  w .j a v a 2s .c  o m*/
    Cluster cluster;
    DescribeClustersResult describeClustersResult = redshiftClient.describeClusters(describeClustersRequest);
    if (describeClustersResult != null && describeClustersResult.getClusters() != null
            && describeClustersResult.getClusters().size() > 0) {
        cluster = describeClustersResult.getClusters().get(0);
        if (!cluster.getClusterStatus().equals(awsDataSourceActiveStatus)) {
            throw new JSException(getErrorMessage("aws.exception.datasource.recovery.instance.not.active"));
        }
        Map<String, String> awsDSInstanceDetails = new HashMap<String, String>();
        awsDSInstanceDetails.put(DB_REGION, parseRegionFromSubRegion(cluster.getAvailabilityZone()));
        String vpcId = cluster.getVpcId();
        if (isNotEmpty(vpcId)) {
            awsDSInstanceDetails.put(DB_VPC_ID, vpcId);
        } else {
            awsDSInstanceDetails.put(DB_VPC_ID, null);
        }

        String instanceSourceIp = determineSourceIpAddress(awsDSInstanceDetails);

        if (!isNotEmpty(instanceSourceIp)) {
            throw new JSException(
                    getErrorMessage("aws.exception.datasource.recovery.public.ip.not.determined"));
        }
        //IP that should be added in CIDRIP of JS DB Security Group
        String ingressIpMask = instanceSourceIp + ingressIpPermission;

        String vpcSecurityGroupId = null;
        if (awsDSInstanceDetails.get(DB_VPC_ID) != null) {
            //Recover VPC Security Group.
            vpcSecurityGroupId = recoverVpcSecurityGroup(awsReportDataSource,
                    awsDSInstanceDetails.get(DB_VPC_ID), ingressIpMask);
        } else {
            //Recover Cluster Security Group.

            //Fount existing JS DB Security Group
            Boolean jsSecurityGroupMembershipFount = true;

            ClusterSecurityGroup clusterSecurityGroup = null;
            try {
                DescribeClusterSecurityGroupsRequest describeClusterSecurityGroupsRequest = new DescribeClusterSecurityGroupsRequest()
                        .withClusterSecurityGroupName(awsProperties.getSecurityGroupName());
                DescribeClusterSecurityGroupsResult describeClusterSecurityGroupsResult = redshiftClient
                        .describeClusterSecurityGroups(describeClusterSecurityGroupsRequest);
                clusterSecurityGroup = describeClusterSecurityGroupsResult.getClusterSecurityGroups().get(0);
            } catch (ClusterSecurityGroupNotFoundException ex) {
                jsSecurityGroupMembershipFount = false;
            }

            boolean ingressIpMaskExist = false;
            if (jsSecurityGroupMembershipFount) {
                List<com.amazonaws.services.redshift.model.IPRange> ipRanges = clusterSecurityGroup
                        .getIPRanges();
                for (com.amazonaws.services.redshift.model.IPRange ipRange : ipRanges) {
                    if (ipRange.getCIDRIP().contains(ingressIpMask)) {
                        ingressIpMaskExist = true;
                        break;
                    }
                }
                if (!ingressIpMaskExist) {
                    //Remove old ingress Ips
                    for (com.amazonaws.services.redshift.model.IPRange ipRange : ipRanges) {
                        RevokeClusterSecurityGroupIngressRequest revokeClusterSecurityGroupIngressRequest = new RevokeClusterSecurityGroupIngressRequest()
                                .withClusterSecurityGroupName(awsProperties.getSecurityGroupName())
                                .withCIDRIP(ipRange.getCIDRIP());
                        redshiftClient
                                .revokeClusterSecurityGroupIngress(revokeClusterSecurityGroupIngressRequest);
                    }
                }
            } else {
                clusterSecurityGroup = redshiftClient
                        .createClusterSecurityGroup(new CreateClusterSecurityGroupRequest()
                                .withClusterSecurityGroupName(awsProperties.getSecurityGroupName())
                                .withDescription(awsProperties.getSecurityGroupDescription()));
            }
            if (!ingressIpMaskExist) {
                redshiftClient
                        .authorizeClusterSecurityGroupIngress(new AuthorizeClusterSecurityGroupIngressRequest()
                                .withClusterSecurityGroupName(
                                        clusterSecurityGroup.getClusterSecurityGroupName())
                                .withCIDRIP(ingressIpMask));
            }
        }
        if (vpcSecurityGroupId == null) {
            List<ClusterSecurityGroupMembership> clusterSecurityGroupMemberships = cluster
                    .getClusterSecurityGroups();
            List<String> clusterSecurityGroupNames = new ArrayList<String>();
            for (ClusterSecurityGroupMembership clusterSecurityGroupMembership : clusterSecurityGroupMemberships) {
                clusterSecurityGroupNames.add(clusterSecurityGroupMembership.getClusterSecurityGroupName());
            }
            //If Redshift Instance does not contain JSSecurityGroup that we should assign it to.
            if (!clusterSecurityGroupNames.contains(awsProperties.getSecurityGroupName())) {
                clusterSecurityGroupNames.add(awsProperties.getSecurityGroupName());
                ModifyClusterRequest modifyClusterRequest = new ModifyClusterRequest()
                        .withClusterSecurityGroups(clusterSecurityGroupNames)
                        .withClusterIdentifier(cluster.getClusterIdentifier());
                redshiftClient.modifyCluster(modifyClusterRequest);
            }
        } else {
            List<com.amazonaws.services.redshift.model.VpcSecurityGroupMembership> vpcSecurityGroupMemberships = cluster
                    .getVpcSecurityGroups();
            List<String> vpcSecurityGroupIds = new ArrayList<String>();
            for (com.amazonaws.services.redshift.model.VpcSecurityGroupMembership vpcSecurityGroupMembership : vpcSecurityGroupMemberships) {
                vpcSecurityGroupIds.add(vpcSecurityGroupMembership.getVpcSecurityGroupId());
            }
            //If Redshift Instance does not contain VPC Security Group that we should assign it to.
            if (!vpcSecurityGroupIds.contains(vpcSecurityGroupId)) {
                vpcSecurityGroupIds.add(vpcSecurityGroupId);
                ModifyClusterRequest modifyClusterRequest = new ModifyClusterRequest()
                        .withVpcSecurityGroupIds(vpcSecurityGroupIds)
                        .withClusterIdentifier(cluster.getClusterIdentifier());
                redshiftClient.modifyCluster(modifyClusterRequest);
            }
        }
    }
}