Example usage for com.amazonaws.services.rds AmazonRDSClient AmazonRDSClient

List of usage examples for com.amazonaws.services.rds AmazonRDSClient AmazonRDSClient

Introduction

In this page you can find the example usage for com.amazonaws.services.rds AmazonRDSClient AmazonRDSClient.

Prototype

AmazonRDSClient(AwsSyncClientParams clientParams) 

Source Link

Document

Constructs a new client to invoke service methods on Amazon RDS using the specified parameters.

Usage

From source file:beanstalk.BeansDatabase.java

License:Apache License

public DatabaseObject createDatabase(String AWSKeyId, String AWSSecretKey, String dbname, String dbType,
        String dbIdentifier, String dbClass, int dbSize, String dbUser, String dbPassword) {//throws Exception {
    DatabaseObject dbobj = new DatabaseObject();

    String endpoint_str = "";
    BasicAWSCredentials basic_credentials = new BasicAWSCredentials(AWSKeyId, AWSSecretKey);

    AmazonRDSClient rDSClient = new AmazonRDSClient(basic_credentials);

    //Minimum size per db type, as described in API Reference
    if (dbType != null && dbType.equalsIgnoreCase("MySQL") && dbSize < 5) {
        dbSize = 5;//from   w  w  w .  j  a v  a  2  s  .  co m
    }

    if (dbType != null && (dbType.equalsIgnoreCase("oracle-se1") || dbType.equalsIgnoreCase("oracle-se")
            || dbType.equalsIgnoreCase("oracle-ee")) && dbSize < 10) {
        dbSize = 10;
    }

    CreateDBInstanceRequest create_dbinstance = new CreateDBInstanceRequest();
    create_dbinstance.setDBName(dbname);
    create_dbinstance.setEngine(dbType);
    create_dbinstance.setMasterUsername(dbUser);
    create_dbinstance.setMasterUserPassword(dbPassword);
    create_dbinstance.setDBInstanceIdentifier(dbIdentifier);
    create_dbinstance.setAllocatedStorage(dbSize);//min size =5GB for mysql,10gb for oracle
    create_dbinstance.setDBInstanceClass("db.m1.small");//db.m1.small//db.m1.large//db.m1.xlarge//db.m2.xlarge//db.m2.2xlarge//db.m2.4xlarge....
    String group = "c4sallowallipgroup";
    Vector sec_groups = new Vector();
    sec_groups.add(group);
    create_dbinstance.setDBSecurityGroups(sec_groups);
    DBInstance dbInstance_create = new DBInstance();
    System.out.println("will call createDBInstance");

    try {
        dbInstance_create = rDSClient.createDBInstance(create_dbinstance);
    } catch (AmazonClientException amazonClientException) {
        System.out.println("Error in Database Creation.");
    }
    System.out.println("called createDBInstance");

    String status = "";
    System.out.println("called createDBInstance");

    //wait 5 minutes for the first time
    int busyWaitingTime = 3000;//3sec
    //int busyWaitingTime=300000;
    //int busyWaitingTime=40000;  

    while (!status.equalsIgnoreCase("available")) {
        System.out.println("just got inside while");

        try {
            System.out.println("preparing for sleep");

            Thread.sleep(busyWaitingTime);
            System.out.println("woke up");

        } catch (InterruptedException ex) {
            Logger.getLogger(BeansDatabase.class.getName()).log(Level.SEVERE, null, ex);

        }

        DescribeDBInstancesRequest describeDBInstancesRequest = new DescribeDBInstancesRequest();

        describeDBInstancesRequest.setDBInstanceIdentifier(dbname + "cloud4soaid");
        //describeDBInstancesRequest.setDBInstanceIdentifier("c4sdb2cloud4soaid");
        DBInstance dbInstance = new DBInstance();

        System.out.println("will call describeDBInstances");
        DescribeDBInstancesResult describeDBInstances = rDSClient
                .describeDBInstances(describeDBInstancesRequest);
        System.out.println("called describeDBInstances");

        List<DBInstance> dbInstances = describeDBInstances.getDBInstances();

        System.out.println("size--->" + dbInstances.size());
        System.out.println("status--->" + dbInstances.get(0).getDBInstanceStatus());
        System.out.println("dbname--->" + dbInstances.get(0).getDBName());
        dbobj.setDbhost(dbInstances.get(0).getDBInstanceStatus());

        if (dbInstances.get(0).getEndpoint() != null) {

            System.out.println("endpoint--->" + dbInstances.get(0).getEndpoint().getAddress());
            System.out.println("port--->" + dbInstances.get(0).getEndpoint().getPort());
            System.out.println("all--->" + dbInstances.get(0).toString());

            dbobj.setDbhost(dbInstances.get(0).getEndpoint().getAddress());
            dbobj.setDbname(dbInstances.get(0).getDBName());
            dbobj.setPort(dbInstances.get(0).getEndpoint().getPort());

            status = dbInstances.get(0).getDBInstanceStatus();

            //after the first 5 minutes test every minute
            busyWaitingTime = 60000;
        }
    }
    System.out.println("just got outside while");
    System.out.println(
            "just got outside while and endpoint is :" + dbobj.getDbhost() + ", name is " + dbobj.getDbname());

    /*
     *
     * DescribeDBInstancesRequest describeDBInstancesRequest = new
     * DescribeDBInstancesRequest();
     *
     * describeDBInstancesRequest.setDBInstanceIdentifier(dbname +
     * "cloud4soaid");
     * //describeDBInstancesRequest.setDBInstanceIdentifier("c4sdb2cloud4soaid");
     * DBInstance dbInstance = new DBInstance();
     *
     * System.out.println("will call describeDBInstances");
     * DescribeDBInstancesResult describeDBInstances =
     * rDSClient.describeDBInstances(describeDBInstancesRequest);
     * System.out.println("called describeDBInstances");
     *
     * List<DBInstance> dbInstances = describeDBInstances.getDBInstances();
     *
     * System.out.println("size--->" + dbInstances.size());
     * System.out.println("status--->" +
     * dbInstances.get(0).getDBInstanceStatus());
     * System.out.println("dbname--->" + dbInstances.get(0).getDBName());
     * dbobj.setDbhost(dbInstances.get(0).getDBInstanceStatus());
     *
     *
     * if(dbInstances.get(0).getEndpoint()!=null){
     *
     *
     * System.out.println("endpoint--->" +
     * dbInstances.get(0).getEndpoint().getAddress());
     * System.out.println("port--->" +
     * dbInstances.get(0).getEndpoint().getPort());
     * System.out.println("all--->" + dbInstances.get(0).toString());
     *
     * dbobj.setDbhost(dbInstances.get(0).getEndpoint().getAddress());
     * dbobj.setDbname(dbInstances.get(0).getDBName());
     * dbobj.setPort(dbInstances.get(0).getEndpoint().getPort()); }
     */

    return dbobj;
}

