Search in sources :

Example 26 with APIRevisionDeployment

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

the class APIMgtDBUtil method mergeRevisionDeploymentDTOs.

/**
 * Handle connection rollback logic. Rethrow original exception so that it can be handled centrally.
 * @param rs result set
 * @throws SQLException sql exception
 * @throws APIManagementException api management exception
 */
public static List<APIRevisionDeployment> mergeRevisionDeploymentDTOs(ResultSet rs) throws APIManagementException, SQLException {
    List<APIRevisionDeployment> apiRevisionDeploymentList = new ArrayList<>();
    Map<String, APIRevisionDeployment> uniqueSet = new HashMap<>();
    while (rs.next()) {
        APIRevisionDeployment apiRevisionDeployment;
        String environmentName = rs.getString("NAME");
        String vhost = VHostUtils.resolveIfNullToDefaultVhost(environmentName, rs.getString("VHOST"));
        String revisionUuid = rs.getString("REVISION_UUID");
        String uniqueKey = (environmentName != null ? environmentName : "") + (vhost != null ? vhost : "") + (revisionUuid != null ? revisionUuid : "");
        if (!uniqueSet.containsKey(uniqueKey)) {
            apiRevisionDeployment = new APIRevisionDeployment();
            apiRevisionDeployment.setDeployment(environmentName);
            apiRevisionDeployment.setVhost(vhost);
            apiRevisionDeployment.setRevisionUUID(revisionUuid);
            apiRevisionDeployment.setDisplayOnDevportal(rs.getBoolean("DISPLAY_ON_DEVPORTAL"));
            apiRevisionDeployment.setDeployedTime(rs.getString("DEPLOY_TIME"));
            apiRevisionDeployment.setSuccessDeployedTime(rs.getString("DEPLOYED_TIME"));
            apiRevisionDeploymentList.add(apiRevisionDeployment);
            uniqueSet.put(uniqueKey, apiRevisionDeployment);
        } else {
            apiRevisionDeployment = uniqueSet.get(uniqueKey);
            if (!apiRevisionDeployment.isDisplayOnDevportal()) {
                apiRevisionDeployment.setDisplayOnDevportal(rs.getBoolean("DISPLAY_ON_DEVPORTAL"));
            }
            if (apiRevisionDeployment.getDeployedTime() == null) {
                apiRevisionDeployment.setDeployedTime(rs.getString("DEPLOY_TIME"));
            }
            if (apiRevisionDeployment.getSuccessDeployedTime() == null) {
                apiRevisionDeployment.setSuccessDeployedTime(rs.getString("DEPLOYED_TIME"));
            }
        }
    }
    return apiRevisionDeploymentList;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)

Example 27 with APIRevisionDeployment

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

the class ExternallyDeployedApiNotifier method undeployWhenDeleting.

/**
 * Undeploy APIs from external gateway when API is deleted
 *
 * @param apiEvent APIEvent to undeploy APIs from external gateway
 * @throws NotifierException if error occurs when undeploying APIs from external gateway
 */
