Skip to content

Commit

Permalink
Merge pull request Azure#1601 from jianghaolu/appsamples
Browse files Browse the repository at this point in the history
Web App Samples
  • Loading branch information
Martin Sawicki authored Apr 14, 2017
2 parents cb70ace + 6f04c9f commit 222ff34
Show file tree
Hide file tree
Showing 47 changed files with 22,655 additions and 8,424 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.microsoft.azure.management.resources.fluentcore.model.Refreshable;
import com.microsoft.azure.management.resources.fluentcore.model.Updatable;
import com.microsoft.azure.management.storage.StorageAccount;
import rx.Observable;

/**
* An immutable client-side representation of an Azure Function App.
Expand All @@ -30,6 +31,16 @@ public interface FunctionApp extends
*/
StorageAccount storageAccount();

/**
* @return the master key for the function app
*/
String getMasterKey();

/**
* @return the master key for the function app
*/
Observable<String> getMasterKeyAsync();

/**************************************************************
* Fluent interfaces to provision a Function App
**************************************************************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
import com.microsoft.azure.management.storage.SkuName;
import com.microsoft.azure.management.storage.StorageAccount;
import com.microsoft.azure.management.storage.StorageAccountKey;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.Path;
import retrofit2.http.Query;
import rx.Observable;
import rx.functions.Action0;
import rx.functions.Func1;

import java.util.List;
import java.util.Map;

/**
* The implementation for FunctionApp.
Expand All @@ -39,10 +45,12 @@ class FunctionAppImpl
private Creatable<StorageAccount> storageAccountCreatable;
private StorageAccount storageAccountToSet;
private StorageAccount currentStorageAccount;
private final FunctionAppKeyService functionAppKeyService;

FunctionAppImpl(String name, SiteInner innerObject, SiteConfigResourceInner configObject, AppServiceManager manager) {
super(name, innerObject, configObject, manager);
innerObject.withKind("functionapp");
functionAppKeyService = manager.restClient().retrofit().create(FunctionAppKeyService.class);
}

@Override
Expand Down Expand Up @@ -160,6 +168,22 @@ public StorageAccount storageAccount() {
return currentStorageAccount;
}

@Override
public String getMasterKey() {
return getMasterKeyAsync().toBlocking().single();
}

@Override
public Observable<String> getMasterKeyAsync() {
return functionAppKeyService.getMasterKey(resourceGroupName(), name(), manager().subscriptionId(), "2016-08-01", manager().inner().userAgent())
.map(new Func1<Map<String, String>, String>() {
@Override
public String call(Map<String, String> stringStringMap) {
return stringStringMap.get("masterKey");
}
});
}

@Override
public Observable<Indexable> createAsync() {
if (inner().serverFarmId() == null) {
Expand All @@ -170,4 +194,11 @@ public Observable<Indexable> createAsync() {
}
return super.createAsync();
}

private interface FunctionAppKeyService {
@Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.appservice.WebApps getByResourceGroup" })
@GET("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/functions/admin/masterkey")
Observable<Map<String, String>> getMasterKey(@Path("resourceGroupName") String resourceGroupName, @Path("name") String name, @Path("subscriptionId") String subscriptionId, @Query("api-version") String apiVersion, @Header("User-Agent") String userAgent);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.File;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.concurrent.TimeUnit;

public abstract class TestBase extends MockIntegrationTestBase {
private PrintStream out;
Expand Down Expand Up @@ -65,6 +66,7 @@ public void write(int b) {
.withResponseBuilderFactory(new AzureResponseBuilder.Factory())
.withCredentials(credentials)
.withLogLevel(LogLevel.BODY_AND_HEADERS)
.withReadTimeout(3, TimeUnit.MINUTES)
.withNetworkInterceptor(interceptor), false);

defaultSubscription = credentials.defaultSubscriptionId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@

/**
* Azure App Service basic sample for managing function apps.
* - Create 3 function apps in the same resource group
* - 1 & 2 are under the same consumption plan
* - 3 is in a basic app service plan
* - 2 is created with a sample function (in C#)
* - 1 is updated to have a daily quota and latest runtime
* - List function apps
* - Delete a function app
* - Create 3 function apps under the same new app service plan:
* - 1, 2 are in the same resource group, 3 in a different one
* - 1, 3 are under the same consumption plan, 2 under a basic app service plan
* - List function apps
* - Delete a function app
*/
public final class ManageFunctionApp {
public final class ManageFunctionAppBasic {

/**
* Main function which runs the actual sample.
Expand All @@ -36,79 +34,72 @@ public final class ManageFunctionApp {
*/
public static boolean runSample(Azure azure) {
// New resources
final String app1Name = SdkContext.randomResourceName("funcapp-", 20);
final String app2Name = SdkContext.randomResourceName("funcapp-", 20);
final String app3Name = SdkContext.randomResourceName("funcapp-", 20);
final String app1Name = SdkContext.randomResourceName("webapp1-", 20);
final String app2Name = SdkContext.randomResourceName("webapp2-", 20);
final String app3Name = SdkContext.randomResourceName("webapp3-", 20);
final String rg1Name = SdkContext.randomResourceName("rg1NEMV_", 24);
final String rg2Name = SdkContext.randomResourceName("rg2NEMV_", 24);

try {


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

System.out.println("Creating function app " + app1Name + " in resource group " + rg1Name + "...");

FunctionApp app1 = azure.appServices().functionApps()
.define(app1Name)
.withRegion(Region.US_WEST)
.withNewResourceGroup(rg1Name)
.withNewConsumptionPlan()
.withRuntimeVersion("1")
.create();

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

//============================================================
// Create a second function app with the same consumption plan

System.out.println("Creating function app " + app2Name + " in resource group " + rg1Name + "...");

AppServicePlan consumptionPlan = azure.appServices().appServicePlans().getById(app1.appServicePlanId());
// Create a second function app with the same app service plan

System.out.println("Creating another function app " + app2Name + " in resource group " + rg1Name + "...");
AppServicePlan plan = azure.appServices().appServicePlans().getById(app1.appServicePlanId());
FunctionApp app2 = azure.appServices().functionApps()
.define(app2Name)
.withExistingAppServicePlan(consumptionPlan)
.withRegion(Region.US_WEST)
.withExistingResourceGroup(rg1Name)
.withExistingStorageAccount(app1.storageAccount())
.defineSourceControl()
.withPublicGitRepository("https://github.com/azure-appservice-samples/AzureFunctions-Samples.git")
.withBranch("master")
.attach()
.withNewAppServicePlan(PricingTier.BASIC_B1)
.create();

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

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

System.out.println("Creating function app " + app3Name + " in resource group " + rg1Name + "...");
// Create a third function app with the same app service plan, but
// in a different resource group

System.out.println("Creating another function app " + app3Name + " in resource group " + rg2Name + "...");
FunctionApp app3 = azure.appServices().functionApps()
.define(app3Name)
.withRegion(Region.US_WEST)
.withExistingResourceGroup(rg1Name)
.withNewAppServicePlan(PricingTier.BASIC_B1)
.withExistingStorageAccount(app1.storageAccount())
.withExistingAppServicePlan(plan)
.withNewResourceGroup(rg2Name)
.create();

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

//============================================================
// Update the first function app with latest runtime and 1GB-sec quota

System.out.println("Updating function app " + "...");

app1.update()
.withDailyUsageQuota(1)
.withRuntimeVersion("latest")
.apply();

System.out.println("Updated function app " + app1.name());
// stop and start app1, restart app 2
System.out.println("Stopping function app " + app1.name());
app1.stop();
System.out.println("Stopped function app " + app1.name());
Utils.print(app1);
System.out.println("Starting function app " + app1.name());
app1.start();
System.out.println("Started function app " + app1.name());
Utils.print(app1);
System.out.println("Restarting function app " + app2.name());
app2.restart();
System.out.println("Restarted function app " + app2.name());
Utils.print(app2);

//=============================================================
// List function apps
Expand All @@ -119,14 +110,20 @@ public static boolean runSample(Azure azure) {
Utils.print(functionApp);
}

System.out.println("Printing list of function apps in resource group " + rg2Name + "...");

for (FunctionApp functionApp : azure.appServices().functionApps().listByResourceGroup(rg2Name)) {
Utils.print(functionApp);
}

//=============================================================
// Delete a function app

System.out.println("Deleting function app " + app1Name + "...");
azure.webApps().deleteByResourceGroup(rg1Name, app1Name);
azure.appServices().functionApps().deleteByResourceGroup(rg1Name, app1Name);
System.out.println("Deleted function app " + app1Name + "...");

System.out.println("Printing list of web apps in resource group " + rg1Name + " again...");
System.out.println("Printing list of function apps in resource group " + rg1Name + " again...");
for (FunctionApp functionApp : azure.appServices().functionApps().listByResourceGroup(rg1Name)) {
Utils.print(functionApp);
}
Expand All @@ -138,6 +135,10 @@ public static boolean runSample(Azure azure) {
try {
System.out.println("Deleting Resource Group: " + rg1Name);
azure.resourceGroups().beginDeleteByName(rg1Name);
System.out.println("Deleted Resource Group: " + rg1Name);
System.out.println("Deleting Resource Group: " + rg2Name);
azure.resourceGroups().beginDeleteByName(rg2Name);
System.out.println("Deleted Resource Group: " + rg2Name);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
Expand Down
Loading

0 comments on commit 222ff34

Please sign in to comment.