Search in sources :

Example 6 with TrafficManagerProfile

use of com.microsoft.azure.management.trafficmanager.TrafficManagerProfile in project azure-sdk-for-java by Azure.

the class CreateVirtualMachinesInParallel 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("rgCOPD", 24);
    final String userName = "tirekicker";
    final String sshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD azjava@javalib.com";
    Map<Region, Integer> virtualMachinesByLocation = new HashMap<Region, Integer>();
    // debug target
    /**
         virtualMachinesByLocation.put(Region.US_EAST, 5);
         virtualMachinesByLocation.put(Region.US_SOUTH_CENTRAL, 5);
         */
    // final demo target
    virtualMachinesByLocation.put(Region.US_EAST, 12);
    virtualMachinesByLocation.put(Region.US_SOUTH_CENTRAL, 12);
    virtualMachinesByLocation.put(Region.US_WEST, 12);
    virtualMachinesByLocation.put(Region.US_NORTH_CENTRAL, 12);
    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());
        List<String> publicIpCreatableKeys = new ArrayList<>();
        // Prepare a batch of Creatable definitions
        //
        List<Creatable<VirtualMachine>> creatableVirtualMachines = new ArrayList<>();
        for (Map.Entry<Region, Integer> entry : virtualMachinesByLocation.entrySet()) {
            Region region = entry.getKey();
            Integer vmCount = entry.getValue();
            //=============================================================
            // Create 1 network creatable per region
            // Prepare Creatable Network definition (Where all the virtual machines get added to)
            //
            String networkName = SdkContext.randomResourceName("vnetCOPD-", 20);
            Creatable<Network> networkCreatable = azure.networks().define(networkName).withRegion(region).withExistingResourceGroup(resourceGroup).withAddressSpace("172.16.0.0/16");
            //=============================================================
            // Create 1 storage creatable per region (For storing VMs disk)
            //
            String storageAccountName = SdkContext.randomResourceName("stgcopd", 20);
            Creatable<StorageAccount> storageAccountCreatable = azure.storageAccounts().define(storageAccountName).withRegion(region).withExistingResourceGroup(resourceGroup);
            String linuxVMNamePrefix = SdkContext.randomResourceName("vm-", 15);
            for (int i = 1; i <= vmCount; i++) {
                //=============================================================
                // Create 1 public IP address creatable
                //
                Creatable<PublicIPAddress> publicIPAddressCreatable = azure.publicIPAddresses().define(String.format("%s-%d", linuxVMNamePrefix, i)).withRegion(region).withExistingResourceGroup(resourceGroup).withLeafDomainLabel(SdkContext.randomResourceName("pip", 10));
                publicIpCreatableKeys.add(publicIPAddressCreatable.key());
                //=============================================================
                // Create 1 virtual machine creatable
                Creatable<VirtualMachine> virtualMachineCreatable = azure.virtualMachines().define(String.format("%s-%d", linuxVMNamePrefix, i)).withRegion(region).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 !!
        StopWatch stopwatch = new StopWatch();
        System.out.println("Creating the virtual machines");
        stopwatch.start();
        CreatedResources<VirtualMachine> virtualMachines = azure.virtualMachines().create(creatableVirtualMachines);
        stopwatch.stop();
        System.out.println("Created virtual machines");
        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");
        List<String> publicIpResourceIds = new ArrayList<>();
        for (String publicIpCreatableKey : publicIpCreatableKeys) {
            PublicIPAddress pip = (PublicIPAddress) virtualMachines.createdRelatedResource(publicIpCreatableKey);
            publicIpResourceIds.add(pip.id());
        }
        //=============================================================
        // Create 1 Traffic Manager Profile
        //
        String trafficManagerName = SdkContext.randomResourceName("tra", 15);
        TrafficManagerProfile.DefinitionStages.WithEndpoint profileWithEndpoint = azure.trafficManagerProfiles().define(trafficManagerName).withExistingResourceGroup(resourceGroup).withLeafDomainLabel(trafficManagerName).withPerformanceBasedRouting();
        int endpointPriority = 1;
        TrafficManagerProfile.DefinitionStages.WithCreate profileWithCreate = null;
        for (String publicIpResourceId : publicIpResourceIds) {
            String endpointName = String.format("azendpoint-%d", endpointPriority);
            if (endpointPriority == 1) {
                profileWithCreate = profileWithEndpoint.defineAzureTargetEndpoint(endpointName).toResourceId(publicIpResourceId).withRoutingPriority(endpointPriority).attach();
            } else {
                profileWithCreate = profileWithCreate.defineAzureTargetEndpoint(endpointName).toResourceId(publicIpResourceId).withRoutingPriority(endpointPriority).attach();
            }
            endpointPriority++;
        }
        System.out.println("Creating a traffic manager profile for the VMs");
        stopwatch.reset();
        stopwatch.start();
        TrafficManagerProfile trafficManagerProfile = profileWithCreate.create();
        stopwatch.stop();
        System.out.println("Created a traffic manager profile (took " + (stopwatch.getTime() / 1000) + " seconds to create): " + trafficManagerProfile.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 (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 : HashMap(java.util.HashMap) TrafficManagerProfile(com.microsoft.azure.management.trafficmanager.TrafficManagerProfile) ArrayList(java.util.ArrayList) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) Network(com.microsoft.azure.management.network.Network) Creatable(com.microsoft.azure.management.resources.fluentcore.model.Creatable) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) StopWatch(org.apache.commons.lang3.time.StopWatch) StorageAccount(com.microsoft.azure.management.storage.StorageAccount) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) HashMap(java.util.HashMap) Map(java.util.Map) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Aggregations

TrafficManagerProfile (com.microsoft.azure.management.trafficmanager.TrafficManagerProfile)6 Region (com.microsoft.azure.management.resources.fluentcore.arm.Region)4 AppServicePlan (com.microsoft.azure.management.appservice.AppServicePlan)3 WebApp (com.microsoft.azure.management.appservice.WebApp)3 ArrayList (java.util.ArrayList)3 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)2 PublicIPAddress (com.microsoft.azure.management.network.PublicIPAddress)2 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)2 Creatable (com.microsoft.azure.management.resources.fluentcore.model.Creatable)2 IOException (java.io.IOException)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 AppServiceDomain (com.microsoft.azure.management.appservice.AppServiceDomain)1 Network (com.microsoft.azure.management.network.Network)1 StorageAccount (com.microsoft.azure.management.storage.StorageAccount)1 TrafficManagerAzureEndpoint (com.microsoft.azure.management.trafficmanager.TrafficManagerAzureEndpoint)1 TrafficManagerExternalEndpoint (com.microsoft.azure.management.trafficmanager.TrafficManagerExternalEndpoint)1 TrafficManagerNestedProfileEndpoint (com.microsoft.azure.management.trafficmanager.TrafficManagerNestedProfileEndpoint)1 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1