Search in sources :

Example 1 with CommandRequest

use of org.apache.geode.management.internal.cli.CommandRequest in project geode by apache.

the class CLIMultiStepHelper method executeStep.

private static Result executeStep(final LogWrapper logWrapper, final Gfsh shell, final CLIStep nextStep, final ParseResult parseResult, final SectionResultData nextStepArgs) {
    try {
        if (nextStep instanceof CLIRemoteStep) {
            if (shell.isConnectedAndReady()) {
                if (GfshParseResult.class.isInstance(parseResult)) {
                    GfshParseResult gfshParseResult = (GfshParseResult) parseResult;
                    // stepArgs
                    if (nextStepArgs != null) {
                        GfJsonObject argsJSon = nextStepArgs.getSectionGfJsonObject();
                        shell.setEnvProperty(CLIMultiStepHelper.STEP_ARGS, argsJSon.toString());
                    }
                    CommandRequest commandRequest = new CommandRequest(gfshParseResult, shell.getEnv());
                    commandRequest.setCustomInput(changeStepName(gfshParseResult.getUserInput(), nextStep.getName()));
                    commandRequest.getCustomParameters().put(CliStrings.QUERY__STEPNAME, nextStep.getName());
                    String json = (String) shell.getOperationInvoker().processCommand(commandRequest);
                    return ResultBuilder.fromJson(json);
                } else {
                    throw new IllegalArgumentException("Command Configuration/Definition error.");
                }
            } else {
                try {
                    throw new Exception();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                throw new IllegalStateException("Can't execute a remote command without connection. Use 'connect' first to connect.");
            }
        } else {
            Map<String, String> args = CommandExecutionContext.getShellEnv();
            if (args == null) {
                args = new HashMap<String, String>();
                CommandExecutionContext.setShellEnv(args);
            }
            if (nextStepArgs != null) {
                GfJsonObject argsJSon = nextStepArgs.getSectionGfJsonObject();
                Gfsh.getCurrentInstance().setEnvProperty(CLIMultiStepHelper.STEP_ARGS, argsJSon.toString());
            }
            return nextStep.exec();
        }
    } catch (CLIStepExecption e) {
        logWrapper.severe("CLIStep " + nextStep.getName() + " failed aborting command");
        throw e;
    }
}
Also used : CommandRequest(org.apache.geode.management.internal.cli.CommandRequest) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject) GfshParseResult(org.apache.geode.management.internal.cli.GfshParseResult) GfJsonException(org.apache.geode.management.internal.cli.json.GfJsonException)

Example 2 with CommandRequest

use of org.apache.geode.management.internal.cli.CommandRequest in project geode by apache.

the class RestHttpOperationInvokerJUnitTest method testCreateHttpRequest.

