|
| 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.resources.samples; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 11 | +import com.fasterxml.jackson.databind.JsonNode; |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 14 | +import com.microsoft.azure.management.Azure; |
| 15 | +import com.microsoft.azure.management.resources.Deployment; |
| 16 | +import com.microsoft.azure.management.resources.DeploymentMode; |
| 17 | +import com.microsoft.azure.management.resources.fluentcore.arm.Region; |
| 18 | +import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer; |
| 19 | +import okhttp3.logging.HttpLoggingInterceptor; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | + |
| 25 | +/** |
| 26 | + * Azure Resource sample for deploying resources using an ARM template and |
| 27 | + * showing progress. |
| 28 | + */ |
| 29 | + |
| 30 | +public final class DeployUsingARMTemplateWithProgress { |
| 31 | + |
| 32 | + /** |
| 33 | + * Main entry point. |
| 34 | + * @param args the parameters |
| 35 | + */ |
| 36 | + public static void main(String[] args) { |
| 37 | + try { |
| 38 | + final String rgName = ResourceNamer.randomResourceName("rgRSAP", 24); |
| 39 | + final String deploymentName = ResourceNamer.randomResourceName("dpRSAP", 24); |
| 40 | + |
| 41 | + try { |
| 42 | + |
| 43 | + |
| 44 | + //================================================================= |
| 45 | + // Authenticate |
| 46 | + |
| 47 | + final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION")); |
| 48 | + |
| 49 | + Azure azure = Azure.configure() |
| 50 | + .withLogLevel(HttpLoggingInterceptor.Level.BASIC) |
| 51 | + .authenticate(credFile) |
| 52 | + .withDefaultSubscription(); |
| 53 | + |
| 54 | + try { |
| 55 | + String templateJson = DeployUsingARMTemplateWithProgress.getTemplate(); |
| 56 | + |
| 57 | + //============================================================= |
| 58 | + // Create resource group. |
| 59 | + |
| 60 | + System.out.println("Creating a resource group with name: " + rgName); |
| 61 | + |
| 62 | + azure.resourceGroups().define(rgName) |
| 63 | + .withRegion(Region.US_WEST) |
| 64 | + .create(); |
| 65 | + |
| 66 | + System.out.println("Created a resource group with name: " + rgName); |
| 67 | + |
| 68 | + |
| 69 | + //============================================================= |
| 70 | + // Create a deployment for an Azure App Service via an ARM |
| 71 | + // template. |
| 72 | + |
| 73 | + System.out.println("Starting a deployment for an Azure App Service: " + deploymentName); |
| 74 | + |
| 75 | + azure.deployments().define(deploymentName) |
| 76 | + .withExistingResourceGroup(rgName) |
| 77 | + .withTemplate(templateJson) |
| 78 | + .withParameters("{}") |
| 79 | + .withMode(DeploymentMode.INCREMENTAL) |
| 80 | + .beginCreate(); |
| 81 | + |
| 82 | + System.out.println("Started a deployment for an Azure App Service: " + deploymentName); |
| 83 | + |
| 84 | + Deployment deployment = azure.deployments().getByGroup(rgName, deploymentName); |
| 85 | + System.out.println("Current deployment status : " + deployment.provisioningState()); |
| 86 | + |
| 87 | + while (!(deployment.provisioningState().equalsIgnoreCase("Succeeded") |
| 88 | + || deployment.provisioningState().equalsIgnoreCase("Failed") |
| 89 | + || deployment.provisioningState().equalsIgnoreCase("Cancelled"))) { |
| 90 | + Thread.sleep(10000); |
| 91 | + deployment = azure.deployments().getByGroup(rgName, deploymentName); |
| 92 | + System.out.println("Current deployment status : " + deployment.provisioningState()); |
| 93 | + } |
| 94 | + } catch (Exception f) { |
| 95 | + |
| 96 | + System.out.println(f.getMessage()); |
| 97 | + f.printStackTrace(); |
| 98 | + |
| 99 | + } finally { |
| 100 | + |
| 101 | + try { |
| 102 | + System.out.println("Deleting Resource Group: " + rgName); |
| 103 | + azure.resourceGroups().delete(rgName); |
| 104 | + System.out.println("Deleted Resource Group: " + rgName); |
| 105 | + } catch (NullPointerException npe) { |
| 106 | + System.out.println("Did not create any resources in Azure. No clean up is necessary"); |
| 107 | + } catch (Exception g) { |
| 108 | + g.printStackTrace(); |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + } catch (Exception e) { |
| 113 | + System.out.println(e.getMessage()); |
| 114 | + e.printStackTrace(); |
| 115 | + } |
| 116 | + } catch (Exception e) { |
| 117 | + System.err.println(e.getMessage()); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private static String getTemplate() throws IllegalAccessException, JsonProcessingException, IOException { |
| 122 | + final String hostingPlanName = ResourceNamer.randomResourceName("hpRSAT", 24); |
| 123 | + final String webappName = ResourceNamer.randomResourceName("wnRSAT", 24); |
| 124 | + final InputStream embeddedTemplate; |
| 125 | + embeddedTemplate = DeployUsingARMTemplate.class.getResourceAsStream("/templateValue.json"); |
| 126 | + |
| 127 | + final ObjectMapper mapper = new ObjectMapper(); |
| 128 | + final JsonNode tmp = mapper.readTree(embeddedTemplate); |
| 129 | + |
| 130 | + DeployUsingARMTemplateWithProgress.validateAndAddFieldValue("string", hostingPlanName, "hostingPlanName", null, tmp); |
| 131 | + DeployUsingARMTemplateWithProgress.validateAndAddFieldValue("string", webappName, "webSiteName", null, tmp); |
| 132 | + DeployUsingARMTemplateWithProgress.validateAndAddFieldValue("string", "F1", "skuName", null, tmp); |
| 133 | + DeployUsingARMTemplateWithProgress.validateAndAddFieldValue("int", "1", "skuCapacity", null, tmp); |
| 134 | + |
| 135 | + return tmp.toString(); |
| 136 | + } |
| 137 | + |
| 138 | + private static void validateAndAddFieldValue(String type, String fieldValue, String fieldName, String errorMessage, |
| 139 | + JsonNode tmp) throws IllegalAccessException { |
| 140 | + // Add count variable for loop.... |
| 141 | + final ObjectMapper mapper = new ObjectMapper(); |
| 142 | + final ObjectNode parameter = mapper.createObjectNode(); |
| 143 | + parameter.put("type", type); |
| 144 | + if (type == "int") { |
| 145 | + parameter.put("defaultValue", Integer.parseInt(fieldValue)); |
| 146 | + } else { |
| 147 | + parameter.put("defaultValue", fieldValue); |
| 148 | + } |
| 149 | + ObjectNode.class.cast(tmp.get("parameters")).replace(fieldName, parameter); |
| 150 | + } |
| 151 | + |
| 152 | + private DeployUsingARMTemplateWithProgress() { |
| 153 | + |
| 154 | + } |
| 155 | +} |
0 commit comments