Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save WikkidEdd/daff060aa1b36fdab83213d96f6a557c to your computer and use it in GitHub Desktop.
Save WikkidEdd/daff060aa1b36fdab83213d96f6a557c to your computer and use it in GitHub Desktop.
Quick and dirty extension method to fix up incorrectly parsed ContainerAppJobExecutionResource. Just call Fixup() on the Response<ContainerAppJobExecutionResource> object
using Azure.ResourceManager.AppContainers;
using Azure;
using System.Text.Json;
using System.Threading.Tasks;
using Azure.ResourceManager.AppContainers.Models;
using System;
using System.Reflection;
// This is a fix up due to a bug in the original parsing of ContainerAppJobExecutionData
// More detail on the bug here https://github.com/Azure/azure-sdk-for-net/issues/38385
// It's basically just reparsing the raw data to match the format that is actually returned from the service.
public static class ContainerAppJobExecutionResourceFixUp
{
static public async Task FixUpAsync(this Response<ContainerAppJobExecutionResource> response)
{
var containerAppJobExecutionData = response.Value.Data;
response.GetRawResponse().ContentStream.Position = 0;
using JsonDocument jsonDocument = await JsonDocument.ParseAsync(response.GetRawResponse().ContentStream, default(JsonDocumentOptions)).ConfigureAwait(continueOnCapturedContext: false);
JsonElement element = jsonDocument.RootElement;
JobExecutionRunningState? status = null;
DateTimeOffset? startTime = null;
DateTimeOffset? endTime = null;
foreach (JsonProperty item in element.EnumerateObject())
{
if (!item.NameEquals("properties") || item.Value.ValueKind == JsonValueKind.Null)
{
continue;
}
foreach (JsonProperty item3 in item.Value.EnumerateObject())
{
if (item3.NameEquals("status"))
{
if (item3.Value.ValueKind != JsonValueKind.Null)
{
status = new JobExecutionRunningState(item3.Value.GetString());
}
}
else if (item3.NameEquals("startTime"))
{
if (item3.Value.ValueKind != JsonValueKind.Null)
{
JsonElement element2 = item3.Value;
startTime = element2.GetDateTimeOffset();
}
}
else if (item3.NameEquals("endTime"))
{
if (item3.Value.ValueKind != JsonValueKind.Null)
{
JsonElement element2 = item3.Value;
endTime = element2.GetDateTimeOffset();
}
}
}
}
containerAppJobExecutionData.SetProperty("Status", status);
containerAppJobExecutionData.SetProperty("StartOn", startTime);
containerAppJobExecutionData.SetProperty("EndOn", endTime);
}
static private void SetProperty(this object myObject, string propertyName, object value)
{
myObject.GetType().GetField($"<{propertyName}>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(myObject, value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment