/// <summary> /// Create a function app, upload the files for his running state. The files are run.csx, function.json and host.json /// DO NOT WORK ATM : the inputs / outputs of the function app does not works. /// </summary> public static void CreateFunctionApp() { string appName = SdkContext.RandomResourceName(functionAppPrefix, 20); string suffix = ".azurewebsites.net"; string appUrl = appName + suffix; spin.setMessage("Creating function app " + appName + " in resource group " + rgName + "..."); //Console.WriteLine("Creating function app " + appName + " in resource group " + rgName + "..."); //Console.ReadLine(); IFunctionApp app1 = azure.AppServices.FunctionApps.Define(appName) .WithRegion(Region.EuropeWest) .WithExistingResourceGroup(rgName) .Create(); spin.Stop(); Console.WriteLine("Created Function App"); Console.WriteLine(app1); Console.WriteLine(""); spin.setMessage("Deploying to function app" + appName + " with FTP..."); spin.Start(); IPublishingProfile profile = app1.GetPublishingProfile(); Utilities.UploadFileToFunctionApp(profile, Path.Combine(Utilities.ProjectPath, "FunctionAppCore", "host.json")); Utilities.UploadFileToFunctionApp(profile, Path.Combine(Utilities.ProjectPath, "FunctionAppCore", "IoTHubTrigger", "function.json"), "IoTHubTrigger/function.json"); Utilities.UploadFileToFunctionApp(profile, Path.Combine(Utilities.ProjectPath, "FunctionAppCore", "IoTHubTrigger", "run.csx"), "IoTHubTrigger/run.csx"); //sync triggers app1.SyncTriggers(); spin.Stop(); Console.WriteLine("Deployment iotHubTrigger to web app" + app1.Name + " completed"); //warm up //Console.WriteLine("Warming up " + appUrl + "/api/IoTHubTrigger"); //Utilities.PostAddress("http://" + appUrl + "/api/IoTHubTrigger", "toto"); //SdkContext.DelayProvider.Delay(5000); //Console.WriteLine("Curling..."); //Console.WriteLine(Utilities.PostAddress("http://" + appUrl + "/api/IoTHubTrigger", "toto")); }
/** * Azure App Service basic sample for managing function apps. * - Create 4 function apps under the same new app service plan: * - Deploy to 1 using FTP * - Deploy to 2 using local Git repository * - Deploy to 3 using a publicly available Git repository * - Deploy to 4 using a GitHub repository with continuous integration */ public static void RunSample(IAzure azure) { // New resources string suffix = ".azurewebsites.net"; string app1Name = SdkContext.RandomResourceName("webapp1-", 20); string app2Name = SdkContext.RandomResourceName("webapp2-", 20); string app3Name = SdkContext.RandomResourceName("webapp3-", 20); string app4Name = SdkContext.RandomResourceName("webapp4-", 20); string app1Url = app1Name + suffix; string app2Url = app2Name + suffix; string app3Url = app3Name + suffix; string app4Url = app4Name + suffix; string rgName = SdkContext.RandomResourceName("rg1NEMV_", 24); try { //============================================================ // Create a function app with a new app service plan Utilities.Log("Creating function app " + app1Name + " in resource group " + rgName + "..."); IFunctionApp app1 = azure.AppServices.FunctionApps.Define(app1Name) .WithRegion(Region.USWest) .WithNewResourceGroup(rgName) .Create(); Utilities.Log("Created function app " + app1.Name); Utilities.Print(app1); //============================================================ // Deploy to app 1 through FTP Utilities.Log("Deploying a function app to " + app1Name + " through FTP..."); IPublishingProfile profile = app1.GetPublishingProfile(); Utilities.UploadFileToFtp(profile, Path.Combine(Utilities.ProjectPath, "Asset", "square-function-app", "host.json")); Utilities.UploadFileToFtp(profile, Path.Combine(Utilities.ProjectPath, "Asset", "square-function-app", "square", "function.json"), "square/function.json"); Utilities.UploadFileToFtp(profile, Path.Combine(Utilities.ProjectPath, "Asset", "square-function-app", "square", "index.js"), "square/index.js"); Utilities.Log("Deployment square app to web app " + app1.Name + " completed"); Utilities.Print(app1); // warm up Utilities.Log("Warming up " + app1Url + "/api/square..."); Utilities.PostAddress("http://" + app1Url + "/api/square", "625"); SdkContext.DelayProvider.Delay(5000); Utilities.Log("CURLing " + app1Url + "/api/square..."); Utilities.Log(Utilities.PostAddress("http://" + app1Url + "/api/square", "625")); //============================================================ // Create a second function app with local git source control Utilities.Log("Creating another function app " + app2Name + " in resource group " + rgName + "..."); IAppServicePlan plan = azure.AppServices.AppServicePlans.GetById(app1.AppServicePlanId); IFunctionApp app2 = azure.AppServices.FunctionApps.Define(app2Name) .WithExistingAppServicePlan(plan) .WithExistingResourceGroup(rgName) .WithExistingStorageAccount(app1.StorageAccount) .WithLocalGitSourceControl() .Create(); Utilities.Log("Created function app " + app2.Name); Utilities.Print(app2); //============================================================ // Deploy to app 2 through local Git Utilities.Log("Deploying a local Tomcat source to " + app2Name + " through Git..."); profile = app2.GetPublishingProfile(); Utilities.DeployByGit(profile, "square-function-app"); Utilities.Log("Deployment to function app " + app2.Name + " completed"); Utilities.Print(app2); // warm up Utilities.Log("Warming up " + app2Url + "/api/square..."); Utilities.PostAddress("http://" + app2Url + "/api/square", "725"); SdkContext.DelayProvider.Delay(5000); Utilities.Log("CURLing " + app2Url + "/api/square..."); Utilities.Log("Square of 725 is " + Utilities.PostAddress("http://" + app2Url + "/api/square", "725")); //============================================================ // Create a 3rd function app with a public GitHub repo in Azure-Samples Utilities.Log("Creating another function app " + app3Name + "..."); IFunctionApp app3 = azure.AppServices.FunctionApps.Define(app3Name) .WithExistingAppServicePlan(plan) .WithNewResourceGroup(rgName) .WithExistingStorageAccount(app2.StorageAccount) .DefineSourceControl() .WithPublicGitRepository("https://github.com/jianghaolu/square-function-app-sample") .WithBranch("master") .Attach() .Create(); Utilities.Log("Created function app " + app3.Name); Utilities.Print(app3); // warm up Utilities.Log("Warming up " + app3Url + "/api/square..."); Utilities.PostAddress("http://" + app3Url + "/api/square", "825"); SdkContext.DelayProvider.Delay(5000); Utilities.Log("CURLing " + app3Url + "/api/square..."); Utilities.Log("Square of 825 is " + Utilities.PostAddress("http://" + app3Url + "/api/square", "825")); //============================================================ // Create a 4th function app with a personal GitHub repo and turn on continuous integration Utilities.Log("Creating another function app " + app4Name + "..."); IFunctionApp app4 = azure.AppServices.FunctionApps .Define(app4Name) .WithExistingAppServicePlan(plan) .WithExistingResourceGroup(rgName) .WithExistingStorageAccount(app3.StorageAccount) // Uncomment the following lines to turn on 4th scenario //.DefineSourceControl() // .WithContinuouslyIntegratedGitHubRepository("username", "reponame") // .WithBranch("master") // .WithGitHubAccessToken("YOUR GITHUB PERSONAL TOKEN") // .Attach() .Create(); Utilities.Log("Created function app " + app4.Name); Utilities.Print(app4); // warm up Utilities.Log("Warming up " + app4Url + "..."); Utilities.CheckAddress("http://" + app4Url); SdkContext.DelayProvider.Delay(5000); Utilities.Log("CURLing " + app4Url + "..."); Utilities.Log(Utilities.CheckAddress("http://" + app4Url)); } finally { try { Utilities.Log("Deleting Resource Group: " + rgName); azure.ResourceGroups.DeleteByName(rgName); Utilities.Log("Deleted Resource Group: " + rgName); } catch (NullReferenceException) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } catch (Exception g) { Utilities.Log(g); } } }
/** * Azure App Service basic sample for managing function apps. * - Create 4 function apps under the same new app service plan: * - Deploy to 1 using FTP * - Deploy to 2 using local Git repository * - Deploy to 3 using a publicly available Git repository * - Deploy to 4 using a GitHub repository with continuous integration */ public static void RunSample(IAzure azure) { // New resources string suffix = ".azurewebsites.net"; string app1Name = SdkContext.RandomResourceName("webapp1-", 20); string app2Name = SdkContext.RandomResourceName("webapp2-", 20); string app1Url = app1Name + suffix; string app2Url = app2Name + suffix; string rgName = SdkContext.RandomResourceName("rg1NEMV_", 24); try { //============================================================ // Create a function app with admin level auth Utilities.Log("Creating function app " + app1Name + " in resource group " + rgName + " with admin level auth..."); IFunctionApp app1 = azure.AppServices.FunctionApps.Define(app1Name) .WithRegion(Region.USWest) .WithNewResourceGroup(rgName) .WithLocalGitSourceControl() .Create(); Utilities.Log("Created function app " + app1.Name); Utilities.Print(app1); //============================================================ // Create a second function app with function level auth Utilities.Log("Creating another function app " + app2Name + " in resource group " + rgName + " with function level auth..."); IAppServicePlan plan = azure.AppServices.AppServicePlans.GetById(app1.AppServicePlanId); IFunctionApp app2 = azure.AppServices.FunctionApps.Define(app2Name) .WithExistingAppServicePlan(plan) .WithExistingResourceGroup(rgName) .WithExistingStorageAccount(app1.StorageAccount) .WithLocalGitSourceControl() .Create(); Utilities.Log("Created function app " + app2.Name); Utilities.Print(app2); //============================================================ // Deploy to app 1 through Git Utilities.Log("Deploying a local function app to " + app1Name + " through Git..."); IPublishingProfile profile = app1.GetPublishingProfile(); Utilities.DeployByGit(profile, "square-function-app-admin-auth"); // warm up Utilities.Log("Warming up " + app1Url + "/api/square..."); Utilities.PostAddress("http://" + app1Url + "/api/square", "625"); SdkContext.DelayProvider.Delay(5000); Utilities.Log("CURLing " + app1Url + "/api/square..."); Utilities.Log("Square of 625 is " + Utilities.PostAddress("http://" + app1Url + "/api/square?code=" + app1.GetMasterKey(), "625")); //============================================================ // Deploy to app 2 through Git Utilities.Log("Deploying a local function app to " + app2Name + " through Git..."); profile = app2.GetPublishingProfile(); Utilities.DeployByGit(profile, "square-function-app-function-auth"); Utilities.Log("Deployment to function app " + app2.Name + " completed"); Utilities.Print(app2); string masterKey = app2.GetMasterKey(); var functionsHeader = new Dictionary <string, string>(); functionsHeader["x-functions-key"] = masterKey; string response = Utilities.CheckAddress("http://" + app2Url + "/admin/functions/square/keys", functionsHeader); Regex pattern = new Regex(@"""name"":""default"",""value"":""([\w=/]+)"""); Match matcher = pattern.Match(response); string functionKey = matcher.Captures[0].Value; // warm up Utilities.Log("Warming up " + app2Url + "/api/square..."); Utilities.PostAddress("http://" + app2Url + "/api/square", "725"); SdkContext.DelayProvider.Delay(5000); Utilities.Log("CURLing " + app2Url + "/api/square..."); Utilities.Log("Square of 725 is " + Utilities.PostAddress("http://" + app2Url + "/api/square?code=" + functionKey, "725")); } finally { try { Utilities.Log("Deleting Resource Group: " + rgName); azure.ResourceGroups.DeleteByName(rgName); Utilities.Log("Deleted Resource Group: " + rgName); } catch (NullReferenceException) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } catch (Exception g) { Utilities.Log(g); } } }
/// <summary> /// Copies the site configurations from a given function app. /// </summary> /// <param name="app">The function app to copy the configurations from.</param> /// <return>The next stage of the definition.</return> FunctionDeploymentSlot.Definition.IWithCreate FunctionDeploymentSlot.Definition.IWithConfiguration.WithConfigurationFromFunctionApp(IFunctionApp app) { return(this.WithConfigurationFromFunctionApp(app) as FunctionDeploymentSlot.Definition.IWithCreate); }
/** * Azure App Service basic sample for managing function apps. * - 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 static void RunSample(IAzure azure) { // New resources string app1Name = SdkContext.RandomResourceName("webapp1-", 20); string app2Name = SdkContext.RandomResourceName("webapp2-", 20); string app3Name = SdkContext.RandomResourceName("webapp3-", 20); string rg1Name = SdkContext.RandomResourceName("rg1NEMV_", 24); string rg2Name = SdkContext.RandomResourceName("rg2NEMV_", 24); try { //============================================================ // Create a function app with a new app service plan Utilities.Log("Creating function app " + app1Name + " in resource group " + rg1Name + "..."); IFunctionApp app1 = azure.AppServices.FunctionApps .Define(app1Name) .WithRegion(Region.USWest) .WithNewResourceGroup(rg1Name) .Create(); Utilities.Log("Created function app " + app1.Name); Utilities.Print(app1); //============================================================ // Create a second function app with the same app service plan Utilities.Log("Creating another function app " + app2Name + " in resource group " + rg1Name + "..."); IAppServicePlan plan = azure.AppServices.AppServicePlans.GetById(app1.AppServicePlanId); IFunctionApp app2 = azure.AppServices.FunctionApps .Define(app2Name) .WithRegion(Region.USWest) .WithExistingResourceGroup(rg1Name) .WithNewAppServicePlan(PricingTier.BasicB1) .Create(); Utilities.Log("Created function app " + app2.Name); Utilities.Print(app2); //============================================================ // Create a third function app with the same app service plan, but // in a different resource group Utilities.Log("Creating another function app " + app3Name + " in resource group " + rg2Name + "..."); IFunctionApp app3 = azure.AppServices.FunctionApps .Define(app3Name) .WithExistingAppServicePlan(plan) .WithNewResourceGroup(rg2Name) .Create(); Utilities.Log("Created function app " + app3.Name); Utilities.Print(app3); //============================================================ // stop and start app1, restart app 2 Utilities.Log("Stopping function app " + app1.Name); app1.Stop(); Utilities.Log("Stopped function app " + app1.Name); Utilities.Print(app1); Utilities.Log("Starting function app " + app1.Name); app1.Start(); Utilities.Log("Started function app " + app1.Name); Utilities.Print(app1); Utilities.Log("Restarting function app " + app2.Name); app2.Restart(); Utilities.Log("Restarted function app " + app2.Name); Utilities.Print(app2); //============================================================= // List function apps Utilities.Log("Printing list of function apps in resource group " + rg1Name + "..."); foreach (IFunctionApp functionApp in azure.AppServices.FunctionApps.ListByResourceGroup(rg1Name)) { Utilities.Print(functionApp); } Utilities.Log("Printing list of function apps in resource group " + rg2Name + "..."); foreach (IFunctionApp functionApp in azure.AppServices.FunctionApps.ListByResourceGroup(rg2Name)) { Utilities.Print(functionApp); } //============================================================= // Delete a function app Utilities.Log("Deleting function app " + app1Name + "..."); azure.AppServices.FunctionApps.DeleteByResourceGroup(rg1Name, app1Name); Utilities.Log("Deleted function app " + app1Name + "..."); Utilities.Log("Printing list of function apps in resource group " + rg1Name + " again..."); foreach (IFunctionApp functionApp in azure.AppServices.FunctionApps.ListByResourceGroup(rg1Name)) { Utilities.Print(functionApp); } } finally { try { Utilities.Log("Deleting Resource Group: " + rg2Name); azure.ResourceGroups.DeleteByName(rg2Name); Utilities.Log("Deleted Resource Group: " + rg2Name); Utilities.Log("Deleting Resource Group: " + rg1Name); azure.ResourceGroups.DeleteByName(rg1Name); Utilities.Log("Deleted Resource Group: " + rg1Name); } catch (NullReferenceException) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } catch (Exception g) { Utilities.Log(g); } } }
private async Task <int> OnExecuteAsync(CommandLineApplication app, CancellationToken cancellationToken = default) { var authenticationOptions = AuthenticationOptions.BuildFrom(this.UseAzCliDevAuth, this.TenantId); IAppServiceManager appServiceManager = AppServiceManagerSource.Get( authenticationOptions, this.SubscriptionId); IWebAppAuthentication webAppAuthConfig; ManagedServiceIdentity managedIdentity; IFunctionApp function = null; try { function = appServiceManager.FunctionApps.GetByResourceGroup(this.ResourceGroupName, this.AppName); } catch (NullReferenceException) { // Unhelpfully, we seem to get a null reference exception if the app isn't found } if (function != null) { managedIdentity = function.Inner.Identity; webAppAuthConfig = await function.GetAuthenticationConfigAsync(cancellationToken).ConfigureAwait(false); } else { IWebApp webApp = appServiceManager.WebApps.GetByResourceGroup(this.ResourceGroupName, this.AppName); if (webApp == null) { app.Error.WriteLine($"Unable to find either a Function or Web App in resource group '{this.ResourceGroupName}' called '{this.AppName}'"); return(-1); } managedIdentity = webApp.Inner.Identity; webAppAuthConfig = await webApp.GetAuthenticationConfigAsync(cancellationToken).ConfigureAwait(false); } if (webAppAuthConfig.Inner.Enabled == true) { app.Out.WriteLine($"Default Easy Auth: {webAppAuthConfig.Inner.DefaultProvider}"); app.Out.WriteLine($" Client ID: {webAppAuthConfig.Inner.ClientId}"); } else { app.Out.WriteLine("Easy Auth not enabled"); } if (managedIdentity == null) { app.Out.WriteLine("No managed identity"); } else { app.Out.WriteLine("Managed identity:"); app.Out.WriteLine($" Type: {managedIdentity.Type}"); app.Out.WriteLine($" TenantId: {managedIdentity.TenantId}"); app.Out.WriteLine($" PrincipalId: {managedIdentity.PrincipalId}"); if (managedIdentity.UserAssignedIdentities != null) { foreach ((string id, ManagedServiceIdentityUserAssignedIdentitiesValue value) in managedIdentity.UserAssignedIdentities) { app.Out.WriteLine($" UserAssignedIdentity: Id = {id}, ClientId = {value.ClientId}, PrincipalId = {value.PrincipalId}"); } } } return(0); }
/** * Azure App Service sample for managing function apps. * - app service plan, function app * - Create 2 function apps under the same new app service plan * - domain * - Create a domain * - certificate * - Upload a self-signed wildcard certificate * - update both function apps to use the domain and the created wildcard SSL certificate */ public static void RunSample(IAzure azure) { string app1Name = SdkContext.RandomResourceName("webapp1-", 20); string app2Name = SdkContext.RandomResourceName("webapp2-", 20); string rgName = SdkContext.RandomResourceName("rgNEMV_", 24); string domainName = SdkContext.RandomResourceName("jsdkdemo-", 20) + ".com"; string certPassword = "******"; try { //============================================================ // Create a function app with a new app service plan Utilities.Log("Creating function app " + app1Name + "..."); IFunctionApp app1 = azure.AppServices.FunctionApps.Define(app1Name) .WithRegion(Region.USWest) .WithNewResourceGroup(rgName) .Create(); Utilities.Log("Created function app " + app1.Name); Utilities.Print(app1); //============================================================ // Create a second function app with the same app service plan Utilities.Log("Creating another function app " + app2Name + "..."); IFunctionApp app2 = azure.AppServices.FunctionApps.Define(app2Name) .WithRegion(Region.USWest) .WithExistingResourceGroup(rgName) .Create(); Utilities.Log("Created function app " + app2.Name); Utilities.Print(app2); //============================================================ // Purchase a domain (will be canceled for a full refund) Utilities.Log("Purchasing a domain " + domainName + "..."); IAppServiceDomain domain = azure.AppServices.AppServiceDomains.Define(domainName) .WithExistingResourceGroup(rgName) .DefineRegistrantContact() .WithFirstName("Jon") .WithLastName("Doe") .WithEmail("*****@*****.**") .WithAddressLine1("123 4th Ave") .WithCity("Redmond") .WithStateOrProvince("WA") .WithCountry(CountryISOCode.UnitedStates) .WithPostalCode("98052") .WithPhoneCountryCode(CountryPhoneCode.UnitedStates) .WithPhoneNumber("4258828080") .Attach() .WithDomainPrivacyEnabled(true) .WithAutoRenewEnabled(false) .Create(); Utilities.Log("Purchased domain " + domain.Name); Utilities.Print(domain); //============================================================ // Bind domain to function app 1 Utilities.Log("Binding http://" + app1Name + "." + domainName + " to function app " + app1Name + "..."); app1 = app1.Update() .DefineHostnameBinding() .WithAzureManagedDomain(domain) .WithSubDomain(app1Name) .WithDnsRecordType(CustomHostNameDnsRecordType.CName) .Attach() .Apply(); Utilities.Log("Finished binding http://" + app1Name + "." + domainName + " to function app " + app1Name); Utilities.Print(app1); //============================================================ // Create a self-singed SSL certificate var pfxPath = "webapp_" + nameof(ManageFunctionAppWithDomainSsl).ToLower() + ".pfx"; Utilities.Log("Creating a self-signed certificate " + pfxPath + "..."); Utilities.CreateCertificate(domainName, pfxPath, CertificatePassword); Utilities.Log("Created self-signed certificate " + pfxPath); //============================================================ // Bind domain to function app 2 and turn on wild card SSL for both Utilities.Log("Binding https://" + app1Name + "." + domainName + " to function app " + app1Name + "..."); app1 = app1.Update() .WithManagedHostnameBindings(domain, app1Name) .DefineSslBinding() .ForHostname(app1Name + "." + domainName) .WithPfxCertificateToUpload(Path.Combine(Utilities.ProjectPath, "Asset", pfxPath), certPassword) .WithSniBasedSsl() .Attach() .Apply(); Utilities.Log("Finished binding http://" + app1Name + "." + domainName + " to function app " + app1Name); Utilities.Print(app1); Utilities.Log("Binding https://" + app2Name + "." + domainName + " to function app " + app2Name + "..."); app2 = app2.Update() .WithManagedHostnameBindings(domain, app2Name) .DefineSslBinding() .ForHostname(app2Name + "." + domainName) .WithPfxCertificateToUpload(Path.Combine(Utilities.ProjectPath, "Asset", pfxPath), certPassword) .WithSniBasedSsl() .Attach() .Apply(); Utilities.Log("Finished binding http://" + app2Name + "." + domainName + " to function app " + app2Name); Utilities.Print(app2); } finally { try { Utilities.Log("Deleting Resource Group: " + rgName); azure.ResourceGroups.DeleteByName(rgName); Utilities.Log("Deleted Resource Group: " + rgName); } catch (NullReferenceException) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } catch (Exception g) { Utilities.Log(g); } } }
private static IStorageAccount getStorageAccount(IStorageManager storageManager, IFunctionApp functionApp, out IReadOnlyDictionary <string, IAppSetting> appSettings, out StorageSettings storageSettings) { appSettings = functionApp.GetAppSettings(); storageSettings = new StorageSettings(); string storageAccountConnectionString = appSettings[KeyAzureWebJobsStorage].Value; string[] segments = storageAccountConnectionString.Split(";"); foreach (string segment in segments) { if (segment.StartsWith(AccountNameSegment)) { storageSettings.AccountName = segment.Remove(0, AccountNameSegment.Length); } else if (segment.StartsWith(AccountKeySegment)) { storageSettings.AccountKey = segment.Remove(0, AccountKeySegment.Length); } } if (storageSettings.AccountName != null) { IEnumerable <IStorageAccount> storageAccounts = storageManager.StorageAccounts.List(); foreach (IStorageAccount storageAccount in storageAccounts) { if (storageAccount.Name == storageSettings.AccountName) { return(storageAccount); } } } throw new System.InvalidOperationException("storage account not found for connection string: " + storageAccountConnectionString); }
public FunctionDeploymentSlotImpl WithConfigurationFromFunctionApp(IFunctionApp app) { CopyConfigurations(((FunctionAppImpl)app).SiteConfig, app.AppSettings.Values.ToList(), app.ConnectionStrings.Values.ToList()); return(this); }
/** * 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 for 30 seconds */ public static void RunSample(IAzure azure) { // New resources string suffix = ".azurewebsites.net"; string appName = SdkContext.RandomResourceName("webapp1-", 20); string appUrl = appName + suffix; string rgName = SdkContext.RandomResourceName("rg1NEMV_", 24); try { //============================================================ // Create a function app with a new app service plan Utilities.Log("Creating function app " + appName + " in resource group " + rgName + "..."); IFunctionApp app = azure.AppServices.FunctionApps.Define(appName) .WithRegion(Region.USWest) .WithNewResourceGroup(rgName) .Create(); Utilities.Log("Created function app " + app.Name); Utilities.Print(app); //============================================================ // Deploy to app 1 through FTP Utilities.Log("Deploying a function app to " + appName + " through FTP..."); IPublishingProfile profile = app.GetPublishingProfile(); Utilities.UploadFileToFunctionApp(profile, Path.Combine(Utilities.ProjectPath, "Asset", "square-function-app", "host.json")); Utilities.UploadFileToFunctionApp(profile, Path.Combine(Utilities.ProjectPath, "Asset", "square-function-app", "square", "function.json"), "square/function.json"); Utilities.UploadFileToFunctionApp(profile, Path.Combine(Utilities.ProjectPath, "Asset", "square-function-app", "square", "index.js"), "square/index.js"); // sync triggers app.SyncTriggers(); Utilities.Log("Deployment square app to web app " + app.Name + " completed"); Utilities.Print(app); // warm up Utilities.Log("Warming up " + appUrl + "/api/square..."); Utilities.PostAddress("http://" + appUrl + "/api/square", "625"); SdkContext.DelayProvider.Delay(5000); Utilities.Log("CURLing " + appUrl + "/api/square..."); Utilities.Log(Utilities.PostAddress("http://" + appUrl + "/api/square", "625")); //============================================================ // Listen to logs synchronously for 30 seconds using (var stream = app.StreamApplicationLogs()) { var reader = new StreamReader(stream); Utilities.Log("Streaming logs from function app " + appName + "..."); string line = reader.ReadLine(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Task.Factory.StartNew(() => { Utilities.PostAddress("http://" + appUrl + "/api/square", "625"); SdkContext.DelayProvider.Delay(10000); Utilities.PostAddress("http://" + appUrl + "/api/square", "725"); SdkContext.DelayProvider.Delay(10000); Utilities.PostAddress("http://" + appUrl + "/api/square", "825"); }); while (line != null && stopWatch.ElapsedMilliseconds < 30000) { Utilities.Log(line); line = reader.ReadLine(); } } } finally { try { Utilities.Log("Deleting Resource Group: " + rgName); azure.ResourceGroups.DeleteByName(rgName); Utilities.Log("Deleted Resource Group: " + rgName); } catch (NullReferenceException) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } catch (Exception g) { Utilities.Log(g); } } }
/** * Azure App Service basic sample for managing function apps. * - Create 3 function apps under the same new app service plan and with the same storage account * - Deploy 1 & 2 via Git a function that calculates the square of a number * - Deploy 3 via Web Deploy * - Enable app level authentication for the 1st function app * - Verify the 1st function app can be accessed with the admin key * - Enable function level authentication for the 2nd function app * - Verify the 2nd function app can be accessed with the function key * - Enable function level authentication for the 3rd function app * - Verify the 3rd function app can be accessed with the function key */ public static void RunSample(IAzure azure) { // New resources string suffix = ".azurewebsites.net"; string app1Name = SdkContext.RandomResourceName("webapp1-", 20); string app2Name = SdkContext.RandomResourceName("webapp2-", 20); string app3Name = SdkContext.RandomResourceName("webapp3-", 20); string app1Url = app1Name + suffix; string app2Url = app2Name + suffix; string app3Url = app3Name + suffix; string rgName = SdkContext.RandomResourceName("rg1NEMV_", 24); try { //============================================================ // Create a function app with admin level auth Utilities.Log("Creating function app " + app1Name + " in resource group " + rgName + " with admin level auth..."); IFunctionApp app1 = azure.AppServices.FunctionApps.Define(app1Name) .WithRegion(Region.USWest) .WithNewResourceGroup(rgName) .WithLocalGitSourceControl() .Create(); Utilities.Log("Created function app " + app1.Name); Utilities.Print(app1); //============================================================ // Create a second function app with function level auth Utilities.Log("Creating another function app " + app2Name + " in resource group " + rgName + " with function level auth..."); IAppServicePlan plan = azure.AppServices.AppServicePlans.GetById(app1.AppServicePlanId); IFunctionApp app2 = azure.AppServices.FunctionApps.Define(app2Name) .WithExistingAppServicePlan(plan) .WithExistingResourceGroup(rgName) .WithExistingStorageAccount(app1.StorageAccount) .WithLocalGitSourceControl() .Create(); Utilities.Log("Created function app " + app2.Name); Utilities.Print(app2); //============================================================ // Create a third function app with function level auth Utilities.Log("Creating another function app " + app3Name + " in resource group " + rgName + " with function level auth..."); IFunctionApp app3 = azure.AppServices.FunctionApps.Define(app3Name) .WithExistingAppServicePlan(plan) .WithExistingResourceGroup(rgName) .WithExistingStorageAccount(app1.StorageAccount) .WithLocalGitSourceControl() .Create(); Utilities.Log("Created function app " + app3.Name); Utilities.Print(app3); //============================================================ // Deploy to app 1 through Git Utilities.Log("Deploying a local function app to " + app1Name + " through Git..."); IPublishingProfile profile = app1.GetPublishingProfile(); Utilities.DeployByGit(profile, "square-function-app-admin-auth"); // warm up Utilities.Log("Warming up " + app1Url + "/api/square..."); Utilities.PostAddress("http://" + app1Url + "/api/square", "625"); SdkContext.DelayProvider.Delay(5000); Utilities.Log("CURLing " + app1Url + "/api/square..."); Utilities.Log("Square of 625 is " + Utilities.PostAddress("http://" + app1Url + "/api/square?code=" + app1.GetMasterKey(), "625")); //============================================================ // Deploy to app 2 through Git Utilities.Log("Deploying a local function app to " + app2Name + " through Git..."); profile = app2.GetPublishingProfile(); Utilities.DeployByGit(profile, "square-function-app-function-auth"); Utilities.Log("Deployment to function app " + app2.Name + " completed"); Utilities.Print(app2); string functionKey = app2.ListFunctionKeys("square").Values.First(); // warm up Utilities.Log("Warming up " + app2Url + "/api/square..."); Utilities.PostAddress("http://" + app2Url + "/api/square", "725"); SdkContext.DelayProvider.Delay(5000); Utilities.Log("CURLing " + app2Url + "/api/square..."); Utilities.Log("Square of 725 is " + Utilities.PostAddress("http://" + app2Url + "/api/square?code=" + functionKey, "725")); Utilities.Log("Adding a new key to function app " + app2.Name + "..."); var newKey = app2.AddFunctionKey("square", "newkey", null); Utilities.Log("CURLing " + app2Url + "/api/square..."); Utilities.Log("Square of 825 is " + Utilities.PostAddress("http://" + app2Url + "/api/square?code=" + newKey.Value, "825")); //============================================================ // Deploy to app 3 through web deploy Utilities.Log("Deploying a local function app to " + app3Name + " throuh web deploy..."); app3.Deploy() .WithPackageUri("https://github.com/Azure/azure-libraries-for-net/raw/master/Samples/Asset/square-function-app-function-auth.zip") .WithExistingDeploymentsDeleted(false) .Execute(); Utilities.Log("Deployment to function app " + app3.Name + " completed"); Utilities.Log("Adding a new key to function app " + app3.Name + "..."); app3.AddFunctionKey("square", "newkey", "mysecretkey"); // warm up Utilities.Log("Warming up " + app3Url + "/api/square..."); Utilities.PostAddress("http://" + app3Url + "/api/square", "925"); SdkContext.DelayProvider.Delay(5000); Utilities.Log("CURLing " + app3Url + "/api/square..."); Utilities.Log("Square of 925 is " + Utilities.PostAddress("http://" + app3Url + "/api/square?code=mysecretkey", "925")); } finally { try { Utilities.Log("Deleting Resource Group: " + rgName); azure.ResourceGroups.DeleteByName(rgName); Utilities.Log("Deleted Resource Group: " + rgName); } catch (NullReferenceException) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } catch (Exception g) { Utilities.Log(g); } } }