Search in sources :

Example 6 with SqlServer

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

the class ManageLinuxWebAppSqlConnection 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) {
    // New resources
    final String suffix = ".azurewebsites.net";
    final String appName = SdkContext.randomResourceName("webapp1-", 20);
    final String appUrl = appName + suffix;
    final String sqlServerName = SdkContext.randomResourceName("jsdkserver", 20);
    final String sqlDbName = SdkContext.randomResourceName("jsdkdb", 20);
    final String admin = "jsdkadmin";
    final String password = "StrongPass!123";
    final String rgName = SdkContext.randomResourceName("rg1NEMV_", 24);
    try {
        //============================================================
        // Create a sql server
        System.out.println("Creating SQL server " + sqlServerName + "...");
        SqlServer server = azure.sqlServers().define(sqlServerName).withRegion(Region.US_WEST).withNewResourceGroup(rgName).withAdministratorLogin(admin).withAdministratorPassword(password).create();
        System.out.println("Created SQL server " + server.name());
        //============================================================
        // Create a sql database for the web app to use
        System.out.println("Creating SQL database " + sqlDbName + "...");
        SqlDatabase db = server.databases().define(sqlDbName).create();
        System.out.println("Created SQL database " + db.name());
        //============================================================
        // Create a web app with a new app service plan
        System.out.println("Creating web app " + appName + "...");
        WebApp app = azure.webApps().define(appName).withRegion(Region.US_WEST).withExistingResourceGroup(rgName).withNewLinuxPlan(PricingTier.STANDARD_S1).withBuiltInImage(RuntimeStack.PHP_5_6_23).defineSourceControl().withPublicGitRepository("https://github.com/ProjectNami/projectnami").withBranch("master").attach().withAppSetting("ProjectNami.DBHost", server.fullyQualifiedDomainName()).withAppSetting("ProjectNami.DBName", db.name()).withAppSetting("ProjectNami.DBUser", admin).withAppSetting("ProjectNami.DBPass", password).create();
        System.out.println("Created web app " + app.name());
        Utils.print(app);
        //============================================================
        // Allow web app to access the SQL server
        System.out.println("Allowing web app " + appName + " to access SQL server...");
        SqlServer.Update update = server.update();
        for (String ip : app.outboundIPAddresses()) {
            update = update.withNewFirewallRule(ip);
        }
        server = update.apply();
        System.out.println("Firewall rules added for web app " + appName);
        Utils.print(server);
        System.out.println("Your WordPress app is ready.");
        System.out.println("Please navigate to http://" + appUrl + " to finish the GUI setup. Press enter to exit.");
        System.in.read();
        return true;
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().beginDeleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : SqlDatabase(com.microsoft.azure.management.sql.SqlDatabase) SqlServer(com.microsoft.azure.management.sql.SqlServer) WebApp(com.microsoft.azure.management.appservice.WebApp)

Aggregations

SqlServer (com.microsoft.azure.management.sql.SqlServer)6 SqlDatabase (com.microsoft.azure.management.sql.SqlDatabase)5 SqlFirewallRule (com.microsoft.azure.management.sql.SqlFirewallRule)3 WebApp (com.microsoft.azure.management.appservice.WebApp)2 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)1 Network (com.microsoft.azure.management.network.Network)1 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)1 Region (com.microsoft.azure.management.resources.fluentcore.arm.Region)1 Creatable (com.microsoft.azure.management.resources.fluentcore.model.Creatable)1 ElasticPoolActivity (com.microsoft.azure.management.sql.ElasticPoolActivity)1 ElasticPoolDatabaseActivity (com.microsoft.azure.management.sql.ElasticPoolDatabaseActivity)1 ElasticPoolEditions (com.microsoft.azure.management.sql.ElasticPoolEditions)1 SqlElasticPool (com.microsoft.azure.management.sql.SqlElasticPool)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1