From source file:beanstalk.BeansDatabase.java

License:Apache License

public DatabaseObject getDBInstanceInfo(String AWSKeyId, String AWSSecretKey, String dbname) {//throws Exception {
    BasicAWSCredentials basic_credentials = new BasicAWSCredentials(AWSKeyId, AWSSecretKey);
    AmazonRDSClient rDSClient = new AmazonRDSClient(basic_credentials);
    DescribeDBInstancesRequest describeDBInstancesRequest = new DescribeDBInstancesRequest();
    describeDBInstancesRequest.setDBInstanceIdentifier(dbname + "cloud4soaid");
    DBInstance dbInstance = new DBInstance();
    DescribeDBInstancesResult describeDBInstances = rDSClient.describeDBInstances(describeDBInstancesRequest);
    List<DBInstance> dbInstances = describeDBInstances.getDBInstances();

    System.out.println("size--->" + dbInstances.size());
    System.out.println("dbname--->" + dbInstances.get(0).getDBName());
    System.out.println("endpoint--->" + dbInstances.get(0).getEndpoint().getAddress());
    System.out.println("port--->" + dbInstances.get(0).getEndpoint().getPort());
    System.out.println("all--->" + dbInstances.get(0).toString());
    DatabaseObject dbobj = new DatabaseObject();
    dbobj.setDbhost(dbInstances.get(0).getEndpoint().getAddress());
    dbobj.setDbname(dbInstances.get(0).getDBName());
    dbobj.setPort(dbInstances.get(0).getEndpoint().getPort());

    return dbobj;

}

From source file:beanstalk.BeansDatabase.java

License:Apache License

