Skip to content

Commit

Permalink
Add a command to list functions in a function app. Closes #187 (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmelsayed authored Sep 8, 2017
1 parent 73fbde7 commit a920cf6
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.Functions.Cli.Arm;
using Colors.Net;
using static Azure.Functions.Cli.Common.OutputTheme;
using Azure.Functions.Cli.Helpers;
using System.Net.Http;
using Azure.Functions.Cli.Common;
using Azure.Functions.Cli.Interfaces;
using Azure.Functions.Cli.Arm.Models;

namespace Azure.Functions.Cli.Actions.AzureActions
{
[Action(Name = "list-functions", Context = Context.Azure, SubContext = Context.FunctionApp, HelpText = "List functions in a given function app on azure.")]
internal class ListFunctionsActions : BaseFunctionAppAction
{
private readonly ISettings _settings;

public ListFunctionsActions(IArmManager armManager, ISettings settings) : base(armManager)
{
_settings = settings;
}

public override async Task RunAsync()
{
var functionApp = await _armManager.GetFunctionAppAsync(FunctionAppName);
if (functionApp != null)
{
await RetryHelper.Retry(async () =>
{
using (var client = new HttpClient() { BaseAddress = new Uri($"https://{functionApp.ScmUri}") })
{
client.DefaultRequestHeaders.Authorization = await _armManager.GetAuthenticationHeader(_settings.CurrentSubscription);

var response = await client.GetAsync(new Uri("api/functions", UriKind.Relative));

if (!response.IsSuccessStatusCode)
{
throw new CliException($"Error trying to retrieve list of functions ({response.StatusCode}).");
}

var functions = await response.Content.ReadAsAsync<IEnumerable<FunctionInfo>>();

ColoredConsole.WriteLine(TitleColor($"Functions in {FunctionAppName}:"));
foreach (var function in functions)
{
var trigger = function
.Config?["bindings"]
?.FirstOrDefault(o => o["type"]?.ToString().IndexOf("Trigger", StringComparison.OrdinalIgnoreCase) != -1)
?["type"];

trigger = trigger ?? "No Trigger Found";

ColoredConsole.WriteLine($" {function.Name} - [{VerboseColor(trigger.ToString())}]");
}
}
}, 2);
}
else
{
ColoredConsole.Error.WriteLine(ErrorColor($"Can't find function app by name {FunctionAppName}"));
}
}
}
}
41 changes: 41 additions & 0 deletions src/Azure.Functions.Cli/Arm/Models/FunctionInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace Azure.Functions.Cli.Arm.Models
{
// https://github.com/projectkudu/kudu/blob/master/Kudu.Contracts/Functions/FunctionEnvelope.cs
internal class FunctionInfo
{
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }

[JsonProperty(PropertyName = "function_app_id")]
public string FunctionAppId { get; set; }

[JsonProperty(PropertyName = "script_root_path_href")]
public Uri ScriptRootPathHref { get; set; }

[JsonProperty(PropertyName = "script_href")]
public Uri ScriptHref { get; set; }

[JsonProperty(PropertyName = "config_href")]
public Uri ConfigHref { get; set; }

[JsonProperty(PropertyName = "secrets_file_href")]
public Uri SecretsFileHref { get; set; }

[JsonProperty(PropertyName = "href")]
public Uri Href { get; set; }

[JsonProperty(PropertyName = "config")]
public JObject Config { get; set; }

[JsonProperty(PropertyName = "files")]
public IDictionary<string, string> Files { get; set; }

[JsonProperty(PropertyName = "test_data")]
public string TestData { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/Azure.Functions.Cli/Azure.Functions.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@
<Compile Include="Actions\AzureActions\GetPublishUsernameAction.cs" />
<Compile Include="Actions\AzureActions\ListAzureAccountsAction.cs" />
<Compile Include="Actions\AzureActions\ListFunctionAppsAction.cs" />
<Compile Include="Actions\AzureActions\ListFunctionsActions.cs" />
<Compile Include="Actions\AzureActions\ListStorageAction.cs" />
<Compile Include="Actions\AzureActions\LoginAction.cs" />
<Compile Include="Actions\AzureActions\LogoutAction.cs" />
Expand Down Expand Up @@ -619,6 +620,7 @@
<Compile Include="Arm\Models\ArmWebsitePublishingCredentials.cs" />
<Compile Include="Arm\Models\ArmWrapper.cs" />
<Compile Include="Arm\Models\BaseResource.cs" />
<Compile Include="Arm\Models\FunctionInfo.cs" />
<Compile Include="Arm\Models\ResourceGroup.cs" />
<Compile Include="Arm\Models\Site.cs" />
<Compile Include="Arm\Models\StorageAccount.cs" />
Expand Down

0 comments on commit a920cf6

Please sign in to comment.