Search in sources :

Example 41 with CLIException

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

the class LoginCommand method login.

@Override
protected String login(ApplicationContext currentContext) throws CLIException {
    String password = currentContext.getProperty(PASSWORD, String.class);
    if (password == null) {
        password = new String(readPassword(currentContext, "password:"));
    }
    HttpPost request = new HttpPost(currentContext.getResourceUrl("login"));
    StringEntity entity = new StringEntity("username=" + username + "&password=" + password, APPLICATION_FORM_URLENCODED);
    request.setEntity(entity);
    HttpResponseWrapper response = execute(request, currentContext);
    if (statusCode(OK) == statusCode(response)) {
        return StringUtility.responseAsString(response).trim();
    } else {
        throw buildCLIException(REASON_OTHER, response, currentContext);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper)

Example 42 with CLIException

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

the class LoginWithCredentialsCommand method login.

@Override
protected String login(ApplicationContext currentContext) throws CLIException {
    File credentials = new File(pathname);
    if (!credentials.exists()) {
        throw new CLIException(REASON_INVALID_ARGUMENTS, String.format("File does not exist: %s", credentials.getAbsolutePath()));
    }
    if (warn) {
        writeLine(currentContext, "Using the default credentials file: %s", credentials.getAbsolutePath());
    }
    HttpPost request = new HttpPost(currentContext.getResourceUrl("login"));
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("credential", new ByteArrayBody(FileUtility.byteArray(credentials), APPLICATION_OCTET_STREAM.getMimeType()));
    request.setEntity(entity);
    HttpResponseWrapper response = execute(request, currentContext);
    if (statusCode(OK) == statusCode(response)) {
        return StringUtility.responseAsString(response).trim();
    } else {
        handleError("An error occurred while logging: ", response, currentContext);
        throw new CLIException(REASON_OTHER, "An error occurred while logging.");
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) ByteArrayBody(org.apache.http.entity.mime.content.ByteArrayBody) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) File(java.io.File)

Example 43 with CLIException

use of org.ow2.proactive_grid_cloud_portal.cli.CLIException 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 44 with CLIException

use of org.ow2.proactive_grid_cloud_portal.cli.CLIException 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 45 with CLIException

use of org.ow2.proactive_grid_cloud_portal.cli.CLIException 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)

Aggregations

CLIException (org.ow2.proactive_grid_cloud_portal.cli.CLIException)49 SchedulerRestInterface (org.ow2.proactive_grid_cloud_portal.common.SchedulerRestInterface)30 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 IOException (java.io.IOException)8 File (java.io.File)6 List (java.util.List)5 Throwables.getStackTraceAsString (com.google.common.base.Throwables.getStackTraceAsString)4 NSStateView (org.ow2.proactive_grid_cloud_portal.cli.json.NSStateView)4 PluginView (org.ow2.proactive_grid_cloud_portal.cli.json.PluginView)4 ArrayList (java.util.ArrayList)3 RmStateView (org.ow2.proactive_grid_cloud_portal.cli.json.RmStateView)3 FileInputStream (java.io.FileInputStream)2 Stack (java.util.Stack)2 HttpPut (org.apache.http.client.methods.HttpPut)2 ConfigurableFieldView (org.ow2.proactive_grid_cloud_portal.cli.json.ConfigurableFieldView)2 TaskResultData (org.ow2.proactive_grid_cloud_portal.scheduler.dto.TaskResultData)2 NotConnectedRestException (org.ow2.proactive_grid_cloud_portal.scheduler.exception.NotConnectedRestException)2