public String getDBEndpoint(String AWSKeyId, String AWSSecretKey, String dbname) {//throws Exception {

    String endpoint_str = "";
    BasicAWSCredentials basic_credentials = new BasicAWSCredentials(AWSKeyId, AWSSecretKey);

    AmazonRDSClient rDSClient = new AmazonRDSClient(basic_credentials);

    ModifyDBInstanceRequest mod_db = new ModifyDBInstanceRequest(dbname + "cloud4soaid");
    DBInstance dbInstance = new DBInstance();
    ////CHECK THIS. in order to make correct ModifyDBInstanceRequest an attribute must be sent for modification.
    ///5 (GB) is the minimum
    mod_db.setAllocatedStorage(6);//  ww w  .j a  v  a  2 s  .c om

    dbInstance = rDSClient.modifyDBInstance(mod_db);
    Endpoint endpoint = new Endpoint();
    endpoint = dbInstance.getEndpoint();
    if (endpoint != null) {
        endpoint_str = endpoint.getAddress();
        System.out.println("endpoint to string:" + endpoint_str);/////{Address: cloud4soadbid.coaodqyxxykq.us-east-1.rds.amazonaws.com, Port: 3306, }
        System.out.println("endpoint get address:" + endpoint.getAddress());/////cloud4soadbid.coaodqyxxykq.us-east-1.rds.amazonaws.com
    }

    return endpoint_str;
}

From source file:beanstalk.BeansDatabase.java

License:Apache License

public boolean deleteDatabase(String AWSKeyId, String AWSSecretKey, String dbIdentifier) {//throws Exception {

    boolean ret = false;
    //  credentials = new PropertiesCredentials(BeanstalkDeployNoGUI.class.getResourceAsStream("AwsCredentials.properties"))
    BasicAWSCredentials basic_credentials = new BasicAWSCredentials(AWSKeyId, AWSSecretKey);

    //  SimpleDBUtils simpledb= new SimpleDBUtils();
    AmazonRDSClient rDSClient = new AmazonRDSClient(basic_credentials);
    // RestoreDBInstanceFromDBSnapshotRequest req= new RestoreDBInstanceFromDBSnapshotRequest();
    //  req.setDBName("dbname");
    //  req.setPort(3306);

    DescribeDBInstancesRequest ddbi = new DescribeDBInstancesRequest();

    DBInstance dbins = new DBInstance();
    dbins.getEndpoint();/* www .  j a  v a 2 s .  com*/
    DeleteDBInstanceRequest delRequest = new DeleteDBInstanceRequest(dbIdentifier);
    // rDSClient.restoreDBInstanceFromDBSnapshot(req);
    delRequest.setSkipFinalSnapshot(true);
    rDSClient.deleteDBInstance(delRequest);

    return ret;
}

From source file:beanstalk.BeansDatabase.java

License:Apache License

public boolean allowIPConnectionWithDB(String AWSKeyId, String AWSSecretKey, String dbIdentifier) {//throws Exception {

    boolean ret = false;
    String security_group = "Cloud4SoaSecGroup";
    BasicAWSCredentials basic_credentials = new BasicAWSCredentials(AWSKeyId, AWSSecretKey);

    AmazonRDSClient rDSClient = new AmazonRDSClient(basic_credentials);
    //1st step-->add group cloud4soa if not exist

    CreateDBSecurityGroupRequest create_secGroupRequest = new CreateDBSecurityGroupRequest(security_group,
            "GroupGeneratedByCloud4SoaAdapter");
    DBSecurityGroup securityGroup = new DBSecurityGroup();

    try {/*from  w  ww  .  j  a  v a  2 s .com*/
        securityGroup = rDSClient.createDBSecurityGroup(create_secGroupRequest);
    } catch (AmazonClientException amazonClientException) {
        System.out.print("Error when trying to add Security Group.Security Group might exist already!");
    }

    //2nd step--> add IP to list of specific Security Group

    AuthorizeDBSecurityGroupIngressRequest ip2SecGroup = new AuthorizeDBSecurityGroupIngressRequest(
            security_group);
    //allow specific ip
    //ip2SecGroup.setCIDRIP("91.132.244.150/5");
    //allow everyone
    ip2SecGroup.setCIDRIP("0.0.0.0/0");
    try {
        rDSClient.authorizeDBSecurityGroupIngress(ip2SecGroup);
    } catch (AmazonClientException amazonClientException) {
        System.out.print(
                "Error when trying to add the specific IP address to the security group.IP might be already entered!");

    }

    return ret;
}

From source file:br.com.objectos.aws.rds.maven.plugin.CreateSnapshotMojo.java

License:Apache License

