Skip to content

Commit

Permalink
Release azure-resourcemanager 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Azure-Fluent committed Oct 26, 2020
1 parent 857fa32 commit 9796aeb
Show file tree
Hide file tree
Showing 5 changed files with 1,813 additions and 1,601 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extensions:

To run this sample:

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-libraries-for-java/blob/master/AUTH.md).
See [DefaultAzureCredential](https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/identity/azure-identity#defaultazurecredential) and prepare the authentication works best for you. For more details on authentication, please refer to [AUTH.md](https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/resourcemanager/docs/AUTH.md).

git clone https://github.com/Azure-Samples/app-service-java-manage-logs-for-function-apps.git

Expand All @@ -33,9 +33,11 @@ Set the environment variable `AZURE_AUTH_LOCATION` with the full path for an aut

## More information ##

[http://azure.com/java](http://azure.com/java)
For general documentation as well as quickstarts on how to use Azure Management Libraries for Java, please see [here](https://aka.ms/azsdk/java/mgmt).

If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212)
Start to develop applications with Java on Azure [here](http://azure.com/java).

If you don't have a Microsoft Azure subscription you can get a FREE trial account [here](http://go.microsoft.com/fwlink/?LinkId=330212).

---

Expand Down
29 changes: 15 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
<artifactId>exec-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<mainClass>com.microsoft.azure.management.appservice.samples.ManageFunctionAppLogs</mainClass>
<mainClass>com.azure.resourcemanager.appservice.samples.ManageFunctionAppLogs</mainClass>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<version>3.8.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand All @@ -40,7 +41,7 @@
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.microsoft.azure.management.appservice.samples.ManageFunctionAppLogs.java</mainClass>
<mainClass>com.azure.resourcemanager.appservice.samples.ManageFunctionAppLogs.java</mainClass>
</manifest>
</archive>
</configuration>
Expand All @@ -51,24 +52,24 @@
</build>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.36.3</version>
<groupId>com.azure.resourcemanager</groupId>
<artifactId>azure-resourcemanager</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
<version>3.6</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.resourcemanager.appservice.samples;

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.management.AzureEnvironment;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.resourcemanager.AzureResourceManager;
import com.azure.resourcemanager.appservice.models.FunctionApp;
import com.azure.resourcemanager.appservice.models.LogLevel;
import com.azure.core.management.Region;
import com.azure.core.management.profile.AzureProfile;
import com.azure.resourcemanager.resources.fluentcore.utils.ResourceManagerUtils;
import com.azure.resourcemanager.samples.Utils;
import org.apache.commons.lang.time.StopWatch;
import reactor.core.publisher.BaseSubscriber;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicInteger;


/**
* Azure App Service basic sample for managing function apps.
* - Create a function app under the same new app service plan:
* - Deploy to app using FTP
* - stream logs synchronously for 30 seconds
* - stream logs asynchronously until 3 requests are completed
*/
public final class ManageFunctionAppLogs {

/**
* Main function which runs the actual sample.
* @param azureResourceManager instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(AzureResourceManager azureResourceManager) throws IOException {
// New resources
final String suffix = ".azurewebsites.net";
final String appName = Utils.randomResourceName(azureResourceManager, "webapp1-", 20);
final String appUrl = appName + suffix;
final String rgName = Utils.randomResourceName(azureResourceManager, "rg1NEMV_", 24);

try {


//============================================================
// Create a function app with a new app service plan

System.out.println("Creating function app " + appName + " in resource group " + rgName + "...");

FunctionApp app = azureResourceManager.functionApps().define(appName)
.withRegion(Region.US_WEST)
.withNewResourceGroup(rgName)
.defineDiagnosticLogsConfiguration()
.withApplicationLogging()
.withLogLevel(LogLevel.VERBOSE)
.withApplicationLogsStoredOnFileSystem()
.attach()
.create();

System.out.println("Created function app " + app.name());
Utils.print(app);

//============================================================
// Deploy to app 1 through FTP

System.out.println("Deploying a function app to " + appName + " through FTP...");

Utils.uploadFileForFunctionViaFtp(app.getPublishingProfile(), "host.json", ManageFunctionAppLogs.class.getResourceAsStream("/square-function-app/host.json"));
Utils.uploadFileForFunctionViaFtp(app.getPublishingProfile(), "square/function.json", ManageFunctionAppLogs.class.getResourceAsStream("/square-function-app/square/function.json"));
Utils.uploadFileForFunctionViaFtp(app.getPublishingProfile(), "square/index.js", ManageFunctionAppLogs.class.getResourceAsStream("/square-function-app/square/index.js"));

// sync triggers
app.syncTriggers();

System.out.println("Deployment square app to function app " + app.name() + " completed");
Utils.print(app);

// warm up
System.out.println("Warming up " + appUrl + "/api/square...");
Utils.sendPostRequest("http://" + appUrl + "/api/square", "625");
ResourceManagerUtils.sleep(Duration.ofSeconds(5));

//============================================================
// Listen to logs synchronously for 30 seconds

final InputStream stream = app.streamApplicationLogs();
System.out.println("Streaming logs from function app " + appName + "...");
String line = readLine(stream);
StopWatch stopWatch = new StopWatch();
stopWatch.start();
new Thread(() -> {
Utils.sendPostRequest("http://" + appUrl + "/api/square", "625");
ResourceManagerUtils.sleep(Duration.ofSeconds(10));
Utils.sendPostRequest("http://" + appUrl + "/api/square", "725");
ResourceManagerUtils.sleep(Duration.ofSeconds(10));
Utils.sendPostRequest("http://" + appUrl + "/api/square", "825");
}).start();
while (line != null && stopWatch.getTime() < 90000) {
System.out.println(line);
line = readLine(stream);
}
stream.close();

//============================================================
// Listen to logs asynchronously until 3 requests are completed

new Thread(() -> {
ResourceManagerUtils.sleep(Duration.ofSeconds(5));
System.out.println("Starting hitting");
Utils.sendPostRequest("http://" + appUrl + "/api/square", "625");
ResourceManagerUtils.sleep(Duration.ofSeconds(10));
Utils.sendPostRequest("http://" + appUrl + "/api/square", "725");
ResourceManagerUtils.sleep(Duration.ofSeconds(10));
Utils.sendPostRequest("http://" + appUrl + "/api/square", "825");
}).start();

final AtomicInteger count = new AtomicInteger(0);
app.streamApplicationLogsAsync().subscribe(new BaseSubscriber<String>() {
@Override
protected void hookOnNext(String value) {
System.out.println(value);
if (value.contains("Function completed")) {
if (count.incrementAndGet() >= 3) {
this.dispose();
}
}
super.hookOnNext(value);
}

@Override
protected void hookOnError(Throwable throwable) {
throwable.printStackTrace();
super.hookOnError(throwable);
}
});

return true;
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azureResourceManager.resourceGroups().beginDeleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
}
/**
* Main entry point.
* @param args the parameters
*/
public static void main(String[] args) {
try {

//=============================================================
// Authenticate

final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
final TokenCredential credential = new DefaultAzureCredentialBuilder()
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
.build();

AzureResourceManager azureResourceManager = AzureResourceManager
.configure()
.withLogLevel(HttpLogDetailLevel.BASIC)
.authenticate(credential, profile)
.withDefaultSubscription();

// Print selected subscription
System.out.println("Selected subscription: " + azureResourceManager.subscriptionId());

runSample(azureResourceManager);
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}

private static String readLine(InputStream in) throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
int c;
for (c = in.read(); c != '\n' && c >= 0; c = in.read()) {
stream.write(c);
}
if (c == -1 && stream.size() == 0) {
return null;
}
return stream.toString("UTF-8");
}
}
Loading

0 comments on commit 9796aeb

Please sign in to comment.