Search in sources :

Example 1 with IApplication

use of com.openshift.client.IApplication in project camel by apache.

the class OpenShiftConsumer method doPollOnChange.

protected int doPollOnChange(IDomain domain) {
    // an app can either be
    // - added
    // - removed
    // - state changed
    Map<ApplicationState, ApplicationState> newState = new HashMap<ApplicationState, ApplicationState>();
    List<IApplication> apps = domain.getApplications();
    for (IApplication app : apps) {
        ApplicationState state = new ApplicationState(app.getUUID(), app, OpenShiftHelper.getStateForApplication(app));
        newState.put(state, state);
    }
    // compute what is the delta from last time
    // so we split up into 3 groups, of added/removed/changed
    Map<ApplicationState, ApplicationState> added = new HashMap<ApplicationState, ApplicationState>();
    Map<ApplicationState, ApplicationState> removed = new HashMap<ApplicationState, ApplicationState>();
    Map<ApplicationState, ApplicationState> changed = new HashMap<ApplicationState, ApplicationState>();
    for (ApplicationState state : newState.keySet()) {
        if (!oldState.containsKey(state)) {
            // its a new app added
            added.put(state, state);
        } else {
            ApplicationState old = oldState.get(state);
            if (old != null && !old.getState().equals(state.getState())) {
                // the state changed
                state.setOldState(old.getState());
                changed.put(state, state);
            }
        }
    }
    for (ApplicationState state : oldState.keySet()) {
        if (!newState.containsKey(state)) {
            // its a app removed
            removed.put(state, state);
        }
    }
    // only start emitting events after first init poll
    int processed = 0;
    if (!initialPoll) {
        for (ApplicationState add : added.keySet()) {
            Exchange exchange = getEndpoint().createExchange(add.getApplication());
            exchange.getIn().setHeader(OpenShiftConstants.EVENT_TYPE, "added");
            try {
                processed++;
                getProcessor().process(exchange);
            } catch (Exception e) {
                exchange.setException(e);
            }
            if (exchange.getException() != null) {
                getExceptionHandler().handleException("Error during processing exchange.", exchange, exchange.getException());
            }
        }
        for (ApplicationState remove : removed.keySet()) {
            Exchange exchange = getEndpoint().createExchange(remove.getApplication());
            exchange.getIn().setHeader(OpenShiftConstants.EVENT_TYPE, "removed");
            try {
                processed++;
                getProcessor().process(exchange);
            } catch (Exception e) {
                exchange.setException(e);
            }
            if (exchange.getException() != null) {
                getExceptionHandler().handleException("Error during processing exchange.", exchange, exchange.getException());
            }
        }
        for (ApplicationState change : changed.keySet()) {
            Exchange exchange = getEndpoint().createExchange(change.getApplication());
            exchange.getIn().setHeader(OpenShiftConstants.EVENT_TYPE, "changed");
            exchange.getIn().setHeader(OpenShiftConstants.EVENT_OLD_STATE, change.getOldState());
            exchange.getIn().setHeader(OpenShiftConstants.EVENT_NEW_STATE, change.getState());
            try {
                processed++;
                getProcessor().process(exchange);
            } catch (Exception e) {
                exchange.setException(e);
            }
            if (exchange.getException() != null) {
                getExceptionHandler().handleException("Error during processing exchange.", exchange, exchange.getException());
            }
        }
    }
    // update old state with latest state for next poll
    oldState.clear();
    oldState.putAll(newState);
    initialPoll = false;
    return processed;
}
Also used : Exchange(org.apache.camel.Exchange) IApplication(com.openshift.client.IApplication) HashMap(java.util.HashMap) Endpoint(org.apache.camel.Endpoint)

Example 2 with IApplication

use of com.openshift.client.IApplication in project camel by apache.

the class OpenShiftProducer method doStart.