private void execute0() throws MojoExecutionException {
    AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonRDSClient client = new AmazonRDSClient(credentials);

    CreateDBSnapshotRequest createDBSnapshotRequest = new CreateDBSnapshotRequest()
            .withDBInstanceIdentifier(dBInstanceIdentifier).withDBSnapshotIdentifier(dBSnapshotIdentifier);

    info("Starting RDS Snapshot '%s' at '%s'", dBSnapshotIdentifier, dBInstanceIdentifier);
    long startTime = System.currentTimeMillis();

    DBSnapshot snapshot = client.createDBSnapshot(createDBSnapshotRequest);
    info("Backing up... please wait.");

    while (!isDone(snapshot)) {
        try {//from  www .j a  va2s . co  m
            Thread.sleep(5000);
            snapshot = describeSnapshot(client);

            if (snapshot == null) {
                break;
            }
        } catch (InterruptedException e) {
            throw new MojoExecutionException("Interrupted while waiting", e);
        }
    }

    long endTime = System.currentTimeMillis();

    info("Snapshot took %d ms", endTime - startTime);
}

From source file:ch.admin.isb.hermes5.tools.deploysupport.DeploySupport.java

License:Apache License

private AmazonRDS rds() {
    AmazonRDSClient rds = new AmazonRDSClient(credentials());
    rds.setEndpoint(rdsRegionEndpoint);
    return rds;
}

From source file:com.aipo.aws.rds.RDS.java

License:Open Source License

public static AmazonRDS getClient() {
    AWSContext awsContext = AWSContext.get();
    if (awsContext == null) {
        throw new IllegalStateException("AWSContext is not initialized.");
    }//from www .j  a va  2  s.  c  om
    AmazonRDS client = new AmazonRDSClient(awsContext.getAwsCredentials());
    String endpoint = awsContext.getRdsEndpoint();
    if (endpoint != null && endpoint != "") {
        client.setEndpoint(endpoint);
    }
    return client;
}

From source file:com.amazon.aws.myyoutube.videoUtil.GetRDSInstance.java

License:Open Source License

public static void main(String[] args) throws Exception {

    credentials = new PropertiesCredentials(
            GetRDSInstance.class.getResourceAsStream("AwsCredentials.properties"));

    try {//from   w  w w .j a  v a  2  s.  com
        rds = new AmazonRDSClient(credentials);

        /*********************************************
         *  RDS DB
         *********************************************/

        System.out.println("Creating a database instance");

        CreateDBSecurityGroupRequest d = new CreateDBSecurityGroupRequest()
                .withDBSecurityGroupName("javaSecurityGroup1")
                .withDBSecurityGroupDescription("DB security group1");
        rds.createDBSecurityGroup(d);

        AuthorizeDBSecurityGroupIngressRequest auth = new AuthorizeDBSecurityGroupIngressRequest()
                .withDBSecurityGroupName("javaSecurityGroup1")
                //         .withEC2SecurityGroupName("javaSecurityGroup")
                .withCIDRIP("0.0.0.0/0");
        //         .withCIDRIP("216.165.95.69/32");

        DBSecurityGroup dbsecuritygroup = rds.authorizeDBSecurityGroupIngress(auth);
        String[] dBSecurityGroups = { dbsecuritygroup.getDBSecurityGroupName() };

        CreateDBInstanceRequest createDBInstanceRequest = new CreateDBInstanceRequest().withEngine("MySQL")
                .withLicenseModel("general-public-license").withEngineVersion("5.6.13")
                .withDBInstanceClass("db.t1.micro").withMultiAZ(false).withAutoMinorVersionUpgrade(true)
                .withAllocatedStorage(5).withDBInstanceIdentifier("mydbinstance1").withMasterUsername("awsuser")
                .withMasterUserPassword("mypassword").withDBName("dbname1").withPort(3306)
                .withAvailabilityZone(null).withDBSecurityGroups(dBSecurityGroups);

        ArrayList<String> arrDbSecur = new ArrayList<String>();
        arrDbSecur.add("javaSecurityGroup1");
        createDBInstanceRequest.setDBSecurityGroups(arrDbSecur);

        DBInstance dbInstance = rds.createDBInstance(createDBInstanceRequest);

        Thread.sleep(600000);

        DescribeDBInstancesRequest instRequest = new DescribeDBInstancesRequest()
                .withDBInstanceIdentifier("mydbinstance1");

        DescribeDBInstancesResult instres = rds.describeDBInstances(instRequest);

        Endpoint e = instres.getDBInstances().get(0).getEndpoint();
        System.out.println("ENd point " + e.getAddress() + " " + e.getPort());
        System.out.println("Database Created");
        System.out.println("Creating a table");
        //connection
        java.sql.Connection con = null;
        Statement st = null;
        // Format "jdbc:mysql://" + hostname + ":" + port + "/" + dbName + "?user=" + userName + "&password=" + password;
        String url = "jdbc:mysql://" + e.getAddress() + ":" + e.getPort()
                + "/dbname1?user=awsuser&password=mypassword"; //  "jdbc:mysql://master:password@"+e+"/dbname";
        System.out.println("Url is " + url);
        String user = "awsuser";
        String password = "mypassword";

        con = DriverManager.getConnection(url, user, password);

        System.out.println("Connection created");

        java.sql.Statement stat = con.createStatement();

        String query = "CREATE TABLE Items ( item_id VARCHAR(200), type INTEGER, quantity INTEGER, user VARCHAR(100), price FLOAT(5,2) );";
        stat.execute(query);

        String query1 = "CREATE TABLE WishList ( user VARCHAR(200), wishlistId VARCHAR(100) );";
        stat.execute(query1);

    } catch (AmazonServiceException ase) {
        System.out.println("Caught Exception: " + ase.getMessage());
        System.out.println("Response Status Code: " + ase.getStatusCode());
        System.out.println("Error Code: " + ase.getErrorCode());
        System.out.println("Request ID: " + ase.getRequestId());
    }
}

