Search in sources :

Example 16 with HttpResponseWrapper

use of org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper in project scheduling by ow2-proactive.

the class DefineNodeSourceCommand method execute.

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    QueryStringBuilder infrastructure = currentContext.getProperty(SET_INFRASTRUCTURE, QueryStringBuilder.class);
    QueryStringBuilder policy = currentContext.getProperty(SET_POLICY, QueryStringBuilder.class);
    if (infrastructure == null) {
        throw new CLIException(REASON_INVALID_ARGUMENTS, "Infrastructure not specified");
    }
    if (policy == null) {
        throw new CLIException(REASON_INVALID_ARGUMENTS, "Policy not specified");
    }
    if (currentContext.getProperty(SET_NODE_SOURCE, String.class) != null) {
        nodeSource = currentContext.getProperty(SET_NODE_SOURCE, String.class);
    }
    HttpPost request = new HttpPost(currentContext.getResourceUrl(RM_REST_ENDPOINT));
    QueryStringBuilder queryStringBuilder = new QueryStringBuilder();
    queryStringBuilder.add("nodeSourceName", nodeSource).addAll(infrastructure).addAll(policy).add("nodesRecoverable", nodesRecoverable);
    request.setEntity(queryStringBuilder.buildEntity(APPLICATION_FORM_URLENCODED));
    HttpResponseWrapper response = execute(request, currentContext);
    if (statusCode(OK) == statusCode(response)) {
        NSStateView nsState = readValue(response, NSStateView.class, currentContext);
        boolean success = nsState.isResult();
        resultStack(currentContext).push(success);
        if (success) {
            writeLine(currentContext, "Node source successfully defined.");
        } else {
            writeLine(currentContext, "%s %s", "Cannot define node source:", nodeSource);
        }
    } else {
        handleError("An error occurred while defining node source:", response, currentContext);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) NSStateView(org.ow2.proactive_grid_cloud_portal.cli.json.NSStateView) QueryStringBuilder(org.ow2.proactive_grid_cloud_portal.cli.utils.QueryStringBuilder)

Example 17 with HttpResponseWrapper

use of org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper in project scheduling by ow2-proactive.

the class GetTopologyCommand method execute.

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    HttpGet request = new HttpGet(currentContext.getResourceUrl("topology"));
    HttpResponseWrapper response = execute(request, currentContext);
    if (statusCode(OK) == statusCode(response)) {
        TopologyView topology = readValue(response, TopologyView.class, currentContext);
        resultStack(currentContext).push(topology);
        if (!currentContext.isSilent()) {
            writeLine(currentContext, "%s", StringUtility.string(topology));
        }
    } else {
        handleError("An error occurred while retrieving the topology:", response, currentContext);
    }
}
Also used : HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper) HttpGet(org.apache.http.client.methods.HttpGet) TopologyView(org.ow2.proactive_grid_cloud_portal.cli.json.TopologyView)

Example 18 with HttpResponseWrapper

use of org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper in project scheduling by ow2-proactive.

the class ListNodeCommand method execute.

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    HttpGet request = new HttpGet(currentContext.getResourceUrl("monitoring"));
    HttpResponseWrapper response = execute(request, currentContext);
    if (statusCode(OK) == statusCode(response)) {
        RmStateView state = readValue(response, RmStateView.class, currentContext);
        NodeEventView[] nodeEvents = state.getNodesEvents();
        NodeEventView[] selectedNodeEvents = null;
        if (nodeEvents != null) {
            if (nodeSource == null) {
                selectedNodeEvents = nodeEvents;
            } else {
                List<NodeEventView> selectedList = new ArrayList<>();
                for (NodeEventView nodeEvent : nodeEvents) {
                    if (!nodeSource.equals(nodeEvent.getNodeSource())) {
                        // node source doesn't match
                        continue;
                    } else {
                        selectedList.add(nodeEvent);
                    }
                }
                selectedNodeEvents = selectedList.toArray(new NodeEventView[selectedList.size()]);
            }
        }
        // filter out all node events that was removed
        // so rm client does not display them
        List<NodeEventView> filtered = new ArrayList<>();
        for (NodeEventView nodeEvent : selectedNodeEvents) {
            if (!nodeEvent.isRemoved()) {
                filtered.add(nodeEvent);
            }
        }
        NodeEventView[] result = new NodeEventView[filtered.size()];
        result = filtered.toArray(result);
        resultStack(currentContext).push(result);
        writeLine(currentContext, "%s", StringUtility.string(result));
    } else {
        handleError("An error occurred while retrieving nodes:", response, currentContext);
    }
}
Also used : HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) NodeEventView(org.ow2.proactive_grid_cloud_portal.cli.json.NodeEventView) RmStateView(org.ow2.proactive_grid_cloud_portal.cli.json.RmStateView)

