Search in sources :

Example 1 with DeployedAPIRevision

use of org.wso2.carbon.apimgt.api.model.DeployedAPIRevision in project carbon-apimgt by wso2.

the class APIProviderImpl method removeUnDeployedAPIRevision.

@Override
public void removeUnDeployedAPIRevision(String apiId, String apiRevisionUUID, String environment) throws APIManagementException {
    Set<DeployedAPIRevision> environmentsToRemove = new HashSet<>();
    environmentsToRemove.add(new DeployedAPIRevision(apiRevisionUUID, environment));
    apiMgtDAO.removeDeployedAPIRevision(apiId, environmentsToRemove);
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with DeployedAPIRevision

use of org.wso2.carbon.apimgt.api.model.DeployedAPIRevision in project carbon-apimgt by wso2.

the class ApiMgtDAO method removeDeployedAPIRevision.

/**
 * Remove an deployed API revision in the database
 *
 * @param apiUUID     uuid of the revision
 * @param deployments content of the revision deployment mapping objects
 * @throws APIManagementException if an error occurs when adding a new API revision
 */
public void removeDeployedAPIRevision(String apiUUID, Set<DeployedAPIRevision> deployments) throws APIManagementException {
    if (deployments.size() > 0) {
        try (Connection connection = APIMgtDBUtil.getConnection()) {
            connection.setAutoCommit(false);
            // Remove an entry from AM_DEPLOYED_REVISION table
            try (PreparedStatement statement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.REMOVE_DEPLOYED_API_REVISION)) {
                for (DeployedAPIRevision deployment : deployments) {
                    statement.setString(1, deployment.getDeployment());
                    statement.setString(2, deployment.getRevisionUUID());
                    statement.addBatch();
                }
                statement.executeBatch();
                connection.commit();
            } catch (SQLException e) {
                connection.rollback();
                handleException("Failed to remove deployed API Revision entry for API UUID " + apiUUID, e);
            }
        } catch (SQLException e) {
            handleException("Failed to remove deployed API Revision entry for API UUID " + apiUUID, e);
        }
    }
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 3 with DeployedAPIRevision

use of org.wso2.carbon.apimgt.api.model.DeployedAPIRevision in project carbon-apimgt by wso2.

the class ApiMgtDAO method addDeployedAPIRevision.

/**
 * Adds an deployed API revision to the database
 *
 * @param deployedAPIRevisionList content of the revision deployment mapping objects
 * @throws APIManagementException if an error occurs when adding a new API revision
 */
public void addDeployedAPIRevision(String apiRevisionId, List<DeployedAPIRevision> deployedAPIRevisionList) throws APIManagementException {
    if (deployedAPIRevisionList.size() > 0) {
        try (Connection connection = APIMgtDBUtil.getConnection()) {
            connection.setAutoCommit(false);
            // Adding to AM_DEPLOYED_REVISION table
            try (PreparedStatement statement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.ADD_DEPLOYED_API_REVISION)) {
                for (DeployedAPIRevision deployedAPIRevision : deployedAPIRevisionList) {
                    String envName = deployedAPIRevision.getDeployment();
                    String vhost = deployedAPIRevision.getVhost();
                    // set VHost as null, if it is the default vhost of the read only environment
                    statement.setString(1, deployedAPIRevision.getDeployment());
                    statement.setString(2, VHostUtils.resolveIfDefaultVhostToNull(envName, vhost));
                    statement.setString(3, apiRevisionId);
                    statement.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
                    statement.addBatch();
                }
                statement.executeBatch();
                connection.commit();
            } catch (SQLException e) {
                connection.rollback();
                // handle concurrent db entry update. Fix duplicate primary key issue.
                if (e.getMessage().toLowerCase().contains("primary key violation") || e.getMessage().toLowerCase().contains("duplicate entry")) {
                    log.debug("Duplicate entries detected for Revision UUID " + apiRevisionId + " while adding deployed API revisions", e);
                } else {
                    handleException("Failed to add deployed API Revision for Revision UUID " + apiRevisionId, e);
                }
            }
        } catch (SQLException e) {
            handleException("Failed to add deployed API Revision for Revision UUID " + apiRevisionId, e);
        }
    }
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 4 with DeployedAPIRevision

use of org.wso2.carbon.apimgt.api.model.DeployedAPIRevision in project carbon-apimgt by wso2.

the class ApiMgtDAO method getDeployedAPIRevisionByApiUUID.

/**
 * Get DeployedAPIRevision details by providing ApiUUID
 *
 * @return List<DeployedAPIRevision> object
 * @throws APIManagementException if an error occurs while retrieving revision deployment mapping details
 */
public List<DeployedAPIRevision> getDeployedAPIRevisionByApiUUID(String apiUUID) throws APIManagementException {
    List<DeployedAPIRevision> deployedAPIRevisionList = new ArrayList<>();
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_DEPLOYED_REVISION_BY_API_UUID)) {
        statement.setString(1, apiUUID);
        try (ResultSet rs = statement.executeQuery()) {
            while (rs.next()) {
                DeployedAPIRevision deployedAPIRevision = new DeployedAPIRevision();
                String environmentName = rs.getString("NAME");
                String vhost = rs.getString("VHOST");
                deployedAPIRevision.setDeployment(environmentName);
                deployedAPIRevision.setVhost(VHostUtils.resolveIfNullToDefaultVhost(environmentName, vhost));
                deployedAPIRevision.setRevisionUUID(rs.getString("REVISION_UUID"));
                deployedAPIRevision.setDeployedTime(rs.getString("DEPLOYED_TIME"));
                deployedAPIRevisionList.add(deployedAPIRevision);
            }
        }
    } catch (SQLException e) {
        handleException("Failed to get deployed API Revision details for api uuid: " + apiUUID, e);
    }
    return deployedAPIRevisionList;
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 5 with DeployedAPIRevision