From source file:com.hangum.tadpole.aws.rds.commons.core.utils.AmazonRDSUtsils.java

License:Open Source License

/**
 * Get RDS to Tadpole UserDB data./*from w w  w . j a va2s  . c om*/
 * 
 * @param accessKey
 * @param secretKey
 * @param regionName
 * @return 
 * @throws Exception
 */
public static List<AWSRDSUserDBDAO> getDBList(String accessKey, String secretKey, String regionName)
        throws Exception {
    List<AWSRDSUserDBDAO> returnDBList = new ArrayList<AWSRDSUserDBDAO>();

    try {
        BasicAWSCredentials awsCredential = new BasicAWSCredentials(accessKey, secretKey);
        AmazonRDSClient rdsClient = new AmazonRDSClient(awsCredential);
        rdsClient.setRegion(RegionUtils.getRegion(regionName));

        DescribeDBInstancesResult describeDBInstance = rdsClient.describeDBInstances();
        List<DBInstance> listDBInstance = describeDBInstance.getDBInstances();
        for (DBInstance rdsDbInstance : listDBInstance) {
            AWSRDSUserDBDAO rdsUserDB = new AWSRDSUserDBDAO();

            // rds information
            rdsUserDB.setAccessKey(accessKey);
            rdsUserDB.setSecretKey(secretKey);
            rdsUserDB.setEndPoint(regionName);

            // ext information
            rdsUserDB.setExt1(rdsDbInstance.getDBInstanceClass());
            rdsUserDB.setExt2(rdsDbInstance.getAvailabilityZone());

            // db information
            String strDBMStype = rdsDbInstance.getEngine();
            if (strDBMStype.startsWith("sqlserver")) {
                String strEngVer = rdsDbInstance.getEngineVersion();
                //               if(strEngVer.startsWith("11")) 
                //               else strDBMStype = "MSSQL_8_LE";

                strDBMStype = DBDefine.MSSQL_DEFAULT.getDBToString();
            } else if (strDBMStype.startsWith("oracle")) {
                strDBMStype = DBDefine.ORACLE_DEFAULT.getDBToString();
            }

            rdsUserDB.setDbms_types(DBDefine.getDBDefine(strDBMStype).getDBToString());
            rdsUserDB.setDisplay_name(
                    rdsDbInstance.getDBInstanceIdentifier() + "." + rdsDbInstance.getAvailabilityZone());
            rdsUserDB.setOperation_type(DBOperationType.DEVELOP.toString());
            rdsUserDB.setDb(rdsDbInstance.getDBInstanceIdentifier());//getDBName());
            rdsUserDB.setHost(rdsDbInstance.getEndpoint().getAddress());
            rdsUserDB.setPort("" + rdsDbInstance.getEndpoint().getPort());
            rdsUserDB.setLocale(
                    rdsDbInstance.getCharacterSetName() == null ? "" : rdsDbInstance.getCharacterSetName());
            rdsUserDB.setUsers(rdsDbInstance.getMasterUsername());
            rdsUserDB.setPasswd("");

            returnDBList.add(rdsUserDB);
        }
    } catch (Exception e) {
        throw e;
    }

    return returnDBList;
}