Last active
March 2, 2018 16:21
-
-
Save chgeuer/da025a19dc4af663e65eb5af50d2dd40 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.microsoft.azure.management.resources.samples; | |
import com.microsoft.azure.management.Azure; | |
import com.microsoft.azure.management.resources.DeploymentMode; | |
import com.microsoft.azure.management.resources.fluentcore.arm.Region; | |
import com.microsoft.rest.LogLevel; | |
import java.io.File; | |
import java.net.*; | |
import java.io.*; | |
import java.util.LinkedHashMap; | |
public final class DeployUsingARMTemplate { | |
public static void main(String[] args) { | |
try { | |
// See https://github.com/Azure/azure-sdk-for-java/blob/master/archive/AUTH.md on how that credFile is structured | |
String f = "C:\\Users\\chgeuer\\.azure\\java-sdk-auth.json"; | |
final String templateUrl = "https://raw.githubusercontent.com/chgeuer/saphec_azure_arm_training/1fae2d814abb522bf73221d7b2d171c3e94b55b8/06-nested-variables.json"; | |
final String region = "westeurope"; | |
final String rgName = "foo2"; | |
final String deploymentName = rgName; | |
final File credFile = new File(f); | |
Azure azure = Azure.configure() | |
.withLogLevel(LogLevel.NONE) | |
.authenticate(credFile) | |
.withDefaultSubscription(); | |
System.out.println("Creating a resource group with name: " + rgName + " in subscription " + azure.subscriptionId()); | |
azure.resourceGroups().define(rgName).withRegion(Region.findByLabelOrName(region)).create(); | |
System.out.println("Created a resource group with name: " + rgName); | |
String templateJson = getJSON(templateUrl); | |
com.microsoft.azure.management.resources.Deployment deployment = azure.deployments().define(deploymentName) | |
.withExistingResourceGroup(rgName) | |
.withTemplate(templateJson) | |
.withParameters("{\"deploymentName\":{\"value\":\""+deploymentName+"\"}}") | |
.withMode(DeploymentMode.INCREMENTAL) | |
.create(); | |
System.out.println("Started a deployment correlationID" + deployment.correlationId()); | |
LinkedHashMap<String, LinkedHashMap<String, Object>> outputs = (LinkedHashMap<String, LinkedHashMap<String, Object>>) deployment.outputs(); | |
String dns = (String) outputs.get("ipaddress").get("value"); | |
System.out.println(dns); | |
} catch (Exception e) { | |
System.out.println(e.getMessage()); | |
e.printStackTrace(); | |
} | |
} | |
public static String getJSON(String urlToRead) throws Exception { | |
StringBuilder result = new StringBuilder(); | |
URL url = new URL(urlToRead); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod("GET"); | |
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
String line; | |
while ((line = rd.readLine()) != null) { | |
result.append(line); | |
} | |
rd.close(); | |
return result.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment