Search in sources :

Example 11 with VHost

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

the class ImportUtils method getValidatedDeploymentsList.

/**
 * This method is used to validate the Gateway environments from the deployment environments file. Gateway
 * environments will be validated with a set of all the labels and environments of the tenant domain. If
 * environment is not found in this set, it will be skipped with an error message in the console. This method is
 * common to both APIs and API Products
 *
 * @param deploymentInfoArray Deployment environment array found in the import artifact
 * @param tenantDomain        Tenant domain
 * @param apiProvider         Provider of the API/ API Product
 * @return a list of API/API Product revision deployments ready to be deployed.
 * @throws APIManagementException If an error occurs when validating the deployments list
 */
private static List<APIRevisionDeployment> getValidatedDeploymentsList(JsonArray deploymentInfoArray, String tenantDomain, APIProvider apiProvider, String organization) throws APIManagementException {
    List<APIRevisionDeployment> apiRevisionDeployments = new ArrayList<>();
    if (deploymentInfoArray != null && deploymentInfoArray.size() > 0) {
        Map<String, Environment> gatewayEnvironments = APIUtil.getEnvironments(organization);
        for (int i = 0; i < deploymentInfoArray.size(); i++) {
            JsonObject deploymentJson = deploymentInfoArray.get(i).getAsJsonObject();
            JsonElement deploymentNameElement = deploymentJson.get(ImportExportConstants.DEPLOYMENT_NAME);
            if (deploymentNameElement != null) {
                String deploymentName = deploymentNameElement.getAsString();
                Environment gatewayEnvironment = gatewayEnvironments.get(deploymentName);
                if (gatewayEnvironment != null) {
                    JsonElement deploymentVhostElement = deploymentJson.get(ImportExportConstants.DEPLOYMENT_VHOST);
                    String deploymentVhost;
                    if (deploymentVhostElement != null) {
                        deploymentVhost = deploymentVhostElement.getAsString();
                    } else {
                        // set the default vhost of the given environment
                        if (gatewayEnvironment.getVhosts().isEmpty()) {
                            throw new APIManagementException("No VHosts defined for the environment: " + deploymentName);
                        }
                        deploymentVhost = gatewayEnvironment.getVhosts().get(0).getHost();
                    }
                    // resolve vhost to null if it is the default vhost of read only environment
                    deploymentVhost = VHostUtils.resolveIfDefaultVhostToNull(deploymentName, deploymentVhost);
                    JsonElement displayOnDevportalElement = deploymentJson.get(ImportExportConstants.DISPLAY_ON_DEVPORTAL_OPTION);
                    boolean displayOnDevportal = displayOnDevportalElement == null || displayOnDevportalElement.getAsBoolean();
                    APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
                    apiRevisionDeployment.setDeployment(deploymentName);
                    apiRevisionDeployment.setVhost(deploymentVhost);
                    apiRevisionDeployment.setDisplayOnDevportal(displayOnDevportal);
                    apiRevisionDeployments.add(apiRevisionDeployment);
                } else {
                    throw new APIManagementException("Label " + deploymentName + " is not a defined gateway environment. Hence " + "skipped without deployment", ExceptionCodes.from(ExceptionCodes.GATEWAY_ENVIRONMENT_NOT_FOUND, String.format("label '%s'", deploymentName)));
                }
            }
        }
    }
    return apiRevisionDeployments;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JsonElement(com.google.gson.JsonElement) ArrayList(java.util.ArrayList) Environment(org.wso2.carbon.apimgt.api.model.Environment) JsonObject(com.google.gson.JsonObject) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)

Example 12 with VHost

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

the class EnvironmentMappingUtil method fromVHostToVHostDTO.

/**
 * Converts VHost into a VHostDTO.
 *
 * @param vHost VHost object
 * @return VHostDTO
 */
public static VHostDTO fromVHostToVHostDTO(VHost vHost) {
    VHostDTO vHostDTO = new VHostDTO();
    vHostDTO.setHost(vHost.getHost());
    vHostDTO.setHttpContext(vHost.getHttpContext());
    vHostDTO.setHttpPort(vHost.getHttpPort());
    vHostDTO.setHttpsPort(vHost.getHttpsPort());
    vHostDTO.setWsPort(vHost.getWsPort());
    vHostDTO.setWssPort(vHost.getWssPort());
    vHostDTO.setWebsubHttpPort(vHost.getWebsubHttpPort());
    vHostDTO.setWebsubHttpsPort(vHost.getWebsubHttpsPort());
    return vHostDTO;
}
Also used : VHostDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.VHostDTO)

Example 13 with VHost

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

the class ApisApiServiceImpl method deployAPIRevision.

/**
 * Deploy a revision
 *
 * @param apiId             UUID of the API
 * @param revisionId     Revision ID of the API
 * @param messageContext    message context object
 * @return response with 200 status code
 */
