Search in sources :

Example 1 with ConfigurableFieldView

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

the class SetPolicyCommand method execute.

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    Map<String, PluginView> knownPolicies = currentContext.getPolicies();
    if (knownPolicies == null) {
        HttpGet request = new HttpGet(currentContext.getResourceUrl("policies"));
        HttpResponseWrapper response = execute(request, currentContext);
        if (statusCode(OK) == statusCode(response)) {
            knownPolicies = new HashMap<>();
            List<PluginView> pluginViewList = readValue(response, new TypeReference<List<PluginView>>() {
            }, currentContext);
            for (PluginView pluginView : pluginViewList) {
                knownPolicies.put(pluginView.getPluginName(), pluginView);
            }
            currentContext.setPolicies(knownPolicies);
        } else {
            handleError("Unable to retrieve known policy types:", response, currentContext);
        }
    }
    if (knownPolicies != null) {
        PluginView pluginView = knownPolicies.get(policyType);
        if (pluginView == null) {
            throw new CLIException(REASON_INVALID_ARGUMENTS, String.format("Unknown policy type: %s", policyType));
        }
        ConfigurableFieldView[] configurableFields = pluginView.getConfigurableFields();
        if (configurableFields.length != policyArgs.length) {
            throw new CLIException(REASON_INVALID_ARGUMENTS, String.format("Invalid number of arguments specified for '%s' type.", policyType));
        }
        QueryStringBuilder queryStringBuilder = new QueryStringBuilder();
        queryStringBuilder.add("policyType", policyType);
        for (int index = 0; index < configurableFields.length; index++) {
            ConfigurableFieldView cf = configurableFields[index];
            Type ft = cf.getMeta().type();
            if (FILEBROWSER.equals(ft) || CREDENTIAL.equals(ft)) {
                String contents = FileUtility.readFileToString(new File(policyArgs[index]));
                queryStringBuilder.add("policyFileParameters", contents);
            } else {
                queryStringBuilder.add("policyParameters", policyArgs[index]);
            }
        }
        currentContext.setProperty(SET_POLICY, queryStringBuilder);
    }
}
Also used : HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper) HttpGet(org.apache.http.client.methods.HttpGet) ConfigurableFieldView(org.ow2.proactive_grid_cloud_portal.cli.json.ConfigurableFieldView) QueryStringBuilder(org.ow2.proactive_grid_cloud_portal.cli.utils.QueryStringBuilder) Type(org.ow2.proactive_grid_cloud_portal.cli.json.FieldMetaDataView.Type) PluginView(org.ow2.proactive_grid_cloud_portal.cli.json.PluginView) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) List(java.util.List) File(java.io.File)

Example 2 with ConfigurableFieldView

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

the class SetInfrastructureCommand method execute.

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    Map<String, PluginView> infrastructures = currentContext.getInfrastructures();
    if (infrastructures == null) {
        HttpGet request = new HttpGet(currentContext.getResourceUrl("infrastructures"));
        HttpResponseWrapper response = execute(request, currentContext);
        if (statusCode(OK) == statusCode(response)) {
            infrastructures = new HashMap<>();
            List<PluginView> pluginViewList = readValue(response, new TypeReference<List<PluginView>>() {
            }, currentContext);
            for (PluginView pluginView : pluginViewList) {
                infrastructures.put(pluginView.getPluginName(), pluginView);
            }
            currentContext.setInfrastructures(infrastructures);
        } else {
            handleError("Unable to retrieve known infrastructure types:", response, currentContext);
        }
    }
    if (infrastructures != null) {
        PluginView pluginView = infrastructures.get(infrastructureType);
        if (pluginView == null) {
            throw new CLIException(REASON_INVALID_ARGUMENTS, String.format("Unknown infrastructure type: %s", infrastructureType));
        }
        ConfigurableFieldView[] configurableFields = pluginView.getConfigurableFields();
        if (configurableFields.length != infrastructureArgs.length) {
            throw new CLIException(REASON_INVALID_ARGUMENTS, String.format("Invalid number of arguments specified for '%s' type.", infrastructureType));
        }
        QueryStringBuilder queryStringBuilder = new QueryStringBuilder();
        queryStringBuilder.add("infrastructureType", infrastructureType);
        for (int index = 0; index < configurableFields.length; index++) {
            ConfigurableFieldView cf = configurableFields[index];
            Type ft = cf.getMeta().type();
            if (FILEBROWSER.equals(ft) || CREDENTIAL.equals(ft)) {
                if ("".equals(infrastructureArgs[index])) {
                    String contents = "";
                    queryStringBuilder.add("infrastructureFileParameters", contents);
                } else {
                    String contents = FileUtility.readFileToString(new File(infrastructureArgs[index]));
                    queryStringBuilder.add("infrastructureFileParameters", contents);
                }
            } else {
                queryStringBuilder.add("infrastructureParameters", infrastructureArgs[index]);
            }
        }
        currentContext.setProperty(SET_INFRASTRUCTURE, queryStringBuilder);
    }
}
Also used : HttpResponseWrapper(org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper) HttpGet(org.apache.http.client.methods.HttpGet) ConfigurableFieldView(org.ow2.proactive_grid_cloud_portal.cli.json.ConfigurableFieldView) QueryStringBuilder(org.ow2.proactive_grid_cloud_portal.cli.utils.QueryStringBuilder) Type(org.ow2.proactive_grid_cloud_portal.cli.json.FieldMetaDataView.Type) PluginView(org.ow2.proactive_grid_cloud_portal.cli.json.PluginView) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException) List(java.util.List) File(java.io.File)

Aggregations

File (java.io.File)2 List (java.util.List)2 HttpGet (org.apache.http.client.methods.HttpGet)2 CLIException (org.ow2.proactive_grid_cloud_portal.cli.CLIException)2 ConfigurableFieldView (org.ow2.proactive_grid_cloud_portal.cli.json.ConfigurableFieldView)2 Type (org.ow2.proactive_grid_cloud_portal.cli.json.FieldMetaDataView.Type)2 PluginView (org.ow2.proactive_grid_cloud_portal.cli.json.PluginView)2 HttpResponseWrapper (org.ow2.proactive_grid_cloud_portal.cli.utils.HttpResponseWrapper)2 QueryStringBuilder (org.ow2.proactive_grid_cloud_portal.cli.utils.QueryStringBuilder)2