private void undeployWhenDeleting(APIEvent apiEvent) throws NotifierException {
    Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
    boolean deleted;
    String apiId = apiEvent.getUuid();
    try {
        List<APIRevisionDeployment> test = apiMgtDAO.getAPIRevisionDeploymentsByApiUUID(apiId);
        for (APIRevisionDeployment deployment : test) {
            String deploymentEnv = deployment.getDeployment();
            if (gatewayEnvironments.containsKey(deploymentEnv)) {
                ExternalGatewayDeployer deployer = ServiceReferenceHolder.getInstance().getExternalGatewayDeployer(gatewayEnvironments.get(deploymentEnv).getProvider());
                if (deployer != null) {
                    try {
                        deleted = deployer.undeploy(apiEvent.getApiName(), apiEvent.getApiVersion(), apiEvent.getApiContext(), gatewayEnvironments.get(deploymentEnv));
                        if (!deleted) {
                            throw new NotifierException("Error while deleting API product from Solace broker");
                        }
                    } catch (DeployerException e) {
                        throw new NotifierException(e.getMessage());
                    }
                }
            }
        }
    } catch (APIManagementException e) {
        throw new NotifierException(e.getMessage());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ExternalGatewayDeployer(org.wso2.carbon.apimgt.impl.deployer.ExternalGatewayDeployer) Environment(org.wso2.carbon.apimgt.api.model.Environment) DeployerException(org.wso2.carbon.apimgt.impl.deployer.exceptions.DeployerException) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) NotifierException(org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException)

Example 28 with APIRevisionDeployment

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

the class GatewayArtifactsMgtDAOTest method testAddGatewayAPIArtifactAndMetaData.

@Test
public void testAddGatewayAPIArtifactAndMetaData() throws APIManagementException {
    String uuid = UUID.randomUUID().toString();
    String name = "apiname";
    String version = "1.0.0";
    String revision = UUID.randomUUID().toString();
    URL resource = getClass().getClassLoader().getResource("admin-PizzaShackAPI-1.0.0.zip");
    File file = new File(resource.getPath());
    gatewayArtifactsMgtDAO.addGatewayAPIArtifactAndMetaData(uuid, name, version, revision, "carbon.super", APIConstants.HTTP_PROTOCOL, file);
    API api = new API(new APIIdentifier("test-provider", name, version));
    api.setContext("/context1");
    api.setContextTemplate("/context1/{version}");
    api.setUUID(uuid);
    apiMgtDAO.addAPI(api, -1234, "testOrg");
    String gatewayAPIId = gatewayArtifactsMgtDAO.getGatewayAPIId(name, version, "carbon.super");
    Assert.assertEquals(gatewayAPIId, uuid);
    Map<String, String> gatewayVhosts = new HashMap<>();
    gatewayVhosts.put("label1", "dev.wso2.com");
    gatewayArtifactsMgtDAO.addAndRemovePublishedGatewayLabels(uuid, revision, Collections.asSet("label1"), gatewayVhosts);
    List<APIRuntimeArtifactDto> artifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByAPIIDAndLabel(uuid, new String[] { "label1" }, "carbon.super");
    Assert.assertEquals(artifacts.size(), 1);
    RuntimeArtifactDto artifact = artifacts.get(0);
    Assert.assertNotNull(artifact);
    APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
    apiRevisionDeployment.setRevisionUUID(revision);
    apiRevisionDeployment.setDeployment("label1");
    gatewayVhosts = new HashMap<>();
    gatewayVhosts.put("label2", "prod.wso2.com");
    gatewayArtifactsMgtDAO.addAndRemovePublishedGatewayLabels(uuid, revision, Collections.asSet("label2"), gatewayVhosts, Collections.asSet(apiRevisionDeployment));
    artifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByAPIIDAndLabel(uuid, new String[] { "label1" }, "carbon.super");
    Assert.assertEquals(artifacts.size(), 0);
    artifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByAPIIDAndLabel(uuid, new String[] { "label2" }, "carbon.super");
    Assert.assertEquals(artifacts.size(), 1);
    artifact = artifacts.get(0);
    Assert.assertNotNull(artifact);
}
Also used : APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) HashMap(java.util.HashMap) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) File(java.io.File) URL(java.net.URL) Test(org.junit.Test)

Example 29 with APIRevisionDeployment

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

the class ApiProductsApiServiceImpl method deployAPIProductRevision.

@Override
public Response deployAPIProductRevision(String apiProductId, String revisionId, List<APIRevisionDeploymentDTO> apIRevisionDeploymentDTO, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    Map<String, Environment> environments = APIUtil.getEnvironments(organization);
    List<APIRevisionDeployment> apiRevisionDeployments = new ArrayList<>();
    for (APIRevisionDeploymentDTO apiRevisionDeploymentDTO : apIRevisionDeploymentDTO) {
        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.deployAPIProductRevision(apiProductId, revisionId, apiRevisionDeployments);
    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 : Response(javax.ws.rs.core.Response) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) 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 30 with APIRevisionDeployment

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

the class ApiProductsApiServiceImpl method undeployAPIProductRevision.

@Override
public Response undeployAPIProductRevision(String apiProductId, String revisionId, String revisionNumber, Boolean allEnvironments, List<APIRevisionDeploymentDTO> apIRevisionDeploymentDTO, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    if (revisionId == null && revisionNumber != null) {
        revisionId = apiProvider.getAPIRevisionUUID(revisionNumber, apiProductId);
        if (revisionId == null) {
            return Response.status(Response.Status.BAD_REQUEST).entity(null).build();
        }
    }
    String organization = RestApiUtil.getValidatedOrganization(messageContext);
    Map<String, Environment> environments = APIUtil.getEnvironments(organization);
    List<APIRevisionDeployment> apiRevisionDeployments = new ArrayList<>();
    if (allEnvironments) {
        apiRevisionDeployments = apiProvider.getAPIRevisionDeploymentList(revisionId);
    } else {
        for (APIRevisionDeploymentDTO apiRevisionDeploymentDTO : apIRevisionDeploymentDTO) {
            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.undeployAPIProductRevisionDeployment(apiProductId, revisionId, apiRevisionDeployments);
    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 : Response(javax.ws.rs.core.Response) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) 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)

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