Skip to content

Commit 466cc49

Browse files
author
Martin Sawicki
authored
Merge pull request #1 from anudeepsharma/UpdatedSample
Updated sample
2 parents dbe6266 + 0e1e14c commit 466cc49

File tree

5 files changed

+901
-10
lines changed

5 files changed

+901
-10
lines changed

.gitignore

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
*.class
2+
3+
# Auth filed
4+
*.auth
5+
*.azureauth
6+
7+
# Mobile Tools for Java (J2ME)
8+
.mtj.tmp/
9+
10+
# Package Files #
11+
*.jar
12+
*.war
13+
*.ear
14+
15+
# Azure Tooling #
16+
node_modules
17+
packages
18+
19+
# Eclipse #
20+
*.pydevproject
21+
.project
22+
.metadata
23+
bin/**
24+
tmp/**
25+
tmp/**/*
26+
*.tmp
27+
*.bak
28+
*.swp
29+
*~.nib
30+
local.properties
31+
.classpath
32+
.settings/
33+
.loadpath
34+
35+
# Other Tooling #
36+
.classpath
37+
.project
38+
target
39+
.idea
40+
*.iml
41+
42+
# Mac OS #
43+
.DS_Store
44+
.DS_Store?
45+
46+
# Windows #
47+
Thumbs.db
48+
49+
# reduced pom files should not be included
50+
dependency-reduced-pom.xml

README.md

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
1-
# resources-java-deploy-using-arm-template-with-progress
2-
Getting started on deploying using an ARM template and show progress
3-
## Running this sample
4-
Coming soon...
5-
## Deploy this sample to Azure
6-
Coming soon...
7-
## About the code
8-
Coming soon...
9-
## More information
10-
Coming soon...
1+
---
2+
services: Resources
3+
platforms: java
4+
author: anudeepsharma
5+
---
6+
7+
#Getting Started with Resources - Deploy Using ARM Template With Progress - in Java #
8+
9+
10+
Azure Resource sample for deploying resources using an ARM template and
11+
showing progress.
12+
13+
14+
## Running this Sample ##
15+
16+
To run this sample:
17+
18+
Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an auth file. See [how to create an auth file](https://github.com/Azure/azure-sdk-for-java/blob/master/AUTH.md).
19+
20+
git clone https://github.com/Azure-Samples/resources-java-deploy-using-arm-template-with-progress.git
21+
22+
cd resources-java-deploy-using-arm-template-with-progress
23+
24+
mvn clean compile exec:java
25+
26+
## More information ##
27+
28+
[http://azure.com/java] (http://azure.com/java)
29+
30+
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212)
31+
32+
---
33+
34+
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.

pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.microsoft.azure</groupId>
4+
<artifactId>resources-java-deploy-using-arm-template-with-progress</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<name>DeployUsingARMTemplateWithProgress</name>
7+
<description></description>
8+
<url>https://github.com/Azure/resources-java-deploy-using-arm-template-with-progress</url>
9+
<dependencies>
10+
<dependency>
11+
<groupId>com.microsoft.azure</groupId>
12+
<artifactId>azure</artifactId>
13+
<version>1.0.0-beta3</version>
14+
</dependency>
15+
</dependencies>
16+
<build>
17+
<sourceDirectory>src</sourceDirectory>
18+
<resources>
19+
<resource>
20+
<directory>resources</directory>
21+
</resource>
22+
</resources>
23+
<plugins>
24+
<plugin>
25+
<groupId>org.codehaus.mojo</groupId>
26+
<artifactId>exec-maven-plugin</artifactId>
27+
<version>1.4.0</version>
28+
<configuration>
29+
<mainClass>com.microsoft.azure.management.resources.samples.DeployUsingARMTemplateWithProgress</mainClass>
30+
</configuration>
31+
</plugin>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-compiler-plugin</artifactId>
35+
<version>3.0</version>
36+
<configuration>
37+
<source>1.7</source>
38+
<target>1.7</target>
39+
</configuration>
40+
</plugin>
41+
<plugin>
42+
<!-- Generate a fully packaged executable jar with dependencies -->
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-assembly-plugin</artifactId>
45+
<executions>
46+
<execution>
47+
<goals>
48+
<goal>attached</goal>
49+
</goals>
50+
<phase>package</phase>
51+
<configuration>
52+
<descriptorRefs>
53+
<descriptorRef>jar-with-dependencies</descriptorRef>
54+
</descriptorRefs>
55+
<archive>
56+
<manifest>
57+
<mainClass>com.microsoft.azure.management.resources.samples.DeployUsingARMTemplateWithProgress</mainClass>
58+
</manifest>
59+
</archive>
60+
</configuration>
61+
</execution>
62+
</executions>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
</project>
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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

Comments
 (0)