protected void doStart(Exchange exchange, IDomain domain) throws CamelExchangeException {
    String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
    if (name == null) {
        throw new CamelExchangeException("Application not specified", exchange);
    }
    IApplication app = domain.getApplicationByName(name);
    if (app == null) {
        throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
    } else {
        app.start();
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) IApplication(com.openshift.client.IApplication)

Example 3 with IApplication

use of com.openshift.client.IApplication in project camel by apache.

the class OpenShiftProducer method doRemoveEnvironmentVariable.

protected void doRemoveEnvironmentVariable(Exchange exchange, IDomain domain) throws CamelExchangeException {
    String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
    if (name == null) {
        throw new CamelExchangeException("Application not specified", exchange);
    }
    IApplication app = domain.getApplicationByName(name);
    if (app == null) {
        throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
    } else {
        String variableName = exchange.getIn().getHeader(OpenShiftConstants.ENVIRONMENT_VARIABLE_NAME, getEndpoint().getApplication(), String.class);
        if (!app.canGetEnvironmentVariables()) {
            throw new CamelExchangeException("The application with id " + name + " can't get Environment Variables", exchange);
        }
        if (ObjectHelper.isNotEmpty(variableName)) {
            app.removeEnvironmentVariable(variableName);
            exchange.getIn().setBody(variableName);
        } else {
            throw new CamelExchangeException("Environment variable name not specified", exchange);
        }
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) IApplication(com.openshift.client.IApplication)

Example 4 with IApplication

use of com.openshift.client.IApplication in project camel by apache.

the class OpenShiftProducer method doGetDeploymentType.

protected void doGetDeploymentType(Exchange exchange, IDomain domain) throws CamelExchangeException {
    String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
    if (name == null) {
        throw new CamelExchangeException("Application not specified", exchange);
    }
    IApplication app = domain.getApplicationByName(name);
    if (app == null) {
        throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
    } else {
        String deploymentType = app.getDeploymentType();
        exchange.getIn().setBody(deploymentType);
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) IApplication(com.openshift.client.IApplication)

Example 5 with IApplication

use of com.openshift.client.IApplication in project camel by apache.

the class OpenShiftProducer method doGetEnvironmentVariableValue.

protected void doGetEnvironmentVariableValue(Exchange exchange, IDomain domain) throws CamelExchangeException {
    String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
    if (name == null) {
        throw new CamelExchangeException("Application not specified", exchange);
    }
    IApplication app = domain.getApplicationByName(name);
    if (app == null) {
        throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
    } else {
        String variableName = exchange.getIn().getHeader(OpenShiftConstants.ENVIRONMENT_VARIABLE_NAME, getEndpoint().getApplication(), String.class);
        if (!app.canGetEnvironmentVariables()) {
            throw new CamelExchangeException("The application with id " + name + " can't get Environment Variables", exchange);
        }
        if (ObjectHelper.isNotEmpty(variableName)) {
            IEnvironmentVariable result = app.getEnvironmentVariable(variableName);
            exchange.getIn().setBody(result.getValue());
        } else {
            throw new CamelExchangeException("Environment variable name not specified", exchange);
        }
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) IApplication(com.openshift.client.IApplication) IEnvironmentVariable(com.openshift.client.IEnvironmentVariable)

Aggregations

IApplication (com.openshift.client.IApplication)25 CamelExchangeException (org.apache.camel.CamelExchangeException)23 IEnvironmentVariable (com.openshift.client.IEnvironmentVariable)5 IEmbeddedCartridge (com.openshift.client.cartridge.IEmbeddedCartridge)4 ApplicationScale (com.openshift.client.ApplicationScale)2 LatestEmbeddableCartridge (com.openshift.client.cartridge.query.LatestEmbeddableCartridge)2 IGear (com.openshift.client.IGear)1 IGearGroup (com.openshift.client.IGearGroup)1 IGearProfile (com.openshift.client.IGearProfile)1 OpenShiftException (com.openshift.client.OpenShiftException)1 IDeployedStandaloneCartridge (com.openshift.client.cartridge.IDeployedStandaloneCartridge)1 IEmbeddableCartridge (com.openshift.client.cartridge.IEmbeddableCartridge)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Endpoint (org.apache.camel.Endpoint)1 Exchange (org.apache.camel.Exchange)1