Skip to content
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

Added parameter POLICY_CONTENT_FORMAT to export valid XML (defautl to previous value RAWXML) #613

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions tools/code/common/ApiOperationPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,18 @@ public static IAsyncEnumerable<ApiOperationPolicyName> ListNames(this ApiOperati
.Select(jsonObject => jsonObject.GetStringProperty("name"))
.Select(ApiOperationPolicyName.From);

public static IAsyncEnumerable<(ApiOperationPolicyName Name, ApiOperationPolicyDto Dto)> List(this ApiOperationPoliciesUri apioperationPoliciesUri, HttpPipeline pipeline, CancellationToken cancellationToken) =>
public static IAsyncEnumerable<(ApiOperationPolicyName Name, ApiOperationPolicyDto Dto)> List(this ApiOperationPoliciesUri apioperationPoliciesUri, HttpPipeline pipeline, CancellationToken cancellationToken, PolicyContentFormat policyContentFormat) =>
apioperationPoliciesUri.ListNames(pipeline, cancellationToken)
.SelectAwait(async name =>
{
var uri = new ApiOperationPolicyUri { Parent = apioperationPoliciesUri, Name = name };
var dto = await uri.GetDto(pipeline, cancellationToken);
var dto = await uri.GetDto(pipeline, cancellationToken, policyContentFormat);
return (name, dto);
});

public static async ValueTask<ApiOperationPolicyDto> GetDto(this ApiOperationPolicyUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
public static async ValueTask<ApiOperationPolicyDto> GetDto(this ApiOperationPolicyUri uri, HttpPipeline pipeline, CancellationToken cancellationToken, PolicyContentFormat policyContentFormat)
{
var contentUri = uri.ToUri().AppendQueryParam("format", "rawxml").ToUri();
var contentUri = uri.ToUri().AppendQueryParam("format", policyContentFormat.GetPolicyContentFormat).ToUri();
var content = await pipeline.GetContent(contentUri, cancellationToken);
return content.ToObjectFromJson<ApiOperationPolicyDto>();
}
Expand All @@ -129,7 +129,8 @@ public static async ValueTask Delete(this ApiOperationPolicyUri uri, HttpPipelin
public static async ValueTask PutDto(this ApiOperationPolicyUri uri, ApiOperationPolicyDto dto, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = BinaryData.FromObjectAsJson(dto);
await pipeline.PutContent(uri.ToUri(), content, cancellationToken);
var contentUri = string.IsNullOrEmpty(dto.Properties.Format) ? uri.ToUri() : uri.ToUri().AppendQueryParam("format", dto.Properties.Format).ToUri();
await pipeline.PutContent(contentUri, content, cancellationToken);
}

public static async ValueTask WritePolicy(this ApiOperationPolicyFile file, string policy, CancellationToken cancellationToken)
Expand Down
11 changes: 6 additions & 5 deletions tools/code/common/ApiPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,18 @@ public static IAsyncEnumerable<ApiPolicyName> ListNames(this ApiPoliciesUri uri,
.Select(jsonObject => jsonObject.GetStringProperty("name"))
.Select(ApiPolicyName.From);

public static IAsyncEnumerable<(ApiPolicyName Name, ApiPolicyDto Dto)> List(this ApiPoliciesUri apiPoliciesUri, HttpPipeline pipeline, CancellationToken cancellationToken) =>
public static IAsyncEnumerable<(ApiPolicyName Name, ApiPolicyDto Dto)> List(this ApiPoliciesUri apiPoliciesUri, HttpPipeline pipeline, CancellationToken cancellationToken, PolicyContentFormat policyContentFormat) =>
apiPoliciesUri.ListNames(pipeline, cancellationToken)
.SelectAwait(async name =>
{
var uri = new ApiPolicyUri { Parent = apiPoliciesUri, Name = name };
var dto = await uri.GetDto(pipeline, cancellationToken);
var dto = await uri.GetDto(pipeline, cancellationToken, policyContentFormat);
return (name, dto);
});

public static async ValueTask<ApiPolicyDto> GetDto(this ApiPolicyUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
public static async ValueTask<ApiPolicyDto> GetDto(this ApiPolicyUri uri, HttpPipeline pipeline, CancellationToken cancellationToken, PolicyContentFormat policyContentFormat)
{
var contentUri = uri.ToUri().AppendQueryParam("format", "rawxml").ToUri();
var contentUri = uri.ToUri().AppendQueryParam("format", policyContentFormat.GetPolicyContentFormat).ToUri();
var content = await pipeline.GetContent(contentUri, cancellationToken);
return content.ToObjectFromJson<ApiPolicyDto>();
}
Expand All @@ -135,7 +135,8 @@ public static async ValueTask Delete(this ApiPolicyUri uri, HttpPipeline pipelin
public static async ValueTask PutDto(this ApiPolicyUri uri, ApiPolicyDto dto, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = BinaryData.FromObjectAsJson(dto);
await pipeline.PutContent(uri.ToUri(), content, cancellationToken);
var contentUri = string.IsNullOrEmpty(dto.Properties.Format) ? uri.ToUri() : uri.ToUri().AppendQueryParam("format", dto.Properties.Format).ToUri();
await pipeline.PutContent(contentUri, content, cancellationToken);
}

public static IEnumerable<ApiPolicyFile> ListPolicyFiles(ApiName apiName, ManagementServiceDirectory serviceDirectory)
Expand Down
36 changes: 36 additions & 0 deletions tools/code/common/PolicyContentFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace common
{
public abstract record PolicyContentFormat
{
private static readonly Lazy<PolicyContentFormat> instance = new Lazy<PolicyContentFormat>(() => new RawXml());

public static PolicyContentFormat Default => instance.Value;

public sealed record RawXml : PolicyContentFormat;
public sealed record Xml : PolicyContentFormat;
public sealed record RawXmlLink : PolicyContentFormat;
public sealed record XmlLink : PolicyContentFormat;


public string GetPolicyContentFormat
{
get
{
return this switch
{
PolicyContentFormat.RawXml => "rawxml",
PolicyContentFormat.Xml => "xml",
PolicyContentFormat.RawXmlLink => "rawxml-link",
PolicyContentFormat.XmlLink => "xml-link",
_ => throw new NotSupportedException()
};
}
}
}
}
64 changes: 64 additions & 0 deletions tools/code/common/PolicyContentFormatModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using common;
using LanguageExt;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using System;
using System.Globalization;
using System.Text.Json.Serialization;
using System.Text.Json;

namespace common;

public static class PolicyContentFormatModule
{
public static void ConfigureDefaultPolicyContentFormat(IHostApplicationBuilder builder)
{
builder.Services.TryAddSingleton(GetDefaultPolicyContentFormat);
}

private static PolicyContentFormat GetDefaultPolicyContentFormat(IServiceProvider provider)
{
var configuration = provider.GetRequiredService<IConfiguration>();

var formatOption = configuration.TryGetValue("POLICY_CONTENT_FORMAT")
| configuration.TryGetValue("policyContentFormat");

var specification = formatOption.Map(format => format switch
{
var value when "RAWXML".Equals(value, StringComparison.OrdinalIgnoreCase) => new PolicyContentFormat.RawXml() as PolicyContentFormat,
var value when "XML".Equals(value, StringComparison.OrdinalIgnoreCase) => new PolicyContentFormat.Xml(),
var value => throw new NotSupportedException($"Policy content format '{value}' defined in configuration is not supported.")
}).IfNone(() => new PolicyContentFormat.RawXml());

return specification;
}
}

//public class PolicyContentFormatJsonConverter : JsonConverter<PolicyContentFormat>
//{
// public override PolicyContentFormat Read(
// ref Utf8JsonReader reader,
// Type typeToConvert,
// JsonSerializerOptions options)
// {
// return reader.GetString() switch
// {
// "rawxml" => new PolicyContentFormat.RawXml(),
// "xml" => new PolicyContentFormat.Xml(),
// "rawxml-link" => new PolicyContentFormat.RawXmlLink(),
// "xml-link" => new PolicyContentFormat.XmlLink(),
// _ => throw new NotSupportedException()
// };
// }

// public override void Write(
// Utf8JsonWriter writer,
// PolicyContentFormat value,
// JsonSerializerOptions options)
// {
// // switch on PolicyContentFormat to determine the string to render
// writer.WriteStringValue(value.GetPolicyContentFormat);
// }
//}
11 changes: 6 additions & 5 deletions tools/code/common/PolicyFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,18 @@ public static IAsyncEnumerable<PolicyFragmentName> ListNames(this PolicyFragment
.Select(jsonObject => jsonObject.GetStringProperty("name"))
.Select(PolicyFragmentName.From);

public static IAsyncEnumerable<(PolicyFragmentName Name, PolicyFragmentDto Dto)> List(this PolicyFragmentsUri policyFragmentsUri, HttpPipeline pipeline, CancellationToken cancellationToken) =>
public static IAsyncEnumerable<(PolicyFragmentName Name, PolicyFragmentDto Dto)> List(this PolicyFragmentsUri policyFragmentsUri, HttpPipeline pipeline, CancellationToken cancellationToken, PolicyContentFormat policyContentFormat) =>
policyFragmentsUri.ListNames(pipeline, cancellationToken)
.SelectAwait(async name =>
{
var uri = new PolicyFragmentUri { Parent = policyFragmentsUri, Name = name };
var dto = await uri.GetDto(pipeline, cancellationToken);
var dto = await uri.GetDto(pipeline, cancellationToken, policyContentFormat);
return (name, dto);
});

public static async ValueTask<PolicyFragmentDto> GetDto(this PolicyFragmentUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
public static async ValueTask<PolicyFragmentDto> GetDto(this PolicyFragmentUri uri, HttpPipeline pipeline, CancellationToken cancellationToken, PolicyContentFormat policyContentFormat)
{
var contentUri = uri.ToUri().AppendQueryParam("format", "rawxml").ToUri();
var contentUri = uri.ToUri().AppendQueryParam("format", policyContentFormat.GetPolicyContentFormat).ToUri();
var content = await pipeline.GetContent(contentUri, cancellationToken);
return content.ToObjectFromJson<PolicyFragmentDto>();
}
Expand All @@ -197,7 +197,8 @@ public static async ValueTask Delete(this PolicyFragmentUri uri, HttpPipeline pi
public static async ValueTask PutDto(this PolicyFragmentUri uri, PolicyFragmentDto dto, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = BinaryData.FromObjectAsJson(dto);
await pipeline.PutContent(uri.ToUri(), content, cancellationToken);
var contentUri = string.IsNullOrEmpty(dto.Properties.Format) ? uri.ToUri() : uri.ToUri().AppendQueryParam("format", dto.Properties.Format).ToUri();
await pipeline.PutContent(contentUri, content, cancellationToken);
}

public static IEnumerable<PolicyFragmentDirectory> ListDirectories(ManagementServiceDirectory serviceDirectory)
Expand Down
11 changes: 6 additions & 5 deletions tools/code/common/ProductPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,18 @@ public static IAsyncEnumerable<ProductPolicyName> ListNames(this ProductPolicies
.Select(jsonObject => jsonObject.GetStringProperty("name"))
.Select(ProductPolicyName.From);

public static IAsyncEnumerable<(ProductPolicyName Name, ProductPolicyDto Dto)> List(this ProductPoliciesUri productPoliciesUri, HttpPipeline pipeline, CancellationToken cancellationToken) =>
public static IAsyncEnumerable<(ProductPolicyName Name, ProductPolicyDto Dto)> List(this ProductPoliciesUri productPoliciesUri, HttpPipeline pipeline, CancellationToken cancellationToken, PolicyContentFormat policyContentFormat) =>
productPoliciesUri.ListNames(pipeline, cancellationToken)
.SelectAwait(async name =>
{
var uri = new ProductPolicyUri { Parent = productPoliciesUri, Name = name };
var dto = await uri.GetDto(pipeline, cancellationToken);
var dto = await uri.GetDto(pipeline, cancellationToken, policyContentFormat);
return (name, dto);
});

public static async ValueTask<ProductPolicyDto> GetDto(this ProductPolicyUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
public static async ValueTask<ProductPolicyDto> GetDto(this ProductPolicyUri uri, HttpPipeline pipeline, CancellationToken cancellationToken, PolicyContentFormat policyContentFormat)
{
var contentUri = uri.ToUri().AppendQueryParam("format", "rawxml").ToUri();
var contentUri = uri.ToUri().AppendQueryParam("format", policyContentFormat.GetPolicyContentFormat).ToUri();
var content = await pipeline.GetContent(contentUri, cancellationToken);
return content.ToObjectFromJson<ProductPolicyDto>();
}
Expand All @@ -130,7 +130,8 @@ public static async ValueTask Delete(this ProductPolicyUri uri, HttpPipeline pip
public static async ValueTask PutDto(this ProductPolicyUri uri, ProductPolicyDto dto, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = BinaryData.FromObjectAsJson(dto);
await pipeline.PutContent(uri.ToUri(), content, cancellationToken);
var contentUri = string.IsNullOrEmpty(dto.Properties.Format) ? uri.ToUri() : uri.ToUri().AppendQueryParam("format", dto.Properties.Format).ToUri();
await pipeline.PutContent(contentUri, content, cancellationToken);
}

public static IEnumerable<ProductPolicyFile> ListPolicyFiles(ProductName productName, ManagementServiceDirectory serviceDirectory)
Expand Down
14 changes: 8 additions & 6 deletions tools/code/common/ServicePolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using LanguageExt;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -87,7 +88,7 @@ public sealed record ServicePolicyContract

[JsonPropertyName("format")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
public string? Format { get; init; }
public string? Format { get ; init ; }

[JsonPropertyName("value")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
Expand All @@ -108,18 +109,18 @@ public static IAsyncEnumerable<ServicePolicyName> ListNames(this ServicePolicies
.Select(jsonObject => jsonObject.GetStringProperty("name"))
.Select(ServicePolicyName.From);

public static IAsyncEnumerable<(ServicePolicyName Name, ServicePolicyDto Dto)> List(this ServicePoliciesUri servicePoliciesUri, HttpPipeline pipeline, CancellationToken cancellationToken) =>
public static IAsyncEnumerable<(ServicePolicyName Name, ServicePolicyDto Dto)> List(this ServicePoliciesUri servicePoliciesUri, HttpPipeline pipeline, CancellationToken cancellationToken, PolicyContentFormat policyContentFormat) =>
servicePoliciesUri.ListNames(pipeline, cancellationToken)
.SelectAwait(async name =>
{
var uri = new ServicePolicyUri { Parent = servicePoliciesUri, Name = name };
var dto = await uri.GetDto(pipeline, cancellationToken);
var dto = await uri.GetDto(pipeline, cancellationToken, policyContentFormat);
return (name, dto);
});

public static async ValueTask<ServicePolicyDto> GetDto(this ServicePolicyUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
public static async ValueTask<ServicePolicyDto> GetDto(this ServicePolicyUri uri, HttpPipeline pipeline, CancellationToken cancellationToken, PolicyContentFormat policyContentFormat)
{
var contentUri = uri.ToUri().AppendQueryParam("format", "rawxml").ToUri();
var contentUri = uri.ToUri().AppendQueryParam("format", policyContentFormat.GetPolicyContentFormat).ToUri();
var content = await pipeline.GetContent(contentUri, cancellationToken);
return content.ToObjectFromJson<ServicePolicyDto>();
}
Expand All @@ -130,7 +131,8 @@ public static async ValueTask Delete(this ServicePolicyUri uri, HttpPipeline pip
public static async ValueTask PutDto(this ServicePolicyUri uri, ServicePolicyDto dto, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = BinaryData.FromObjectAsJson(dto);
await pipeline.PutContent(uri.ToUri(), content, cancellationToken);
var contentUri = string.IsNullOrEmpty(dto.Properties.Format) ? uri.ToUri() : uri.ToUri().AppendQueryParam("format", dto.Properties.Format).ToUri();
await pipeline.PutContent(contentUri, content, cancellationToken);
}

public static IEnumerable<ServicePolicyFile> ListPolicyFiles(ManagementServiceDirectory serviceDirectory) =>
Expand Down
12 changes: 7 additions & 5 deletions tools/code/common/WorkspacePolicy.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Azure.Core.Pipeline;
using Flurl;
using LanguageExt;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -117,18 +118,18 @@ public static async IAsyncEnumerable<WorkspacePolicyName> ListNames(this Workspa
}
}

public static IAsyncEnumerable<(WorkspacePolicyName Name, WorkspacePolicyDto Dto)> List(this WorkspacePoliciesUri workspacePoliciesUri, HttpPipeline pipeline, CancellationToken cancellationToken) =>
public static IAsyncEnumerable<(WorkspacePolicyName Name, WorkspacePolicyDto Dto)> List(this WorkspacePoliciesUri workspacePoliciesUri, HttpPipeline pipeline, CancellationToken cancellationToken, PolicyContentFormat policyContentFormat) =>
workspacePoliciesUri.ListNames(pipeline, cancellationToken)
.SelectAwait(async name =>
{
var uri = new WorkspacePolicyUri { Parent = workspacePoliciesUri, Name = name };
var dto = await uri.GetDto(pipeline, cancellationToken);
var dto = await uri.GetDto(pipeline, cancellationToken, policyContentFormat);
return (name, dto);
});

public static async ValueTask<WorkspacePolicyDto> GetDto(this WorkspacePolicyUri uri, HttpPipeline pipeline, CancellationToken cancellationToken)
public static async ValueTask<WorkspacePolicyDto> GetDto(this WorkspacePolicyUri uri, HttpPipeline pipeline, CancellationToken cancellationToken, PolicyContentFormat policyContentFormat)
{
var contentUri = uri.ToUri().AppendQueryParam("format", "rawxml").ToUri();
var contentUri = uri.ToUri().AppendQueryParam("format", policyContentFormat.GetPolicyContentFormat).ToUri();
var content = await pipeline.GetContent(contentUri, cancellationToken);
return content.ToObjectFromJson<WorkspacePolicyDto>();
}
Expand All @@ -139,7 +140,8 @@ public static async ValueTask Delete(this WorkspacePolicyUri uri, HttpPipeline p
public static async ValueTask PutDto(this WorkspacePolicyUri uri, WorkspacePolicyDto dto, HttpPipeline pipeline, CancellationToken cancellationToken)
{
var content = BinaryData.FromObjectAsJson(dto);
await pipeline.PutContent(uri.ToUri(), content, cancellationToken);
var contentUri = string.IsNullOrEmpty(dto.Properties.Format) ? uri.ToUri() : uri.ToUri().AppendQueryParam("format", dto.Properties.Format).ToUri();
await pipeline.PutContent(contentUri, content, cancellationToken);
}

public static IEnumerable<WorkspacePolicyFile> ListPolicyFiles(WorkspaceName workspaceName, ManagementServiceDirectory serviceDirectory)
Expand Down
Loading