Search in sources :

Example 1 with TrafficManagerProfile

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

the class ManageTrafficManager 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("rgNEMV_", 24);
    final String domainName = SdkContext.randomResourceName("jsdkdemo-", 20) + ".com";
    final String certPassword = "StrongPass!12";
    final String appServicePlanNamePrefix = SdkContext.randomResourceName("jplan1_", 15);
    final String webAppNamePrefix = SdkContext.randomResourceName("webapp1-", 20);
    final String tmName = SdkContext.randomResourceName("jsdktm-", 20);
    final List<Region> regions = new ArrayList<>();
    // The regions in which web app needs to be created
    //
    regions.add(Region.US_WEST2);
    regions.add(Region.US_EAST2);
    regions.add(Region.ASIA_EAST);
    regions.add(Region.INDIA_WEST);
    regions.add(Region.US_CENTRAL);
    try {
        azure.resourceGroups().define(rgName).withRegion(Region.US_WEST).create();
        //============================================================
        // Purchase a domain (will be canceled for a full refund)
        System.out.println("Purchasing a domain " + domainName + "...");
        AppServiceDomain domain = azure.appServices().domains().define(domainName).withExistingResourceGroup(rgName).defineRegistrantContact().withFirstName("Jon").withLastName("Doe").withEmail("jondoe@contoso.com").withAddressLine1("123 4th Ave").withCity("Redmond").withStateOrProvince("WA").withCountry(CountryIsoCode.UNITED_STATES).withPostalCode("98052").withPhoneCountryCode(CountryPhoneCode.UNITED_STATES).withPhoneNumber("4258828080").attach().withDomainPrivacyEnabled(true).withAutoRenewEnabled(false).create();
        System.out.println("Purchased domain " + domain.name());
        Utils.print(domain);
        //============================================================
        // Create a self-singed SSL certificate
        String pfxPath = ManageTrafficManager.class.getResource("/").getPath() + webAppNamePrefix + "." + domainName + ".pfx";
        String cerPath = ManageTrafficManager.class.getResource("/").getPath() + webAppNamePrefix + "." + domainName + ".cer";
        System.out.println("Creating a self-signed certificate " + pfxPath + "...");
        Utils.createCertificate(cerPath, pfxPath, domainName, certPassword, "*." + domainName);
        //============================================================
        // Creates app service in 5 different region
        List<AppServicePlan> appServicePlans = new ArrayList<>();
        int id = 0;
        for (Region region : regions) {
            String planName = appServicePlanNamePrefix + id;
            System.out.println("Creating an app service plan " + planName + " in region " + region + "...");
            AppServicePlan appServicePlan = azure.appServices().appServicePlans().define(planName).withRegion(region).withExistingResourceGroup(rgName).withPricingTier(PricingTier.BASIC_B1).withOperatingSystem(OperatingSystem.WINDOWS).create();
            System.out.println("Created app service plan " + planName);
            Utils.print(appServicePlan);
            appServicePlans.add(appServicePlan);
            id++;
        }
        //============================================================
        // Creates websites using previously created plan
        List<WebApp> webApps = new ArrayList<>();
        id = 0;
        for (AppServicePlan appServicePlan : appServicePlans) {
            String webAppName = webAppNamePrefix + id;
            System.out.println("Creating a web app " + webAppName + " using the plan " + appServicePlan.name() + "...");
            WebApp webApp = azure.webApps().define(webAppName).withExistingWindowsPlan(appServicePlan).withExistingResourceGroup(rgName).withManagedHostnameBindings(domain, webAppName).defineSslBinding().forHostname(webAppName + "." + domain.name()).withPfxCertificateToUpload(new File(pfxPath), certPassword).withSniBasedSsl().attach().defineSourceControl().withPublicGitRepository("https://github.com/jianghaolu/azure-site-test").withBranch("master").attach().create();
            System.out.println("Created web app " + webAppName);
            Utils.print(webApp);
            webApps.add(webApp);
            id++;
        }
        //============================================================
        // Creates a traffic manager profile
        System.out.println("Creating a traffic manager profile " + tmName + " for the web apps...");
        TrafficManagerProfile.DefinitionStages.WithEndpoint tmDefinition = azure.trafficManagerProfiles().define(tmName).withExistingResourceGroup(rgName).withLeafDomainLabel(tmName).withPriorityBasedRouting();
        Creatable<TrafficManagerProfile> tmCreatable = null;
        int priority = 1;
        for (WebApp webApp : webApps) {
            tmCreatable = tmDefinition.defineAzureTargetEndpoint("endpoint-" + priority).toResourceId(webApp.id()).withRoutingPriority(priority).attach();
            priority++;
        }
        TrafficManagerProfile trafficManagerProfile = tmCreatable.create();
        System.out.println("Created traffic manager " + trafficManagerProfile.name());
        Utils.print(trafficManagerProfile);
        //============================================================
        // Disables one endpoint and removes another endpoint
        System.out.println("Disabling and removing endpoint...");
        trafficManagerProfile = trafficManagerProfile.update().updateAzureTargetEndpoint("endpoint-1").withTrafficDisabled().parent().withoutEndpoint("endpoint-2").apply();
        System.out.println("Endpoints updated");
        //============================================================
        // Enables an endpoint
        System.out.println("Enabling endpoint...");
        trafficManagerProfile = trafficManagerProfile.update().updateAzureTargetEndpoint("endpoint-1").withTrafficEnabled().parent().apply();
        System.out.println("Endpoint updated");
        Utils.print(trafficManagerProfile);
        //============================================================
        // Change/configure traffic manager routing method
        System.out.println("Changing traffic manager profile routing method...");
        trafficManagerProfile = trafficManagerProfile.update().withPerformanceBasedRouting().apply();
        System.out.println("Changed traffic manager profile routing method");
        //============================================================
        // Disables the traffic manager profile
        System.out.println("Disabling traffic manager profile...");
        trafficManagerProfile.update().withProfileStatusDisabled().apply();
        System.out.println("Traffic manager profile disabled");
        //============================================================
        // Enables the traffic manager profile
        System.out.println("Enabling traffic manager profile...");
        trafficManagerProfile.update().withProfileStatusDisabled().apply();
        System.out.println("Traffic manager profile enabled");
        //============================================================
        // Deletes the traffic manager profile
        System.out.println("Deleting the traffic manger profile...");
        azure.trafficManagerProfiles().deleteById(trafficManagerProfile.id());
        System.out.println("Traffic manager profile deleted");
        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 : TrafficManagerProfile(com.microsoft.azure.management.trafficmanager.TrafficManagerProfile) ArrayList(java.util.ArrayList) AppServicePlan(com.microsoft.azure.management.appservice.AppServicePlan) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) File(java.io.File) AppServiceDomain(com.microsoft.azure.management.appservice.AppServiceDomain) WebApp(com.microsoft.azure.management.appservice.WebApp)

