use of com.microsoft.azure.management.sql.SqlDatabase 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;
}
use of com.microsoft.azure.management.sql.SqlDatabase in project azure-sdk-for-java by Azure.
the class ManageSqlDatabasesAcrossDifferentDataCenters 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("rgRSSDRE");
final String administratorLogin = "sqladmin3423";
final String administratorPassword = "myS3cureP@ssword";
final String slaveSqlServer1Name = "slave1sql";
final String slaveSqlServer2Name = "slave2sql";
final String databaseName = "mydatabase";
final String networkNamePrefix = "network";
final String virtualMachineNamePrefix = "samplevm";
try {
// ============================================================
// Create a SQL Server, with 2 firewall rules.
SqlServer masterSqlServer = azure.sqlServers().define(sqlServerName).withRegion(Region.US_EAST).withNewResourceGroup(rgName).withAdministratorLogin(administratorLogin).withAdministratorPassword(administratorPassword).create();
Utils.print(masterSqlServer);
// ============================================================
// Create a Database in master SQL server created above.
System.out.println("Creating a database");
SqlDatabase masterDatabase = masterSqlServer.databases().define(databaseName).withEdition(DatabaseEditions.BASIC).create();
Utils.print(masterDatabase);
// ============================================================
// Create secondary SQLServer/Database for the master database
System.out.println("Creating server in secondary location for master SQL Server");
SqlServer sqlServerInSecondaryLocation = azure.sqlServers().define(Utils.createRandomName(slaveSqlServer1Name)).withRegion(masterDatabase.defaultSecondaryLocation()).withExistingResourceGroup(rgName).withAdministratorLogin(administratorLogin).withAdministratorPassword(administratorPassword).create();
Utils.print(sqlServerInSecondaryLocation);
System.out.println("Creating database in slave SQL Server.");
SqlDatabase secondaryDatabase = sqlServerInSecondaryLocation.databases().define(databaseName).withSourceDatabase(masterDatabase).withMode(CreateMode.ONLINE_SECONDARY).create();
Utils.print(secondaryDatabase);
// ============================================================
// Create another slave SQLServer/Database for the master database
System.out.println("Creating server in another location for master SQL Server");
SqlServer sqlServerInEurope = azure.sqlServers().define(Utils.createRandomName(slaveSqlServer2Name)).withRegion(Region.EUROPE_WEST).withExistingResourceGroup(rgName).withAdministratorLogin(administratorLogin).withAdministratorPassword(administratorPassword).create();
Utils.print(sqlServerInEurope);
System.out.println("Creating database in second slave SQL Server.");
SqlDatabase secondaryDatabaseInEurope = sqlServerInEurope.databases().define(databaseName).withSourceDatabase(masterDatabase).withMode(CreateMode.ONLINE_SECONDARY).create();
Utils.print(secondaryDatabaseInEurope);
// ============================================================
// Create Virtual Networks in different regions
List<Region> regions = new ArrayList<>();
regions.add(Region.US_EAST);
regions.add(Region.US_WEST);
regions.add(Region.EUROPE_NORTH);
regions.add(Region.ASIA_SOUTHEAST);
regions.add(Region.JAPAN_EAST);
List<Creatable<Network>> creatableNetworks = new ArrayList<>();
System.out.println("Creating virtual networks in different regions.");
for (Region region : regions) {
creatableNetworks.add(azure.networks().define(Utils.createRandomName(networkNamePrefix)).withRegion(region).withExistingResourceGroup(rgName));
}
Collection<Network> networks = azure.networks().create(creatableNetworks).values();
// ============================================================
// Create virtual machines attached to different virtual networks created above.
List<Creatable<VirtualMachine>> creatableVirtualMachines = new ArrayList<>();
System.out.println("Creating virtual machines in different regions.");
for (Network network : networks) {
String vmName = Utils.createRandomName(virtualMachineNamePrefix);
Creatable<PublicIPAddress> publicIPAddressCreatable = azure.publicIPAddresses().define(vmName).withRegion(network.region()).withExistingResourceGroup(rgName).withLeafDomainLabel(vmName);
creatableVirtualMachines.add(azure.virtualMachines().define(vmName).withRegion(network.region()).withExistingResourceGroup(rgName).withExistingPrimaryNetwork(network).withSubnet(network.subnets().values().iterator().next().name()).withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(publicIPAddressCreatable).withPopularWindowsImage(KnownWindowsVirtualMachineImage.WINDOWS_SERVER_2012_R2_DATACENTER).withAdminUsername(administratorLogin).withAdminPassword(administratorPassword).withSize(VirtualMachineSizeTypes.STANDARD_D3_V2));
}
HashMap<String, String> ipAddresses = new HashMap<>();
for (VirtualMachine virtualMachine : azure.virtualMachines().create(creatableVirtualMachines).values()) {
ipAddresses.put(virtualMachine.name(), virtualMachine.getPrimaryPublicIPAddress().ipAddress());
}
System.out.println("Adding firewall rule for each of virtual network network");
List<SqlServer> sqlServers = new ArrayList<>();
sqlServers.add(sqlServerInSecondaryLocation);
sqlServers.add(sqlServerInEurope);
sqlServers.add(masterSqlServer);
for (SqlServer sqlServer : sqlServers) {
for (Map.Entry<String, String> ipAddress : ipAddresses.entrySet()) {
sqlServer.firewallRules().define(ipAddress.getKey()).withIPAddress(ipAddress.getValue()).create();
}
}
for (SqlServer sqlServer : sqlServers) {
System.out.println("Print firewall rules in Sql Server in " + sqlServer.regionName());
List<SqlFirewallRule> firewallRules = sqlServer.firewallRules().list();
for (SqlFirewallRule firewallRule : firewallRules) {
Utils.print(firewallRule);
}
}
// Delete the SQL Server.
System.out.println("Deleting all Sql Servers");
for (SqlServer sqlServer : sqlServers) {
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;
}
use of com.microsoft.azure.management.sql.SqlDatabase in project azure-sdk-for-java by Azure.
the class ManageSqlDatabase 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("rgRSDSI");
final String administratorLogin = "sqladmin3423";
final String administratorPassword = "myS3cureP@ssword";
final String firewallRuleIPAddress = "10.0.0.1";
final String firewallRuleStartIPAddress = "10.2.0.1";
final String firewallRuleEndIPAddress = "10.2.0.10";
final String databaseName = "mydatabase";
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).withNewFirewallRule(firewallRuleIPAddress).withNewFirewallRule(firewallRuleStartIPAddress, firewallRuleEndIPAddress).create();
Utils.print(sqlServer);
// ============================================================
// Create a Database in SQL server created above.
System.out.println("Creating a database");
SqlDatabase database = sqlServer.databases().define(databaseName).create();
Utils.print(database);
// ============================================================
// Update the edition of database.
System.out.println("Updating a database");
database = database.update().withEdition(DatabaseEditions.STANDARD).withServiceObjective(ServiceObjectiveName.S3).apply();
Utils.print(database);
// ============================================================
// List and delete all firewall rules.
System.out.println("Listing all firewall rules");
List<SqlFirewallRule> firewallRules = sqlServer.firewallRules().list();
for (SqlFirewallRule firewallRule : firewallRules) {
// Print information of the firewall rule.
Utils.print(firewallRule);
// Delete the firewall rule.
System.out.println("Deleting a firewall rule");
firewallRule.delete();
}
// ============================================================
// Add new firewall rules.
System.out.println("Creating a firewall rule for SQL Server");
SqlFirewallRule firewallRule = sqlServer.firewallRules().define("myFirewallRule").withIPAddress("10.10.10.10").create();
Utils.print(firewallRule);
// Delete the database.
System.out.println("Deleting a database");
database.delete();
// 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;
}
use of com.microsoft.azure.management.sql.SqlDatabase in project azure-sdk-for-java by Azure.
the class ManageWebAppSqlConnection 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).withNewWindowsPlan(PricingTier.STANDARD_S1).withPhpVersion(PhpVersion.PHP5_6).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;
}
use of com.microsoft.azure.management.sql.SqlDatabase 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;
}
Aggregations