@Test
public void testCreateHttpRequest() {
    final Map<String, String> commandOptions = new HashMap<>();
    commandOptions.put("author", "Adams");
    commandOptions.put("blankOption", "  ");
    commandOptions.put("category", "sci-fi");
    commandOptions.put("emptyOption", StringUtils.EMPTY);
    commandOptions.put("isbn", "0-123456789");
    commandOptions.put("nullOption", null);
    commandOptions.put("title", "Hitch Hiker's Guide to the Galaxy");
    commandOptions.put("year", "1983");
    final CommandRequest command = createCommandRequest("add-book", commandOptions);
    final ClientHttpRequest request = getOperationInvoker().createHttpRequest(command);
    assertNotNull(request);
    assertEquals("POST http://host.domain.com/service/v1/libraries/{name}/books", request.getLink().toHttpRequestLine());
    assertEquals("Adams", request.getParameterValue("author"));
    assertEquals("sci-fi", request.getParameterValue("category"));
    assertEquals("0-123456789", request.getParameterValue("isbn"));
    assertEquals("Hitch Hiker's Guide to the Galaxy", request.getParameterValue("title"));
    assertEquals("1983", request.getParameterValue("year"));
    assertTrue(request.getParameters().containsKey("blankOption"));
    assertTrue(request.getParameters().containsKey("emptyOption"));
    assertFalse(request.getParameters().containsKey("nullOption"));
    for (String requestParameter : request.getParameters().keySet()) {
        assertFalse(requestParameter.startsWith(RestHttpOperationInvoker.ENVIRONMENT_VARIABLE_REQUEST_PARAMETER_PREFIX));
    }
    assertNull(request.getParameterValue(RestHttpOperationInvoker.RESOURCES_REQUEST_PARAMETER));
}
Also used : CommandRequest(org.apache.geode.management.internal.cli.CommandRequest) HashMap(java.util.HashMap) ClientHttpRequest(org.apache.geode.management.internal.web.http.ClientHttpRequest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 3 with CommandRequest

use of org.apache.geode.management.internal.cli.CommandRequest in project geode by apache.

the class RestHttpOperationInvokerJUnitTest method testCreateHttpRequestWithEnvironmentVariables.

@Test
public void testCreateHttpRequestWithEnvironmentVariables() {
    final Map<String, String> commandOptions = new HashMap<>(2);
    commandOptions.put("name", "ElLibreDeCongress");
    commandOptions.put("isbn", "${ISBN}");
    final Map<String, String> environment = new HashMap<>(2);
    environment.put("ISBN", "0-987654321");
    environment.put("VAR", "test");
    final CommandRequest command = createCommandRequest("get-book", commandOptions, environment);
    final ClientHttpRequest request = getOperationInvoker().createHttpRequest(command);
    assertNotNull(request);
    assertEquals("GET http://host.domain.com/service/v1/libraries/{name}/books/{isbn}", request.getLink().toHttpRequestLine());
    assertEquals("${ISBN}", request.getParameterValue("isbn"));
    assertFalse(request.getParameters().containsKey("ISBN"));
    assertEquals("0-987654321", request.getParameterValue(RestHttpOperationInvoker.ENVIRONMENT_VARIABLE_REQUEST_PARAMETER_PREFIX + "ISBN"));
    assertFalse(request.getParameters().containsKey("VAR"));
    assertEquals("test", request.getParameterValue(RestHttpOperationInvoker.ENVIRONMENT_VARIABLE_REQUEST_PARAMETER_PREFIX + "VAR"));
}
Also used : CommandRequest(org.apache.geode.management.internal.cli.CommandRequest) HashMap(java.util.HashMap) ClientHttpRequest(org.apache.geode.management.internal.web.http.ClientHttpRequest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 4 with CommandRequest

use of org.apache.geode.management.internal.cli.CommandRequest in project geode by apache.

the class RestHttpOperationInvokerJUnitTest method testCreateHttpRequestWithFileData.

@Test
public void testCreateHttpRequestWithFileData() {
    final Map<String, String> commandOptions = Collections.singletonMap("isbn", "0-123456789");
    final byte[][] fileData = { "/path/to/book/content.txt".getBytes(), "Once upon a time in a galaxy far, far away...".getBytes() };
    final CommandRequest command = createCommandRequest("add-book", commandOptions, fileData);
    final ClientHttpRequest request = getOperationInvoker().createHttpRequest(command);
    assertNotNull(request);
    assertEquals("POST http://host.domain.com/service/v1/libraries/{name}/books", request.getLink().toHttpRequestLine());
    assertEquals("0-123456789", request.getParameterValue("isbn"));
    assertTrue(request.getParameters().containsKey(RestHttpOperationInvoker.RESOURCES_REQUEST_PARAMETER));
    assertTrue(request.getParameterValue(RestHttpOperationInvoker.RESOURCES_REQUEST_PARAMETER) instanceof Resource);
    final List<Object> resources = request.getParameterValues(RestHttpOperationInvoker.RESOURCES_REQUEST_PARAMETER);
    assertNotNull(resources);
    assertFalse(resources.isEmpty());
    assertEquals(1, resources.size());
}
Also used : CommandRequest(org.apache.geode.management.internal.cli.CommandRequest) Resource(org.springframework.core.io.Resource) ClientHttpRequest(org.apache.geode.management.internal.web.http.ClientHttpRequest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 5 with CommandRequest

use of org.apache.geode.management.internal.cli.CommandRequest in project geode by apache.

the class SimpleHttpOperationInvokerJUnitTest method testCreateLink.

@Test
public void testCreateLink() throws Exception {
    final CommandRequest command = createCommandRequest("delete resource --id=1");
    final Link actualLink = getOperationInvoker().createLink(command);
    assertNotNull(actualLink);
    assertEquals(SimpleHttpOperationInvoker.LINK_RELATION, actualLink.getRelation());
    assertEquals(HttpMethod.POST, actualLink.getMethod());
    assertTrue(toString(actualLink.getHref()).endsWith(command.getInput()));
}
Also used : CommandRequest(org.apache.geode.management.internal.cli.CommandRequest) Link(org.apache.geode.management.internal.web.domain.Link) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Aggregations

CommandRequest (org.apache.geode.management.internal.cli.CommandRequest)8 UnitTest (org.apache.geode.test.junit.categories.UnitTest)6 Test (org.junit.Test)6 ClientHttpRequest (org.apache.geode.management.internal.web.http.ClientHttpRequest)4 HashMap (java.util.HashMap)2 GfshParseResult (org.apache.geode.management.internal.cli.GfshParseResult)2 Link (org.apache.geode.management.internal.web.domain.Link)2 Path (java.nio.file.Path)1 CommandProcessingException (org.apache.geode.management.cli.CommandProcessingException)1 Result (org.apache.geode.management.cli.Result)1 CliAroundInterceptor (org.apache.geode.management.internal.cli.CliAroundInterceptor)1 CommandResponse (org.apache.geode.management.internal.cli.CommandResponse)1 GfJsonException (org.apache.geode.management.internal.cli.json.GfJsonException)1 GfJsonObject (org.apache.geode.management.internal.cli.json.GfJsonObject)1 FileResult (org.apache.geode.management.internal.cli.result.FileResult)1 NotAuthorizedException (org.apache.geode.security.NotAuthorizedException)1 Resource (org.springframework.core.io.Resource)1 ParseResult (org.springframework.shell.event.ParseResult)1