Example 19 with HttpResponseWrapper

use of org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper in project scheduling by ow2-proactive.

the class ListPolicyCommand method execute.

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    Map<String, PluginView> knownPolicyMap = currentContext.getPolicies();
    if (knownPolicyMap == null) {
        HttpGet request = new HttpGet(currentContext.getResourceUrl("policies"));
        HttpResponseWrapper response = execute(request, currentContext);
        if (statusCode(OK) == statusCode(response)) {
            List<PluginView> pluginViewList = readValue(response, new TypeReference<List<PluginView>>() {
            }, currentContext);
            resultStack(currentContext).push(pluginViewList.toArray(new PluginView[pluginViewList.size()]));
            knownPolicyMap = new HashMap<>();
            for (PluginView pluginView : pluginViewList) {
                knownPolicyMap.put(pluginView.getPluginName(), pluginView);
            }
            currentContext.setPolicies(knownPolicyMap);
        } else {
            handleError("An error occurred while retrieving supported policy types:", response, currentContext);
        }
    }
    if (!currentContext.isSilent()) {
        if (knownPolicyMap != null) {
            writeLine(currentContext, "%n%s:%n", "Supported policy types");
            for (PluginView policy : knownPolicyMap.values()) {
                writeLine(currentContext, "%s%n", policy.toString());
            }
        }
    }
}
Also used : HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper) HttpGet(org.apache.http.client.methods.HttpGet) PluginView(org.ow2.proactive_grid_cloud_portal.cli.json.PluginView) List(java.util.List)

Example 20 with HttpResponseWrapper

use of org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper in project scheduling by ow2-proactive.

the class LockNodeCommand method execute.

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    HttpPost request = new HttpPost(currentContext.getResourceUrl("node/lock"));
    QueryStringBuilder queryStringBuilder = new QueryStringBuilder();
    for (String nodeUrl : nodeUrls) {
        queryStringBuilder.add("nodeurls", nodeUrl);
    }
    request.setEntity(queryStringBuilder.buildEntity(APPLICATION_FORM_URLENCODED));
    HttpResponseWrapper response = execute(request, currentContext);
    if (statusCode(OK) == statusCode(response)) {
        boolean successful = readValue(response, Boolean.TYPE, currentContext);
        resultStack(currentContext).push(successful);
        if (successful) {
            writeLine(currentContext, "Node(s) locked successfully.");
        } else {
            writeLine(currentContext, "Cannot lock node(s).");
        }
    } else {
        handleError("An error occurred while locking nodes:", response, currentContext);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper) QueryStringBuilder(org.ow2.proactive_grid_cloud_portal.cli.utils.QueryStringBuilder)

Aggregations

HttpResponseWrapper (org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper)22 QueryStringBuilder (org.ow2.proactive_grid_cloud_portal.cli.utils.QueryStringBuilder)12 HttpGet (org.apache.http.client.methods.HttpGet)10 HttpPost (org.apache.http.client.methods.HttpPost)9 CLIException (org.ow2.proactive_grid_cloud_portal.cli.CLIException)7 List (java.util.List)5 NSStateView (org.ow2.proactive_grid_cloud_portal.cli.json.NSStateView)4 PluginView (org.ow2.proactive_grid_cloud_portal.cli.json.PluginView)4 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)3 File (java.io.File)3 RmStateView (org.ow2.proactive_grid_cloud_portal.cli.json.RmStateView)3 ArrayList (java.util.ArrayList)2 HttpPut (org.apache.http.client.methods.HttpPut)2 ConfigurableFieldView (org.ow2.proactive_grid_cloud_portal.cli.json.ConfigurableFieldView)2 ErrorView (org.ow2.proactive_grid_cloud_portal.cli.json.ErrorView)2 Type (org.ow2.proactive_grid_cloud_portal.cli.json.FieldMetaDataView.Type)2 NodeEventView (org.ow2.proactive_grid_cloud_portal.cli.json.NodeEventView)2 IOException (java.io.IOException)1 Stack (java.util.Stack)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1