Search in sources :

Example 91 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment 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 92 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.

the class ExternalGatewayNotifier method deployApi.

/**
 * Deploy APIs to external gateway
 *
 * @param deployAPIInGatewayEvent DeployAPIInGatewayEvent to deploy APIs to external gateway
 * @throws NotifierException if error occurs when deploying APIs to external gateway
 */
private void deployApi(DeployAPIInGatewayEvent deployAPIInGatewayEvent) throws NotifierException {
    boolean deployed;
    Set<String> gateways = deployAPIInGatewayEvent.getGatewayLabels();
    String apiId = deployAPIInGatewayEvent.getUuid();
    try {
        Map<String, Environment> environments = APIUtil.getEnvironments(deployAPIInGatewayEvent.getTenantDomain());
        APIProvider apiProvider = APIManagerFactory.getInstance().getAPIProvider(CarbonContext.getThreadLocalCarbonContext().getUsername());
        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 {
                        deployed = deployer.deploy(api, environments.get(deploymentEnv));
                        if (!deployed) {
                            throw new APIManagementException("Error while deploying API product to Solace broker");
                        }
                    } catch (DeployerException e) {
                        throw new APIManagementException(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 93 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment 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 94 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.

the class APIManagerConfigurationTest method testEnvironmentsConfigProvided.

@Test
public void testEnvironmentsConfigProvided() throws XMLStreamException, APIManagementException {
    String envConfig = "<Environment type=\"hybrid\" api-console=\"true\" isDefault=\"true\">\n" + "                <Name>Default</Name>\n" + "                <DisplayName></DisplayName>\n" + "                <Description>This is a hybrid gateway that handles both production and sandbox token traffic.</Description>\n" + "                <!-- Server URL of the API gateway -->\n" + "                <ServerURL>https://localhost:9440/services/</ServerURL>\n" + "                <!-- Admin username for the API gateway. -->\n" + "                <Username>${admin.username}</Username>\n" + "                <!-- Admin password for the API gateway.-->\n" + "                <Password>${admin.password}</Password>\n" + "                <!-- Provider Vendor of the API gateway.-->\n" + "                <Provider>wso2</Provider>\n" + "                <!-- Endpoint URLs for the APIs hosted in this API gateway.-->\n" + "                <GatewayEndpoint>https://localhost:9440,http://localhost:9440</GatewayEndpoint>\n" + "                <!-- Additional properties for External Gateways -->\n" + "                <!-- Endpoint URLs of the WebSocket APIs hosted in this API Gateway -->\n" + "                <GatewayWSEndpoint>ws://localhost:9099,wss://localhost:8099</GatewayWSEndpoint>\n" + "                <!-- Endpoint URLs of the WebSub APIs hosted in this API Gateway -->\n" + "                <GatewayWebSubEndpoint>http://localhost:9021,https://localhost:8021</GatewayWebSubEndpoint>\n" + "                <VirtualHosts>\n" + "                </VirtualHosts>\n" + "            </Environment>";
    OMElement element = AXIOMUtil.stringToOM(envConfig);
    APIManagerConfiguration config = new APIManagerConfiguration();
    config.setEnvironmentConfig(element);
    Map<String, Environment> environmentsList = config.getGatewayEnvironments();
    Assert.assertFalse(environmentsList.isEmpty());
    Environment defaultEnv = environmentsList.get("Default");
    Assert.assertEquals(defaultEnv.getProvider(), "wso2");
    Assert.assertTrue(defaultEnv.getAdditionalProperties().isEmpty());
}
Also used : Environment(org.wso2.carbon.apimgt.api.model.Environment) OMElement(org.apache.axiom.om.OMElement) Test(org.junit.Test)

Example 95 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.

the class TestUtils method mockAPIMConfiguration.

public static ServiceReferenceHolder mockAPIMConfiguration(String propertyName, String value, int tenantId) throws RegistryException, UserStoreException, XMLStreamException {
    ServiceReferenceHolder sh = mockRegistryAndUserRealm(tenantId);
    APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
    APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
    PowerMockito.when(sh.getAPIManagerConfigurationService()).thenReturn(amConfigService);
    PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
    PowerMockito.when(amConfig.getFirstProperty(propertyName)).thenReturn(value);
    Map<String, Environment> apiGatewayEnvironments = new HashMap<String, Environment>();
    Environment env1 = new Environment();
    apiGatewayEnvironments.put("PROD", env1);
    // Mocking some commonly used configs
    PowerMockito.when(amConfig.getApiGatewayEnvironments()).thenReturn(apiGatewayEnvironments);
    PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_GATEWAY_TYPE)).thenReturn(APIConstants.API_GATEWAY_TYPE_SYNAPSE);
    PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_PUBLISHER_ENABLE_API_DOC_VISIBILITY_LEVELS)).thenReturn("true", "false");
    return sh;
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) HashMap(java.util.HashMap) Environment(org.wso2.carbon.apimgt.api.model.Environment)

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