Search in sources :

Example 21 with APIRevisionDeployment

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

the class ApisApiServiceImpl method undeployAPIRevision.

@Override
public Response undeployAPIRevision(String apiId, String revisionId, String revisionNum, Boolean allEnvironments, List<APIRevisionDeploymentDTO> apIRevisionDeploymentDTOList, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    // validate if api exists
    APIInfo apiInfo = validateAPIExistence(apiId);
    // validate API update operation permitted based on the LC state
    validateAPIOperationsPerLC(apiInfo.getStatus().toString());
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    if (revisionId == null && revisionNum != null) {
        revisionId = apiProvider.getAPIRevisionUUID(revisionNum, apiId);
        if (revisionId == null) {
            return Response.status(Response.Status.BAD_REQUEST).entity(null).build();
        }
    }
    Map<String, Environment> environments = APIUtil.getEnvironments(organization);
    List<APIRevisionDeployment> apiRevisionDeployments = new ArrayList<>();
    if (allEnvironments) {
        apiRevisionDeployments = apiProvider.getAPIRevisionDeploymentList(revisionId);
    } else {
        for (APIRevisionDeploymentDTO apiRevisionDeploymentDTO : apIRevisionDeploymentDTOList) {
            APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
            apiRevisionDeployment.setRevisionUUID(revisionId);
            String environment = apiRevisionDeploymentDTO.getName();
            if (environments.get(environment) == null) {
                RestApiUtil.handleBadRequest("Gateway environment not found: " + environment, log);
            }
            apiRevisionDeployment.setDeployment(environment);
            apiRevisionDeployment.setVhost(apiRevisionDeploymentDTO.getVhost());
            apiRevisionDeployment.setDisplayOnDevportal(apiRevisionDeploymentDTO.isDisplayOnDevportal());
            apiRevisionDeployments.add(apiRevisionDeployment);
        }
    }
    apiProvider.undeployAPIRevisionDeployment(apiId, revisionId, apiRevisionDeployments, organization);
    List<APIRevisionDeployment> apiRevisionDeploymentsResponse = apiProvider.getAPIRevisionDeploymentList(revisionId);
    List<APIRevisionDeploymentDTO> apiRevisionDeploymentDTOS = new ArrayList<>();
    for (APIRevisionDeployment apiRevisionDeployment : apiRevisionDeploymentsResponse) {
        apiRevisionDeploymentDTOS.add(APIMappingUtil.fromAPIRevisionDeploymenttoDTO(apiRevisionDeployment));
    }
    Response.Status status = Response.Status.CREATED;
    return Response.status(status).entity(apiRevisionDeploymentDTOS).build();
}
Also used : HttpResponse(org.apache.http.HttpResponse) WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse) Response(javax.ws.rs.core.Response) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) APIInfo(org.wso2.carbon.apimgt.api.model.APIInfo) ArrayList(java.util.ArrayList) APIRevisionDeploymentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIRevisionDeploymentDTO) Environment(org.wso2.carbon.apimgt.api.model.Environment) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 22 with APIRevisionDeployment

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

the class APIMappingUtil method fromAPIRevisionListToEndpointsList.

public static List<APIEndpointURLsDTO> fromAPIRevisionListToEndpointsList(APIDTO apidto, String organization) throws APIManagementException {
    Map<String, Environment> environments = APIUtil.getEnvironments(organization);
    APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
    List<APIRevisionDeployment> revisionDeployments = apiConsumer.getAPIRevisionDeploymentListOfAPI(apidto.getId());
    // custom gateway URL of tenant
    Map<String, String> domains = new HashMap<>();
    if (organization != null) {
        domains = apiConsumer.getTenantDomainMappings(organization, APIConstants.API_DOMAIN_MAPPINGS_GATEWAY);
    }
    String customGatewayUrl = domains.get(APIConstants.CUSTOM_URL);
    List<APIEndpointURLsDTO> endpointUrls = new ArrayList<>();
    for (APIRevisionDeployment revisionDeployment : revisionDeployments) {
        if (revisionDeployment.isDisplayOnDevportal()) {
            // Deployed environment
            Environment environment = environments.get(revisionDeployment.getDeployment());
            if (environment != null) {
                APIEndpointURLsDTO apiEndpointURLsDTO = fromAPIRevisionToEndpoints(apidto, environment, revisionDeployment.getVhost(), customGatewayUrl, organization);
                endpointUrls.add(apiEndpointURLsDTO);
            }
        }
    }
    return endpointUrls;
}
Also used : HashMap(java.util.HashMap) APIEndpointURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO) ArrayList(java.util.ArrayList) Environment(org.wso2.carbon.apimgt.api.model.Environment) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 23 with APIRevisionDeployment

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

the class ApiMgtDAO method updateAPIRevisionDeployment.

/**
 * Update API revision Deployment mapping record
 *
 * @param apiUUID     API UUID
 * @param deployments content of the revision deployment mapping objects
 * @throws APIManagementException if an error occurs when adding a new API revision
 */
