Skip to content

Commit

Permalink
Add id to builders
Browse files Browse the repository at this point in the history
  • Loading branch information
Mielek committed Jan 18, 2024
1 parent 516bdaa commit 7f10ac7
Show file tree
Hide file tree
Showing 48 changed files with 598 additions and 527 deletions.
21 changes: 21 additions & 0 deletions src/Core/Builders/BaseBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Xml.Linq;

namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders;

public abstract class BaseBuilder<T> where T : BaseBuilder<T>
{
private string? _id;

public T Id(string id)
{
_id = id;
return (T)this;
}

protected XElement CreateElement(string name)
{
var element = new XElement(name);
if(_id != null) element.Add(new XAttribute("id", _id));
return element;
}
}
17 changes: 8 additions & 9 deletions src/Core/Builders/Policies/AuthenticationBasicPolicyBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using Mielek.Azure.ApiManagement.PolicyToolkit.Exceptions;

namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Policies;

using System.Xml.Linq;

using Mielek.Azure.ApiManagement.PolicyToolkit.Exceptions;
using Mielek.Azure.ApiManagement.PolicyToolkit.Generators.Attributes;

namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Policies;

[GenerateBuilderSetters]
[
AddToSectionBuilder(typeof(InboundSectionBuilder)),
AddToSectionBuilder(typeof(PolicyFragmentBuilder))
]
public partial class AuthenticationBasicPolicyBuilder
public partial class AuthenticationBasicPolicyBuilder : BaseBuilder<AuthenticationBasicPolicyBuilder>
{
private string? _username;
private string? _password;
Expand All @@ -21,9 +20,9 @@ public XElement Build()
if (_username == null) throw new PolicyValidationException("Username is required for AuthenticationBasic");
if (_password == null) throw new PolicyValidationException("Password is required for AuthenticationBasic");

return new XElement("authentication-basic", new object[] {
new XAttribute("username", _username),
new XAttribute("password", _password),
});
var element = this.CreateElement("authentication-basic");
element.Add(new XAttribute("username", _username));
element.Add(new XAttribute("password", _password));
return element;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using Mielek.Azure.ApiManagement.PolicyToolkit.Exceptions;

namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Policies;

using System.Collections.Immutable;
using System.Xml.Linq;

using Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Expressions;
using Mielek.Azure.ApiManagement.PolicyToolkit.Exceptions;
using Mielek.Azure.ApiManagement.PolicyToolkit.Generators.Attributes;

namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Policies;

[GenerateBuilderSetters]
[
[
AddToSectionBuilder(typeof(InboundSectionBuilder)),
AddToSectionBuilder(typeof(PolicyFragmentBuilder))
]
public partial class AuthenticationCertificatePolicyBuilder
public partial class AuthenticationCertificatePolicyBuilder : BaseBuilder<AuthenticationBasicPolicyBuilder>
{
private string? _thumbprint;
private string? _certificateId;
Expand All @@ -22,33 +20,35 @@ public partial class AuthenticationCertificatePolicyBuilder

public XElement Build()
{
if ((_thumbprint == null) == (_certificateId == null)) throw new PolicyValidationException("Either thumbprint or certificate-id is required for AuthenticationCertificate");
if (_password != null && _body == null) throw new PolicyValidationException("Password is only valid with body for AuthenticationCertificate");
if ((_thumbprint == null) == (_certificateId == null))
throw new PolicyValidationException(
"Either thumbprint or certificate-id is required for AuthenticationCertificate");

if (_password != null && _body == null)
throw new PolicyValidationException("Password is only valid with body for AuthenticationCertificate");

var attributes = ImmutableArray.CreateBuilder<object>();
var element = this.CreateElement("authentication-certificate");

if (_thumbprint != null)
{
attributes.Add(new XAttribute("thumbprint", _thumbprint));
element.Add(new XAttribute("thumbprint", _thumbprint));
}

if (_certificateId != null)
{
attributes.Add(new XAttribute("certificate-id", _certificateId));
element.Add(new XAttribute("certificate-id", _certificateId));
}

if (_body != null)
{
attributes.Add(_body.GetXAttribute("body"));
element.Add(_body.GetXAttribute("body"));
}

if (_password != null)
{
attributes.Add(new XAttribute("password", _password));
element.Add(new XAttribute("password", _password));
}


return new XElement("authentication-certificate", attributes.ToArray());
return element;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using Mielek.Azure.ApiManagement.PolicyToolkit.Exceptions;

namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Policies;

using System.Collections.Immutable;
using System.Xml.Linq;

using Mielek.Azure.ApiManagement.PolicyToolkit.Exceptions;
using Mielek.Azure.ApiManagement.PolicyToolkit.Generators.Attributes;

namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Policies;

[GenerateBuilderSetters]
[
AddToSectionBuilder(typeof(InboundSectionBuilder)),
AddToSectionBuilder(typeof(PolicyFragmentBuilder))
]
public partial class AuthenticationManagedIdentityPolicyBuilder
public partial class
AuthenticationManagedIdentityPolicyBuilder : BaseBuilder<AuthenticationManagedIdentityPolicyBuilder>
{
private string? _resource;
private string? _clientId;
Expand All @@ -21,24 +20,28 @@ public partial class AuthenticationManagedIdentityPolicyBuilder

public XElement Build()
{
if (_resource == null) throw new PolicyValidationException("Resource is required for AuthenticationManagedIdentity");
if (_resource == null)
throw new PolicyValidationException("Resource is required for AuthenticationManagedIdentity");

var element = this.CreateElement("authentication-managed-identity");

var attributes = ImmutableArray.CreateBuilder<object>();
element.Add(new XAttribute("resource", _resource));

attributes.Add(new XAttribute("resource", _resource));
if (_clientId != null)
{
attributes.Add(new XAttribute("client-id", _clientId));
element.Add(new XAttribute("client-id", _clientId));
}

if (_outputTokenVariableName != null)
{
attributes.Add(new XAttribute("output-token-variable-name", _outputTokenVariableName));
element.Add(new XAttribute("output-token-variable-name", _outputTokenVariableName));
}

if (_ignoreError != null)
{
attributes.Add(new XAttribute("ignore-error", _ignoreError));
element.Add(new XAttribute("ignore-error", _ignoreError));
}

return new XElement("authentication-managed-identity", attributes.ToArray());
return element;
}
}
4 changes: 2 additions & 2 deletions src/Core/Builders/Policies/BasePolicyBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders;

using System.Xml.Linq;

namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders;

public partial class InboundSectionBuilder
{
public InboundSectionBuilder Base()
Expand Down
43 changes: 24 additions & 19 deletions src/Core/Builders/Policies/CacheLookupPolicyBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@


using Mielek.Azure.ApiManagement.PolicyToolkit.Exceptions;

namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Policies;

using System.Collections.Immutable;
using System.Xml.Linq;

using Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Expressions;
using Mielek.Azure.ApiManagement.PolicyToolkit.Exceptions;
using Mielek.Azure.ApiManagement.PolicyToolkit.Generators.Attributes;

namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Policies;

[GenerateBuilderSetters]
[
AddToSectionBuilder(typeof(InboundSectionBuilder)),
AddToSectionBuilder(typeof(PolicyFragmentBuilder))
]
public partial class CacheLookupPolicyBuilder
public partial class CacheLookupPolicyBuilder : BaseBuilder<CacheLookupPolicyBuilder>
{
public enum CacheLookupCachingType { PreferExternal, External, Internal }

public enum CacheLookupDownstreamCachingType { None, Private, Public }

private bool? _varyByDeveloper;
Expand All @@ -31,48 +29,55 @@ public enum CacheLookupDownstreamCachingType { None, Private, Public }

public XElement Build()
{
if (_varyByDeveloper == null) throw new PolicyValidationException("Vary by developer is required for CacheLookup");
if (_varyByDeveloperGroup == null) throw new PolicyValidationException("Vary by developer group is required for CacheLookup");
if (_varyByDeveloper == null)
throw new PolicyValidationException("Vary by developer is required for CacheLookup");

if (_varyByDeveloperGroup == null)
throw new PolicyValidationException("Vary by developer group is required for CacheLookup");

var children = ImmutableArray.CreateBuilder<object>();
var element = this.CreateElement("cache-lookup");

children.Add(new XAttribute("vary-by-developer", _varyByDeveloper));
children.Add(new XAttribute("vary-by-developer-group", _varyByDeveloperGroup));
element.Add(new XAttribute("vary-by-developer", _varyByDeveloper));
element.Add(new XAttribute("vary-by-developer-group", _varyByDeveloperGroup));

if (_catchingType != null)
{
children.Add(new XAttribute("cache-type", TranslateCachingType(_catchingType)));
element.Add(new XAttribute("cache-type", TranslateCachingType(_catchingType)));
}

if (_downstreamCachingType != null)
{
children.Add(new XAttribute("downstream-caching-type", TranslateDownstreamCachingType(_downstreamCachingType)));
element.Add(new XAttribute("downstream-caching-type",
TranslateDownstreamCachingType(_downstreamCachingType)));
}

if (_mustRevalidate != null)
{
children.Add(new XAttribute("must-revalidate", _mustRevalidate));
element.Add(new XAttribute("must-revalidate", _mustRevalidate));
}

if (_allowPrivateResponseCaching != null)
{
children.Add(_allowPrivateResponseCaching.GetXAttribute("allow-private-response-caching"));
element.Add(_allowPrivateResponseCaching.GetXAttribute("allow-private-response-caching"));
}

if (_varyByHeaders != null)
{
foreach (var varyByHeader in _varyByHeaders)
{
children.Add(new XElement("vary-by-header", varyByHeader));
element.Add(new XElement("vary-by-header", varyByHeader));
}
}

if (_varyByQueryParameters != null)
{
foreach (var varyByQueryParam in _varyByQueryParameters)
{
children.Add(new XElement("vary-by-query-parameter", varyByQueryParam));
element.Add(new XElement("vary-by-query-parameter", varyByQueryParam));
}
}

return new XElement("cache-lookup", children.ToArray());
return element;
}

private static string TranslateCachingType(CacheLookupCachingType? type) => type switch
Expand Down
25 changes: 12 additions & 13 deletions src/Core/Builders/Policies/CacheLookupValuePolicyBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using Mielek.Azure.ApiManagement.PolicyToolkit.Exceptions;

namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Policies;

using System.Collections.Immutable;
using System.Xml.Linq;

using Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Expressions;
using Mielek.Azure.ApiManagement.PolicyToolkit.Exceptions;
using Mielek.Azure.ApiManagement.PolicyToolkit.Generators.Attributes;

namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Policies;

[GenerateBuilderSetters]
[
AddToSectionBuilder(typeof(InboundSectionBuilder)),
Expand All @@ -16,7 +14,7 @@ namespace Mielek.Azure.ApiManagement.PolicyToolkit.Builders.Policies;
AddToSectionBuilder(typeof(OnErrorSectionBuilder)),
AddToSectionBuilder(typeof(PolicyFragmentBuilder))
]
public partial class CacheLookupValuePolicyBuilder
public partial class CacheLookupValuePolicyBuilder : BaseBuilder<CacheLookupValuePolicyBuilder>
{
public enum CachingTypeEnum { Internal, External, PreferExternal }

Expand All @@ -27,24 +25,25 @@ public enum CachingTypeEnum { Internal, External, PreferExternal }

public XElement Build()
{
if (_variableName == null) throw new PolicyValidationException("Variable name is required for CacheLookupValue");
if (_variableName == null)
throw new PolicyValidationException("Variable name is required for CacheLookupValue");

var children = ImmutableArray.CreateBuilder<XObject>();
children.Add(new XAttribute("variable-name", _variableName));
var element = this.CreateElement("cache-lookup-value");
element.Add(new XAttribute("variable-name", _variableName));
if (_catchingType != null)
{
children.Add(new XAttribute("caching-type", TranslateCachingType(_catchingType)));
element.Add(new XAttribute("caching-type", TranslateCachingType(_catchingType)));
}
if (_key != null)
{
children.Add(_key.GetXAttribute("key"));
element.Add(_key.GetXAttribute("key"));
}
if (_defaultValue != null)
{
children.Add(_defaultValue.GetXAttribute("default-value"));
element.Add(_defaultValue.GetXAttribute("default-value"));
}

return new XElement("cache-lookup-value", children.ToArray());
return element;
}

private string TranslateCachingType(CachingTypeEnum? cachingType)
Expand Down
Loading

0 comments on commit 7f10ac7

Please sign in to comment.