-
Notifications
You must be signed in to change notification settings - Fork 22
feat: Implement support for creating simple policy fragments #167
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
42073ff
implemented support for creating policy fragments
FilPag 8a557af
copied policy context interfaces for IFragmentContext
FilPag 7b30b72
ved Extraction functions to SyntaxExtensions
FilPag cd5f08a
fixed ExtractDocumentType
FilPag 4787903
Update src/Authoring/Attributes/DocumentAttribute.cs
FilPag 1d62916
Tests and examples now assigned through equals
FilPag 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using Microsoft.Azure.ApiManagement.PolicyToolkit.Authoring; | ||
| using Microsoft.Azure.ApiManagement.PolicyToolkit.Authoring.Expressions; | ||
|
|
||
| namespace Microsoft.Azure.ApiManagement.PolicyToolkit.Example; | ||
|
|
||
| [Document("authentication-fragment", type: DocumentType.Fragment)] | ||
| public class AuthenticationFragment : IFragment | ||
| { | ||
| public void Fragment(IFragmentContext context) | ||
| { | ||
| context.SetHeader("X-Auth-Fragment", "true"); | ||
| context.AuthenticationBasic("{{username}}", "{{password}}"); | ||
| } | ||
| } |
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
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
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,18 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Azure.ApiManagement.PolicyToolkit.Authoring; | ||
|
|
||
| /// <summary> | ||
| /// Interface for policy fragments. Policy fragments are reusable policy elements | ||
| /// that can be included in policy definitions using the include-fragment policy. | ||
| /// </summary> | ||
| public interface IFragment | ||
| { | ||
| /// <summary> | ||
| /// Defines the policy elements that make up this fragment. | ||
| /// The policies defined in this method will be compiled directly into the fragment. | ||
| /// </summary> | ||
| /// <param name="context">The fragment context providing access to all policy operations.</param> | ||
| void Fragment(IFragmentContext context); | ||
| } |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
40 changes: 40 additions & 0 deletions
40
src/Templates/content/create-policy-fragment/.template.config/template.json
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,40 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/template", | ||
| "author": "Microsoft", | ||
| "classifications": [ | ||
| "Policy Toolkit", | ||
| "Policy Fragment", | ||
| "Class" | ||
| ], | ||
| "name": "Policy Toolkit Policy Fragment Class", | ||
| "generatorVersions": "[1.0.0.0-*)", | ||
| "description": "Creates a new Policy Toolkit policy fragment class", | ||
| "groupIdentity": "Azure.ApiManagement.PolicyToolkit.Templates.PolicyFragment", | ||
| "identity": "Azure.ApiManagement.PolicyToolkit.Templates.PolicyFragment.1.0", | ||
| "shortName": "policytoolkitfragment", | ||
| "tags": { | ||
| "language": "C#", | ||
| "type": "item" | ||
| }, | ||
| "sourceName": "PolicyFragment1", | ||
| "preferDefaultName": true, | ||
| "defaultName": "PolicyFragment1", | ||
| "primaryOutputs": [ | ||
| { | ||
| "path": ".cs" | ||
| } | ||
| ], | ||
| "symbols": { | ||
| "DefaultNamespace": { | ||
| "type": "bind", | ||
| "binding": "msbuild:RootNamespace", | ||
| "replaces": "Company.PolicyProject1" | ||
| } | ||
| }, | ||
| "constraints": { | ||
| "csharp-only": { | ||
| "type": "project-capability", | ||
| "args": "CSharp" | ||
| } | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/Templates/content/create-policy-fragment/PolicyFragment1.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,17 @@ | ||
| namespace Company.PolicyProject1; | ||
|
|
||
| [Document(type: DocumentType.Fragment)] | ||
| public class PolicyFragment1 : IFragment | ||
| { | ||
| public void Fragment(IFragmentContext context) | ||
| { | ||
| // Add reusable policy logic here | ||
| // This fragment can be included in any policy section (inbound, outbound, backend, on-error) | ||
|
|
||
| // Example: Set a common header | ||
| // context.SetHeader("X-Custom-Header", "fragment-value"); | ||
|
|
||
| // Example: Set a variable | ||
| // context.SetVariable("fragment-processed", "true"); | ||
| } | ||
| } |
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,100 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace Microsoft.Azure.ApiManagement.PolicyToolkit.Compiling; | ||
|
|
||
| [TestClass] | ||
| public class DocumentTypeTests | ||
| { | ||
| [TestMethod] | ||
| [DataRow( | ||
| """ | ||
| [Document] | ||
| public class PolicyDocument : IDocument | ||
| { | ||
| public void Inbound(IInboundContext context) | ||
| { | ||
| context.SetHeader("X-Test", "value"); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| <policies> | ||
| <inbound> | ||
| <set-header name="X-Test" exists-action="override"> | ||
| <value>value</value> | ||
| </set-header> | ||
| </inbound> | ||
| </policies> | ||
| """, | ||
| DisplayName = "Should compile regular policy document with policies root" | ||
| )] | ||
| [DataRow( | ||
| """ | ||
| [Document(type: DocumentType.Policy)] | ||
| public class PolicyDocument : IDocument | ||
| { | ||
| public void Inbound(IInboundContext context) | ||
| { | ||
| context.SetHeader("X-Test", "value"); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| <policies> | ||
| <inbound> | ||
| <set-header name="X-Test" exists-action="override"> | ||
| <value>value</value> | ||
| </set-header> | ||
| </inbound> | ||
| </policies> | ||
| """, | ||
| DisplayName = "Should compile explicit policy document with policies root" | ||
| )] | ||
| [DataRow( | ||
| """ | ||
| [Document(type: DocumentType.Fragment)] | ||
| public class PolicyFragment : IFragment | ||
| { | ||
| public void Fragment(IFragmentContext context) | ||
| { | ||
| context.SetHeader("X-Fragment", "fragment-value"); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| <fragment> | ||
| <set-header name="X-Fragment" exists-action="override"> | ||
| <value>fragment-value</value> | ||
| </set-header> | ||
| </fragment> | ||
| """, | ||
| DisplayName = "Should compile policy fragment with fragment root using Fragment method" | ||
| )] | ||
| [DataRow( | ||
| """ | ||
| [Document("my-fragment", type: DocumentType.Fragment)] | ||
| public class NamedPolicyFragment : IFragment | ||
| { | ||
| public void Fragment(IFragmentContext context) | ||
| { | ||
| context.SetHeader("X-Named-Fragment", "named-value"); | ||
| context.Base(); | ||
| } | ||
| } | ||
| """, | ||
| """ | ||
| <fragment> | ||
| <set-header name="X-Named-Fragment" exists-action="override"> | ||
| <value>named-value</value> | ||
| </set-header> | ||
| <base /> | ||
| </fragment> | ||
| """, | ||
| DisplayName = "Should compile named policy fragment with multiple policies using Fragment method" | ||
| )] | ||
| public void ShouldCompileDocumentWithCorrectType(string code, string expectedXml) | ||
| { | ||
| code.CompileDocument().Should().BeSuccessful().And.DocumentEquivalentTo(expectedXml); | ||
| } | ||
| } |
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.