use of com.microsoft.azure.management.network.ApplicationGateway in project azure-sdk-for-java by Azure.
the class ManageApplicationGateway 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 rgName = SdkContext.randomResourceName("rgNEAG", 15);
final String pipName = SdkContext.randomResourceName("pip" + "-", 18);
final String userName = "tirekicker";
final String sshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD azjava@javalib.com";
int backendPools = 2;
int vmCountInAPool = 4;
Region[] regions = { Region.US_EAST, Region.UK_WEST };
String[] addressSpaces = { "172.16.0.0/16", "172.17.0.0/16" };
String[][] publicIpCreatableKeys = new String[backendPools][vmCountInAPool];
String[][] ipAddresses = new String[backendPools][vmCountInAPool];
try {
//=============================================================
// Create a resource group (Where all resources gets created)
//
ResourceGroup resourceGroup = azure.resourceGroups().define(rgName).withRegion(Region.US_EAST).create();
System.out.println("Created a new resource group - " + resourceGroup.id());
//=============================================================
// Create a public IP address for the Application Gateway
System.out.println("Creating a public IP address for the application gateway ...");
PublicIPAddress publicIPAddress = azure.publicIPAddresses().define(pipName).withRegion(Region.US_EAST).withExistingResourceGroup(rgName).create();
System.out.println("Created a public IP address");
// Print the virtual network details
Utils.print(publicIPAddress);
//=============================================================
// Create backend pools
// Prepare a batch of Creatable definitions
List<Creatable<VirtualMachine>> creatableVirtualMachines = new ArrayList<>();
for (int i = 0; i < backendPools; i++) {
//=============================================================
// Create 1 network creatable per region
// Prepare Creatable Network definition (Where all the virtual machines get added to)
String networkName = SdkContext.randomResourceName("vnetNEAG-", 20);
Creatable<Network> networkCreatable = azure.networks().define(networkName).withRegion(regions[i]).withExistingResourceGroup(resourceGroup).withAddressSpace(addressSpaces[i]);
//=============================================================
// Create 1 storage creatable per region (For storing VMs disk)
String storageAccountName = SdkContext.randomResourceName("stgneag", 20);
Creatable<StorageAccount> storageAccountCreatable = azure.storageAccounts().define(storageAccountName).withRegion(regions[i]).withExistingResourceGroup(resourceGroup);
String linuxVMNamePrefix = SdkContext.randomResourceName("vm-", 15);
for (int j = 0; j < vmCountInAPool; j++) {
//=============================================================
// Create 1 public IP address creatable
Creatable<PublicIPAddress> publicIPAddressCreatable = azure.publicIPAddresses().define(String.format("%s-%d", linuxVMNamePrefix, j)).withRegion(regions[i]).withExistingResourceGroup(resourceGroup).withLeafDomainLabel(String.format("%s-%d", linuxVMNamePrefix, j));
publicIpCreatableKeys[i][j] = publicIPAddressCreatable.key();
//=============================================================
// Create 1 virtual machine creatable
Creatable<VirtualMachine> virtualMachineCreatable = azure.virtualMachines().define(String.format("%s-%d", linuxVMNamePrefix, j)).withRegion(regions[i]).withExistingResourceGroup(resourceGroup).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(publicIPAddressCreatable).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshKey).withSize(VirtualMachineSizeTypes.STANDARD_DS3_V2).withNewStorageAccount(storageAccountCreatable);
creatableVirtualMachines.add(virtualMachineCreatable);
}
}
//=============================================================
// Create two backend pools of virtual machines
StopWatch stopwatch = new StopWatch();
System.out.println("Creating virtual machines (two backend pools)");
stopwatch.start();
CreatedResources<VirtualMachine> virtualMachines = azure.virtualMachines().create(creatableVirtualMachines);
stopwatch.stop();
System.out.println("Created virtual machines (two backend pools)");
for (VirtualMachine virtualMachine : virtualMachines.values()) {
System.out.println(virtualMachine.id());
}
System.out.println("Virtual machines created: (took " + (stopwatch.getTime() / 1000) + " seconds) to create == " + virtualMachines.size() + " == virtual machines (4 virtual machines per backend pool)");
//=======================================================================
// Get IP addresses from created resources
System.out.println("IP Addresses in the backend pools are - ");
for (int i = 0; i < backendPools; i++) {
for (int j = 0; j < vmCountInAPool; j++) {
PublicIPAddress pip = (PublicIPAddress) virtualMachines.createdRelatedResource(publicIpCreatableKeys[i][j]);
pip.refresh();
ipAddresses[i][j] = pip.ipAddress();
System.out.println(String.format("[backend pool = %d][vm = %d] = %s", i, j, ipAddresses[i][j]));
}
System.out.println("======");
}
//=======================================================================
// Create an application gateway
System.out.println("================= CREATE ======================");
System.out.println("Creating an application gateway");
stopwatch.reset();
stopwatch.start();
final String sslCertificatePfxPath = ManageApplicationGateway.class.getClassLoader().getResource("myTest._pfx").getPath();
final String sslCertificatePfxPath2 = ManageApplicationGateway.class.getClassLoader().getResource("myTest2._pfx").getPath();
ApplicationGateway applicationGateway = azure.applicationGateways().define("myFirstAppGateway").withRegion(Region.US_EAST).withExistingResourceGroup(resourceGroup).defineRequestRoutingRule("HTTP-80-to-8080").fromPublicFrontend().fromFrontendHttpPort(80).toBackendHttpPort(8080).toBackendIPAddress(ipAddresses[0][0]).toBackendIPAddress(ipAddresses[0][1]).toBackendIPAddress(ipAddresses[0][2]).toBackendIPAddress(ipAddresses[0][3]).attach().defineRequestRoutingRule("HTTPs-443-to-8080").fromPublicFrontend().fromFrontendHttpsPort(443).withSslCertificateFromPfxFile(new File(sslCertificatePfxPath)).withSslCertificatePassword("Abc123").toBackendHttpPort(8080).toBackendIPAddress(ipAddresses[1][0]).toBackendIPAddress(ipAddresses[1][1]).toBackendIPAddress(ipAddresses[1][2]).toBackendIPAddress(ipAddresses[1][3]).attach().withExistingPublicIPAddress(publicIPAddress).create();
stopwatch.stop();
System.out.println("Application gateway created: (took " + (stopwatch.getTime() / 1000) + " seconds)");
Utils.print(applicationGateway);
//=======================================================================
// Update an application gateway
// configure the first routing rule for SSL offload
System.out.println("================= UPDATE ======================");
System.out.println("Updating the application gateway");
stopwatch.reset();
stopwatch.start();
applicationGateway.update().withoutRequestRoutingRule("HTTP-80-to-8080").defineRequestRoutingRule("HTTPs-1443-to-8080").fromPublicFrontend().fromFrontendHttpsPort(1443).withSslCertificateFromPfxFile(new File(sslCertificatePfxPath2)).withSslCertificatePassword("Abc123").toBackendHttpPort(8080).toBackendIPAddress(ipAddresses[0][0]).toBackendIPAddress(ipAddresses[0][1]).toBackendIPAddress(ipAddresses[0][2]).toBackendIPAddress(ipAddresses[0][3]).withHostName("www.contoso.com").withCookieBasedAffinity().attach().apply();
stopwatch.stop();
System.out.println("Application gateway updated: (took " + (stopwatch.getTime() / 1000) + " seconds)");
Utils.print(applicationGateway);
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 (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.network.ApplicationGateway in project azure-sdk-for-java by Azure.
the class AzureTests method testApplicationGatewaysInParallel.
@Test
public void testApplicationGatewaysInParallel() throws Exception {
String rgName = SdkContext.randomResourceName("rg", 13);
Region region = Region.US_EAST;
Creatable<ResourceGroup> resourceGroup = azure.resourceGroups().define(rgName).withRegion(region);
List<Creatable<ApplicationGateway>> agCreatables = new ArrayList<>();
agCreatables.add(azure.applicationGateways().define(SdkContext.randomResourceName("ag", 13)).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroup).defineRequestRoutingRule("rule1").fromPrivateFrontend().fromFrontendHttpPort(80).toBackendHttpPort(8080).toBackendIPAddress("10.0.0.1").toBackendIPAddress("10.0.0.2").attach());
agCreatables.add(azure.applicationGateways().define(SdkContext.randomResourceName("ag", 13)).withRegion(Region.US_EAST).withNewResourceGroup(resourceGroup).defineRequestRoutingRule("rule1").fromPrivateFrontend().fromFrontendHttpPort(80).toBackendHttpPort(8080).toBackendIPAddress("10.0.0.3").toBackendIPAddress("10.0.0.4").attach());
CreatedResources<ApplicationGateway> created = azure.applicationGateways().create(agCreatables);
List<ApplicationGateway> ags = new ArrayList<>();
List<String> agIds = new ArrayList<>();
for (Creatable<ApplicationGateway> creatable : agCreatables) {
ApplicationGateway ag = created.get(creatable.key());
Assert.assertNotNull(ag);
ags.add(ag);
agIds.add(ag.id());
}
azure.applicationGateways().stop(agIds);
for (ApplicationGateway ag : ags) {
Assert.assertEquals(ApplicationGatewayOperationalState.STOPPED, ag.refresh().operationalState());
}
azure.applicationGateways().start(agIds);
for (ApplicationGateway ag : ags) {
Assert.assertEquals(ApplicationGatewayOperationalState.RUNNING, ag.refresh().operationalState());
}
azure.applicationGateways().deleteByIds(agIds);
for (String id : agIds) {
Assert.assertNull(azure.applicationGateways().getById(id));
}
azure.resourceGroups().beginDeleteByName(rgName);
}
use of com.microsoft.azure.management.network.ApplicationGateway in project azure-sdk-for-java by Azure.
the class ManageSimpleApplicationGateway 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 rgName = SdkContext.randomResourceName("rgNEAGS", 15);
try {
//=======================================================================
// Create an application gateway
System.out.println("================= CREATE ======================");
System.out.println("Creating an application gateway... (this can take about 20 min)");
long t1 = System.currentTimeMillis();
ApplicationGateway applicationGateway = azure.applicationGateways().define("myFirstAppGateway").withRegion(Region.US_EAST).withNewResourceGroup(rgName).defineRequestRoutingRule("HTTP-80-to-8080").fromPublicFrontend().fromFrontendHttpPort(80).toBackendHttpPort(8080).toBackendIPAddress("11.1.1.1").toBackendIPAddress("11.1.1.2").toBackendIPAddress("11.1.1.3").toBackendIPAddress("11.1.1.4").attach().withNewPublicIPAddress().create();
long t2 = System.currentTimeMillis();
System.out.println("Application gateway created: (took " + (t2 - t1) / 1000 + " seconds)");
Utils.print(applicationGateway);
//=======================================================================
// Update an application gateway
// configure the first routing rule for SSL offload
System.out.println("================= UPDATE ======================");
System.out.println("Updating the application gateway");
t1 = System.currentTimeMillis();
applicationGateway.update().withoutRequestRoutingRule("HTTP-80-to-8080").defineRequestRoutingRule("HTTPs-1443-to-8080").fromPublicFrontend().fromFrontendHttpsPort(1443).withSslCertificateFromPfxFile(new File(ManageSimpleApplicationGateway.class.getClassLoader().getResource("myTest.pfx").getPath())).withSslCertificatePassword("Abc123").toBackendHttpPort(8080).toBackendIPAddress("11.1.1.1").toBackendIPAddress("11.1.1.2").toBackendIPAddress("11.1.1.3").toBackendIPAddress("11.1.1.4").withHostName("www.contoso.com").withCookieBasedAffinity().attach().apply();
t2 = System.currentTimeMillis();
System.out.println("Application gateway updated: (took " + (t2 - t1) / 1000 + " seconds)");
Utils.print(applicationGateway);
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 (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