Example 2 with TrafficManagerProfile

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

the class TestTrafficManager method createResource.

@Override
public TrafficManagerProfile createResource(TrafficManagerProfiles profiles) throws Exception {
    final Region region = Region.US_EAST;
    final String groupName = "rg" + this.testId;
    final String pipName = "pip" + this.testId;
    final String pipDnsLabel = SdkContext.randomResourceName("contoso", 15);
    final String tmProfileName = "tm" + this.testId;
    final String nestedTmProfileName = "nested" + tmProfileName;
    final String tmProfileDnsLabel = SdkContext.randomResourceName("tmdns", 15);
    final String nestedTmProfileDnsLabel = "nested" + tmProfileDnsLabel;
    ResourceGroup.DefinitionStages.WithCreate rgCreatable = profiles.manager().resourceManager().resourceGroups().define(groupName).withRegion(region);
    // Creates a TM profile that will be used as a nested profile endpoint in parent TM profile
    //
    TrafficManagerProfile nestedProfile = profiles.define(nestedTmProfileName).withNewResourceGroup(rgCreatable).withLeafDomainLabel(nestedTmProfileDnsLabel).withPriorityBasedRouting().defineExternalTargetEndpoint("external-ep-1").toFqdn("www.gitbook.com").fromRegion(Region.INDIA_CENTRAL).attach().withHttpsMonitoring().withTimeToLive(500).create();
    Assert.assertTrue(nestedProfile.isEnabled());
    Assert.assertNotNull(nestedProfile.monitorStatus());
    Assert.assertEquals(nestedProfile.monitoringPort(), 443);
    Assert.assertEquals(nestedProfile.monitoringPath(), "/");
    Assert.assertEquals(nestedProfile.azureEndpoints().size(), 0);
    Assert.assertEquals(nestedProfile.nestedProfileEndpoints().size(), 0);
    Assert.assertEquals(nestedProfile.externalEndpoints().size(), 1);
    Assert.assertEquals(nestedProfile.fqdn(), nestedTmProfileDnsLabel + ".trafficmanager.net");
    Assert.assertEquals(nestedProfile.timeToLive(), 500);
    // Creates a public ip to be used as an Azure endpoint
    //
    PublicIPAddress publicIPAddress = this.publicIPAddresses.define(pipName).withRegion(region).withNewResourceGroup(rgCreatable).withLeafDomainLabel(pipDnsLabel).create();
    Assert.assertNotNull(publicIPAddress.fqdn());
    // Creates a TM profile
    //
    TrafficManagerProfile profile = profiles.define(tmProfileName).withNewResourceGroup(rgCreatable).withLeafDomainLabel(tmProfileDnsLabel).withWeightBasedRouting().defineExternalTargetEndpoint(externalEndpointName21).toFqdn(externalFqdn21).fromRegion(Region.US_EAST).withRoutingPriority(1).withRoutingWeight(1).attach().defineExternalTargetEndpoint(externalEndpointName22).toFqdn(externalFqdn22).fromRegion(Region.US_EAST2).withRoutingPriority(2).withRoutingWeight(1).withTrafficDisabled().attach().defineAzureTargetEndpoint(azureEndpointName).toResourceId(publicIPAddress.id()).withRoutingPriority(3).attach().defineNestedTargetEndpoint(nestedProfileEndpointName).toProfile(nestedProfile).fromRegion(Region.INDIA_CENTRAL).withMinimumEndpointsToEnableTraffic(1).withRoutingPriority(4).attach().withHttpMonitoring().create();
    Assert.assertTrue(profile.isEnabled());
    Assert.assertNotNull(profile.monitorStatus());
    Assert.assertEquals(profile.monitoringPort(), 80);
    Assert.assertEquals(profile.monitoringPath(), "/");
    Assert.assertEquals(profile.azureEndpoints().size(), 1);
    Assert.assertEquals(profile.nestedProfileEndpoints().size(), 1);
    Assert.assertEquals(profile.externalEndpoints().size(), 2);
    Assert.assertEquals(profile.fqdn(), tmProfileDnsLabel + ".trafficmanager.net");
    // Default
    Assert.assertEquals(profile.timeToLive(), 300);
    profile = profile.refresh();
    Assert.assertEquals(profile.azureEndpoints().size(), 1);
    Assert.assertEquals(profile.nestedProfileEndpoints().size(), 1);
    Assert.assertEquals(profile.externalEndpoints().size(), 2);
    int c = 0;
    for (TrafficManagerExternalEndpoint endpoint : profile.externalEndpoints().values()) {
        Assert.assertEquals(endpoint.endpointType(), EndpointType.EXTERNAL);
        if (endpoint.name().equalsIgnoreCase(externalEndpointName21)) {
            Assert.assertEquals(endpoint.routingPriority(), 1);
            Assert.assertEquals(endpoint.fqdn(), externalFqdn21);
            Assert.assertNotNull(endpoint.monitorStatus());
            Assert.assertEquals(endpoint.sourceTrafficLocation(), Region.US_EAST);
            c++;
        } else if (endpoint.name().equalsIgnoreCase(externalEndpointName22)) {
            Assert.assertEquals(endpoint.routingPriority(), 2);
            Assert.assertEquals(endpoint.fqdn(), externalFqdn22);
            Assert.assertNotNull(endpoint.monitorStatus());
            Assert.assertEquals(endpoint.sourceTrafficLocation(), Region.US_EAST2);
            c++;
        }
    }
    Assert.assertEquals(c, 2);
    c = 0;
    for (TrafficManagerAzureEndpoint endpoint : profile.azureEndpoints().values()) {
        Assert.assertEquals(endpoint.endpointType(), EndpointType.AZURE);
        if (endpoint.name().equalsIgnoreCase(azureEndpointName)) {
            Assert.assertEquals(endpoint.routingPriority(), 3);
            Assert.assertNotNull(endpoint.monitorStatus());
            Assert.assertEquals(endpoint.targetAzureResourceId(), publicIPAddress.id());
            Assert.assertEquals(endpoint.targetResourceType(), TargetAzureResourceType.PUBLICIP);
            c++;
        }
    }
    Assert.assertEquals(c, 1);
    c = 0;
    for (TrafficManagerNestedProfileEndpoint endpoint : profile.nestedProfileEndpoints().values()) {
        Assert.assertEquals(endpoint.endpointType(), EndpointType.NESTED_PROFILE);
        if (endpoint.name().equalsIgnoreCase(nestedProfileEndpointName)) {
            Assert.assertEquals(endpoint.routingPriority(), 4);
            Assert.assertNotNull(endpoint.monitorStatus());
            Assert.assertEquals(endpoint.minimumChildEndpointCount(), 1);
            Assert.assertEquals(endpoint.nestedProfileId(), nestedProfile.id());
            Assert.assertEquals(endpoint.sourceTrafficLocation(), Region.INDIA_CENTRAL);
            c++;
        }
    }
    Assert.assertEquals(c, 1);
    return profile;
}
Also used : TrafficManagerExternalEndpoint(com.microsoft.azure.management.trafficmanager.TrafficManagerExternalEndpoint) TrafficManagerNestedProfileEndpoint(com.microsoft.azure.management.trafficmanager.TrafficManagerNestedProfileEndpoint) TrafficManagerAzureEndpoint(com.microsoft.azure.management.trafficmanager.TrafficManagerAzureEndpoint) TrafficManagerProfile(com.microsoft.azure.management.trafficmanager.TrafficManagerProfile) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) PublicIPAddress(com.microsoft.azure.management.network.PublicIPAddress) TrafficManagerExternalEndpoint(com.microsoft.azure.management.trafficmanager.TrafficManagerExternalEndpoint) TrafficManagerAzureEndpoint(com.microsoft.azure.management.trafficmanager.TrafficManagerAzureEndpoint) TrafficManagerNestedProfileEndpoint(com.microsoft.azure.management.trafficmanager.TrafficManagerNestedProfileEndpoint)

