|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | + * |
| 6 | + */ |
| 7 | + |
| 8 | +package com.microsoft.azure.management.appservice.samples; |
| 9 | + |
| 10 | +import com.microsoft.azure.management.Azure; |
| 11 | +import com.microsoft.azure.management.appservice.AppServicePlan; |
| 12 | +import com.microsoft.azure.management.appservice.AppServicePricingTier; |
| 13 | +import com.microsoft.azure.management.appservice.JavaVersion; |
| 14 | +import com.microsoft.azure.management.appservice.WebApp; |
| 15 | +import com.microsoft.azure.management.appservice.WebContainer; |
| 16 | +import com.microsoft.azure.management.resources.fluentcore.arm.Region; |
| 17 | +import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer; |
| 18 | +import com.microsoft.azure.management.samples.Utils; |
| 19 | +import okhttp3.logging.HttpLoggingInterceptor; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | + |
| 23 | +/** |
| 24 | + * Azure App Service basic sample for managing web apps. |
| 25 | + * - Create 3 web apps under the same new app service plan: |
| 26 | + * - 1, 2 are in the same resource group, 3 in a different one |
| 27 | + * - Stop and start 1, restart 2 |
| 28 | + * - Add Java support to app 3 |
| 29 | + * - List web apps |
| 30 | + * - Delete a web app |
| 31 | + */ |
| 32 | +public final class ManageWebAppBasic { |
| 33 | + |
| 34 | + /** |
| 35 | + * Main entry point. |
| 36 | + * @param args the parameters |
| 37 | + */ |
| 38 | + public static void main(String[] args) { |
| 39 | + // New resources |
| 40 | + final String app1Name = ResourceNamer.randomResourceName("webapp1-", 20); |
| 41 | + final String app2Name = ResourceNamer.randomResourceName("webapp2-", 20); |
| 42 | + final String app3Name = ResourceNamer.randomResourceName("webapp3-", 20); |
| 43 | + final String planName = ResourceNamer.randomResourceName("jplan_", 15); |
| 44 | + final String rg1Name = ResourceNamer.randomResourceName("rg1NEMV_", 24); |
| 45 | + final String rg2Name = ResourceNamer.randomResourceName("rg2NEMV_", 24); |
| 46 | + |
| 47 | + try { |
| 48 | + |
| 49 | + //============================================================= |
| 50 | + // Authenticate |
| 51 | + |
| 52 | + final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION")); |
| 53 | + |
| 54 | + Azure azure = Azure |
| 55 | + .configure() |
| 56 | + .withLogLevel(HttpLoggingInterceptor.Level.NONE) |
| 57 | + .authenticate(credFile) |
| 58 | + .withDefaultSubscription(); |
| 59 | + |
| 60 | + // Print selected subscription |
| 61 | + System.out.println("Selected subscription: " + azure.subscriptionId()); |
| 62 | + try { |
| 63 | + |
| 64 | + |
| 65 | + //============================================================ |
| 66 | + // Create a web app with a new app service plan |
| 67 | + |
| 68 | + System.out.println("Creating web app " + app1Name + " in resource group " + rg1Name + "..."); |
| 69 | + |
| 70 | + WebApp app1 = azure.webApps() |
| 71 | + .define(app1Name) |
| 72 | + .withNewResourceGroup(rg1Name) |
| 73 | + .withNewAppServicePlan(planName) |
| 74 | + .withRegion(Region.US_WEST) |
| 75 | + .withPricingTier(AppServicePricingTier.STANDARD_S1) |
| 76 | + .create(); |
| 77 | + |
| 78 | + System.out.println("Created web app " + app1.name()); |
| 79 | + Utils.print(app1); |
| 80 | + |
| 81 | + //============================================================ |
| 82 | + // Create a second web app with the same app service plan |
| 83 | + |
| 84 | + System.out.println("Creating another web app " + app2Name + " in resource group " + rg1Name + "..."); |
| 85 | + AppServicePlan plan = azure.appServices().appServicePlans().getByGroup(rg1Name, planName); |
| 86 | + WebApp app2 = azure.webApps() |
| 87 | + .define(app2Name) |
| 88 | + .withExistingResourceGroup(rg1Name) |
| 89 | + .withExistingAppServicePlan(plan) |
| 90 | + .create(); |
| 91 | + |
| 92 | + System.out.println("Created web app " + app2.name()); |
| 93 | + Utils.print(app2); |
| 94 | + |
| 95 | + //============================================================ |
| 96 | + // Create a third web app with the same app service plan, but |
| 97 | + // in a different resource group |
| 98 | + |
| 99 | + System.out.println("Creating another web app " + app3Name + " in resource group " + rg2Name + "..."); |
| 100 | + WebApp app3 = azure.webApps() |
| 101 | + .define(app3Name) |
| 102 | + .withNewResourceGroup(rg2Name) |
| 103 | + .withExistingAppServicePlan(plan) |
| 104 | + .create(); |
| 105 | + |
| 106 | + System.out.println("Created web app " + app3.name()); |
| 107 | + Utils.print(app3); |
| 108 | + |
| 109 | + //============================================================ |
| 110 | + // stop and start app1, restart app 2 |
| 111 | + System.out.println("Stopping web app " + app1.name()); |
| 112 | + app1.stop(); |
| 113 | + System.out.println("Stopped web app " + app1.name()); |
| 114 | + Utils.print(app1); |
| 115 | + System.out.println("Starting web app " + app1.name()); |
| 116 | + app1.start(); |
| 117 | + System.out.println("Started web app " + app1.name()); |
| 118 | + Utils.print(app1); |
| 119 | + System.out.println("Restarting web app " + app2.name()); |
| 120 | + app2.restart(); |
| 121 | + System.out.println("Restarted web app " + app2.name()); |
| 122 | + Utils.print(app2); |
| 123 | + |
| 124 | + //============================================================ |
| 125 | + // Configure app 3 to have Java 8 enabled |
| 126 | + System.out.println("Adding Java support to web app " + app3Name + "..."); |
| 127 | + app3.update() |
| 128 | + .withJavaVersion(JavaVersion.JAVA_8_NEWEST) |
| 129 | + .withWebContainer(WebContainer.TOMCAT_8_0_NEWEST) |
| 130 | + .apply(); |
| 131 | + System.out.println("Java supported on web app " + app3Name + "..."); |
| 132 | + |
| 133 | + //============================================================= |
| 134 | + // List web apps |
| 135 | + |
| 136 | + System.out.println("Printing list of web apps in resource group " + rg1Name + "..."); |
| 137 | + |
| 138 | + for (WebApp webApp : azure.webApps().listByGroup(rg1Name)) { |
| 139 | + Utils.print(webApp); |
| 140 | + } |
| 141 | + |
| 142 | + System.out.println("Printing list of web apps in resource group " + rg2Name + "..."); |
| 143 | + |
| 144 | + for (WebApp webApp : azure.webApps().listByGroup(rg2Name)) { |
| 145 | + Utils.print(webApp); |
| 146 | + } |
| 147 | + |
| 148 | + //============================================================= |
| 149 | + // Delete a web app |
| 150 | + |
| 151 | + System.out.println("Deleting web app " + app1Name + "..."); |
| 152 | + azure.webApps().deleteByGroup(rg1Name, app1Name); |
| 153 | + System.out.println("Deleted web app " + app1Name + "..."); |
| 154 | + |
| 155 | + System.out.println("Printing list of web apps in resource group " + rg1Name + " again..."); |
| 156 | + for (WebApp webApp : azure.webApps().listByGroup(rg1Name)) { |
| 157 | + Utils.print(webApp); |
| 158 | + } |
| 159 | + |
| 160 | + } catch (Exception e) { |
| 161 | + System.err.println(e.getMessage()); |
| 162 | + e.printStackTrace(); |
| 163 | + } finally { |
| 164 | + try { |
| 165 | + System.out.println("Deleting Resource Group: " + rg1Name); |
| 166 | + azure.resourceGroups().beginDeleteByName(rg1Name); |
| 167 | + System.out.println("Deleted Resource Group: " + rg1Name); |
| 168 | + System.out.println("Deleting Resource Group: " + rg2Name); |
| 169 | + azure.resourceGroups().beginDeleteByName(rg2Name); |
| 170 | + System.out.println("Deleted Resource Group: " + rg2Name); |
| 171 | + } catch (NullPointerException npe) { |
| 172 | + System.out.println("Did not create any resources in Azure. No clean up is necessary"); |
| 173 | + } catch (Exception g) { |
| 174 | + g.printStackTrace(); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + } catch (Exception e) { |
| 179 | + System.out.println(e.getMessage()); |
| 180 | + e.printStackTrace(); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments