-
Notifications
You must be signed in to change notification settings - Fork 249
Addition & Deserialization of Azure Log Analytics Properties #2727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
16
commits into
main
Choose a base branch
from
copilot/fix-2726
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d3b408c
Initial plan for issue
Copilot a264c40
Complete: Add Azure Log Analytics properties support to DAB
Copilot 54387e7
Merge branch 'main' into copilot/fix-2726
RubenCerna2079 505ed04
Fix Unit Tests
RubenCerna2079 b7d5e91
Fix Unit Tests
RubenCerna2079 f63211f
Fix Unit Tests
RubenCerna2079 de6138f
Changes to properties and tests
RubenCerna2079 6ecdc63
Change tests
RubenCerna2079 43e59ef
Fix Unit Tests
RubenCerna2079 5dac9fc
Update AzureLogAnalyticsOptions to follow established pattern with Us…
Copilot 0883122
Add JsonConverterFactory for AzureLogAnalyticsOptions to use UserProv…
Copilot 739130b
Fix unit tests
RubenCerna2079 d7de182
Merge branch 'main' into copilot/fix-2726
RubenCerna2079 e6c86e3
Changes based on comments
RubenCerna2079 a4ae0c0
Changes based on comments
RubenCerna2079 530423a
Merge branch 'main' into copilot/fix-2726
RubenCerna2079 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
src/Config/Converters/AzureLogAnalyticsAuthOptionsConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Azure.DataApiBuilder.Config.ObjectModel; | ||
|
||
namespace Azure.DataApiBuilder.Config.Converters; | ||
|
||
internal class AzureLogAnalyticsAuthOptionsConverter : JsonConverter<AzureLogAnalyticsAuthOptions> | ||
{ | ||
// Determines whether to replace environment variable with its | ||
// value or not while deserializing. | ||
private bool _replaceEnvVar; | ||
|
||
/// <param name="replaceEnvVar">Whether to replace environment variable with its | ||
/// value or not while deserializing.</param> | ||
public AzureLogAnalyticsAuthOptionsConverter(bool replaceEnvVar) | ||
{ | ||
_replaceEnvVar = replaceEnvVar; | ||
} | ||
|
||
/// <summary> | ||
/// Defines how DAB reads Azure Log Analytics Auth options and defines which values are | ||
/// used to instantiate AzureLogAnalyticsAuthOptions. | ||
/// Uses default deserialize. | ||
/// </summary> | ||
/// <exception cref="JsonException">Thrown when improperly formatted Azure Log Analytics Auth options are provided.</exception> | ||
public override AzureLogAnalyticsAuthOptions? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType is JsonTokenType.StartObject) | ||
{ | ||
AzureLogAnalyticsAuthOptions? authOptions = new(); | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndObject) | ||
{ | ||
break; | ||
} | ||
|
||
string? propertyName = reader.GetString(); | ||
|
||
reader.Read(); | ||
switch (propertyName) | ||
{ | ||
case "workspace-id": | ||
if (reader.TokenType is JsonTokenType.String) | ||
{ | ||
string? workspaceId = reader.DeserializeString(_replaceEnvVar); | ||
if (workspaceId is null) | ||
{ | ||
throw new JsonException($"Unsuported null value entered for the property workspace-id"); | ||
} | ||
|
||
authOptions = authOptions with { WorkspaceId = workspaceId }; | ||
} | ||
else | ||
{ | ||
throw new JsonException($"Unexpected type of value entered for workspace-id: {reader.TokenType}"); | ||
} | ||
|
||
break; | ||
|
||
case "dcr-immutable-id": | ||
if (reader.TokenType is JsonTokenType.String) | ||
{ | ||
string? dcrImmutableId = reader.DeserializeString(_replaceEnvVar); | ||
if (dcrImmutableId is null) | ||
{ | ||
throw new JsonException($"Unsuported null value entered for the property dcr-immutable-id"); | ||
} | ||
|
||
authOptions = authOptions with { DcrImmutableId = dcrImmutableId }; | ||
} | ||
else | ||
{ | ||
throw new JsonException($"Unexpected type of value entered for dcr-immutable-id: {reader.TokenType}"); | ||
} | ||
|
||
break; | ||
|
||
case "dce-endpoint": | ||
if (reader.TokenType is JsonTokenType.String) | ||
{ | ||
string? dceEndpoint = reader.DeserializeString(_replaceEnvVar); | ||
if (dceEndpoint is null) | ||
{ | ||
throw new JsonException($"Unsuported null value entered for the property dce-endpoint"); | ||
} | ||
|
||
authOptions = authOptions with { DceEndpoint = dceEndpoint }; | ||
} | ||
else | ||
{ | ||
throw new JsonException($"Unexpected type of value entered for dce-endpoint: {reader.TokenType}"); | ||
} | ||
|
||
break; | ||
|
||
default: | ||
throw new JsonException($"Unexpected property {propertyName}"); | ||
} | ||
} | ||
|
||
return authOptions; | ||
} | ||
|
||
throw new JsonException("Failed to read the Azure Log Analytics Auth Options"); | ||
} | ||
|
||
/// <summary> | ||
/// When writing the AzureLogAnalyticsAuthOptions back to a JSON file, only write the properties | ||
/// if they are user provided. This avoids polluting the written JSON file with properties | ||
/// the user most likely omitted when writing the original DAB runtime config file. | ||
/// This Write operation is only used when a RuntimeConfig object is serialized to JSON. | ||
/// </summary> | ||
public override void Write(Utf8JsonWriter writer, AzureLogAnalyticsAuthOptions value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
|
||
if (value?.UserProvidedWorkspaceId is true) | ||
{ | ||
writer.WritePropertyName("workspace-id"); | ||
JsonSerializer.Serialize(writer, value.WorkspaceId, options); | ||
} | ||
|
||
if (value?.UserProvidedDcrImmutableId is true) | ||
{ | ||
writer.WritePropertyName("drc-immutable-id"); | ||
JsonSerializer.Serialize(writer, value.DcrImmutableId, options); | ||
} | ||
|
||
if (value?.UserProvidedDceEndpoint is true) | ||
{ | ||
writer.WritePropertyName("dce-endpoint"); | ||
JsonSerializer.Serialize(writer, value.DceEndpoint, options); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
} |
187 changes: 187 additions & 0 deletions
187
src/Config/Converters/AzureLogAnalyticsOptionsConverterFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using Azure.DataApiBuilder.Config.ObjectModel; | ||
|
||
namespace Azure.DataApiBuilder.Config.Converters; | ||
|
||
/// <summary> | ||
/// Defines how DAB reads and writes Azure Log Analytics options. | ||
/// </summary> | ||
internal class AzureLogAnalyticsOptionsConverterFactory : JsonConverterFactory | ||
{ | ||
// Determines whether to replace environment variable with its | ||
// value or not while deserializing. | ||
private bool _replaceEnvVar; | ||
|
||
/// <inheritdoc/> | ||
public override bool CanConvert(Type typeToConvert) | ||
{ | ||
return typeToConvert.IsAssignableTo(typeof(AzureLogAnalyticsOptions)); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
return new AzureLogAnalyticsOptionsConverter(_replaceEnvVar); | ||
} | ||
|
||
/// <param name="replaceEnvVar">Whether to replace environment variable with its | ||
/// value or not while deserializing.</param> | ||
internal AzureLogAnalyticsOptionsConverterFactory(bool replaceEnvVar) | ||
{ | ||
_replaceEnvVar = replaceEnvVar; | ||
} | ||
|
||
private class AzureLogAnalyticsOptionsConverter : JsonConverter<AzureLogAnalyticsOptions> | ||
{ | ||
// Determines whether to replace environment variable with its | ||
// value or not while deserializing. | ||
private bool _replaceEnvVar; | ||
|
||
/// <param name="replaceEnvVar">Whether to replace environment variable with its | ||
/// value or not while deserializing.</param> | ||
internal AzureLogAnalyticsOptionsConverter(bool replaceEnvVar) | ||
{ | ||
_replaceEnvVar = replaceEnvVar; | ||
} | ||
|
||
/// <summary> | ||
/// Defines how DAB reads Azure Log Analytics options and defines which values are | ||
/// used to instantiate AzureLogAnalyticsOptions. | ||
/// Uses default deserialize. | ||
/// </summary> | ||
/// <exception cref="JsonException">Thrown when improperly formatted Azure Log Analytics options are provided.</exception> | ||
public override AzureLogAnalyticsOptions? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
RubenCerna2079 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (reader.TokenType is JsonTokenType.StartObject) | ||
{ | ||
AzureLogAnalyticsOptions azureLogAnalyticsOptions = new(); | ||
AzureLogAnalyticsAuthOptionsConverter authOptionsConverter = new(_replaceEnvVar); | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndObject) | ||
{ | ||
break; | ||
} | ||
|
||
string? propertyName = reader.GetString(); | ||
|
||
reader.Read(); | ||
switch (propertyName) | ||
{ | ||
case "enabled": | ||
if (reader.TokenType is JsonTokenType.True || reader.TokenType is JsonTokenType.False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we give the default value in the case of a null here, and for log-type and flush-interval? |
||
{ | ||
azureLogAnalyticsOptions = azureLogAnalyticsOptions with { Enabled = reader.GetBoolean() }; | ||
} | ||
else | ||
{ | ||
throw new JsonException($"Unsupported value entered for the property 'enabled': {reader.TokenType}"); | ||
} | ||
|
||
break; | ||
|
||
case "auth": | ||
azureLogAnalyticsOptions = azureLogAnalyticsOptions with { Auth = authOptionsConverter.Read(ref reader, typeToConvert, options) }; | ||
break; | ||
|
||
case "log-type": | ||
if (reader.TokenType is JsonTokenType.String) | ||
{ | ||
string? logType = reader.DeserializeString(_replaceEnvVar); | ||
if (logType is null) | ||
{ | ||
logType = "DabLogs"; | ||
} | ||
|
||
azureLogAnalyticsOptions = azureLogAnalyticsOptions with { LogType = logType }; | ||
} | ||
else | ||
{ | ||
throw new JsonException($"Unexpected type of value entered for log-type: {reader.TokenType}"); | ||
} | ||
|
||
break; | ||
|
||
case "flush-interval-seconds": | ||
if (reader.TokenType is JsonTokenType.Number) | ||
{ | ||
int flushIntSec; | ||
try | ||
{ | ||
flushIntSec = reader.GetInt32(); | ||
} | ||
catch (FormatException) | ||
{ | ||
throw new JsonException($"The JSON token value is of the incorrect numeric format."); | ||
} | ||
|
||
if (flushIntSec <= 0) | ||
{ | ||
throw new JsonException($"Invalid flush-interval-seconds: {flushIntSec}. Specify a number > 0."); | ||
} | ||
|
||
azureLogAnalyticsOptions = azureLogAnalyticsOptions with { FlushIntervalSeconds = flushIntSec }; | ||
} | ||
else | ||
{ | ||
throw new JsonException($"Unsupported value entered for flush-interval-seconds: {reader.TokenType}"); | ||
} | ||
|
||
break; | ||
|
||
default: | ||
throw new JsonException($"Unexpected property {propertyName}"); | ||
} | ||
} | ||
|
||
return azureLogAnalyticsOptions; | ||
} | ||
|
||
throw new JsonException("Failed to read the Azure Log Analytics Options"); | ||
} | ||
|
||
/// <summary> | ||
/// When writing the AzureLogAnalyticsOptions back to a JSON file, only write the properties | ||
/// if they are user provided. This avoids polluting the written JSON file with properties | ||
/// the user most likely omitted when writing the original DAB runtime config file. | ||
/// This Write operation is only used when a RuntimeConfig object is serialized to JSON. | ||
/// </summary> | ||
public override void Write(Utf8JsonWriter writer, AzureLogAnalyticsOptions value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
|
||
if (value?.UserProvidedEnabled is true) | ||
{ | ||
writer.WritePropertyName("enabled"); | ||
JsonSerializer.Serialize(writer, value.Enabled, options); | ||
} | ||
|
||
if (value?.Auth is not null) | ||
{ | ||
AzureLogAnalyticsAuthOptionsConverter authOptionsConverter = options.GetConverter(typeof(AzureLogAnalyticsAuthOptions)) as AzureLogAnalyticsAuthOptionsConverter ?? | ||
throw new JsonException("Failed to get azure-log-analytics.auth options converter"); | ||
|
||
authOptionsConverter.Write(writer, value.Auth, options); | ||
} | ||
|
||
if (value?.UserProvidedLogType is true) | ||
{ | ||
writer.WritePropertyName("log-type"); | ||
JsonSerializer.Serialize(writer, value.LogType, options); | ||
} | ||
|
||
if (value?.UserProvidedFlushIntervalSeconds is true) | ||
{ | ||
writer.WritePropertyName("flush-interval-seconds"); | ||
JsonSerializer.Serialize(writer, value.FlushIntervalSeconds, options); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.