Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

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 b40742c commit 9a74095
Show file tree
Hide file tree
Showing 4 changed files with 1,670 additions and 1,455 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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-authentication-for-functions.git

Expand All @@ -38,9 +38,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
36 changes: 16 additions & 20 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.ManageFunctionAppWithAuthentication</mainClass>
<mainClass>com.azure.resourcemanager.appservice.samples.ManageFunctionAppWithAuthentication</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.ManageFunctionAppWithAuthentication.java</mainClass>
<mainClass>com.azure.resourcemanager.appservice.samples.ManageFunctionAppWithAuthentication.java</mainClass>
</manifest>
</archive>
</configuration>
Expand All @@ -51,29 +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>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.5.0.201609210915-r</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>
<version>4.5.7.201904151645-r</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

package com.microsoft.azure.management.appservice.samples;

import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.appservice.AppServicePlan;
import com.microsoft.azure.management.appservice.FunctionApp;
import com.microsoft.azure.management.appservice.NameValuePair;
import com.microsoft.azure.management.appservice.PublishingProfile;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext;
import com.microsoft.azure.management.samples.Utils;
import com.microsoft.rest.LogLevel;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
// 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.management.AzureEnvironment;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.resourcemanager.AzureResourceManager;
import com.azure.resourcemanager.appservice.models.AppServicePlan;
import com.azure.resourcemanager.appservice.models.FunctionApp;
import com.azure.resourcemanager.appservice.models.NameValuePair;
import com.azure.resourcemanager.appservice.models.PublishingProfile;
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 com.azure.core.http.policy.HttpLogDetailLevel;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.PushCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.time.Duration;


/**
* Azure App Service basic sample for managing function apps.
Expand All @@ -44,23 +40,21 @@
*/
public final class ManageFunctionAppWithAuthentication {

private static OkHttpClient httpClient;

/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @param azureResourceManager instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
public static boolean runSample(AzureResourceManager azureResourceManager) throws GitAPIException {
// New resources
final String suffix = ".azurewebsites.net";
final String app1Name = SdkContext.randomResourceName("webapp1-", 20);
final String app2Name = SdkContext.randomResourceName("webapp2-", 20);
final String app3Name = SdkContext.randomResourceName("webapp3-", 20);
final String app1Name = Utils.randomResourceName(azureResourceManager, "webapp1-", 20);
final String app2Name = Utils.randomResourceName(azureResourceManager, "webapp2-", 20);
final String app3Name = Utils.randomResourceName(azureResourceManager, "webapp3-", 20);
final String app1Url = app1Name + suffix;
final String app2Url = app2Name + suffix;
final String app3Url = app3Name + suffix;
final String rgName = SdkContext.randomResourceName("rg1NEMV_", 24);
final String rgName = Utils.randomResourceName(azureResourceManager, "rg1NEMV_", 24);

try {

Expand All @@ -70,7 +64,7 @@ public static boolean runSample(Azure azure) {

System.out.println("Creating function app " + app1Name + " in resource group " + rgName + " with admin level auth...");

FunctionApp app1 = azure.appServices().functionApps().define(app1Name)
FunctionApp app1 = azureResourceManager.functionApps().define(app1Name)
.withRegion(Region.US_WEST)
.withNewResourceGroup(rgName)
.withLocalGitSourceControl()
Expand All @@ -83,8 +77,8 @@ public static boolean runSample(Azure azure) {
// Create a second function app with function level auth

System.out.println("Creating another function app " + app2Name + " in resource group " + rgName + " with function level auth...");
AppServicePlan plan = azure.appServices().appServicePlans().getById(app1.appServicePlanId());
FunctionApp app2 = azure.appServices().functionApps().define(app2Name)
AppServicePlan plan = azureResourceManager.appServicePlans().getById(app1.appServicePlanId());
FunctionApp app2 = azureResourceManager.functionApps().define(app2Name)
.withExistingAppServicePlan(plan)
.withExistingResourceGroup(rgName)
.withExistingStorageAccount(app1.storageAccount())
Expand All @@ -98,7 +92,7 @@ public static boolean runSample(Azure azure) {
// Create a thrid function app with function level auth

System.out.println("Creating another function app " + app3Name + " in resource group " + rgName + " with function level auth...");
FunctionApp app3 = azure.appServices().functionApps().define(app3Name)
FunctionApp app3 = azureResourceManager.functionApps().define(app3Name)
.withExistingAppServicePlan(plan)
.withExistingResourceGroup(rgName)
.withExistingStorageAccount(app1.storageAccount())
Expand Down Expand Up @@ -132,10 +126,10 @@ public static boolean runSample(Azure azure) {

// warm up
System.out.println("Warming up " + app1Url + "/api/square...");
post("http://" + app1Url + "/api/square", "625");
SdkContext.sleep(5000);
Utils.sendPostRequest("http://" + app1Url + "/api/square", "625");
ResourceManagerUtils.sleep(Duration.ofSeconds(5));
System.out.println("CURLing " + app1Url + "/api/square...");
System.out.println("Square of 625 is " + post("http://" + app1Url + "/api/square?code=" + app1.getMasterKey(), "625"));
System.out.println("Square of 625 is " + Utils.sendPostRequest("http://" + app1Url + "/api/square?code=" + app1.getMasterKey(), "625"));

//============================================================
// Deploy to app 2 through Git
Expand Down Expand Up @@ -164,25 +158,25 @@ public static boolean runSample(Azure azure) {

// warm up
System.out.println("Warming up " + app2Url + "/api/square...");
post("http://" + app2Url + "/api/square", "725");
SdkContext.sleep(5000);
Utils.sendPostRequest("http://" + app2Url + "/api/square", "725");
ResourceManagerUtils.sleep(Duration.ofSeconds(5));
System.out.println("CURLing " + app2Url + "/api/square...");
System.out.println("Square of 725 is " + post("http://" + app2Url + "/api/square?code=" + functionKey, "725"));
System.out.println("Square of 725 is " + Utils.sendPostRequest("http://" + app2Url + "/api/square?code=" + functionKey, "725"));

System.out.println("Adding a new key to function app " + app2.name() + "...");

NameValuePair newKey = app2.addFunctionKey("square", "newkey", null);

System.out.println("CURLing " + app2Url + "/api/square...");
System.out.println("Square of 825 is " + post("http://" + app2Url + "/api/square?code=" + newKey.value(), "825"));
System.out.println("Square of 825 is " + Utils.sendPostRequest("http://" + app2Url + "/api/square?code=" + newKey.value(), "825"));

//============================================================
// Deploy to app 3 through Git

System.out.println("Deploying a local function app to " + app3Name + " through web deploy...");

app3.deploy()
.withPackageUri("https://github.com/Azure/azure-libraries-for-java/raw/master/azure-samples/src/main/resources/square-function-app-function-auth.zip")
.withPackageUri("https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/resourcemanager/azure-resourcemanager-samples/src/main/resources/square-function-app-function-auth.zip")
.withExistingDeploymentsDeleted(false)
.execute();

Expand All @@ -195,27 +189,23 @@ public static boolean runSample(Azure azure) {

// warm up
System.out.println("Warming up " + app3Url + "/api/square...");
post("http://" + app3Url + "/api/square", "925");
SdkContext.sleep(5000);
Utils.sendPostRequest("http://" + app3Url + "/api/square", "925");
ResourceManagerUtils.sleep(Duration.ofSeconds(5));
System.out.println("CURLing " + app3Url + "/api/square...");
System.out.println("Square of 925 is " + post("http://" + app3Url + "/api/square?code=mysecretkey", "925"));
System.out.println("Square of 925 is " + Utils.sendPostRequest("http://" + app3Url + "/api/square?code=mysecretkey", "925"));

return true;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().beginDeleteByName(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();
}
}
return false;
}
/**
* Main entry point.
Expand All @@ -227,43 +217,24 @@ public static void main(String[] args) {
//=============================================================
// Authenticate

final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
final TokenCredential credential = new DefaultAzureCredentialBuilder()
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
.build();

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

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

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

private static String curl(String url, Map<String, String> headers) {
Request request = new Request.Builder().url(url).headers(Headers.of(headers)).get().build();
try {
return httpClient.newCall(request).execute().body().string();
} catch (IOException e) {
return null;
}
}

private static String post(String url, String body) {
Request request = new Request.Builder().url(url).post(RequestBody.create(MediaType.parse("text/plain"), body)).build();
try {
return httpClient.newCall(request).execute().body().string();
} catch (IOException e) {
return null;
}
}

static {
httpClient = new OkHttpClient.Builder().readTimeout(1, TimeUnit.MINUTES).build();
}
}
Loading

0 comments on commit 9a74095

Please sign in to comment.