@Override
public Response deployAPIRevision(String apiId, String revisionId, 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);
    // validate whether the API is advertise only
    APIDTO apiDto = getAPIByID(apiId, apiProvider, organization);
    if (apiDto != null && apiDto.getAdvertiseInfo() != null && apiDto.getAdvertiseInfo().isAdvertised()) {
        throw new APIManagementException("Deploying API Revisions is not supported for third party APIs: " + apiId);
    }
    Map<String, Environment> environments = APIUtil.getEnvironments(organization);
    List<APIRevisionDeployment> apiRevisionDeployments = new ArrayList<>();
    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());
        if (StringUtils.isEmpty(apiRevisionDeploymentDTO.getVhost())) {
            // vhost is only required when deploying an revision, not required when un-deploying a revision
            // since the same scheme 'APIRevisionDeployment' is used for deploy and undeploy, handle it here.
            RestApiUtil.handleBadRequest("Required field 'vhost' not found in deployment", log);
        }
        apiRevisionDeployment.setDisplayOnDevportal(apiRevisionDeploymentDTO.isDisplayOnDevportal());
        apiRevisionDeployments.add(apiRevisionDeployment);
    }
    apiProvider.deployAPIRevision(apiId, revisionId, apiRevisionDeployments, organization);
    List<APIRevisionDeployment> apiRevisionDeploymentsResponse = apiProvider.getAPIRevisionsDeploymentList(apiId);
    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 : APIInfo(org.wso2.carbon.apimgt.api.model.APIInfo) ArrayList(java.util.ArrayList) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) 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) APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIRevisionDeploymentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIRevisionDeploymentDTO) Environment(org.wso2.carbon.apimgt.api.model.Environment)

Example 14 with VHost

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

the class APIMappingUtil method fromAPIRevisionToEndpoints.

private static APIEndpointURLsDTO fromAPIRevisionToEndpoints(APIDTO apidto, Environment environment, String host, String customGatewayUrl, String tenantDomain) throws APIManagementException {
    // Deployed VHost
    VHost vHost;
    String context = apidto.getContext();
    if (StringUtils.isEmpty(customGatewayUrl)) {
        vHost = VHostUtils.getVhostFromEnvironment(environment, host);
    } else {
        if (!StringUtils.contains(customGatewayUrl, "://")) {
            customGatewayUrl = APIConstants.HTTPS_PROTOCOL_URL_PREFIX + customGatewayUrl;
        }
        vHost = VHost.fromEndpointUrls(new String[] { customGatewayUrl });
        context = context.replace("/t/" + tenantDomain, "");
    }
    APIEndpointURLsDTO apiEndpointURLsDTO = new APIEndpointURLsDTO();
    apiEndpointURLsDTO.setEnvironmentName(environment.getName());
    apiEndpointURLsDTO.setEnvironmentDisplayName(environment.getDisplayName());
    apiEndpointURLsDTO.setEnvironmentType(environment.getType());
    APIURLsDTO apiurLsDTO = new APIURLsDTO();
    boolean isWs = StringUtils.equalsIgnoreCase("WS", apidto.getType());
    boolean isGQLSubscription = StringUtils.equalsIgnoreCase(APIConstants.GRAPHQL_API, apidto.getType()) && isGraphQLSubscriptionsAvailable(apidto);
    if (!isWs) {
        if (apidto.getTransport().contains(APIConstants.HTTP_PROTOCOL)) {
            apiurLsDTO.setHttp(vHost.getHttpUrl() + context);
        }
        if (apidto.getTransport().contains(APIConstants.HTTPS_PROTOCOL)) {
            apiurLsDTO.setHttps(vHost.getHttpsUrl() + context);
        }
    }
    if (isWs || isGQLSubscription) {
        apiurLsDTO.setWs(vHost.getWsUrl() + context);
        apiurLsDTO.setWss(vHost.getWssUrl() + context);
    }
    apiEndpointURLsDTO.setUrLs(apiurLsDTO);
    APIDefaultVersionURLsDTO apiDefaultVersionURLsDTO = new APIDefaultVersionURLsDTO();
    if (apidto.isIsDefaultVersion() != null && apidto.isIsDefaultVersion()) {
        String defaultContext = context.replaceAll("/" + apidto.getVersion() + "$", "");
        if (!isWs) {
            if (apidto.getTransport().contains(APIConstants.HTTP_PROTOCOL)) {
                apiDefaultVersionURLsDTO.setHttp(vHost.getHttpUrl() + defaultContext);
            }
            if (apidto.getTransport().contains(APIConstants.HTTPS_PROTOCOL)) {
                apiDefaultVersionURLsDTO.setHttps(vHost.getHttpsUrl() + defaultContext);
            }
        }
        if (isWs || isGQLSubscription) {
            apiDefaultVersionURLsDTO.setWs(vHost.getWsUrl() + defaultContext);
            apiDefaultVersionURLsDTO.setWss(vHost.getWssUrl() + defaultContext);
        }
    }
    apiEndpointURLsDTO.setDefaultVersionURLs(apiDefaultVersionURLsDTO);
    return apiEndpointURLsDTO;
}
Also used : VHost(org.wso2.carbon.apimgt.api.model.VHost) APIURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIURLsDTO) APIDefaultVersionURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIDefaultVersionURLsDTO) APIEndpointURLsDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIEndpointURLsDTO)

Example 15 with VHost

use of org.wso2.carbon.apimgt.api.model.VHost 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)

Aggregations

SQLException (java.sql.SQLException)14 PreparedStatement (java.sql.PreparedStatement)13 ArrayList (java.util.ArrayList)13 Connection (java.sql.Connection)12 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)11 ResultSet (java.sql.ResultSet)9 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)8 Environment (org.wso2.carbon.apimgt.api.model.Environment)8 VHost (org.wso2.carbon.apimgt.api.model.VHost)7 IOException (java.io.IOException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 APIRuntimeArtifactDto (org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto)4 HashMap (java.util.HashMap)3 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)3 DeployedAPIRevision (org.wso2.carbon.apimgt.api.model.DeployedAPIRevision)3 JsonObject (com.google.gson.JsonObject)2 Timestamp (java.sql.Timestamp)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2