Skip to content

Commit

Permalink
Migrate the new policies to use the new mediation context
Browse files Browse the repository at this point in the history
  • Loading branch information
pubudu91 committed Dec 13, 2022
1 parent 34bdbe3 commit c7c93bd
Show file tree
Hide file tree
Showing 25 changed files with 163 additions and 121 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target/
.DS_Store
.idea/
6 changes: 4 additions & 2 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

def build():
failed_projects = []
count = 1

for pkg in packages:
bal_toml = toml.load(os.path.join(pkg['name'], 'Ballerina.toml'))
Expand All @@ -40,8 +41,9 @@ def build():
print(pkg['name'], 'is already in the latest version')
continue

print(f'Building \'{pkg["name"]}\'')
exit_code = os.system(f'bal build {pkg["name"]}')
print(f'{count} Building \'{pkg["name"]}\'')
count += 1
exit_code = os.system(f'bal test {pkg["name"]}')

if exit_code != 0:
print(f'Failed to build {pkg["name"]}')
Expand Down
2 changes: 1 addition & 1 deletion mediation-add-header/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "regex"
version = "1.3.0"
version = "1.3.2"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.string"}
Expand Down
2 changes: 1 addition & 1 deletion mediation-add-query-param/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "choreo"
name = "mediation.add_query_param"
version = "0.2.0"
version = "0.3.0"
distribution = "2201.2.3"
keywords=["choreo-apim-mediation-policy", "choreo-apim-mediation-request-flow"]

