Skip to content

Commit

Permalink
Updated to latest API and breaking changes in Azure Function Runtime 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliov committed Oct 22, 2018
1 parent 39cced4 commit 67f57d8
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 48 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ Logon credentials are stored locally and expire after 2 hours.

The PAT is stored in the Azure Function settings: **whoever has access to the Resource Group can read it!**

The Service Principal must have Contributor permission to the Azure Subscription.
In alternative, pre-create the `aggregator-` Resource Group in Azure and give the service account Contributor permission to the Resource Group.
The `instance` parameter prefixes `aggregator-` to identify the Resource Group.

## Usage

Download and unzip the latest CLI.zip file from [Releases](https://github.com/tfsaggregator/aggregator-cli/releases).
Expand Down
Binary file added doc/log-streaming-from-azure-portal.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/aggregator-cli/Rules/run.csx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using aggregator;

public static async Task<object> Run(HttpRequestMessage req, TraceWriter log, ExecutionContext context)
public static async Task<object> Run(HttpRequestMessage req, ILogger logger, ExecutionContext context)
{
var handler = new AzureFunctionHandler(log, context);
var handler = new AzureFunctionHandler(logger, context);
return await handler.Run(req);
}
6 changes: 3 additions & 3 deletions src/aggregator-cli/aggregator-cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.14.0" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager.Fluent" Version="1.14.0" />
<PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.16.1" />
<PackageReference Include="Microsoft.Azure.Management.ResourceManager.Fluent" Version="1.16.1" />
<PackageReference Include="Microsoft.TeamFoundationServer.Client" Version="16.137.0-preview" />
<PackageReference Include="Microsoft.VisualStudio.Services.Client" Version="16.137.0-preview" />
<PackageReference Include="Microsoft.VisualStudio.Services.InteractiveClient" Version="16.137.0-preview" />
<PackageReference Include="Microsoft.VisualStudio.Services.ServiceHooks.WebApi" Version="16.137.0-preview" />
<PackageReference Include="CommandLineParser" Version="2.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="Octokit" Version="0.31.0" />
<PackageReference Include="Octokit" Version="0.32.0" />
<PackageReference Include="semver" Version="2.0.4" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/aggregator-core/aggregator-core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.14.0" />
<PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.16.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
</ItemGroup>

Expand Down
47 changes: 33 additions & 14 deletions src/aggregator-function/AzureFunctionHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System;
using System.Net;
Expand All @@ -14,34 +15,44 @@ namespace aggregator
/// </summary>
public class AzureFunctionHandler
{
private readonly TraceWriter log;
private readonly Microsoft.Extensions.Logging.ILogger log;
private readonly ExecutionContext context;

public AzureFunctionHandler(TraceWriter log, ExecutionContext context)
public AzureFunctionHandler(Microsoft.Extensions.Logging.ILogger logger, ExecutionContext context)
{
this.log = log;
this.log = logger;
this.context = context;
}

public async Task<HttpResponseMessage> Run(HttpRequestMessage req)
{
log.Verbose($"Context: {context.InvocationId} {context.FunctionName} {context.FunctionDirectory} {context.FunctionAppDirectory}");
log.LogDebug($"Context: {context.InvocationId} {context.FunctionName} {context.FunctionDirectory} {context.FunctionAppDirectory}");

// TODO check expected version
string aggregatorVersion = null;

try
{
string rule = context.FunctionName;
log.Info($"Welcome to {rule}");
log.LogInformation($"Welcome to {rule}");
}
catch (Exception ex)
{
log.Warning($"Failed parsing headers: {ex.Message}");
log.LogWarning($"Failed parsing request headers: {ex.Message}");
}

// Get request body
string jsonContent = await req.Content.ReadAsStringAsync();
if (string.IsNullOrWhiteSpace(jsonContent))
{
log.LogWarning($"Failed parsing request body: empty");

var resp = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("Request body is empty")
};
return resp;
}
dynamic data = JsonConvert.DeserializeObject(jsonContent);
string eventType = data.eventType;

Expand All @@ -62,23 +73,31 @@ public async Task<HttpResponseMessage> Run(HttpRequestMessage req)
.Build();
var configuration = AggregatorConfiguration.Read(config);

var logger = new TraceWriterLogger(log);
var logger = new ForwarderLogger(log);
var wrapper = new RuleWrapper(configuration, logger, context.FunctionName, context.FunctionDirectory);
try
{
string execResult = await wrapper.Execute(aggregatorVersion, data);

log.Info($"Returning {execResult}");

var resp = new HttpResponseMessage(HttpStatusCode.OK)
if (string.IsNullOrEmpty(execResult))
{
Content = new StringContent(execResult)
};
return resp;
var resp = new HttpResponseMessage(HttpStatusCode.OK);
return resp;
}
else
{
log.LogInformation($"Returning {execResult}");

var resp = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(execResult)
};
return resp;
}
}
catch (Exception ex)
{
log.Warning($"Rule failed: {ex.Message}");
log.LogWarning($"Rule failed: {ex.Message}");

var resp = new HttpResponseMessage(HttpStatusCode.NotImplemented)
{
Expand Down
4 changes: 2 additions & 2 deletions src/aggregator-function/Engine/EngineContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace aggregator.Engine
{
public class EngineContext
{
internal EngineContext(WorkItemTrackingHttpClient client, ILogger logger)
internal EngineContext(WorkItemTrackingHttpClient client, IAggregatorLogger logger)
{
Client = client;
Logger = logger;
Tracker = new Tracker();
}

internal WorkItemTrackingHttpClient Client { get; }
internal ILogger Logger { get; }
internal IAggregatorLogger Logger { get; }
internal Tracker Tracker { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace aggregator
{
internal class TraceWriterLogger : ILogger
internal class ForwarderLogger : IAggregatorLogger
{
private TraceWriter log;
private Microsoft.Extensions.Logging.ILogger log;

public TraceWriterLogger(TraceWriter log)
public ForwarderLogger(Microsoft.Extensions.Logging.ILogger log)
{
this.log = log;
}

public void WriteError(string message)
{
log.Error(message);
log.LogError(message);
}

public void WriteInfo(string message)
{
log.Info(message);
log.LogInformation(message);
}

public void WriteVerbose(string message)
{
log.Verbose(message);
log.LogDebug(message);
}

public void WriteWarning(string message)
{
log.Warning(message);
log.LogWarning(message);
}
}
}
2 changes: 1 addition & 1 deletion src/aggregator-function/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace aggregator
{
interface ILogger
interface IAggregatorLogger
{
void WriteVerbose(string message);

Expand Down
11 changes: 8 additions & 3 deletions src/aggregator-function/RuleWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ namespace aggregator
internal class RuleWrapper
{
private readonly AggregatorConfiguration configuration;
private readonly ILogger logger;
private readonly IAggregatorLogger logger;
private readonly string ruleName;
private readonly string functionDirectory;

public RuleWrapper(AggregatorConfiguration configuration, ILogger logger, string ruleName, string functionDirectory)
public RuleWrapper(AggregatorConfiguration configuration, IAggregatorLogger logger, string ruleName, string functionDirectory)
{
this.configuration = configuration;
this.logger = logger;
Expand Down Expand Up @@ -98,10 +98,15 @@ internal async Task<string> Execute(string aggregatorVersion, dynamic data)
if (result.Exception != null)
{
logger.WriteError($"Rule failed with {result.Exception}");
} else
}
else if(result.ReturnValue != null)
{
logger.WriteInfo($"Rule succeeded with {result.ReturnValue}");
}
else
{
logger.WriteInfo($"Rule succeeded, no return value");
}

logger.WriteVerbose($"Post-execution, save all changes...");
store.SaveChanges();
Expand Down
2 changes: 1 addition & 1 deletion src/aggregator-function/aggregator-function.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Scripting" Version="2.9.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.14" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.22" />
<PackageReference Include="Microsoft.TeamFoundationServer.Client" Version="16.137.0-preview" />
<PackageReference Include="Microsoft.VisualStudio.Services.Client" Version="16.137.0-preview" />
<PackageReference Include="Microsoft.VisualStudio.Services.ExtensionManagement.WebApi" Version="16.137.0-preview" />
Expand Down
2 changes: 1 addition & 1 deletion src/aggregator-function/aggregator-manifest.ini
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.2.3
version=0.2.4
13 changes: 4 additions & 9 deletions src/aggregator-function/host.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
{
"logger": {
"categoryFilter": {
"defaultLevel": "Trace",
"categoryLevels": {
"Worker": "Trace"
}
},
"fileLoggingMode": "always"
}
"version": "2.0",
"logging": {
"fileLoggingMode": "always"
}
}
4 changes: 2 additions & 2 deletions src/aggregator-function/test/run.csx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using aggregator;

public static async Task<object> Run(HttpRequestMessage req, TraceWriter log, ExecutionContext context)
public static async Task<object> Run(HttpRequestMessage req, ILogger logger, ExecutionContext context)
{
var handler = new AzureFunctionHandler(log, context);
var handler = new AzureFunctionHandler(logger, context);
return await handler.Run(req);
}
2 changes: 1 addition & 1 deletion src/unittests-cli/unittests-cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
Expand Down
2 changes: 1 addition & 1 deletion src/unittests-core/unittests-core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
Expand Down

0 comments on commit 67f57d8

Please sign in to comment.