-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
183 additions
and
144 deletions.
There are no files selected for viewing
This file contains 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,4 @@ | ||
dotnet build || exit -1 | ||
dotnet test || exit -2 | ||
dotnet policy-transformer --dllFile ./source/bin/Debug/.net7/Source.Example.dll --out ./target --format true || exit -3 | ||
az deployment group create --resource-group rmielowski-current-wus2 --template-file .\deployment.bicep --parameters servicename=rmielowski-current-premium --name deploy-1 || exit -4 |
This file contains 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,25 @@ | ||
param servicename string | ||
|
||
resource service 'Microsoft.ApiManagement/service@2023-03-01-preview' existing = { | ||
name: servicename | ||
scope: resourceGroup() | ||
} | ||
|
||
resource echoApi 'Microsoft.ApiManagement/service/apis@2023-03-01-preview' existing = { | ||
parent: service | ||
name: 'echo-api' | ||
} | ||
|
||
resource retrieveResource 'Microsoft.ApiManagement/service/apis/operations@2023-03-01-preview' existing = { | ||
parent: echoApi | ||
name: 'retrieve-resource' | ||
} | ||
|
||
resource retrieveResourcePolicy 'Microsoft.ApiManagement/service/apis/operations/policies@2023-03-01-preview' = { | ||
parent: retrieveResource | ||
name: 'policy' | ||
properties: { | ||
format: 'rawxml' | ||
value: loadTextContent('./target/${echoApi.name}.${retrieveResource.name}.xml', 'utf-8') | ||
} | ||
} |
150 changes: 75 additions & 75 deletions
150
example/source/EchoApi.cs → example/source/ComplexEchoApi.cs
This file contains 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 |
---|---|---|
@@ -1,76 +1,76 @@ | ||
using Mielek.Azure.ApiManagement.PolicyToolkit.Attributes; | ||
using Mielek.Azure.ApiManagement.PolicyToolkit.Builders; | ||
using Mielek.Azure.ApiManagement.PolicyToolkit.Expressions.Context; | ||
|
||
using Newtonsoft.Json.Linq; | ||
|
||
using System.Xml.Linq; | ||
|
||
using static Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Policies.SetHeaderPolicyBuilder; | ||
|
||
namespace Contoso.Apis; | ||
|
||
[Library(Name = "echo-api")] | ||
public class EchoApi | ||
{ | ||
[Document(Name = "retrieve-resource")] | ||
public XElement RetrieveResource() | ||
{ | ||
return Policy.Document() | ||
.Inbound(policies => | ||
{ | ||
policies | ||
.CheckHeader(policy => | ||
policy.Name("X-Checked") | ||
.FailedCheckHttpCode(400) | ||
.FailedCheckErrorMessage("Bad request") | ||
.IgnoreCase(IsVariableSet) | ||
.Value("Test") | ||
.Value("Other-Test")) | ||
.Base() | ||
.SetHeader(policy => | ||
policy.Name("X-Test").ExistsAction(ExistsActionType.Append) | ||
.Value("Test") | ||
.Value(context => context.Deployment.Region) | ||
.Value((context) => | ||
{ | ||
if (context.Variables.ContainsKey("Variable")) | ||
{ | ||
return "ContainsVariable"; | ||
} | ||
|
||
return "NotContainVariable"; | ||
}) | ||
.Value(GetKnownGUIDOrGenerateNew)); | ||
}) | ||
.Outbound(policies => policies.Base().SetBody(policy => policy.Body(Test.FilterBody2))) | ||
.Create(); | ||
} | ||
|
||
[Expression] | ||
public bool IsVariableSet(IContext context) => context.Variables.ContainsKey("Variable"); | ||
|
||
[Expression] | ||
public string GetKnownGUIDOrGenerateNew(IContext context) | ||
{ | ||
if (!context.Variables | ||
.TryGetValue("KnownGUID", out var guid)) | ||
{ | ||
guid = Guid.NewGuid(); | ||
} | ||
|
||
return $"{guid}"; | ||
} | ||
|
||
[Expression] | ||
public string FilterBody(IContext context) | ||
{ | ||
var response = context.Response.Body.As<JObject>(); | ||
foreach (var key in new[] { "current", "minutely", "hourly", "daily", "alerts" }) | ||
{ | ||
response.Property(key)?.Remove(); | ||
} | ||
|
||
return response.ToString(); | ||
} | ||
using Mielek.Azure.ApiManagement.PolicyToolkit.Attributes; | ||
using Mielek.Azure.ApiManagement.PolicyToolkit.Builders; | ||
using Mielek.Azure.ApiManagement.PolicyToolkit.Expressions.Context; | ||
|
||
using Newtonsoft.Json.Linq; | ||
|
||
using System.Xml.Linq; | ||
|
||
using static Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Policies.SetHeaderPolicyBuilder; | ||
|
||
namespace Contoso.Apis; | ||
|
||
[Library(Name = "echo-api")] | ||
public class ComplexEchoApi | ||
{ | ||
[Document(Name = "modify-resource")] | ||
public XElement RetrieveResource() | ||
{ | ||
return Policy.Document() | ||
.Inbound(policies => | ||
{ | ||
policies | ||
.CheckHeader(policy => | ||
policy.Name("X-Checked") | ||
.FailedCheckHttpCode(400) | ||
.FailedCheckErrorMessage("Bad request") | ||
.IgnoreCase(IsVariableSet) | ||
.Value("Test") | ||
.Value("Other-Test")) | ||
.Base() | ||
.SetHeader(policy => | ||
policy.Name("X-Test").ExistsAction(ExistsActionType.Append) | ||
.Value("Test") | ||
.Value(context => context.Deployment.Region) | ||
.Value((context) => | ||
{ | ||
if (context.Variables.ContainsKey("Variable")) | ||
{ | ||
return "ContainsVariable"; | ||
} | ||
|
||
return "NotContainVariable"; | ||
}) | ||
.Value(GetKnownGUIDOrGenerateNew)); | ||
}) | ||
.Outbound(policies => policies.Base().SetBody(policy => policy.Body(ExternalExpressions.FilterBody))) | ||
.Create(); | ||
} | ||
|
||
[Expression] | ||
public bool IsVariableSet(IContext context) => context.Variables.ContainsKey("Variable"); | ||
|
||
[Expression] | ||
public string GetKnownGUIDOrGenerateNew(IContext context) | ||
{ | ||
if (!context.Variables | ||
.TryGetValue("KnownGUID", out var guid)) | ||
{ | ||
guid = Guid.NewGuid(); | ||
} | ||
|
||
return $"{guid}"; | ||
} | ||
|
||
[Expression] | ||
public string FilterBody(IContext context) | ||
{ | ||
var response = context.Response.Body.As<JObject>(); | ||
foreach (var key in new[] { "current", "minutely", "hourly", "daily", "alerts" }) | ||
{ | ||
response.Property(key)?.Remove(); | ||
} | ||
|
||
return response.ToString(); | ||
} | ||
} |
This file contains 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,24 @@ | ||
|
||
using Mielek.Azure.ApiManagement.PolicyToolkit.Attributes; | ||
using Mielek.Azure.ApiManagement.PolicyToolkit.Expressions.Context; | ||
|
||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Contoso.Apis; | ||
|
||
public static class ExternalExpressions | ||
{ | ||
[Expression] | ||
public static string FilterBody(IContext context) | ||
{ | ||
var body = context.Response.Body.As<JObject>(); | ||
foreach (var internalProperty in new string[]{ "location", "secret" }) | ||
{ | ||
if (body.ContainsKey(internalProperty)) | ||
{ | ||
body.Remove(internalProperty); | ||
} | ||
} | ||
return body.ToString(); | ||
} | ||
} |
This file contains 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,37 @@ | ||
using System.Xml.Linq; | ||
|
||
using Mielek.Azure.ApiManagement.PolicyToolkit.Attributes; | ||
using Mielek.Azure.ApiManagement.PolicyToolkit.Builders; | ||
using Mielek.Azure.ApiManagement.PolicyToolkit.Expressions.Context; | ||
|
||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Contoso.Apis; | ||
|
||
[Library(Name = "echo-api")] | ||
public class SimpleEchoApi | ||
{ | ||
[Document(Name = "retrieve-resource")] | ||
public XElement RetrieveResourcePolicyDocument() | ||
{ | ||
return Policy.Document() | ||
.Outbound(o => o | ||
.Base() | ||
.SetBody(p => p.Body(FilterBody))) | ||
.Create(); | ||
} | ||
|
||
[Expression] | ||
public string FilterBody(IContext context) | ||
{ | ||
var body = context.Response.Body.As<JObject>(); | ||
foreach (var internalProperty in new string[]{ "location", "secret" }) | ||
{ | ||
if (body.ContainsKey(internalProperty)) | ||
{ | ||
body.Remove(internalProperty); | ||
} | ||
} | ||
return body.ToString(); | ||
} | ||
} |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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