use of com.microsoft.azure.management.sql.SqlFirewallRule in project azure-sdk-for-java by Azure.
the class ManageSqlFirewallRules 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("rgRSSDFW");
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 myFirewallName = "myFirewallRule";
final String myFirewallRuleIPAddress = "10.10.10.10";
final String otherFirewallRuleStartIPAddress = "121.12.12.1";
final String otherFirewallRuleEndIPAddress = "121.12.12.10";
try {
// ============================================================
// Create a SQL Server, with 2 firewall rules.
System.out.println("Create a SQL server with 2 firewall rules adding a single IP Address and a range of IP Addresses");
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);
// ============================================================
// List and delete all firewall rules.
System.out.println("Listing all firewall rules in SQL Server.");
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 in existing SQL Server");
SqlFirewallRule firewallRule = sqlServer.firewallRules().define(myFirewallName).withIPAddress(myFirewallRuleIPAddress).create();
Utils.print(firewallRule);
System.out.println("Get a particular firewall rule in SQL Server");
firewallRule = sqlServer.firewallRules().get(myFirewallName);
Utils.print(firewallRule);
System.out.println("Deleting and adding new firewall rules as part of SQL Server update.");
sqlServer.update().withoutFirewallRule(myFirewallName).withNewFirewallRule(otherFirewallRuleStartIPAddress, otherFirewallRuleEndIPAddress).apply();
for (SqlFirewallRule sqlFirewallRule : sqlServer.firewallRules().list()) {
// Print information of the firewall rule.
Utils.print(sqlFirewallRule);
}
// 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.SqlFirewallRule 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.SqlFirewallRule 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;
}
Aggregations