Skip to content

Commit

Permalink
Fix: Revision create, delete, revision deploy, revision undeploy, rev…
Browse files Browse the repository at this point in the history
…ision restore scenarios
  • Loading branch information
BLasan committed Sep 6, 2024
1 parent 35f6b89 commit dcf2e6d
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public interface APIProvider extends APIManager {
Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer replyLimit, Integer replyOffset) throws
APIManagementException;
void deleteCustomBackendByID(String backendUUID, String apiUUID, String type) throws APIManagementException;
InputStream getCustomBackendSequenceByAPIAndRevisionUUUID(String apiUUID, String revisionUUID, String type) throws APIManagementException;

/**
* @param apiTypeWrapper Api type wrapper
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.wso2.carbon.apimgt.api.model;

import java.io.InputStream;

public class CustomBackendData {
private String Id;
private InputStream sequence;
private String type;
private String name;
private String apiUUID;
private String revisionUUID;

public InputStream getSequence() {
return sequence;
}

public void setSequence(InputStream sequence) {
this.sequence = sequence;
}

public String getId() {
return Id;
}

public void setId(String id) {
Id = id;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getApiUUID() {
return apiUUID;
}

public void setApiUUID(String apiUUID) {
this.apiUUID = apiUUID;
}

public String getRevisionUUID() {
return revisionUUID;
}

public void setRevisionUUID(String revisionUUID) {
this.revisionUUID = revisionUUID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ private void unDeployAPI(APIGatewayAdmin apiGatewayAdmin, DeployAPIInGatewayEven
}

GatewayUtils.setCustomSequencesToBeRemoved(api, gatewayAPIDTO);
// GatewayUtils.setCustomBackendToBeRemoved(api, gatewayAPIDTO);
GatewayUtils.setCustomBackendToBeRemoved(gatewayAPIDTO);

}
gatewayAPIDTO.setLocalEntriesToBeRemove(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1765,7 +1765,7 @@ private ConfigParameters() {
public static final String ENDPOINT_TYPE_SERVICE = "service";
public static final String ENDPOINT_TYPE_ADDRESS = "address";
public static final String ENDPOINT_TYPE_AWSLAMBDA = "awslambda";
public static final String ENDPOINT_TYPE_SEQUENCE = "custom_sequence";
public static final String ENDPOINT_TYPE_SEQUENCE = "custom_backend";
public static final String SEQUENCE_DATA = "sequence";
public static final String ENDPOINT_PRODUCTION_FAILOVERS = "production_failovers";
public static final String ENDPOINT_SANDBOX_FAILOVERS = "sandbox_failovers";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.common.util.Hash;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
Expand Down Expand Up @@ -1066,6 +1065,11 @@ public Map<String, Object> getCustomBackendOfAPIByUUID(String customBackendUUID,
return apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID, type);
}

@Override
public InputStream getCustomBackendSequenceByAPIAndRevisionUUUID(String apiUUID, String revisionUUID, String type) throws APIManagementException {
return apiMgtDAO.getCustomBackendSequenceByAPIandRevisionUUID(apiUUID, revisionUUID, type);
}

@Override public void addNewCustomBackendForRevision(String revisionUUID, String updatedBackendUUID, String apiUUID,
Map<String, Object> config) throws APIManagementException {
if (config.get("production") != null) {
Expand Down Expand Up @@ -5940,6 +5944,7 @@ public String addAPIRevision(APIRevision apiRevision, String organization) throw
ExceptionCodes.from(ExceptionCodes.API_REVISION_UUID_NOT_FOUND));
}
apiRevision.setRevisionUUID(revisionUUID);

try {
apiMgtDAO.addAPIRevision(apiRevision);
} catch (APIManagementException e) {
Expand Down Expand Up @@ -6521,7 +6526,6 @@ public void deleteAPIRevision(String apiId, String apiRevisionId, String organiz
ERROR_DELETING_API_REVISION,apiRevision.getApiUUID()));
}
apiMgtDAO.deleteAPIRevision(apiRevision);
apiMgtDAO.deleteCustomBackendByRevision(apiId, apiRevisionId);
gatewayArtifactsMgtDAO.deleteGatewayArtifact(apiRevision.getApiUUID(), apiRevision.getRevisionUUID());
if (artifactSaver != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.wso2.carbon.apimgt.api.model.BlockConditionsDTO;
import org.wso2.carbon.apimgt.api.model.Comment;
import org.wso2.carbon.apimgt.api.model.CommentList;
import org.wso2.carbon.apimgt.api.model.CustomBackendData;
import org.wso2.carbon.apimgt.api.model.DeployedAPIRevision;
import org.wso2.carbon.apimgt.api.model.Environment;
import org.wso2.carbon.apimgt.api.model.GatewayPolicyData;
Expand Down Expand Up @@ -11193,14 +11194,37 @@ public Map<String, Object> retrieveCustomBackendOfAPIRevision(String apiUUID, St
return map;
}

public InputStream getCustomBackendSequenceByAPIandRevisionUUID(String apiUUID, String revisionUUID, String type) throws APIManagementException {
String sqlQuery = SQLConstants.CustomBackendConstants.GET_CUSTOM_BACKEND_FROM_API_AND_REVISION_UUID;
boolean isRev = checkAPIUUIDIsARevisionUUID(apiUUID) != null;
InputStream sequence = null;
try (Connection con = APIMgtDBUtil.getConnection();
PreparedStatement pstmt = con.prepareStatement(sqlQuery)) {
pstmt.setString(1, apiUUID);
pstmt.setString(2, revisionUUID);
pstmt.setString(3, type);
try (ResultSet rs = pstmt.executeQuery()) {
while(rs.next()) {
sequence = rs.getBinaryStream("SEQUENCE");
}
}
} catch (SQLException ex) {
handleException("Error when retrieving Custom Backend of API: "+apiUUID, ex);
}
return sequence;
}

public InputStream getCustomBackendSequenceOfAPIByUUID(String backendUUID, String apiUUID, String type) throws APIManagementException {
String sqlQuery = SQLConstants.CustomBackendConstants.GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID;
boolean isRev = checkAPIUUIDIsARevisionUUID(apiUUID) != null;
ResultSet rs = null;
InputStream sequence = null;
try (Connection con = APIMgtDBUtil.getConnection();
PreparedStatement ps = con.prepareStatement(sqlQuery)) {
ps.setString(1, backendUUID);
ps.setString(2, apiUUID);
ps.setString(3, type);

rs = ps.executeQuery();
while(rs.next()) {
try (InputStream in = rs.getBinaryStream("SEQUENCE")) {
Expand Down Expand Up @@ -17189,8 +17213,6 @@ public void addAPIRevision(APIRevision apiRevision) throws APIManagementExceptio
insertScopeResourceMappingStatement.executeBatch();
insertProductResourceMappingStatement.executeBatch();
revisionAPIPolicies(apiRevision, tenantDomain, uriTemplateMap, connection);
// Add Custom Backend
revisionCustomBackend(apiRevision, connection);

// Adding to AM_API_CLIENT_CERTIFICATE
String getClientCertificatesQuery = SQLConstants.APIRevisionSqlConstants.GET_CLIENT_CERTIFICATES_OF_KEY_TYPE;
Expand Down Expand Up @@ -17262,6 +17284,8 @@ public void addAPIRevision(APIRevision apiRevision) throws APIManagementExceptio
insertGraphQLComplexityStatement.executeBatch();
updateLatestRevisionNumber(connection, apiRevision.getApiUUID(), apiRevision.getId());
addAPIRevisionMetaData(connection, apiRevision.getApiUUID(), apiRevision.getRevisionUUID());
// Add Custom Backend
revisionCustomBackend(apiRevision, connection);
connection.commit();
} catch (SQLException e) {
connection.rollback();
Expand Down Expand Up @@ -18113,6 +18137,7 @@ public void restoreAPIRevision(APIRevision apiRevision) throws APIManagementExce
}

restoreAPIPolicies(apiRevision, tenantDomain, uriTemplateMap, connection);
restoreCustomBackend(apiRevision, connection);
insertScopeResourceMappingStatement.executeBatch();
insertProductResourceMappingStatement.executeBatch();

Expand Down Expand Up @@ -18250,6 +18275,9 @@ public void deleteAPIRevision(APIRevision apiRevision) throws APIManagementExcep
// Removing related revision entries from operation policies
deleteAllAPISpecificOperationPoliciesByAPIUUID(connection, apiRevision.getApiUUID(), apiRevision.getRevisionUUID());

// Removing related Custom Backend entries
deleteAllCustomBackendsOfAPIRevision(apiRevision.getApiUUID(), apiRevision.getRevisionUUID(), connection);

connection.commit();
} catch (SQLException e) {
connection.rollback();
Expand All @@ -18262,6 +18290,18 @@ public void deleteAPIRevision(APIRevision apiRevision) throws APIManagementExcep
}
}

private void deleteAllCustomBackendsOfAPIRevision(String apiUUID, String revisionUUID, Connection connection) throws APIManagementException {
String deleteSqlQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION;
try (PreparedStatement pstmt = connection.prepareStatement(deleteSqlQuery)) {
connection.setAutoCommit(false);
pstmt.setString(1, apiUUID);
pstmt.setString(2, revisionUUID);
pstmt.executeUpdate();
} catch (SQLException ex) {
handleException("Error when deleting Custom Backend of API: " + apiUUID, ex);
}
}

/**
* Adds an API Product revision record to the database
*
Expand Down Expand Up @@ -21153,23 +21193,48 @@ private void revisionCustomBackend(APIRevision apiRevision, Connection connectio
throws SQLException, APIManagementException {
String addCBSqlQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND;
String getCBSQLQuery = SQLConstants.CustomBackendConstants.GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS;
ResultSet rs = null;
try (Connection con = APIMgtDBUtil.getConnection();
PreparedStatement getPstmt = con.prepareStatement(getCBSQLQuery);
PreparedStatement addPstmt = con.prepareStatement(addCBSqlQuery)) {
con.setAutoCommit(false);
try (PreparedStatement getPstmt = connection.prepareStatement(getCBSQLQuery);
PreparedStatement addPstmt = connection.prepareStatement(addCBSqlQuery)) {
connection.setAutoCommit(false);
getPstmt.setString(1, apiRevision.getApiUUID());
rs = getPstmt.executeQuery();
while (rs.next()) {
addPstmt.setString(1, rs.getString("ID"));
addPstmt.setString(2, apiRevision.getApiUUID());
addPstmt.setBinaryStream(3, rs.getBinaryStream("SEQUENCE"));
addPstmt.setString(4, rs.getString("TYPE"));
addPstmt.setString(5, apiRevision.getRevisionUUID());
addPstmt.setString(6, rs.getString("NAME"));
addPstmt.addBatch();
}
addPstmt.executeBatch();
List<CustomBackendData> customBackendDataList = new ArrayList<>();
int count = 0;

try (ResultSet rs = getPstmt.executeQuery()) {
while (rs.next()) {
addPstmt.setString(1, rs.getString("ID"));
addPstmt.setString(2, apiRevision.getApiUUID());
addPstmt.setBinaryStream(3, rs.getBinaryStream("SEQUENCE"));
addPstmt.setString(4, rs.getString("TYPE"));
addPstmt.setString(5, apiRevision.getRevisionUUID());
addPstmt.setString(6, rs.getString("NAME"));
addPstmt.addBatch();
count++;

// CustomBackendData customBackendData = new CustomBackendData();
// customBackendData.setId(rs.getString("ID"));
// customBackendData.setName(rs.getString("NAME"));
// customBackendData.setApiUUID(apiRevision.getApiUUID());
// customBackendData.setRevisionUUID(apiRevision.getRevisionUUID());
// customBackendData.setSequence(rs.getBinaryStream("SEQUENCE"));
// customBackendData.setType(rs.getString("TYPE"));
// customBackendDataList.add(customBackendData);
}
}

// for(CustomBackendData customBackendData: customBackendDataList) {
// addPstmt.setString(1, customBackendData.getId());
// addPstmt.setString(2, customBackendData.getApiUUID());
// addPstmt.setBinaryStream(3, customBackendData.getSequence());
// addPstmt.setString(4, customBackendData.getType());
// addPstmt.setString(5, customBackendData.getRevisionUUID());
// addPstmt.setString(6, customBackendData.getName());
// addPstmt.addBatch();
// }

if (count > 0) {
addPstmt.executeBatch();
}
} catch (SQLException ex) {
handleException("Error while adding Custom Backends to the database of API: " + apiRevision.getApiUUID(),
ex);
Expand Down Expand Up @@ -21283,6 +21348,35 @@ private void handlePolicyCloningWhenRevisioning(OperationPolicy policy, String a
}
}

private void restoreCustomBackend(APIRevision apiRevision, Connection connection) throws SQLException {
String deleteSql = SQLConstants.CustomBackendConstants.DELETE_WORKING_COPY_OF_CUSTOM_BACKEND;
String getSql = SQLConstants.CustomBackendConstants.GET_CUSTOM_BACKEND_OF_API_REVISION;
String addSql = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND;
try (PreparedStatement pstmt = connection.prepareStatement(deleteSql);
PreparedStatement pstmtGet = connection.prepareStatement(getSql);
PreparedStatement pstmtAdd = connection.prepareStatement(addSql)) {
connection.setAutoCommit(false);
pstmt.setString(1, apiRevision.getApiUUID());
pstmt.executeUpdate();

pstmtGet.setString(1, apiRevision.getApiUUID());
pstmtGet.setString(2, apiRevision.getRevisionUUID());

try(ResultSet rs = pstmtGet.executeQuery()) {
while(rs.next()) {
pstmtAdd.setString(1, rs.getString("ID"));
pstmtAdd.setString(2, apiRevision.getApiUUID());
pstmtAdd.setBinaryStream(3, rs.getBinaryStream("SEQUENCE"));
pstmtAdd.setString(4, rs.getString("TYPE"));
pstmtAdd.setString(5, "0");
pstmtAdd.setString(6, rs.getString("NAME"));
pstmtAdd.addBatch();
}
}
pstmtAdd.executeBatch();
}
}

/**
* Restore a revision of API policies. This will copy the policy and policy mapping to working copy.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4404,14 +4404,16 @@ public static class SystemConfigsConstants {
public static class CustomBackendConstants {
public static final String ADD_CUSTOM_BACKEND =
"INSERT INTO AM_API_CUSTOM_BACKEND (ID,API_UUID,SEQUENCE,TYPE,REVISION_UUID,NAME) " + "VALUES (?,?,?,?,?,?)";
public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND ID = ? AND TYPE = ? AND REVISION_UUID = 0";
public static final String DELETE_CUSTOM_BACKEND_BY_REVISION = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE REVISION_UUID = ?";
public static final String DELETE_WORKING_COPY_OF_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = '0'";
public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND ID = ? AND TYPE = ? AND REVISION_UUID = '0'";
public static final String DELETE_CUSTOM_BACKEND_BY_REVISION = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?";
public static final String DELETE_CUSTOM_BACKEND_BY_API = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ?";
public static final String GET_CUSTOM_BACKEND_OF_API_REVISION = "SELECT NAME, SEQUENCE, TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?";
public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID IS NULL";
public static final String GET_CUSTOM_BACKEND_OF_API_REVISION = "SELECT ID, NAME, SEQUENCE, TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?";
public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = '0'";
public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ? AND ACB.TYPE = ?";
public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.API_UUID = ? AND ACB.REVISION_UUID = 0 AND ACB.TYPE = ?";
public static final String GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = 0";
public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.API_UUID = ? AND ACB.REVISION_UUID = '0' AND ACB.TYPE = ?";
public static final String GET_CUSTOM_BACKEND_FROM_API_AND_REVISION_UUID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = ? AND ACB.TYPE = ?";
public static final String GET_ALL_API_SPECIFIC_CUSTOM_BACKENDS = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.API_UUID = ? AND ACB.REVISION_UUID = '0'";
public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?";
public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?";
public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_SEQUENCE_FROM_SEQUENCE_ID = "SELECT ACB.SEQUENCE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?";
Expand Down
Loading

0 comments on commit dcf2e6d

Please sign in to comment.