Skip to content

Commit ef23009

Browse files
committed
Moved samples to 4.1
1 parent 6140c21 commit ef23009

File tree

6 files changed

+1978
-2
lines changed

6 files changed

+1978
-2
lines changed

CONTRIBUTING.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Contributing to Azure samples
2+
3+
Thank you for your interest in contributing to Azure samples!
4+
5+
## Ways to contribute
6+
7+
You can contribute to [Azure samples](https://azure.microsoft.com/documentation/samples/) in a few different ways:
8+
9+
- Submit feedback on [this sample page](https://azure.microsoft.com/documentation/samples/app-service-java-manage-functions/) whether it was helpful or not.
10+
- Submit issues through [issue tracker](https://github.com/Azure-Samples/app-service-java-manage-functions/issues) on GitHub. We are actively monitoring the issues and improving our samples.
11+
- If you wish to make code changes to samples, or contribute something new, please follow the [GitHub Forks / Pull requests model](https://help.github.com/articles/fork-a-repo/): Fork the sample repo, make the change and propose it back by submitting a pull request.

README.md

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
1-
# Contributing
1+
---
2+
services: Appservice
3+
platforms: java
4+
author: jianghaolu
5+
---
26

3-
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.
7+
#Getting Started with Appservice - Manage Function App Basic - in Java #
8+
9+
10+
Azure App Service basic sample for managing function apps.
11+
- Create 3 function apps under the same new app service plan:
12+
- 1, 2 are in the same resource group, 3 in a different one
13+
- 1, 3 are under the same consumption plan, 2 under a basic app service plan
14+
- List function apps
15+
- Delete a function app
16+
17+
18+
## Running this Sample ##
19+
20+
To run this sample:
21+
22+
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).
23+
24+
git clone https://github.com/Azure-Samples/app-service-java-manage-functions.git
25+
26+
cd app-service-java-manage-functions
27+
28+
mvn clean compile exec:java
29+
30+
## More information ##
31+
32+
[http://azure.com/java] (http://azure.com/java)
33+
34+
If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212)
35+
36+
---
37+
38+
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

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.microsoft.azure</groupId>
6+
<artifactId>app-service-java-manage-functions</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<name>ManageFunctionAppBasic.java</name>
9+
<description></description>
10+
<url>https://github.com/Azure/app-service-java-manage-functions</url>
11+
<build>
12+
<plugins>
13+
<plugin>
14+
<groupId>org.codehaus.mojo</groupId>
15+
<artifactId>exec-maven-plugin</artifactId>
16+
<version>1.4.0</version>
17+
<configuration>
18+
<mainClass>com.microsoft.azure.management.appservice.samples.ManageFunctionAppBasic</mainClass>
19+
</configuration>
20+
</plugin>
21+
<plugin>
22+
<artifactId>maven-compiler-plugin</artifactId>
23+
<version>3.0</version>
24+
<configuration>
25+
<source>1.7</source>
26+
<target>1.7</target>
27+
</configuration>
28+
</plugin>
29+
<plugin>
30+
<artifactId>maven-assembly-plugin</artifactId>
31+
<executions>
32+
<execution>
33+
<phase>package</phase>
34+
<goals>
35+
<goal>attached</goal>
36+
</goals>
37+
<configuration>
38+
<descriptorRefs>
39+
<descriptorRef>jar-with-dependencies</descriptorRef>
40+
</descriptorRefs>
41+
<archive>
42+
<manifest>
43+
<mainClass>com.microsoft.azure.management.appservice.samples.ManageFunctionAppBasic.java</mainClass>
44+
</manifest>
45+
</archive>
46+
</configuration>
47+
</execution>
48+
</executions>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
<dependencies>
53+
<dependency>
54+
<groupId>com.microsoft.azure</groupId>
55+
<artifactId>azure</artifactId>
56+
<version>1.0.0</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>commons-net</groupId>
60+
<artifactId>commons-net</artifactId>
61+
<version>3.3</version>
62+
</dependency>
63+
</dependencies>
64+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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

Comments
 (0)