Example 3 with TrafficManagerProfile

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

the class ManageWebAppWithTrafficManager 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) {
    if (ManageWebAppWithTrafficManager.azure == null) {
        ManageWebAppWithTrafficManager.azure = azure;
    }
    // New resources
    final String app1Name = SdkContext.randomResourceName("webapp1-", 20);
    final String app2Name = SdkContext.randomResourceName("webapp2-", 20);
    final String app3Name = SdkContext.randomResourceName("webapp3-", 20);
    final String app4Name = SdkContext.randomResourceName("webapp4-", 20);
    final String app5Name = SdkContext.randomResourceName("webapp5-", 20);
    final String plan1Name = SdkContext.randomResourceName("jplan1_", 15);
    final String plan2Name = SdkContext.randomResourceName("jplan2_", 15);
    final String plan3Name = SdkContext.randomResourceName("jplan3_", 15);
    final String domainName = SdkContext.randomResourceName("jsdkdemo-", 20) + ".com";
    final String tmName = SdkContext.randomResourceName("jsdktm-", 20);
    try {
        //============================================================
        // Purchase a domain (will be canceled for a full refund)
        System.out.println("Purchasing a domain " + domainName + "...");
        azure.resourceGroups().define(RG_NAME).withRegion(Region.US_WEST).create();
        domain = azure.appServices().domains().define(domainName).withExistingResourceGroup(RG_NAME).defineRegistrantContact().withFirstName("Jon").withLastName("Doe").withEmail("jondoe@contoso.com").withAddressLine1("123 4th Ave").withCity("Redmond").withStateOrProvince("WA").withCountry(CountryIsoCode.UNITED_STATES).withPostalCode("98052").withPhoneCountryCode(CountryPhoneCode.UNITED_STATES).withPhoneNumber("4258828080").attach().withDomainPrivacyEnabled(true).withAutoRenewEnabled(false).create();
        System.out.println("Purchased domain " + domain.name());
        Utils.print(domain);
        //============================================================
        // Create a self-singed SSL certificate
        pfxPath = ManageWebAppWithTrafficManager.class.getResource("/").getPath() + app2Name + "." + domainName + ".pfx";
        String cerPath = ManageWebAppWithTrafficManager.class.getResource("/").getPath() + app2Name + "." + domainName + ".cer";
        System.out.println("Creating a self-signed certificate " + pfxPath + "...");
        Utils.createCertificate(cerPath, pfxPath, domainName, CERT_PASSWORD, "*." + domainName);
        //============================================================
        // Create 3 app service plans in 3 regions
        System.out.println("Creating app service plan " + plan1Name + " in US West...");
        AppServicePlan plan1 = createAppServicePlan(plan1Name, Region.US_WEST);
        System.out.println("Created app service plan " + plan1.name());
        Utils.print(plan1);
        System.out.println("Creating app service plan " + plan2Name + " in Europe West...");
        AppServicePlan plan2 = createAppServicePlan(plan2Name, Region.EUROPE_WEST);
        System.out.println("Created app service plan " + plan2.name());
        Utils.print(plan1);
        System.out.println("Creating app service plan " + plan3Name + " in Asia East...");
        AppServicePlan plan3 = createAppServicePlan(plan3Name, Region.ASIA_EAST);
        System.out.println("Created app service plan " + plan2.name());
        Utils.print(plan1);
        //============================================================
        // Create 5 web apps under these 3 app service plans
        System.out.println("Creating web app " + app1Name + "...");
        WebApp app1 = createWebApp(app1Name, plan1);
        System.out.println("Created web app " + app1.name());
        Utils.print(app1);
        System.out.println("Creating another web app " + app2Name + "...");
        WebApp app2 = createWebApp(app2Name, plan2);
        System.out.println("Created web app " + app2.name());
        Utils.print(app2);
        System.out.println("Creating another web app " + app3Name + "...");
        WebApp app3 = createWebApp(app3Name, plan3);
        System.out.println("Created web app " + app3.name());
        Utils.print(app3);
        System.out.println("Creating another web app " + app3Name + "...");
        WebApp app4 = createWebApp(app4Name, plan1);
        System.out.println("Created web app " + app4.name());
        Utils.print(app4);
        System.out.println("Creating another web app " + app3Name + "...");
        WebApp app5 = createWebApp(app5Name, plan1);
        System.out.println("Created web app " + app5.name());
        Utils.print(app5);
        //============================================================
        // Create a traffic manager
        System.out.println("Creating a traffic manager " + tmName + " for the web apps...");
        TrafficManagerProfile trafficManager = azure.trafficManagerProfiles().define(tmName).withExistingResourceGroup(RG_NAME).withLeafDomainLabel(tmName).withTrafficRoutingMethod(TrafficRoutingMethod.PRIORITY).defineAzureTargetEndpoint("endpoint1").toResourceId(app1.id()).withRoutingPriority(1).attach().defineAzureTargetEndpoint("endpoint2").toResourceId(app2.id()).withRoutingPriority(2).attach().defineAzureTargetEndpoint("endpoint3").toResourceId(app3.id()).withRoutingPriority(3).attach().create();
        System.out.println("Created traffic manager " + trafficManager.name());
        Utils.print(trafficManager);
        //============================================================
        // Scale up the app service plans
        System.out.println("Scaling up app service plan " + plan1Name + "...");
        plan1.update().withCapacity(plan1.capacity() * 2).apply();
        System.out.println("Scaled up app service plan " + plan1Name);
        Utils.print(plan1);
        System.out.println("Scaling up app service plan " + plan2Name + "...");
        plan2.update().withCapacity(plan2.capacity() * 2).apply();
        System.out.println("Scaled up app service plan " + plan2Name);
        Utils.print(plan2);
        System.out.println("Scaling up app service plan " + plan3Name + "...");
        plan3.update().withCapacity(plan3.capacity() * 2).apply();
        System.out.println("Scaled up app service plan " + plan3Name);
        Utils.print(plan3);
        return true;
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + RG_NAME);
            azure.resourceGroups().beginDeleteByName(RG_NAME);
            System.out.println("Deleted Resource Group: " + RG_NAME);
        } 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 : TrafficManagerProfile(com.microsoft.azure.management.trafficmanager.TrafficManagerProfile) IOException(java.io.IOException) AppServicePlan(com.microsoft.azure.management.appservice.AppServicePlan) WebApp(com.microsoft.azure.management.appservice.WebApp)