Expand Down
21 changes: 12 additions & 9 deletions mediation-add-query-param/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "constraint"
version = "1.0.0"
version = "1.0.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down Expand Up @@ -62,7 +62,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.4.3"
version = "2.4.6"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "cache"},
Expand Down Expand Up @@ -93,7 +93,7 @@ modules = [
[[package]]
org = "ballerina"
name = "io"
version = "1.3.0"
version = "1.3.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.value"}
Expand Down Expand Up @@ -218,7 +218,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.0.5"
version = "1.0.6"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand All @@ -235,7 +235,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "regex"
version = "1.3.0"
version = "1.3.2"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.string"}
Expand Down Expand Up @@ -265,15 +265,15 @@ modules = [
[[package]]
org = "ballerina"
name = "time"
version = "2.2.2"
version = "2.2.3"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "url"
version = "2.2.2"
version = "2.2.3"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand All @@ -293,15 +293,18 @@ modules = [
[[package]]
org = "choreo"
name = "mediation"
version = "0.6.1"
version = "0.7.0"
dependencies = [
{org = "ballerina", name = "regex"}
]
modules = [
{org = "choreo", packageName = "mediation", moduleName = "mediation"}
]

[[package]]
org = "choreo"
name = "mediation.add_query_param"
version = "0.2.0"
version = "0.3.0"
dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "test"},
Expand Down
10 changes: 1 addition & 9 deletions mediation-add-query-param/policy.bal
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ import ballerina/http;
@mediation:RequestFlow
public function addQueryParam(mediation:Context ctx, http:Request req, string Parameter\ Name, string Parameter\ Value)
returns http:Response|false|error|() {
map<string[]> qParams = <map<string[]>>ctx["queryParams"];

if qParams.hasKey(Parameter\ Name) {
qParams.get(Parameter\ Name).push(Parameter\ Value);
} else {
string[] vals = [Parameter\ Value];
qParams[Parameter\ Name] = vals;
}

ctx.addQueryParam(Parameter\ Name, Parameter\ Value);
return ();
}
18 changes: 13 additions & 5 deletions mediation-add-query-param/tests/utest_policy.bal
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ import ballerina/test;

@test:Config {}
public function testRequestFlowSingleInstance() {
mediation:Context ctx = {httpMethod: "get", resourcePath: "/greet", "queryParams": <map<string[]>>{}};
mediation:Context ctx = createContext("get", "/greet");
http:Response|false|error|() result = addQueryParam(ctx, new, "x", "y");
assertResult(result, ctx["queryParams"], {x: ["y"]});
assertResult(result, ctx.queryParams(), {x: ["y"]});
}

@test:Config {}
public function testRequestFlowMultipleInstance() {
mediation:Context ctx = {httpMethod: "get", resourcePath: "/greet", "queryParams": <map<string[]>>{}};
mediation:Context ctx = createContext("get", "/greet");
http:Response|false|error|() result = addQueryParam(ctx, new, "arr", "1");
assertResult(result, ctx["queryParams"], {arr: ["1"]});
assertResult(result, ctx.queryParams(), {arr: ["1"]});

result = addQueryParam(ctx, new, "arr", "2");
assertResult(result, ctx["queryParams"], {arr: ["1", "2"]});
assertResult(result, ctx.queryParams(), {arr: ["1", "2"]});
}

function assertResult(http:Response|false|error|() result, anydata qParamMap, map<string[]> expQParamMap) {
Expand All @@ -42,3 +42,11 @@ function assertResult(http:Response|false|error|() result, anydata qParamMap, ma

test:assertEquals(qParamMap, expQParamMap);
}

function createContext(string httpMethod, string resPath) returns mediation:Context {
mediation:ResourcePath originalPath = checkpanic mediation:createImmutableResourcePath(resPath);
mediation:Context originalCtx =
mediation:createImmutableMediationContext(httpMethod, originalPath.pathSegments(), {}, {});
mediation:ResourcePath mutableResPath = checkpanic mediation:createMutableResourcePath(resPath);
return mediation:createMutableMediationContext(originalCtx, mutableResPath.pathSegments(), {}, {});
}
16 changes: 8 additions & 8 deletions mediation-json-to-xml/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "constraint"
version = "1.0.0"
version = "1.0.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down Expand Up @@ -62,7 +62,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.4.3"
version = "2.4.6"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "cache"},
Expand Down Expand Up @@ -93,7 +93,7 @@ modules = [
[[package]]
org = "ballerina"
name = "io"
version = "1.3.0"
version = "1.3.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.value"}
Expand Down Expand Up @@ -221,7 +221,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.0.5"
version = "1.0.6"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand All @@ -238,7 +238,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "regex"
version = "1.3.0"
version = "1.3.2"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.string"}
Expand Down Expand Up @@ -268,23 +268,23 @@ modules = [
[[package]]
org = "ballerina"
name = "time"
version = "2.2.2"
version = "2.2.3"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "url"
version = "2.2.2"
version = "2.2.3"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "xmldata"
version = "2.3.0"
version = "2.3.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down
2 changes: 1 addition & 1 deletion mediation-log-message/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "choreo"
name = "mediation.log_message"
version = "0.2.0"
version = "0.3.0"
distribution = "2201.2.3"
keywords=["choreo-apim-mediation-policy", "choreo-apim-mediation-request-flow", "choreo-apim-mediation-response-flow", "choreo-apim-mediation-fault-flow"]

Expand Down
21 changes: 12 additions & 9 deletions mediation-log-message/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "constraint"
version = "1.0.0"
version = "1.0.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down Expand Up @@ -62,7 +62,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.4.3"
version = "2.4.6"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "cache"},
Expand Down Expand Up @@ -93,7 +93,7 @@ modules = [
[[package]]
org = "ballerina"
name = "io"
version = "1.3.0"
version = "1.3.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.value"}
Expand Down Expand Up @@ -221,7 +221,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.0.5"
version = "1.0.6"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand All @@ -238,7 +238,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "regex"
version = "1.3.0"
version = "1.3.2"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.string"}
Expand All @@ -259,15 +259,15 @@ dependencies = [
[[package]]
org = "ballerina"
name = "time"
version = "2.2.2"
version = "2.2.3"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "url"
version = "2.2.2"
version = "2.2.3"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand All @@ -287,15 +287,18 @@ modules = [
[[package]]
org = "choreo"
name = "mediation"
version = "0.6.1"
version = "0.7.0"
dependencies = [
{org = "ballerina", name = "regex"}
]
modules = [
{org = "choreo", packageName = "mediation", moduleName = "mediation"}
]

[[package]]
org = "choreo"
name = "mediation.log_message"
version = "0.2.0"
version = "0.3.0"
dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "log"},
Expand Down
6 changes: 3 additions & 3 deletions mediation-log-message/policy.bal
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function logRequestMessage(mediation:Context ctx, http:Request req, boole
mediation\-flow: REQUEST,
request\-id: "",
http\-method: req.method,
resource\-path: ctx.resourcePath
resource\-path: ctx.originalContext().resourcePath().toString()
};

if req.hasHeader(HEADER_X_REQUEST_ID) {
Expand Down Expand Up @@ -65,7 +65,7 @@ public function logResponseMessage(mediation:Context ctx, http:Request req, http
mediation\-flow: RESPONSE,
request\-id: "",
http\-method: req.method,
resource\-path: ctx.resourcePath
resource\-path: ctx.originalContext().resourcePath().toString()
};

if res.hasHeader(HEADER_X_REQUEST_ID) {
Expand Down Expand Up @@ -101,7 +101,7 @@ public function logFaultMessage(mediation:Context ctx, http:Request req, http:Re
mediation\-flow: FAULT,
request\-id: "",
http\-method: req.method,
resource\-path: ctx.resourcePath
resource\-path: ctx.originalContext().resourcePath().toString()
};

// Since the fault flow may be triggered by both the request flow and the response flow, we take the x-request-id
Expand Down
2 changes: 1 addition & 1 deletion mediation-remove-header/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "choreo"
name = "mediation.remove_header"
version = "0.2.0"
version = "0.3.0"
distribution = "2201.2.3"
keywords=["choreo-apim-mediation-policy", "choreo-apim-mediation-request-flow", "choreo-apim-mediation-response-flow", "choreo-apim-mediation-fault-flow"]

Expand Down
Loading

0 comments on commit c7c93bd

Please sign in to comment.