Search in sources :

Example 1 with ElasticPoolEditions

use of com.microsoft.azure.management.sql.ElasticPoolEditions in project azure-sdk-for-java by Azure.

the class ManageSqlDatabaseInElasticPool method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    final String sqlServerName = Utils.createRandomName("sqlserver");
    final String rgName = Utils.createRandomName("rgRSSDEP");
    final String elasticPoolName = "myElasticPool";
    final String elasticPool2Name = "secondElasticPool";
    final String administratorLogin = "sqladmin3423";
    final String administratorPassword = "myS3cureP@ssword";
    final String database1Name = "myDatabase1";
    final String database2Name = "myDatabase2";
    final String anotherDatabaseName = "myAnotherDatabase";
    final ElasticPoolEditions elasticPoolEdition = ElasticPoolEditions.STANDARD;
    try {
        // ============================================================
        // Create a SQL Server, with 2 firewall rules.
        SqlServer sqlServer = azure.sqlServers().define(sqlServerName).withRegion(Region.US_EAST).withNewResourceGroup(rgName).withAdministratorLogin(administratorLogin).withAdministratorPassword(administratorPassword).withNewElasticPool(elasticPoolName, elasticPoolEdition, database1Name, database2Name).create();
        Utils.print(sqlServer);
        // List and prints the elastic pools
        for (SqlElasticPool elasticPool : sqlServer.elasticPools().list()) {
            Utils.print(elasticPool);
        }
        // ============================================================
        // Get and prints the elastic pool
        SqlElasticPool elasticPool = sqlServer.elasticPools().get(elasticPoolName);
        Utils.print(elasticPool);
        // ============================================================
        // Change DTUs in the elastic pools.
        elasticPool = elasticPool.update().withDtu(200).withStorageCapacity(204800).withDatabaseDtuMin(10).withDatabaseDtuMax(50).apply();
        Utils.print(elasticPool);
        System.out.println("Start ------- Current databases in the elastic pool");
        for (SqlDatabase databaseInElasticPool : elasticPool.listDatabases()) {
            Utils.print(databaseInElasticPool);
        }
        System.out.println("End --------- Current databases in the elastic pool");
        // ============================================================
        // Create a Database in SQL server created above.
        System.out.println("Creating a database");
        SqlDatabase database = sqlServer.databases().define("myNewDatabase").create();
        Utils.print(database);
        System.out.println("Start ------- Current databases in the elastic pool");
        for (SqlDatabase databaseInElasticPool : elasticPool.listDatabases()) {
            Utils.print(databaseInElasticPool);
        }
        System.out.println("End --------- Current databases in the elastic pool");
        // ============================================================
        // Move newly created database to the pool.
        System.out.println("Updating a database");
        database = database.update().withExistingElasticPool(elasticPoolName).apply();
        Utils.print(database);
        // ============================================================
        // Create another database and move it in elastic pool as update to the elastic pool.
        SqlDatabase anotherDatabase = sqlServer.databases().define(anotherDatabaseName).create();
        // ============================================================
        // Update the elastic pool to have newly created database.
        elasticPool.update().withExistingDatabase(anotherDatabase).apply();
        System.out.println("Start ------- Current databases in the elastic pool");
        for (SqlDatabase databaseInElasticPool : elasticPool.listDatabases()) {
            Utils.print(databaseInElasticPool);
        }
        System.out.println("End --------- Current databases in the elastic pool");
        // ============================================================
        // Remove the database from the elastic pool.
        System.out.println("Remove the database from the pool.");
        anotherDatabase = anotherDatabase.update().withoutElasticPool().withEdition(DatabaseEditions.STANDARD).apply();
        Utils.print(anotherDatabase);
        System.out.println("Start ------- Current databases in the elastic pool");
        for (SqlDatabase databaseInElasticPool : elasticPool.listDatabases()) {
            Utils.print(databaseInElasticPool);
        }
        System.out.println("End --------- Current databases in the elastic pool");
        // ============================================================
        // Get list of elastic pool's activities and print the same.
        System.out.println("Start ------- Activities in a elastic pool");
        for (ElasticPoolActivity activity : elasticPool.listActivities()) {
            Utils.print(activity);
        }
        System.out.println("End ------- Activities in a elastic pool");
        // ============================================================
        // Get list of elastic pool's database activities and print the same.
        System.out.println("Start ------- Activities in a elastic pool");
        for (ElasticPoolDatabaseActivity databaseActivity : elasticPool.listDatabaseActivities()) {
            Utils.print(databaseActivity);
        }
        System.out.println("End ------- Activities in a elastic pool");
        // ============================================================
        // List databases in the sql server and delete the same.
        System.out.println("List and delete all databases from SQL Server");
        for (SqlDatabase databaseInServer : sqlServer.databases().list()) {
            Utils.print(databaseInServer);
            databaseInServer.delete();
        }
        // ============================================================
        // Create another elastic pool in SQL Server
        System.out.println("Create ElasticPool in existing SQL Server");
        SqlElasticPool elasticPool2 = sqlServer.elasticPools().define(elasticPool2Name).withEdition(elasticPoolEdition).create();
        Utils.print(elasticPool2);
        // ============================================================
        // Deletes the elastic pool.
        System.out.println("Delete the elastic pool from the SQL Server");
        sqlServer.elasticPools().delete(elasticPoolName);
        sqlServer.elasticPools().delete(elasticPool2Name);
        // ============================================================
        // Delete the SQL Server.
        System.out.println("Deleting a Sql Server");
        azure.sqlServers().deleteById(sqlServer.id());
        return true;
    } catch (Exception f) {
        System.out.println(f.getMessage());
        f.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().deleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (Exception e) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        }
    }
    return false;
}
Also used : ElasticPoolActivity(com.microsoft.azure.management.sql.ElasticPoolActivity) ElasticPoolEditions(com.microsoft.azure.management.sql.ElasticPoolEditions) SqlDatabase(com.microsoft.azure.management.sql.SqlDatabase) SqlServer(com.microsoft.azure.management.sql.SqlServer) SqlElasticPool(com.microsoft.azure.management.sql.SqlElasticPool) ElasticPoolDatabaseActivity(com.microsoft.azure.management.sql.ElasticPoolDatabaseActivity)

Aggregations

ElasticPoolActivity (com.microsoft.azure.management.sql.ElasticPoolActivity)1 ElasticPoolDatabaseActivity (com.microsoft.azure.management.sql.ElasticPoolDatabaseActivity)1 ElasticPoolEditions (com.microsoft.azure.management.sql.ElasticPoolEditions)1 SqlDatabase (com.microsoft.azure.management.sql.SqlDatabase)1 SqlElasticPool (com.microsoft.azure.management.sql.SqlElasticPool)1 SqlServer (com.microsoft.azure.management.sql.SqlServer)1