Search in sources :

Example 36 with Environment

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

the class APIAdminImpl method deleteEnvironment.

@Override
public void deleteEnvironment(String tenantDomain, String uuid) throws APIManagementException {
    // check if the VHost exists in the tenant domain with given UUID, throw error if not found
    Environment existingEnv = getEnvironment(tenantDomain, uuid);
    if (existingEnv.isReadOnly()) {
        String errorMessage = String.format("Failed to delete Environment with UUID '%s'. Environment is read only", uuid);
        throw new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.from(ExceptionCodes.READONLY_GATEWAY_ENVIRONMENT, String.format("UUID '%s'", uuid)));
    }
    apiMgtDAO.deleteEnvironment(uuid);
}
Also used : Environment(org.wso2.carbon.apimgt.api.model.Environment) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 37 with Environment

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

the class ExternallyDeployedApiNotifier method undeployApiWhenRetiring.

/**
 * Undeploy APIs from external gateway when life cycle state changed to retire
 *
 * @param apiEvent APIEvent to undeploy APIs from external gateway
 * @throws NotifierException if error occurs when undeploying APIs from external gateway
 */
private void undeployApiWhenRetiring(APIEvent apiEvent) throws NotifierException {
    apiMgtDAO = ApiMgtDAO.getInstance();
    Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
    boolean deleted;
    String apiId = apiEvent.getUuid();
    if (!APIConstants.RETIRED.equals(apiEvent.getApiStatus())) {
        return;
    }
    try {
        APIProvider apiProvider = APIManagerFactory.getInstance().getAPIProvider(CarbonContext.getThreadLocalCarbonContext().getUsername());
        API api = apiProvider.getAPIbyUUID(apiId, apiMgtDAO.getOrganizationByAPIUUID(apiId));
        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.undeployWhenRetire(api, 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) API(org.wso2.carbon.apimgt.api.model.API) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) NotifierException(org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException)

Example 38 with Environment

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

the class ExternalGatewayNotifier method unDeployApi.

/**
 * Undeploy APIs from external gateway
 *
 * @param deployAPIInGatewayEvent DeployAPIInGatewayEvent to undeploy APIs from external gateway
 * @throws NotifierException if error occurs when undeploying APIs from external gateway
 */
private void unDeployApi(DeployAPIInGatewayEvent deployAPIInGatewayEvent) throws NotifierException {
    boolean deleted;
    Set<String> gateways = deployAPIInGatewayEvent.getGatewayLabels();
    String apiId = deployAPIInGatewayEvent.getUuid();
    try {
        Map<String, Environment> environments = APIUtil.getEnvironments(deployAPIInGatewayEvent.getTenantDomain());
        APIProvider apiProvider = APIManagerFactory.getInstance().getAPIProvider(deployAPIInGatewayEvent.getProvider());
        API api = apiProvider.getAPIbyUUID(apiId, apiMgtDAO.getOrganizationByAPIUUID(apiId));
        for (String deploymentEnv : gateways) {
            if (environments.containsKey(deploymentEnv)) {
                ExternalGatewayDeployer deployer = ServiceReferenceHolder.getInstance().getExternalGatewayDeployer(environments.get(deploymentEnv).getProvider());
                if (deployer != null) {
                    try {
                        deleted = deployer.undeploy(api.getId().getName(), api.getId().getVersion(), api.getContext(), environments.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) API(org.wso2.carbon.apimgt.api.model.API) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) NotifierException(org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException)

Example 39 with Environment

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

the class ApisApiServiceImpl method apisApiIdEnvironmentsEnvIdKeysGet.

@Override
public Response apisApiIdEnvironmentsEnvIdKeysGet(String apiId, String envId, MessageContext messageContext) throws APIManagementException {
    // validate api UUID
    validateAPIExistence(apiId);
    // validate environment UUID
    validateEnvironment(envId);
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    // get properties
    EnvironmentPropertiesDTO properties = apiProvider.getEnvironmentSpecificAPIProperties(apiId, envId);
    // convert to string to remove null values
    String jsonContent = new Gson().toJson(properties);
    return Response.ok().entity(jsonContent).build();
}
Also used : Gson(com.google.gson.Gson) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) EnvironmentPropertiesDTO(org.wso2.carbon.apimgt.api.dto.EnvironmentPropertiesDTO)

Example 40 with Environment

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

Aggregations

Environment (org.wso2.carbon.apimgt.api.model.Environment)67 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 ArrayList (java.util.ArrayList)35 HashMap (java.util.HashMap)25 API (org.wso2.carbon.apimgt.api.model.API)22 IOException (java.io.IOException)19 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)19 Map (java.util.Map)15 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)12 JsonObject (com.google.gson.JsonObject)10 PreparedStatement (java.sql.PreparedStatement)10 SQLException (java.sql.SQLException)10 HashSet (java.util.HashSet)10 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)10 SolaceAdminApis (org.wso2.carbon.apimgt.solace.SolaceAdminApis)10 Gson (com.google.gson.Gson)9 JSONObject (org.json.simple.JSONObject)9 Test (org.junit.Test)9 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)9