Example 4 with TrafficManagerProfile

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

the class ManageLinuxWebAppWithTrafficManager 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) {
    if (ManageLinuxWebAppWithTrafficManager.azure == null) {
        ManageLinuxWebAppWithTrafficManager.azure = azure;
    }
    // New resources
    final String app1Name = SdkContext.randomResourceName("webapp1-", 20);
    final String app2Name = SdkContext.randomResourceName("webapp2-", 20);
    final String app3Name = SdkContext.randomResourceName("webapp3-", 20);
    final String app4Name = SdkContext.randomResourceName("webapp4-", 20);
    final String app5Name = SdkContext.randomResourceName("webapp5-", 20);
    final String plan1Name = SdkContext.randomResourceName("jplan1_", 15);
    final String plan2Name = SdkContext.randomResourceName("jplan2_", 15);
    final String plan3Name = SdkContext.randomResourceName("jplan3_", 15);
    final String domainName = SdkContext.randomResourceName("jsdkdemo-", 20) + ".com";
    final String tmName = SdkContext.randomResourceName("jsdktm-", 20);
    try {
        //============================================================
        // Purchase a domain (will be canceled for a full refund)
        System.out.println("Purchasing a domain " + domainName + "...");
        azure.resourceGroups().define(RG_NAME).withRegion(Region.US_WEST).create();
        domain = azure.appServices().domains().define(domainName).withExistingResourceGroup(RG_NAME).defineRegistrantContact().withFirstName("Jon").withLastName("Doe").withEmail("jondoe@contoso.com").withAddressLine1("123 4th Ave").withCity("Redmond").withStateOrProvince("WA").withCountry(CountryIsoCode.UNITED_STATES).withPostalCode("98052").withPhoneCountryCode(CountryPhoneCode.UNITED_STATES).withPhoneNumber("4258828080").attach().withDomainPrivacyEnabled(true).withAutoRenewEnabled(false).create();
        System.out.println("Purchased domain " + domain.name());
        Utils.print(domain);
        //============================================================
        // Create a self-singed SSL certificate
        pfxPath = ManageLinuxWebAppWithTrafficManager.class.getResource("/").getPath() + app2Name + "." + domainName + ".pfx";
        String cerPath = ManageLinuxWebAppWithTrafficManager.class.getResource("/").getPath() + app2Name + "." + domainName + ".cer";
        System.out.println("Creating a self-signed certificate " + pfxPath + "...");
        Utils.createCertificate(cerPath, pfxPath, domainName, CERT_PASSWORD, "*." + domainName);
        //============================================================
        // Create 3 app service plans in 3 regions
        System.out.println("Creating app service plan " + plan1Name + " in US West...");
        AppServicePlan plan1 = createAppServicePlan(plan1Name, Region.US_WEST);
        System.out.println("Created app service plan " + plan1.name());
        Utils.print(plan1);
        System.out.println("Creating app service plan " + plan2Name + " in Europe West...");
        AppServicePlan plan2 = createAppServicePlan(plan2Name, Region.EUROPE_WEST);
        System.out.println("Created app service plan " + plan2.name());
        Utils.print(plan1);
        System.out.println("Creating app service plan " + plan3Name + " in Asia East...");
        AppServicePlan plan3 = createAppServicePlan(plan3Name, Region.ASIA_SOUTHEAST);
        System.out.println("Created app service plan " + plan2.name());
        Utils.print(plan1);
        //============================================================
        // Create 5 web apps under these 3 app service plans
        System.out.println("Creating web app " + app1Name + "...");
        WebApp app1 = createWebApp(app1Name, plan1);
        System.out.println("Created web app " + app1.name());
        Utils.print(app1);
        System.out.println("Creating another web app " + app2Name + "...");
        WebApp app2 = createWebApp(app2Name, plan2);
        System.out.println("Created web app " + app2.name());
        Utils.print(app2);
        System.out.println("Creating another web app " + app3Name + "...");
        WebApp app3 = createWebApp(app3Name, plan3);
        System.out.println("Created web app " + app3.name());
        Utils.print(app3);
        System.out.println("Creating another web app " + app3Name + "...");
        WebApp app4 = createWebApp(app4Name, plan1);
        System.out.println("Created web app " + app4.name());
        Utils.print(app4);
        System.out.println("Creating another web app " + app3Name + "...");
        WebApp app5 = createWebApp(app5Name, plan1);
        System.out.println("Created web app " + app5.name());
        Utils.print(app5);
        //============================================================
        // Create a traffic manager
        System.out.println("Creating a traffic manager " + tmName + " for the web apps...");
        TrafficManagerProfile trafficManager = azure.trafficManagerProfiles().define(tmName).withExistingResourceGroup(RG_NAME).withLeafDomainLabel(tmName).withTrafficRoutingMethod(TrafficRoutingMethod.PRIORITY).defineAzureTargetEndpoint("endpoint1").toResourceId(app1.id()).withRoutingPriority(1).attach().defineAzureTargetEndpoint("endpoint2").toResourceId(app2.id()).withRoutingPriority(2).attach().defineAzureTargetEndpoint("endpoint3").toResourceId(app3.id()).withRoutingPriority(3).attach().create();
        System.out.println("Created traffic manager " + trafficManager.name());
        Utils.print(trafficManager);
        //============================================================
        // Scale up the app service plans
        System.out.println("Scaling up app service plan " + plan1Name + "...");
        plan1.update().withCapacity(plan1.capacity() * 2).apply();
        System.out.println("Scaled up app service plan " + plan1Name);
        Utils.print(plan1);
        System.out.println("Scaling up app service plan " + plan2Name + "...");
        plan2.update().withCapacity(plan2.capacity() * 2).apply();
        System.out.println("Scaled up app service plan " + plan2Name);
        Utils.print(plan2);
        System.out.println("Scaling up app service plan " + plan3Name + "...");
        plan3.update().withCapacity(plan3.capacity() * 2).apply();
        System.out.println("Scaled up app service plan " + plan3Name);
        Utils.print(plan3);
        return true;
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + RG_NAME);
            azure.resourceGroups().beginDeleteByName(RG_NAME);
            System.out.println("Deleted Resource Group: " + RG_NAME);
        } 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 : TrafficManagerProfile(com.microsoft.azure.management.trafficmanager.TrafficManagerProfile) IOException(java.io.IOException) AppServicePlan(com.microsoft.azure.management.appservice.AppServicePlan) WebApp(com.microsoft.azure.management.appservice.WebApp)

