Skip to content

Commit

Permalink
Deployment story
Browse files Browse the repository at this point in the history
  • Loading branch information
Mielek committed Jan 18, 2024
1 parent 26e34f9 commit 516bdaa
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 144 deletions.
4 changes: 4 additions & 0 deletions example/ci-pipeline.cmd
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
25 changes: 25 additions & 0 deletions example/deployment.bicep
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 example/source/EchoApi.cs → example/source/ComplexEchoApi.cs
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();
}
}
24 changes: 24 additions & 0 deletions example/source/ExternalExpressions.cs
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();
}
}
37 changes: 37 additions & 0 deletions example/source/SimpleEchoApi.cs
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();
}
}
4 changes: 0 additions & 4 deletions example/source/Source.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@
<PackageReference Include="Mielek.Azure.ApiManagement.PolicyToolkit" Version="1.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<Target Name="TranformToPolicies" AfterTargets="Build">
<Exec Command="dotnet policy-transformer --dllFile $(TargetPath) --out $(ProjectDir)..\target --format true" />
</Target>
</Project>
21 changes: 0 additions & 21 deletions example/source/Test.cs

This file was deleted.

2 changes: 1 addition & 1 deletion example/test/EchoApiBuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class EchoApiBuildTests
[TestMethod]
public void ShouldBuildCorrectly()
{
new EchoApi().RetrieveResource();
new SimpleEchoApi().RetrieveResourcePolicyDocument();
}
}
60 changes: 17 additions & 43 deletions example/test/EchoApiExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,22 @@ public class EchoApiExpressionTests
public void FilterBody()
{
var context = new MockContext();
context.MockResponse.MockBody.Content = "{ \"current\": \"some current content\", \"other\": \"some other content\" }";
var echoApi = new EchoApi();

var result = echoApi.FilterBody(context);

var actual = JObject.Parse(result);
var expected = JObject.Parse("{ \"other\": \"some other content\" }");

Assert.IsTrue(JObject.DeepEquals(actual, expected));
}

[TestMethod]
public void ShouldBeGuid()
{
var echoApi = new EchoApi();
var result = echoApi.GetKnownGUIDOrGenerateNew(new MockContext());

Guid.Parse(result);
}

[TestMethod]
public void ShouldProduceDifferentGuids()
{
var echoApi = new EchoApi();
var first = echoApi.GetKnownGUIDOrGenerateNew(new MockContext());
var second = echoApi.GetKnownGUIDOrGenerateNew(new MockContext());

Assert.AreNotEqual(first, second);
}

[TestMethod]
public void ShouldProduceKnownGuid()
{
var echoApi = new EchoApi();
var knownGuid = Guid.NewGuid().ToString();
var context = new MockContext();
context.MockVariables["KnownGUID"] = knownGuid;

var guidOne = echoApi.GetKnownGUIDOrGenerateNew(context);
var guidTwo = echoApi.GetKnownGUIDOrGenerateNew(context);

Assert.AreEqual(knownGuid, guidOne);
Assert.AreEqual(guidOne, guidTwo);
context.MockResponse.MockBody.Content =
"""
{
"title": "Software Engineer",
"location": "Redmond",
"secret": "42",
"name": "John Doe"
}
""";

var newBody = new SimpleEchoApi().FilterBody(context);
Assert.IsTrue(JObject.DeepEquals(JObject.Parse(newBody), JObject.Parse("""
{
"title": "Software Engineer",
"name": "John Doe"
}
""")));
}
}

0 comments on commit 516bdaa

Please sign in to comment.