use of org.wso2.carbon.apimgt.api.model.DeployedAPIRevision in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method deployedAPIRevision.

public Response deployedAPIRevision(List<DeployedAPIRevisionDTO> deployedAPIRevisionDTOList, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    List<String> revisionUUIDs = new ArrayList<>();
    String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    for (DeployedAPIRevisionDTO deployedAPIRevisionDTO : deployedAPIRevisionDTOList) {
        // get revision uuid
        String revisionUUID = apiProvider.getAPIRevisionUUID(Integer.toString(deployedAPIRevisionDTO.getRevisionId()), deployedAPIRevisionDTO.getApiId());
        if (revisionUUID == null) {
            return Response.status(Response.Status.BAD_REQUEST).entity(null).build();
        }
        if (!revisionUUIDs.contains(revisionUUID)) {
            revisionUUIDs.add(revisionUUID);
            Map<String, Environment> environments = APIUtil.getEnvironments(tenantDomain);
            List<DeployedAPIRevision> deployedAPIRevisions = new ArrayList<>();
            for (DeployedEnvInfoDTO deployedEnvInfoDTO : deployedAPIRevisionDTO.getEnvInfo()) {
                DeployedAPIRevision deployedAPIRevision = new DeployedAPIRevision();
                deployedAPIRevision.setRevisionUUID(revisionUUID);
                String environment = deployedEnvInfoDTO.getName();
                if (environments.get(environment) == null) {
                    RestApiUtil.handleBadRequest("Gateway environment not found: " + environment, log);
                }
                deployedAPIRevision.setDeployment(environment);
                deployedAPIRevision.setVhost(deployedEnvInfoDTO.getVhost());
                if (StringUtils.isEmpty(deployedEnvInfoDTO.getVhost())) {
                    RestApiUtil.handleBadRequest("Required field 'vhost' not found in deployment", log);
                }
                deployedAPIRevisions.add(deployedAPIRevision);
            }
            apiProvider.addDeployedAPIRevision(deployedAPIRevisionDTO.getApiId(), revisionUUID, deployedAPIRevisions);
        }
    }
    return Response.ok().build();
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) ArrayList(java.util.ArrayList) Environment(org.wso2.carbon.apimgt.api.model.Environment) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) DeployedAPIRevisionDTO(org.wso2.carbon.apimgt.internal.service.dto.DeployedAPIRevisionDTO) UnDeployedAPIRevisionDTO(org.wso2.carbon.apimgt.internal.service.dto.UnDeployedAPIRevisionDTO) DeployedEnvInfoDTO(org.wso2.carbon.apimgt.internal.service.dto.DeployedEnvInfoDTO)

Aggregations

DeployedAPIRevision (org.wso2.carbon.apimgt.api.model.DeployedAPIRevision)6 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 ResultSet (java.sql.ResultSet)1 Timestamp (java.sql.Timestamp)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 Environment (org.wso2.carbon.apimgt.api.model.Environment)1 DeployedAPIRevisionDTO (org.wso2.carbon.apimgt.internal.service.dto.DeployedAPIRevisionDTO)1 DeployedEnvInfoDTO (org.wso2.carbon.apimgt.internal.service.dto.DeployedEnvInfoDTO)1 UnDeployedAPIRevisionDTO (org.wso2.carbon.apimgt.internal.service.dto.UnDeployedAPIRevisionDTO)1