Skip to content

Commit

Permalink
adding api uuid for related other crs
Browse files Browse the repository at this point in the history
  • Loading branch information
CrowleyRajapakse committed Dec 14, 2023
1 parent 3a0cafc commit f270683
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 39 deletions.
36 changes: 21 additions & 15 deletions runtime/config-deployer-service/ballerina/APIClient.bal
Original file line number Diff line number Diff line change
Expand Up @@ -456,24 +456,25 @@ public class APIClient {
operationsArray[row][column] = item;
column = column + 1;
}
int count = 1;
foreach APKOperations[] item in operationsArray {
APKConf clonedAPKConf = apkConf.clone();
clonedAPKConf.operations = item.clone();
_ = check self.putHttpRouteForPartition(apiArtifact, clonedAPKConf, endpoint, uniqueId, endpointType, organization);
_ = check self.putHttpRouteForPartition(apiArtifact, clonedAPKConf, endpoint, uniqueId, endpointType, organization, count);
count = count + 1;
}
}

private isolated function putHttpRouteForPartition(model:APIArtifact apiArtifact, APKConf apkConf, model:Endpoint? endpoint, string uniqueId, string endpointType, commons:Organization organization) returns commons:APKError|error? {
string httpRouteRefName = self.retrieveHttpRouteRefName(apkConf, endpointType, organization);
private isolated function putHttpRouteForPartition(model:APIArtifact apiArtifact, APKConf apkConf, model:Endpoint? endpoint, string uniqueId, string endpointType, commons:Organization organization, int count) returns commons:APKError|error? {
model:Httproute httpRoute = {
metadata:
{
name: httpRouteRefName,
name: uniqueId + "-" + endpointType + "-httproute-" + count.toString(),
labels: self.getLabels(apkConf, organization)
},
spec: {
parentRefs: self.generateAndRetrieveParentRefs(apkConf, uniqueId),
rules: check self.generateHttpRouteRules(apiArtifact, apkConf, endpoint, endpointType, organization, httpRouteRefName),
rules: check self.generateHttpRouteRules(apiArtifact, apkConf, endpoint, endpointType, organization),
hostnames: self.getHostNames(apkConf, uniqueId, endpointType, organization)
}
};
Expand All @@ -497,7 +498,7 @@ public class APIClient {
return parentRefs;
}

private isolated function generateHttpRouteRules(model:APIArtifact apiArtifact, APKConf apkConf, model:Endpoint? endpoint, string endpointType, commons:Organization organization, string httpRouteRefName) returns model:HTTPRouteRule[]|commons:APKError|error {
private isolated function generateHttpRouteRules(model:APIArtifact apiArtifact, APKConf apkConf, model:Endpoint? endpoint, string endpointType, commons:Organization organization) returns model:HTTPRouteRule[]|commons:APKError|error {
model:HTTPRouteRule[] httpRouteRules = [];
APKOperations[]? operations = apkConf.operations;
if operations is APKOperations[] {
Expand All @@ -520,12 +521,14 @@ public class APIClient {
}
string[]? scopes = operation.scopes;
if scopes is string[] {
int count = 1;
foreach string scope in scopes {
model:Scope scopeCr;
if apiArtifact.scopes.hasKey(scope) {
scopeCr = apiArtifact.scopes.get(scope);
} else {
scopeCr = self.generateScopeCR(apiArtifact, apkConf, organization, scope);
scopeCr = self.generateScopeCR(apiArtifact, apkConf, organization, scope, count);
count = count + 1;
}
model:HTTPRouteFilter scopeFilter = {'type: "ExtensionRef", extensionRef: {group: "dp.wso2.com", kind: scopeCr.kind, name: scopeCr.metadata.name}};
(<model:HTTPRouteFilter[]>filters).push(scopeFilter);
Expand Down Expand Up @@ -592,11 +595,10 @@ public class APIClient {
return ();
}

private isolated function generateScopeCR(model:APIArtifact apiArtifact, APKConf apkConf, commons:Organization organization, string scope) returns model:Scope {
string scopeName = uuid:createType1AsString();
private isolated function generateScopeCR(model:APIArtifact apiArtifact, APKConf apkConf, commons:Organization organization, string scope, int count) returns model:Scope {
model:Scope scopeCr = {
metadata: {
name: scopeName,
name: apiArtifact.uniqueId + "-scope-" + count.toString(),
labels: self.getLabels(apkConf, organization)
},
spec: {
Expand Down Expand Up @@ -981,7 +983,7 @@ public class APIClient {
if rateLimit != () {
rateLimitPolicyCR = {
metadata: {
name: self.retrieveRateLimitPolicyRefName(operation),
name: self.retrieveRateLimitPolicyRefName(operation, targetRefName),
labels: self.getLabels(apkConf, organization)
},
spec: {
Expand Down Expand Up @@ -1009,9 +1011,13 @@ public class APIClient {

public isolated function generateAPIPolicyCR(APKConf apkConf, string targetRefName, APKOperations? operation, commons:Organization organization, model:APIPolicyData policyData) returns model:APIPolicy? {
model:APIPolicy? apiPolicyCR = ();
string optype = "api";
if operation is APKOperations {
optype = "resource";
}
apiPolicyCR = {
metadata: {
name: self.retrieveAPIPolicyRefName(),
name: targetRefName +"-"+ optype + "-policy",
labels: self.getLabels(apkConf, organization)
},
spec: {
Expand Down Expand Up @@ -1369,11 +1375,11 @@ public class APIClient {
return uuid:createType1AsString();
}

public isolated function retrieveRateLimitPolicyRefName(APKOperations? operation) returns string {
public isolated function retrieveRateLimitPolicyRefName(APKOperations? operation, string targetRef) returns string {
if operation is APKOperations {
return uuid:createType1AsString();
return "resource-"+ targetRef;
} else {
return "api-" + uuid:createType1AsString();
return "api-" + targetRef;
}
}
private isolated function validateAndRetrieveAPKConfiguration(json apkconfJson) returns APKConf|commons:APKError? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ modules = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.0"
version = "2.10.4"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "cache"},
Expand Down Expand Up @@ -302,7 +302,7 @@ modules = [
[[package]]
org = "ballerina"
name = "sql"
version = "1.11.0"
version = "1.11.1"
dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public function testOperationLevelRateLimitConfigGenerationFromAPKConf() returns
}
};

test:assertEquals(apiArtifact.rateLimitPolicies.length(), 2, "Required number of Rate Limit policies not found");
test:assertEquals(apiArtifact.rateLimitPolicies.length(), 1, "Required number of Rate Limit policies not found");
foreach model:RateLimitPolicy rateLimitPolicy in apiArtifact.rateLimitPolicies {
test:assertEquals(rateLimitPolicy.spec.'default, rateLimitData, "Rate limit policy is not equal to expected Rate limit config");
test:assertEquals(rateLimitPolicy.spec.targetRef.kind, "Resource", "Rate limit type is not equal to expected Rate limit type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,6 @@ Feature: API Deployment with Interceptor
And the response body should contain "\"Interceptor-Header\": \"Interceptor-header-value\""
Then the response status code should be 200
Then the response headers contains key "interceptor-response-header" and value "Interceptor-Response-header-value"
Then I use the APK Conf file "artifacts/apk-confs/interceptors/resourceLevelInterptor.apk-conf"
And the definition file "artifacts/definitions/cors_api.yaml"
And make the API deployment request
Then the response status code should be 200
And the response body should contain "547961eeaafed989119c45ffc13f8b87bfda821d"
And I wait for 1 minute
Then I set headers
|Authorization|bearer ${accessToken}|
And I send "GET" request to "https://default.gw.wso2.com:9095/interceptor/1.0.0/get" with body ""
And the response body should not contain "\"Interceptor-Header\""
Then the response status code should be 200
Then the response headers not contains key "interceptor-response-header"
Then I set headers
|Authorization|bearer ${accessToken}|
And I send "GET" request to "https://default.gw.wso2.com:9095/interceptor/1.0.0/headers" with body ""
And the response body should contain
|"Interceptor-Header": "Interceptor-header-value"|
|"Interceptor-Header-Apigroup": "Gold"|
|"Interceptor-Header-Apitier": "Unlimited"|
Then the response status code should be 200
Then the response headers contains key "interceptor-response-header" and value "Interceptor-Response-header-value"
Then I use the APK Conf file "artifacts/apk-confs/interceptors/withRequestAndResponsetls.apk-conf"
And the definition file "artifacts/definitions/cors_api.yaml"
And make the API deployment request
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Feature: API Deployment with Resource Interceptor
Scenario: Deploying an API
Given The system is ready
And I have a valid subscription
When I use the APK Conf file "artifacts/apk-confs/interceptors/resourceLevelInterptor.apk-conf"
And the definition file "artifacts/definitions/cors_api.yaml"
And make the API deployment request
Then the response status code should be 200
And the response body should contain "547961eeaafed989119c45ffc13f8b87bfda821d"
And I wait for 1 minute
Then I set headers
|Authorization|bearer ${accessToken}|
And I send "GET" request to "https://default.gw.wso2.com:9095/interceptor/1.0.0/get" with body ""
And the response body should not contain "\"Interceptor-Header\""
Then the response status code should be 200
Then the response headers not contains key "interceptor-response-header"
Then I set headers
|Authorization|bearer ${accessToken}|
And I send "GET" request to "https://default.gw.wso2.com:9095/interceptor/1.0.0/headers" with body ""
And the response body should contain
|"Interceptor-Header": "Interceptor-header-value"|
|"Interceptor-Header-Apigroup": "Gold"|
|"Interceptor-Header-Apitier": "Unlimited"|
Then the response status code should be 200
Then the response headers contains key "interceptor-response-header" and value "Interceptor-Response-header-value"
Scenario Outline: Undeploy an API
Given The system is ready
And I have a valid subscription
When I undeploy the API whose ID is "<apiID>"
Then the response status code should be <expectedStatusCode>

Examples:
| apiID | expectedStatusCode |
| 547961eeaafed989119c45ffc13f8b87bfda821d | 202 |

0 comments on commit f270683

Please sign in to comment.