Skip to content

Commit

Permalink
Add config deployer changes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
sgayangi committed Jul 5, 2024
1 parent 81423ab commit e4280a4
Show file tree
Hide file tree
Showing 14 changed files with 364 additions and 121 deletions.
53 changes: 26 additions & 27 deletions runtime/config-deployer-service/ballerina/APIClient.bal
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ public class APIClient {
}
isolated function isPolicyEmpty(APIOperationPolicies? policies) returns boolean {
if policies is APIOperationPolicies {
APKOperationPolicy[]? request = policies.request;
if request is APKOperationPolicy[] {
APKRequestOperationPolicy[]? request = policies.request;
if request is APKRequestOperationPolicy[] {
if (request.length() > 0) {
return false;
}
}
APKOperationPolicy[]? response = policies.response;
if response is APKOperationPolicy[] {
APKResponseOperationPolicy[]? response = policies.response;
if response is APKResponseOperationPolicy[] {
if (response.length() > 0) {
return false;
}
Expand Down Expand Up @@ -694,7 +694,7 @@ public class APIClient {

private isolated function generateAPIPolicyAndBackendCR(model:APIArtifact apiArtifact, APKConf apkConf, APKOperations? operations, APIOperationPolicies? policies, commons:Organization organization, string targetRefName) returns model:APIPolicy?|error {
model:APIPolicyData defaultSpecData = {};
APKOperationPolicy[]? request = policies?.request;
APKRequestOperationPolicy[]? request = policies?.request;
any[] requestPolicy = check self.retrieveAPIPolicyDetails(apiArtifact, apkConf, operations, organization, request, "request");
foreach any item in requestPolicy {
if item is model:InterceptorReference {
Expand All @@ -703,7 +703,7 @@ public class APIClient {
defaultSpecData.backendJwtPolicy = item;
}
}
APKOperationPolicy[]? response = policies?.response;
APKResponseOperationPolicy[]? response = policies?.response;
any[] responseInterceptor = check self.retrieveAPIPolicyDetails(apiArtifact, apkConf, operations, organization, response, "response");
foreach any item in responseInterceptor {
if item is model:InterceptorReference {
Expand Down Expand Up @@ -817,21 +817,21 @@ public class APIClient {
APIOperationPolicies? operationPoliciesToUse = ();
APIOperationPolicies? operationPolicies = apkConf.apiPolicies;
if (operationPolicies is APIOperationPolicies && operationPolicies != {}) {
if operationPolicies.request is APKOperationPolicy[] || operationPolicies.response is APKOperationPolicy[] {
if operationPolicies.request is APKRequestOperationPolicy[] || operationPolicies.response is APKResponseOperationPolicy[] {
operationPoliciesToUse = apkConf.apiPolicies;
}
} else {
operationPoliciesToUse = operation.operationPolicies;
}
if operationPoliciesToUse is APIOperationPolicies {
APKOperationPolicy[]? requestPolicies = operationPoliciesToUse.request;
APKOperationPolicy[]? responsePolicies = operationPoliciesToUse.response;
if requestPolicies is APKOperationPolicy[] && requestPolicies.length() > 0 {
APKRequestOperationPolicy[]? requestPolicies = operationPoliciesToUse.request;
APKResponseOperationPolicy[]? responsePolicies = operationPoliciesToUse.response;
if requestPolicies is APKRequestOperationPolicy[] && requestPolicies.length() > 0 {
model:HTTPRouteFilter[] requestHttpRouteFilters = [];
[requestHttpRouteFilters, hasRedirectPolicy] = self.extractHttpRouteFilter(apiArtifact, apkConf, operation, endpoint, requestPolicies, organization, true);
routeFilters.push(...requestHttpRouteFilters);
}
if responsePolicies is APKOperationPolicy[] && responsePolicies.length() > 0 {
if responsePolicies is APKResponseOperationPolicy[] && responsePolicies.length() > 0 {
model:HTTPRouteFilter[] responseHttpRouteFilters = [];
[responseHttpRouteFilters, _] = self.extractHttpRouteFilter(apiArtifact, apkConf, operation, endpoint, responsePolicies, organization, false);
routeFilters.push(...responseHttpRouteFilters);
Expand Down Expand Up @@ -865,23 +865,22 @@ public class APIClient {
if policy is HeaderModifierPolicy {
HeaderModifierPolicyParameters policyParameters = policy.parameters;
match policy.policyName {
AddHeaders => {
ModifierHeader[] headers = <ModifierHeader[]>policyParameters.headers;
foreach ModifierHeader header in headers {
addHeaders.push(header);
}
AddHeader => {
model:HTTPHeader addHeader = {
name: policyParameters.headerName,
value: <string>policyParameters.headerValue
};
addHeaders.push(addHeader);
}
SetHeaders => {
ModifierHeader[] headers = <ModifierHeader[]>policyParameters.headers;
foreach ModifierHeader header in headers {
setHeaders.push(header);
}
SetHeader => {
model:HTTPHeader setHeader = {
name: policyParameters.headerName,
value: <string>policyParameters.headerValue
};
setHeaders.push(setHeader);
}
RemoveHeaders => {
string[] headers = <string[]>policyParameters.headers;
foreach string header in headers {
removeHeaders.push(header);
}
RemoveHeader => {
removeHeaders.push(policyParameters.headerName);
}
}
} else if policy is RequestMirrorPolicy {
Expand Down Expand Up @@ -1400,7 +1399,7 @@ public class APIClient {
model:BackendJWT backendJwt = self.retrieveBackendJWTPolicy(apkConf, apiArtifact, backendJWTPolicy, operations, organization);
apiArtifact.backendJwt = backendJwt;
policyReferences.push(<model:BackendJwtReference>{name: backendJwt.metadata.name});
} else if policyName != AddHeaders && policyName != SetHeaders && policyName != RemoveHeaders && policyName != RequestMirror && policyName != RequestRedirect {
} else if policyName != AddHeader && policyName != SetHeader && policyName != RemoveHeader && policyName != RequestMirror && policyName != RequestRedirect {
return e909052(error("Incorrect API Policy name provided."));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ components:
request:
type: array
items:
$ref: "#/components/schemas/APKOperationPolicy"
$ref: "#/components/schemas/APKRequestOperationPolicy"
response:
type: array
items:
$ref: "#/components/schemas/APKOperationPolicy"
$ref: "#/components/schemas/APKResponseOperationPolicy"
additionalProperties: false
APKOperationPolicy:
APKRequestOperationPolicy:
title: API Operation Policy
oneOf:
- $ref: "#/components/schemas/InterceptorPolicy"
Expand All @@ -264,11 +264,25 @@ components:
mapping:
BackendJwt: "#/components/schemas/BackendJWTPolicy"
Interceptor: "#/components/schemas/InterceptorPolicy"
AddHeaders: "#/components/schemas/HeaderModifierPolicy"
SetHeaders: "#/components/schemas/HeaderModifierPolicy"
RemoveHeaders: "#/components/schemas/HeaderModifierPolicy"
AddHeader: "#/components/schemas/HeaderModifierPolicy"
SetHeader: "#/components/schemas/HeaderModifierPolicy"
RemoveHeader: "#/components/schemas/HeaderModifierPolicy"
RequestMirror: "#/components/schemas/RequestMirrorPolicy"
RequestRedirect: "#/components/schemas/RequestRedirectPolicy"
APKResponseOperationPolicy:
title: API Operation Policy
oneOf:
- $ref: "#/components/schemas/InterceptorPolicy"
- $ref: "#/components/schemas/BackendJWTPolicy"
- $ref: "#/components/schemas/HeaderModifierPolicy"
discriminator:
propertyName: "policyName"
mapping:
BackendJwt: "#/components/schemas/BackendJWTPolicy"
Interceptor: "#/components/schemas/InterceptorPolicy"
AddHeader: "#/components/schemas/HeaderModifierPolicy"
SetHeader: "#/components/schemas/HeaderModifierPolicy"
RemoveHeader: "#/components/schemas/HeaderModifierPolicy"
BaseOperationPolicy:
title: API Operation Policy
required:
Expand All @@ -278,9 +292,9 @@ components:
policyName:
type: string
enum:
- AddHeaders
- RemoveHeaders
- SetHeaders
- AddHeader
- RemoveHeader
- SetHeader
- Interceptor
- BackendJwt
policyVersion:
Expand Down Expand Up @@ -511,20 +525,13 @@ components:
title: Header Modifier Parameters
type: object
properties:
headers:
type: array
items:
oneOf:
- $ref: "#/components/schemas/Header"
- type: string
additionalProperties: false
Header:
type: object
properties:
name:
headerName:
type: string
value:
headerValue:
type: string
required:
- headerName
additionalProperties: false
RequestMirrorPolicy:
title: Request Mirror Parameters
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,9 @@
"type": "string",
"description": "The name of the operation policy.",
"enum": [
"AddHeaders",
"RemoveHeaders",
"SetHeaders",
"AddHeader",
"RemoveHeader",
"SetHeader",
"Interceptor",
"BackendJwt"
]
Expand Down
34 changes: 16 additions & 18 deletions runtime/config-deployer-service/ballerina/types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,13 @@ public type APKOperations record {
string[] scopes?;
};

# Common type for operation policies.
public type APKOperationPolicy InterceptorPolicy|BackendJWTPolicy|HeaderModifierPolicy|RequestMirrorPolicy|RequestRedirectPolicy;
public type APKOperationPolicy APKRequestOperationPolicy|APKResponseOperationPolicy;

# Common type for request operation policies.
public type APKRequestOperationPolicy InterceptorPolicy|BackendJWTPolicy|HeaderModifierPolicy|RequestMirrorPolicy|RequestRedirectPolicy;

# Common type for response operation policies.
public type APKResponseOperationPolicy InterceptorPolicy|BackendJWTPolicy|HeaderModifierPolicy;

# Header modification configuration for an operation.
#
Expand All @@ -139,18 +144,11 @@ public type HeaderModifierPolicy record {

# Configuration for header modifiers as received from the apk-conf file.
#
# + headers - Headers to be added, set or removed.
# + headerName - Header name to be added, set or removed.
# + headerValue - Header value to be added, set or removed.
public type HeaderModifierPolicyParameters record {|
ModifierHeader[]|string[] headers;
|};

# Configuration for headers.
#
# + name - The name of the header.
# + value - The value of the header.
public type ModifierHeader record {|
string name;
string value;
string headerName;
string headerValue?;
|};

# Request mirror configuration for an operation.
Expand Down Expand Up @@ -199,8 +197,8 @@ public type DeployApiBody record {
# + request - List of policies to be applied on the request.
# + response - List of policies to be applied on the response.
public type APIOperationPolicies record {
APKOperationPolicy[] request?;
APKOperationPolicy[] response?;
APKRequestOperationPolicy[] request?;
APKResponseOperationPolicy[] response?;
};

# Additional properties for APK configuration.
Expand Down Expand Up @@ -373,9 +371,9 @@ public type BaseOperationPolicy record {
public enum PolicyName {
BackendJwt,
Interceptor,
AddHeaders,
SetHeaders,
RemoveHeaders,
AddHeader,
SetHeader,
RemoveHeader,
RequestMirror,
RequestRedirect
}
Expand Down
Loading

0 comments on commit e4280a4

Please sign in to comment.