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