Skip to content
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

[3.2.x] Add support for APIs with APIM 4.x versions #3582

Open
wants to merge 40 commits into
base: 3.2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ac36b60
Change DTOs to support both APIM 3.x and 4.x versions
HeshanSudarshana Jul 26, 2024
4c0c08d
Add apim4xVersion config to the toolkit-config.toml
HeshanSudarshana Jul 26, 2024
0df257b
Add DTOs related to the APIs export method
HeshanSudarshana Jul 28, 2024
81757f3
Refactor ExtendedAPI to support both APIM 3.x and 4x versions
HeshanSudarshana Jul 28, 2024
916e581
Add export APIs implementation for APIM 4.x versions
HeshanSudarshana Jul 28, 2024
a6b73d4
Fix issues in API export flow for APIM 4.x versions
HeshanSudarshana Jul 28, 2024
20492b9
Fix issues in API import flow for APIM 3.x versions
HeshanSudarshana Jul 29, 2024
b55fd8b
Update mustache files to support apiInfo field for ExtendedAPI
HeshanSudarshana Jul 29, 2024
bfb16b8
Add APIM 4.3.0 certificate to the truststore
HeshanSudarshana Jul 29, 2024
db63f4b
Validate label parameter for APIM 4.x API import flow
HeshanSudarshana Jul 29, 2024
9dd1a19
Handle invalid content-types for APIs export flow
HeshanSudarshana Jul 30, 2024
b4e77cd
Use not encoded gateway label for error messages
HeshanSudarshana Jul 30, 2024
eb7c5b0
Setup configs before parameter validation
HeshanSudarshana Jul 30, 2024
76e4534
Add change to handle APIM 4.x JMS messages
HeshanSudarshana Jul 31, 2024
a0ecf35
Add changes to handle APIM 4.x throttle decision JMS messages
HeshanSudarshana Aug 2, 2024
9af9176
Add changes to handle APIM 4.x token revocation JMS messages
HeshanSudarshana Aug 5, 2024
bdeb2bf
Add required scopes for the access token to support export APIs method
HeshanSudarshana Aug 19, 2024
83f9719
Add event model for APIM 4.x Analytics
HeshanSudarshana Sep 6, 2024
807deba
Add java event publishers for APIM 4.x Analytics
HeshanSudarshana Sep 6, 2024
b588e05
Add interop functions to APIM 4.x Analytics publishers
HeshanSudarshana Sep 6, 2024
c4fd204
Add changes required for the new Analytics model
HeshanSudarshana Sep 6, 2024
b826a46
Add configs to initialize ELK analytics configs and populate Fault data
HeshanSudarshana Sep 6, 2024
a284b90
Add required dependencies for APIM 4.x Analytics
HeshanSudarshana Sep 6, 2024
a11b691
Add ballerina constants required for APIM 4.x Analytics
HeshanSudarshana Sep 6, 2024
a21ea61
Add separate filter for APIM 4.x Analytics
HeshanSudarshana Sep 6, 2024
3daa425
Add additional properties for the analytics event for user based anal…
HeshanSudarshana Sep 8, 2024
a2823ec
Refactor analytics based dependencies
HeshanSudarshana Sep 9, 2024
af97855
Add default value to regionId and response details to the fault respo…
HeshanSudarshana Sep 10, 2024
788185f
Add default configurations for ELK analytics
HeshanSudarshana Sep 10, 2024
ee6b210
Add changes to support Choreo based analytics
HeshanSudarshana Sep 10, 2024
16adb2d
Add dependencies related to Choreo based analytics
HeshanSudarshana Sep 10, 2024
73c74f8
Add default configurations for Choreo based analytics
HeshanSudarshana Sep 10, 2024
3ee22e1
Add ExtendedAPIWrapper object to keep backward compatibility for must…
HeshanSudarshana Sep 11, 2024
1054ce5
Add the missing properties to ExtendedAPIWrapper object and refactor …
HeshanSudarshana Sep 13, 2024
4afb77c
Add validation for API name and version for APIM 4.x version import
HeshanSudarshana Sep 14, 2024
b3896b4
Change apim4xVersion config to apimVersion
HeshanSudarshana Sep 15, 2024
5c408c5
Refactor APIM 3x and 4x analytics to the analytics filter
HeshanSudarshana Sep 16, 2024
48afe28
Change the apis/export/apis API for the updated apis/export API
HeshanSudarshana Sep 18, 2024
6b6a88e
Refactor APIM 4.x related code
HeshanSudarshana Sep 26, 2024
cb3ebe0
Upgrade the analytics publisher client version to 1.2.20
HeshanSudarshana Sep 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public class ImportCmd implements LauncherCmd {
private boolean isOverwriteRequired;
private String restVersion;
private String dcrVersion;
private String apimVersion;
private Boolean apim4xVersion;

@Override
public void execute() {
Expand All @@ -139,6 +141,10 @@ public void execute() {
Config config = CmdUtils.getConfig();
isOverwriteRequired = false;

//setup endpoints
Token configToken = config.getToken();
TokenBuilder configTokenValues = setEndpoints(configToken);

validateAPIGetRequestParams(label, apiName, version);
//Setup username
String configuredUser = config.getToken().getUsername();
Expand Down Expand Up @@ -168,10 +174,6 @@ public void execute() {
}
}

//setup endpoints
Token configToken = config.getToken();
TokenBuilder configTokenValues = setEndpoints(configToken);

//configure trust store
String configuredTrustStore = config.getToken().getTrustStoreLocation();
if (StringUtils.isEmpty(configuredTrustStore)) {
Expand Down Expand Up @@ -256,12 +258,16 @@ public void execute() {

List<ExtendedAPI> apis = new ArrayList<>();
RESTAPIService service = new RESTAPIServiceImpl(publisherEndpoint, adminEndpoint, restVersion, isInsecure);
if (label != null) {
apis = service.getAPIs(label, accessToken);
if (apim4xVersion) {
apis = service.exportAPIs(apiName, version, label, accessToken, projectName);
} else {
ExtendedAPI api = service.getAPI(apiName, version, accessToken);
if (api != null) {
apis.add(api);
if (label != null) {
apis = service.getAPIs(label, accessToken);
} else {
ExtendedAPI api = service.getAPI(apiName, version, accessToken);
if (api != null) {
apis.add(api);
}
}
}

Expand All @@ -278,7 +284,7 @@ public void execute() {
//delete the folder if an exception is thrown in following steps
try {
CmdUtils.saveSwaggerDefinitionForMultipleAPIs(projectName, apis,
!restVersion.startsWith(CliConstants.REST_API_V1_PREFIX));
!restVersion.startsWith(CliConstants.REST_API_V1_PREFIX) && !apim4xVersion);
} catch (Exception e) {
throw new CLIInternalException("Exception occurred during codeGeneration process", e);
}
Expand All @@ -297,6 +303,7 @@ public void execute() {
.setClientSecret(encryptedCS)
.setTrustStoreLocation(trustStoreLocation)
.setTrustStorePassword(encryptedTrustStorePass)
.setApimVersion(apimVersion)
.build();
newConfig.setToken(token);
newConfig.setCorsConfiguration(config.getCorsConfiguration());
Expand Down Expand Up @@ -348,12 +355,24 @@ private static void init(String configPath) {
* @param version API version
*/
private void validateAPIGetRequestParams(String label, String apiName, String version) {
if ((StringUtils.isEmpty(label) && (StringUtils.isEmpty(apiName) || StringUtils.isEmpty(version))) ||
StringUtils.isNotEmpty(label) && (StringUtils.isNotEmpty(apiName) || StringUtils.isNotEmpty(version)) ||
(StringUtils.isEmpty(apiName) && StringUtils.isNotEmpty(version)) ||
(StringUtils.isNotEmpty(apiName) && StringUtils.isEmpty(version))) {
throw CmdUtils.createUsageException(
"Missing \"-l <label>\" or \"-a <api-name> -v <version>\" parameters");
if (apim4xVersion) {
if (StringUtils.isEmpty(label)) {
throw CmdUtils.createUsageException(
"Missing \"-l <label>\" parameter");
} else if (StringUtils.isNotEmpty(apiName) && StringUtils.isEmpty(version)) {
throw CmdUtils.createUsageException("Missing \"-v <version>\" parameter");
} else if (StringUtils.isNotEmpty(version) && StringUtils.isEmpty(apiName)) {
throw CmdUtils.createUsageException("Missing \"-a <api-name>\" parameter");
}
} else {
if ((StringUtils.isEmpty(label) && (StringUtils.isEmpty(apiName) || StringUtils.isEmpty(version))) ||
StringUtils.isNotEmpty(label) && (StringUtils.isNotEmpty(apiName) ||
StringUtils.isNotEmpty(version)) || (StringUtils.isEmpty(apiName) &&
StringUtils.isNotEmpty(version)) || (StringUtils.isNotEmpty(apiName) &&
StringUtils.isEmpty(version))) {
throw CmdUtils.createUsageException(
"Missing \"-l <label>\" or \"-a <api-name> -v <version>\" parameters");
}
}
}

Expand All @@ -377,6 +396,8 @@ private TokenBuilder setEndpoints(Token token) {
adminEndpoint = token.getAdminEndpoint();
registrationEndpoint = token.getRegistrationEndpoint();
tokenEndpoint = token.getTokenEndpoint();
apimVersion = token.getApimVersion();
apim4xVersion = token.isApim4xVersion();

//copy current token config values
configTokenValues.setPublisherEndpoint(publisherEndpoint);
Expand All @@ -386,6 +407,7 @@ private TokenBuilder setEndpoints(Token token) {
configTokenValues.setRestVersion(restVersion);
configTokenValues.setDCRVersion(dcrVersion);
configTokenValues.setBaseURL(token.getBaseURL());
configTokenValues.setApimVersion(apimVersion);

isEndPointsNeeded = StringUtils.isEmpty(publisherEndpoint) || StringUtils.isEmpty(adminEndpoint) || StringUtils
.isEmpty(registrationEndpoint) || StringUtils.isEmpty(tokenEndpoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.wso2.apimgt.gateway.cli.exception.CLIRuntimeException;
import org.wso2.apimgt.gateway.cli.model.config.CodeGen;
import org.wso2.apimgt.gateway.cli.model.rest.ext.ExtendedAPI;
import org.wso2.apimgt.gateway.cli.model.rest.ext.ExtendedAPIWrapper;
import org.wso2.apimgt.gateway.cli.model.template.BallerinaToml;
import org.wso2.apimgt.gateway.cli.model.template.GenSrcFile;
import org.wso2.apimgt.gateway.cli.model.template.service.BallerinaOperation;
Expand Down Expand Up @@ -119,13 +120,13 @@ public void generate(String projectName, boolean overwrite)
ExtendedAPI api = OpenAPICodegenUtils.generateAPIFromOpenAPIDef(openAPI, openAPIAsJson);
BallerinaService definitionContext;
OpenAPICodegenUtils.setAdditionalConfigsDevFirst(api, openAPI, path.toString());

definitionContext = new BallerinaService().buildContext(openAPI, api);
ExtendedAPIWrapper apiWrapper = new ExtendedAPIWrapper(api);
definitionContext = new BallerinaService().buildContext(openAPI, apiWrapper);
generateResourceFunctions(definitionContext, genFiles);
genFiles.add(generateService(definitionContext));
serviceList.add(definitionContext);
ballerinaToml.addDependencies(definitionContext);
copySwaggerToResourcesFolder(api.getName(), api.getVersion(), path);
copySwaggerToResourcesFolder(api.getApiInfo().getName(), api.getApiInfo().getVersion(), path);
} catch (BallerinaServiceGenException e) {
throw new CLIRuntimeException("Swagger definition cannot be parsed to ballerina code", e);
} catch (IOException e) {
Expand Down Expand Up @@ -228,7 +229,8 @@ private BallerinaService generateDefinitionContext(OpenAPI openAPI, String openA
}
BallerinaService definitionContext;
OpenAPICodegenUtils.setAdditionalConfigsDevFirst(api, openAPI, path.toString());
definitionContext = new BallerinaService().buildContext(openAPI, api);
ExtendedAPIWrapper apiWrapper = new ExtendedAPIWrapper(api);
definitionContext = new BallerinaService().buildContext(openAPI, apiWrapper);
return definitionContext;
}

Expand All @@ -240,7 +242,8 @@ private void copySwaggerToResourcesFolder(String apiName, String apiVersion, Pat
CliConstants.YAML_EXTENSION :
CliConstants.JSON_EXTENSION;
String destinationFilename = apiName + "_" + apiVersion + fileExtension;
CmdUtils.copyFilesToSources(openAPIFilePath.toString(), resourcesPath + File.separator + destinationFilename);
CmdUtils.copyFilesToSources(openAPIFilePath.toString(), resourcesPath + File.separator
+ destinationFilename);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,8 @@ public class CliConstants {
public static final String PROJECT_GRPC_DEFINITIONS_DIR = "grpc_definitions";
public static final String RESOURCES_GRPC_DIR = "grpc";
public static final String MICROGW_PROJECT_PLACEHOLDER = "\\$MGW-PROJECT_HOME";
public static final String DEPLOYMENTS_JSON = "deployments.json";
public static final String API_JSON = "api.json";
public static final String EXPORTED_API = "exported-api";
public static final String DEFINITIONS_DIR = "Definitions";
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class RESTServiceConstants {
"apis?query=name:" + CliConstants.API_NAME_PLACEHOLDER + "%20version:" + CliConstants.VERSION_PLACEHOLDER
+ "%20status:PUBLISHED&expand=" + CliConstants.EXPAND_PLACEHOLDER;
public static final String API_OPEN_API_GET_URI = "apis/" + CliConstants.API_ID_PLACEHOLDER + "/swagger";
public static final String APIS_EXPORT_URI = "apis/export";

public static final String CONFIG_REST_VERSION = "v1.2";
public static final String CONFIG_DCR_VERSION = "v0.17";
Expand All @@ -64,4 +65,5 @@ public class RESTServiceConstants {
public static final String CERTIFICATE_TIER = "Tier";

public static final String ALL_SERVICES_REGEX_PATH = "/*";
public static final String CONTENT_TYPE_APPLICATION_ZIP = "application/zip";
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class TokenManagementConstants {
public static final String APPLICATION_CALLBACK_URL = "https://wso2.org";
public static final String POLICY_VIEW_TOKEN_SCOPE = "apim:tier_view";
public static final String VIEW_API_SCOPE = "apim:api_view";
public static final String API_CREATE_SCOPE = "apim:api_create";
public static final String API_PUBLISH_SCOPE = "apim:api_publish";
public static final String API_IMPORT_EXPORT_SCOPE = "apim:api_import_export";
public static final String API_MANAGE_SCOPE = "apim:api_manage";
public static final String PASSWORD_GRANT_TYPE = "password";
public static final String CONTENT_TYPE = "Content-Type";
public static final String CONTENT_TYPE_APPLICATION_JSON = "application/json";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private static Map<String, String> getMapOfHashes(List objects) throws HashingEx
for (Object obj : objects) {
String hash = getAnnotatedHash(obj);
if (obj instanceof ExtendedAPI) {
hashes.put(((ExtendedAPI) obj).getId(), hash);
hashes.put(((ExtendedAPI) obj).getApiInfo().getId(), hash);
} else if (obj instanceof ThrottlePolicyDTO) {
hashes.put(((ThrottlePolicyDTO) obj).getPolicyId(), hash);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.wso2.apimgt.gateway.cli.model.config;

import org.wso2.apimgt.gateway.cli.model.rest.APICorsConfigurationDTO;
import org.wso2.apimgt.gateway.cli.model.rest.common.APICorsConfigurationDTO;

/**
* Configuration data holder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.wso2.apimgt.gateway.cli.model.config;

import org.apache.commons.lang3.StringUtils;
import org.wso2.apimgt.gateway.cli.utils.CmdUtils;

import java.io.File;
Expand All @@ -27,6 +28,7 @@
*/
public class Token {

private String apimVersion;
private String baseURL;
private String restVersion;
private String dcrVersion;
Expand Down Expand Up @@ -150,4 +152,19 @@ public String getDcrVersion() {
public void setDcrVersion(String dcrVersion) {
this.dcrVersion = dcrVersion;
}

public String getApimVersion() {
return apimVersion;
}

public void setApimVersion(String apimVersion) {
this.apimVersion = apimVersion;
}

public Boolean isApim4xVersion() {
if (StringUtils.isNotEmpty(apimVersion)) {
return apimVersion.startsWith("4.");
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public TokenBuilder setBaseURL(String baseURL) {
return this;
}

public TokenBuilder setApimVersion(String apimVersion) {
this.token.setApimVersion(apimVersion);
return this;
}

public Token build() {
return token;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.wso2.apimgt.gateway.cli.model.mgwcodegen;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.wso2.apimgt.gateway.cli.model.rest.APIEndpointSecurityDTO;
import org.wso2.apimgt.gateway.cli.model.rest.EndpointUrlTypeEnum;
import org.wso2.apimgt.gateway.cli.model.rest.apim3x.APIEndpointSecurityDTO;
import org.wso2.apimgt.gateway.cli.model.route.EndpointType;

import java.util.List;
Expand Down
Loading