Skip to content

Commit

Permalink
Customer bug fixes and enhancements to Activity History and deploymen…
Browse files Browse the repository at this point in the history
…t settings (#562)

* Remove aadUpdateMode from QA settings to prevent overwrite, #556

* Added Message attribute to model, #560

* Update Activity document on status change, #559
Added Message attribute for failure message to Activity document, #560

* missing status set

* added source to contextcreate

* added serialization settings for http/json results

* fixed syntax

* Update local build to 1.0, #561
  • Loading branch information
jkeane authored Mar 18, 2021
1 parent d02d039 commit b572702
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 46 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/appConfigSettingsQA.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
"SPDeltaDiscoverySchedule": "0 */30 * * * *",
"SPDiscoverQueue": "discoverqa",
"SPEvaluateQueue": "evaluateqa",
"SPUpdateQueue": "updateqa",
"aadUpdateMode": "ReportOnly"
"SPUpdateQueue": "updateqa"
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public void FunctionEvaluateTestCase1()

TestCase thisTestCase = testCaseCollection.TC1;

using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", "TEST", withTracking: true);

using var inputGenerator = new EvaluateInputGenerator(_config, _graphHelperSettings, testCaseCollection, thisTestCase);

Expand Down Expand Up @@ -323,7 +323,7 @@ public void FunctionEvaluateTestCase2()

TestCase thisTestCase = testCaseCollection.TC2;

using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", "TEST", withTracking: true);

using var inputGenerator = new EvaluateInputGenerator(_config, _graphHelperSettings,testCaseCollection, thisTestCase);

Expand Down Expand Up @@ -364,7 +364,7 @@ public void FunctionEvaluateTestCase2_2()

TestCase thisTestCase = testCaseCollection.TC2_2;

using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", "TEST", withTracking: true);

using var inputGenerator = new EvaluateInputGenerator(_config, _graphHelperSettings, testCaseCollection, thisTestCase);

Expand Down Expand Up @@ -405,7 +405,7 @@ public void FunctionEvaluateTestCase3()

TestCase thisTestCase = testCaseCollection.TC3;

using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", "TEST", withTracking: true);

using var inputGenerator = new EvaluateInputGenerator(_config, _graphHelperSettings, testCaseCollection, thisTestCase);

Expand Down Expand Up @@ -439,7 +439,7 @@ public void FunctionEvaluateTestCase3_2()

TestCase thisTestCase = testCaseCollection.TC3_2;

using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", "TEST", withTracking: true);

using var inputGenerator = new EvaluateInputGenerator(_config, _graphHelperSettings, testCaseCollection, thisTestCase);

Expand Down Expand Up @@ -480,7 +480,7 @@ public void FunctionEvaluateTestCase4()

TestCase thisTestCase = testCaseCollection.TC4;

using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", "TEST", withTracking: true);

using var inputGenerator = new EvaluateInputGenerator(_config, _graphHelperSettings, testCaseCollection, thisTestCase);

Expand Down Expand Up @@ -522,7 +522,7 @@ public void FunctionEvaluateTestCase5()

TestCase thisTestCase = testCaseCollection.TC5;

using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", "TEST", withTracking: true);

using var inputGenerator = new EvaluateInputGenerator(_config, _graphHelperSettings, testCaseCollection, thisTestCase);

Expand Down Expand Up @@ -564,7 +564,7 @@ public void FunctionEvaluateTestCase6()

TestCase thisTestCase = testCaseCollection.TC6;

using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - EVALUATE Test Case [{thisTestCase}] ", "TEST", withTracking: true);

using var inputGenerator = new EvaluateInputGenerator(_config, _graphHelperSettings, testCaseCollection, thisTestCase);

Expand Down Expand Up @@ -599,7 +599,7 @@ public void FunctionDiscoverTestCase1()

TestCase thisTestCase = testCaseCollection.TC1;

using var activityContext = _activityService.CreateContext($"Integration Test - DISCOVER Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - DISCOVER Test Case [{thisTestCase}] ", "TEST", withTracking: true);

GraphDeltaProcessorWrapper graphDeltaProcessorWrapper = _graphDeltaProcessorWrapperFactory.GetNewGraphDeltaProcessorWrapper();

Expand Down Expand Up @@ -648,7 +648,7 @@ public void FunctionDiscoverTestCase1_2()

TestCase thisTestCase = testCaseCollection.TC1_2;

using var activityContext = _activityService.CreateContext($"Integration Test - DISCOVER Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - DISCOVER Test Case [{thisTestCase}] ", "TEST", withTracking: true);

GraphDeltaProcessorWrapper graphDeltaProcessorWrapper = _graphDeltaProcessorWrapperFactory.GetNewGraphDeltaProcessorWrapper();

Expand Down Expand Up @@ -701,7 +701,7 @@ public void FunctionDiscoverTestCase2()

TestCase thisTestCase = testCaseCollection.TC2;

using var activityContext = _activityService.CreateContext($"Integration Test - DISCOVER Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - DISCOVER Test Case [{thisTestCase}] ", "TEST", withTracking: true);

GraphDeltaProcessorWrapper graphDeltaProcessorWrapper = _graphDeltaProcessorWrapperFactory.GetNewGraphDeltaProcessorWrapper();

Expand Down Expand Up @@ -759,7 +759,7 @@ public void FunctionDiscoverTestCase3()
using var inputGenerator = new DiscoverInputGenerator(_config, _graphHelperSettings, testCaseCollection, thisTestCase, graphDeltaProcessorWrapper.ConfigId, graphDeltaProcessorHelper);


using var activityContext = _activityService.CreateContext($"Integration Test - DISCOVER Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - DISCOVER Test Case [{thisTestCase}] ", "TEST", withTracking: true);

CloudQueueMessage cloudQueueMessage = new CloudQueueMessage(inputGenerator.GetTestMessageContent(DiscoveryMode.Deltas, "HTTP", activityContext));

Expand Down Expand Up @@ -801,7 +801,7 @@ public void FunctionUpdateTestCase1()

TestCase thisTestCase = testCaseCollection.TC1;

using var activityContext = _activityService.CreateContext($"Integration Test - UPDATE Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - UPDATE Test Case [{thisTestCase}] ", "TEST", withTracking: true);

using var inputGenerator = new UpdateInputGenerator(_config, _graphHelperSettings, testCaseCollection, thisTestCase);

Expand Down Expand Up @@ -841,7 +841,7 @@ public void FunctionUpdateTestCase2()

TestCase thisTestCase = testCaseCollection.TC2;

using var activityContext = _activityService.CreateContext($"Integration Test - UPDATE Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - UPDATE Test Case [{thisTestCase}] ", "TEST", withTracking: true);

using var inputGenerator = new UpdateInputGenerator(_config, _graphHelperSettings, testCaseCollection, thisTestCase);

Expand Down Expand Up @@ -877,7 +877,7 @@ public void FunctionUpdateTestCase3()

TestCase thisTestCase = testCaseCollection.TC3;

using var activityContext = _activityService.CreateContext($"Integration Test - UPDATE Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = _activityService.CreateContext($"Integration Test - UPDATE Test Case [{thisTestCase}] ", "TEST", withTracking: true);

using var inputGenerator = new UpdateInputGenerator(_config, _graphHelperSettings, testCaseCollection, thisTestCase);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected bool RunFullSeedDiscovery()

TestCase thisTestCase = testCaseCollection.TC1;

using var activityContext = GraphDeltaProcessorHelper.ActivityServiceInstance.CreateContext($"Nested execution Integration Test - Test Case [{thisTestCase}] ", withTracking: true);
using var activityContext = GraphDeltaProcessorHelper.ActivityServiceInstance.CreateContext($"Nested execution Integration Test - Test Case [{thisTestCase}] ", "TEST", withTracking: true);

GraphDeltaProcessorHelper.DeleteDynamicCreatedServicePrincipals = false;
string mainTestCaseConfigId = GraphDeltaProcessorHelper.MainTestCaseConfigId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Task<IEnumerable<ActivityHistory>> GetCorrelated(string correlationId)
throw new NotImplementedException();
}

public ActivityContext CreateContext(string name, string correlationId = null, bool withTracking = false)
public ActivityContext CreateContext(string name, string source, string correlationId = null, bool withTracking = false)
{
var now = DateTimeOffset.Now;

Expand All @@ -46,6 +46,7 @@ public ActivityContext CreateContext(string name, string correlationId = null, b
Created = now,
Name = name,
Status = ActivityHistoryStatus.Running,
CommandSource = source,
};

// we need the id of the run when we initiate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private async Task Evaluate(ServicePrincipalEvaluateTestData testData, AuditActi

var activityService = fixture.Host.Services.GetService<IActivityService>() as DefaultActivityService;
Assert.NotNull(activityService);
var context = activityService.CreateContext("Test Case");
var context = activityService.CreateContext("Test Case", "Evaluate TEST");

await processor.Evaluate(context, testData.Target).ConfigureAwait(false);

Expand Down Expand Up @@ -338,7 +338,7 @@ private async Task Discover(ServicePrincipalDiscoverTestData testData)

var activityService = fixture.Host.Services.GetService<IActivityService>() as DefaultActivityService;
Assert.NotNull(activityService);
var context = activityService.CreateContext("Test Case").WithCorrelationId(testData.Target.CorrelationId);
var context = activityService.CreateContext("Test Case", "Discover TEST").WithCorrelationId(testData.Target.CorrelationId);

await processor.DiscoverDeltas(context, testData.Target.DiscoveryMode == DiscoveryMode.FullSeed);

Expand Down
1 change: 1 addition & 0 deletions src/Automation/CSE.Automation/CSE.Automation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<BuildUTC>$([System.DateTime]::UtcNow.ToString("o"))</BuildUTC>
<NeutralLanguage>en-US</NeutralLanguage>
<Version>1.0-local</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
43 changes: 22 additions & 21 deletions src/Automation/CSE.Automation/GraphDeltaProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ internal class GraphDeltaProcessor
private readonly IActivityService activityService;
private readonly IServicePrincipalProcessor processor;
private readonly ILogger logger;
private readonly JsonSerializerSettings httpJsonSerializerSettings;

public GraphDeltaProcessor(VersionMetadata versionMetadata, IServiceProvider serviceProvider, IActivityService activityService, IServicePrincipalProcessor processor, ILogger<GraphDeltaProcessor> logger)
{
this.versionMetadata = versionMetadata;
this.activityService = activityService;
this.processor = processor;
this.logger = logger;
this.httpJsonSerializerSettings = new JsonSerializerSettings()
{
Formatting = Formatting.None,
NullValueHandling = NullValueHandling.Ignore,
};

ValidateServices(serviceProvider);
}
Expand Down Expand Up @@ -78,7 +84,7 @@ public async Task<IActionResult> RequestDiscovery([HttpTrigger(AuthorizationLeve

return hasRedirect
? new RedirectResult($"{uriBuilder.Uri}")
: (IActionResult)new JsonResult(result);
: (IActionResult)new JsonResult(result, httpJsonSerializerSettings);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -135,24 +141,21 @@ public async Task Discover([QueueTrigger(Constants.DiscoverQueueAppSetting)] Clo
}

var operation = command.DiscoveryMode.Description();
using var context = activityService.CreateContext(operation, correlationId: command.CorrelationId, withTracking: true);
using var context = activityService.CreateContext(operation, command.Source, correlationId: command.CorrelationId, withTracking: true);

try
{
log.LogDebug($"Executing Discover QueueTrigger Function - [{context.CorrelationId}/{context.Activity.Id}]");

context.Activity.CommandSource = command.Source;
context.WithProcessorLock(processor);
}
catch (Exception ex)
{
if (context != null)
{
context.Activity.Status = ActivityHistoryStatus.Failed;
}

ex.Data["activityContext"] = context;
log.LogError(ex, Resources.LockConflictMessage);

context?.AsStatus(ActivityHistoryStatus.Failed, Resources.LockConflictMessage);

return; // this will delete the message, we don't want to retry
}

Expand All @@ -164,10 +167,10 @@ await processor
}
catch (Exception ex)
{
context.Activity.Status = ActivityHistoryStatus.Failed;

ex.Data["activityContext"] = context;
log.LogError(ex, Resources.ServicePrincipalDiscoverException);

context?.AsStatus(ActivityHistoryStatus.Failed, Resources.ServicePrincipalDiscoverException);
}
}

Expand Down Expand Up @@ -199,8 +202,7 @@ public async Task Evaluate([QueueTrigger(Constants.EvaluateQueueAppSetting)] Clo
ActivityContext context = null;
try
{
context = activityService.CreateContext("Evaluate Service Principal", command.CorrelationId);
context.Activity.CommandSource = "QUEUE";
context = activityService.CreateContext("Evaluate Service Principal", "QUEUE", command.CorrelationId);

await processor.Evaluate(context, command.Model).ConfigureAwait(false);

Expand Down Expand Up @@ -253,8 +255,7 @@ public async Task UpdateAAD([QueueTrigger(Constants.UpdateQueueAppSetting)]Cloud
ActivityContext context = null;
try
{
context = activityService.CreateContext("Update Service Principal", command.CorrelationId);
context.Activity.CommandSource = "QUEUE";
context = activityService.CreateContext("Update Service Principal", "QUEUE", command.CorrelationId);

var message = JsonConvert.DeserializeObject<QueueMessage<ServicePrincipalUpdateCommand>>(msg.AsString);

Expand Down Expand Up @@ -293,7 +294,7 @@ public async Task<IActionResult> Activities([HttpTrigger(AuthorizationLevel.Func
var activityId = req.Query["activityId"];
var correlationId = req.Query["correlationId"];

using var context = activityService.CreateContext("Activities", withTracking: false);
using var context = activityService.CreateContext("Activities", "HTTP", withTracking: false);
try
{
log.LogDebug("Executing ActivityStatus HttpTrigger Function");
Expand All @@ -313,7 +314,7 @@ public async Task<IActionResult> Activities([HttpTrigger(AuthorizationLevel.Func
context.ElapsedTime,
};

return new JsonResult(result);
return new JsonResult(result, httpJsonSerializerSettings);
}
catch (Exception ex)
{
Expand All @@ -337,7 +338,7 @@ public async Task<IActionResult> Activities([HttpTrigger(AuthorizationLevel.Func
public Task<IActionResult> Version([HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req, ILogger log)
{
log.LogInformation(this.versionMetadata.ProductVersion);
return Task.FromResult((IActionResult)new JsonResult(this.versionMetadata));
return Task.FromResult((IActionResult)new JsonResult(this.versionMetadata, httpJsonSerializerSettings));
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Ensure graceful return under all trappable error conditions.")]
Expand All @@ -347,9 +348,8 @@ private async Task<dynamic> CommandDiscovery(DiscoveryMode discoveryMode, string

try
{
context = activityService.CreateContext($"{discoveryMode.Description()} Request", withTracking: true);
context = activityService.CreateContext($"{discoveryMode.Description()} Request", source, withTracking: true);

context.Activity.CommandSource = source;
await processor.RequestDiscovery(context, discoveryMode, source).ConfigureAwait(false);
var result = new
{
Expand All @@ -365,14 +365,15 @@ private async Task<dynamic> CommandDiscovery(DiscoveryMode discoveryMode, string
}
catch (Exception ex)
{
var message = $"Failed to request Discovery {discoveryMode}";

if (context != null)
{
context.Activity.Status = ActivityHistoryStatus.Failed;

context.Activity.Message = message;
ex.Data["activityContext"] = context;
}

var message = $"Failed to request Discovery {discoveryMode}";
log.LogError(ex, message);

throw;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ internal interface IActivityService
Task<ActivityHistory> Put(ActivityHistory document);
Task<ActivityHistory> Get(string id);
Task<IEnumerable<ActivityHistory>> GetCorrelated(string correlationId);
ActivityContext CreateContext(string name, string correlationId = null, bool withTracking = false);
ActivityContext CreateContext(string name, string source, string correlationId = null, bool withTracking = false);
}
}
Loading

0 comments on commit b572702

Please sign in to comment.