Example 5 with TrafficManagerProfile

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

the class ManageSimpleTrafficManager 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";
    final int vmCountPerRegion = 2;
    Set<Region> regions = new HashSet<>(Arrays.asList(Region.US_EAST, Region.US_WEST));
    try {
        //=============================================================
        // Create a shared resource group for all the resources so they can all be deleted together
        //
        ResourceGroup resourceGroup = azure.resourceGroups().define(rgName).withRegion(Region.US_EAST).create();
        System.out.println("Created a new resource group - " + resourceGroup.id());
        // Prepare a batch of creatable VM definitions to put behind the traffic manager
        //
        List<Creatable<VirtualMachine>> creatableVirtualMachines = new ArrayList<>();
        for (Region region : regions) {
            String linuxVMNamePrefix = SdkContext.randomResourceName("vm", 15);
            for (int i = 0; i < vmCountPerRegion; i++) {
                //=============================================================
                // Create a virtual machine in its own virtual network
                String vmName = String.format("%s-%d", linuxVMNamePrefix, i);
                Creatable<VirtualMachine> vmDefinition = azure.virtualMachines().define(vmName).withRegion(region).withExistingResourceGroup(resourceGroup).withNewPrimaryNetwork("10.0.0.0/29").withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(vmName).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshKey).withSize(VirtualMachineSizeTypes.STANDARD_A1);
                creatableVirtualMachines.add(vmDefinition);
            }
        }
        //=============================================================
        // Create the VMs !!
        StopWatch stopwatch = new StopWatch();
        System.out.println("Creating the virtual machines...");
        stopwatch.start();
        Collection<VirtualMachine> virtualMachines = azure.virtualMachines().create(creatableVirtualMachines).values();
        stopwatch.stop();
        System.out.println(String.format("Created virtual machines in %d seconds.", stopwatch.getTime() / 1000));
        //=============================================================
        // Create 1 traffic manager profile
        //
        String trafficManagerName = SdkContext.randomResourceName("tra", 15);
        TrafficManagerProfile.DefinitionStages.WithEndpoint profileWithEndpoint = azure.trafficManagerProfiles().define(trafficManagerName).withExistingResourceGroup(resourceGroup).withLeafDomainLabel(trafficManagerName).withPerformanceBasedRouting();
        TrafficManagerProfile.DefinitionStages.WithCreate profileWithCreate = null;
        int routingPriority = 1;
        for (VirtualMachine vm : virtualMachines) {
            String endpointName = SdkContext.randomResourceName("ep", 15);
            profileWithCreate = profileWithEndpoint.defineAzureTargetEndpoint(endpointName).toResourceId(vm.getPrimaryPublicIPAddressId()).withRoutingPriority(routingPriority++).attach();
        }
        stopwatch.reset();
        stopwatch.start();
        TrafficManagerProfile trafficManagerProfile = profileWithCreate.create();
        stopwatch.stop();
        System.out.println(String.format("Created a traffic manager profile %s\n in %d seconds.", trafficManagerProfile.id(), stopwatch.getTime() / 1000));
        //=============================================================
        // Modify the traffic manager to use priority based routing
        //
        trafficManagerProfile.update().withPriorityBasedRouting().apply();
        System.out.println("Modified the traffic manager to use priority-based routing.");
        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 : TrafficManagerProfile(com.microsoft.azure.management.trafficmanager.TrafficManagerProfile) ArrayList(java.util.ArrayList) StopWatch(org.apache.commons.lang3.time.StopWatch) Region(com.microsoft.azure.management.resources.fluentcore.arm.Region) Creatable(com.microsoft.azure.management.resources.fluentcore.model.Creatable) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) HashSet(java.util.HashSet) 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