From f270683320adcb54fd3c7e33af8ae74368d65a58 Mon Sep 17 00:00:00 2001 From: CrowleyRajapakse Date: Fri, 1 Dec 2023 12:31:58 +0530 Subject: [PATCH] adding api uuid for related other crs --- .../ballerina/APIClient.bal | 36 +++++++++++-------- .../ballerina/Dependencies.toml.template | 4 +-- .../ballerina/tests/APIClientTest.bal | 2 +- .../resources/tests/api/Interceptor.feature | 21 ----------- .../tests/api/resourceInterceptor.feature | 34 ++++++++++++++++++ 5 files changed, 58 insertions(+), 39 deletions(-) create mode 100644 test/cucumber-tests/src/test/resources/tests/api/resourceInterceptor.feature diff --git a/runtime/config-deployer-service/ballerina/APIClient.bal b/runtime/config-deployer-service/ballerina/APIClient.bal index 64e9f81ed7..0a6fdb796e 100644 --- a/runtime/config-deployer-service/ballerina/APIClient.bal +++ b/runtime/config-deployer-service/ballerina/APIClient.bal @@ -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) } }; @@ -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[] { @@ -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}}; (filters).push(scopeFilter); @@ -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: { @@ -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: { @@ -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: { @@ -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? { diff --git a/runtime/config-deployer-service/ballerina/Dependencies.toml.template b/runtime/config-deployer-service/ballerina/Dependencies.toml.template index 1783b7b2a2..3023a9e10d 100644 --- a/runtime/config-deployer-service/ballerina/Dependencies.toml.template +++ b/runtime/config-deployer-service/ballerina/Dependencies.toml.template @@ -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"}, @@ -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"}, diff --git a/runtime/config-deployer-service/ballerina/tests/APIClientTest.bal b/runtime/config-deployer-service/ballerina/tests/APIClientTest.bal index f8fbf73d62..ef7b7d8de0 100644 --- a/runtime/config-deployer-service/ballerina/tests/APIClientTest.bal +++ b/runtime/config-deployer-service/ballerina/tests/APIClientTest.bal @@ -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"); diff --git a/test/cucumber-tests/src/test/resources/tests/api/Interceptor.feature b/test/cucumber-tests/src/test/resources/tests/api/Interceptor.feature index 9edeee4300..27e5a37fb6 100644 --- a/test/cucumber-tests/src/test/resources/tests/api/Interceptor.feature +++ b/test/cucumber-tests/src/test/resources/tests/api/Interceptor.feature @@ -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 diff --git a/test/cucumber-tests/src/test/resources/tests/api/resourceInterceptor.feature b/test/cucumber-tests/src/test/resources/tests/api/resourceInterceptor.feature new file mode 100644 index 0000000000..93271f5287 --- /dev/null +++ b/test/cucumber-tests/src/test/resources/tests/api/resourceInterceptor.feature @@ -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 "" + Then the response status code should be + + Examples: + | apiID | expectedStatusCode | + | 547961eeaafed989119c45ffc13f8b87bfda821d | 202 | \ No newline at end of file