public void updateAPIRevisionDeployment(String apiUUID, Set<APIRevisionDeployment> deployments) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        connection.setAutoCommit(false);
        // Update an entry from AM_DEPLOYMENT_REVISION_MAPPING table
        try (PreparedStatement statement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.UPDATE_API_REVISION_DEPLOYMENT_MAPPING)) {
            for (APIRevisionDeployment deployment : deployments) {
                statement.setBoolean(1, deployment.isDisplayOnDevportal());
                statement.setString(2, deployment.getDeployment());
                statement.setString(3, deployment.getRevisionUUID());
                statement.addBatch();
            }
            statement.executeBatch();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            throw e;
        }
    } catch (SQLException e) {
        handleException("Failed to update Deployment Mapping entry for API UUID " + apiUUID, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)

Example 24 with APIRevisionDeployment

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

the class ApiMgtDAO method addAPIRevisionDeployment.

/**
 * Adds an API revision Deployment mapping record to the database
 *
 * @param apiRevisionId          uuid of the revision
 * @param apiRevisionDeployments content of the revision deployment mapping objects
 * @throws APIManagementException if an error occurs when adding a new API revision
 */
public void addAPIRevisionDeployment(String apiRevisionId, List<APIRevisionDeployment> apiRevisionDeployments) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            // Adding to AM_DEPLOYMENT_REVISION_MAPPING table
            PreparedStatement statement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.ADD_API_REVISION_DEPLOYMENT_MAPPING);
            for (APIRevisionDeployment apiRevisionDeployment : apiRevisionDeployments) {
                String envName = apiRevisionDeployment.getDeployment();
                String vhost = apiRevisionDeployment.getVhost();
                // set VHost as null, if it is the default vhost of the read only environment
                statement.setString(1, apiRevisionDeployment.getDeployment());
                statement.setString(2, VHostUtils.resolveIfDefaultVhostToNull(envName, vhost));
                statement.setString(3, apiRevisionId);
                statement.setBoolean(4, apiRevisionDeployment.isDisplayOnDevportal());
                statement.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
                statement.addBatch();
            }
            statement.executeBatch();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            handleException("Failed to add API Revision Deployment Mapping entry for Revision UUID " + apiRevisionId, e);
        }
    } catch (SQLException e) {
        handleException("Failed to add API Revision Deployment Mapping entry for Revision UUID " + apiRevisionId, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) Timestamp(java.sql.Timestamp)

Example 25 with APIRevisionDeployment

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

the class ApiMgtDAO method getAPIRevisionDeploymentByNameAndRevsionID.

/**
 * Get APIRevisionDeployment details by providing deployment name and revision uuid
 *
 * @return APIRevisionDeployment object
 * @throws APIManagementException if an error occurs while retrieving revision details
 */
public APIRevisionDeployment getAPIRevisionDeploymentByNameAndRevsionID(String name, String revisionId) throws APIManagementException {
    APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(SQLConstants.APIRevisionSqlConstants.GET_API_REVISION_DEPLOYMENT_MAPPING_BY_NAME_AND_REVISION_UUID)) {
        statement.setString(1, name);
        statement.setString(2, revisionId);
        try (ResultSet rs = statement.executeQuery()) {
            while (rs.next()) {
                String environmentName = rs.getString("NAME");
                String vhost = rs.getString("VHOST");
                apiRevisionDeployment.setDeployment(environmentName);
                apiRevisionDeployment.setVhost(VHostUtils.resolveIfNullToDefaultVhost(environmentName, vhost));
                apiRevisionDeployment.setRevisionUUID(rs.getString("REVISION_UUID"));
                apiRevisionDeployment.setDisplayOnDevportal(rs.getBoolean("DISPLAY_ON_DEVPORTAL"));
                apiRevisionDeployment.setDeployedTime(rs.getString("DEPLOYED_TIME"));
            }
        }
    } catch (SQLException e) {
        handleException("Failed to get API Revision deployment mapping details for deployment name: " + name, e);
    }
    return apiRevisionDeployment;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) PreparedStatement(java.sql.PreparedStatement)

Aggregations

APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)45 ArrayList (java.util.ArrayList)20 Environment (org.wso2.carbon.apimgt.api.model.Environment)17 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)15 APIRevision (org.wso2.carbon.apimgt.api.model.APIRevision)12 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)11 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)10 DeployedAPIRevision (org.wso2.carbon.apimgt.api.model.DeployedAPIRevision)10 APIRevisionDeploymentDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIRevisionDeploymentDTO)10 Connection (java.sql.Connection)9 PreparedStatement (java.sql.PreparedStatement)9 SQLException (java.sql.SQLException)9 HashSet (java.util.HashSet)8 LinkedHashSet (java.util.LinkedHashSet)8 HashMap (java.util.HashMap)6 Response (javax.ws.rs.core.Response)6 API (org.wso2.carbon.apimgt.api.model.API)6 APIStateChangeResponse (org.wso2.carbon.apimgt.api.model.APIStateChangeResponse)6 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)6 List (java.util.List)5