Search in sources :

Example 6 with ClientConfig

use of io.cdap.cdap.client.config.ClientConfig in project cdap by cdapio.

the class ArtifactHttpHandlerTestBase method setup.

@BeforeClass
public static void setup() {
    artifactRepository = getInjector().getInstance(ArtifactRepository.class);
    systemArtifactsDir = getInjector().getInstance(CConfiguration.class).get(Constants.AppFabric.SYSTEM_ARTIFACTS_DIR);
    DiscoveryServiceClient discoveryClient = getInjector().getInstance(DiscoveryServiceClient.class);
    EndpointStrategy endpointStrategy = new RandomEndpointStrategy(() -> discoveryClient.discover(Constants.Service.METADATA_SERVICE));
    Discoverable discoverable = endpointStrategy.pick(1, TimeUnit.SECONDS);
    Assert.assertNotNull(discoverable);
    String host = discoverable.getSocketAddress().getHostName();
    int port = discoverable.getSocketAddress().getPort();
    ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(host).setPort(port).setSSLEnabled(URIScheme.HTTPS.isMatch(discoverable)).build();
    ClientConfig clientConfig = ClientConfig.builder().setVerifySSLCert(false).setConnectionConfig(connectionConfig).build();
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) ClientConfig(io.cdap.cdap.client.config.ClientConfig) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) BeforeClass(org.junit.BeforeClass)

Example 7 with ClientConfig

use of io.cdap.cdap.client.config.ClientConfig in project cdap by cdapio.

the class UpgradeTool method getClientConfig.

private static ClientConfig getClientConfig(CommandLine commandLine) throws IOException {
    String uriStr = commandLine.hasOption("u") ? commandLine.getOptionValue("u") : "localhost:11015";
    if (!uriStr.contains("://")) {
        uriStr = "http://" + uriStr;
    }
    URI uri = URI.create(uriStr);
    String hostname = uri.getHost();
    int port = uri.getPort();
    boolean sslEnabled = "https".equals(uri.getScheme());
    ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(hostname).setPort(port).setSSLEnabled(sslEnabled).build();
    int readTimeout = commandLine.hasOption("t") ? Integer.parseInt(commandLine.getOptionValue("t")) : DEFAULT_READ_TIMEOUT_MILLIS;
    ClientConfig.Builder clientConfigBuilder = ClientConfig.builder().setDefaultReadTimeout(readTimeout).setConnectionConfig(connectionConfig);
    if (commandLine.hasOption("a")) {
        String tokenFilePath = commandLine.getOptionValue("a");
        File tokenFile = new File(tokenFilePath);
        if (!tokenFile.exists()) {
            throw new IllegalArgumentException("Access token file " + tokenFilePath + " does not exist.");
        }
        if (!tokenFile.isFile()) {
            throw new IllegalArgumentException("Access token file " + tokenFilePath + " is not a file.");
        }
        String tokenValue = new String(Files.readAllBytes(tokenFile.toPath()), StandardCharsets.UTF_8).trim();
        AccessToken accessToken = new AccessToken(tokenValue, 82000L, "Bearer");
        clientConfigBuilder.setAccessToken(accessToken);
    }
    return clientConfigBuilder.build();
}
Also used : AccessToken(io.cdap.cdap.security.authentication.client.AccessToken) ClientConfig(io.cdap.cdap.client.config.ClientConfig) URI(java.net.URI) File(java.io.File) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig)

Example 8 with ClientConfig

use of io.cdap.cdap.client.config.ClientConfig in project cdap by cdapio.

the class CLIConfig method checkConnection.

private void checkConnection(ClientConfig baseClientConfig, ConnectionConfig connectionInfo, AccessToken accessToken) throws IOException, UnauthenticatedException, UnauthorizedException {
    ClientConfig clientConfig = new ClientConfig.Builder(baseClientConfig).setConnectionConfig(connectionInfo).setAccessToken(accessToken).build();
    MetaClient metaClient = new MetaClient(clientConfig);
    try {
        metaClient.ping();
    } catch (IOException e) {
        throw new IOException("Check connection parameters and/or accesstoken", e);
    }
}
Also used : MetaClient(io.cdap.cdap.client.MetaClient) IOException(java.io.IOException) ClientConfig(io.cdap.cdap.client.config.ClientConfig)

Example 9 with ClientConfig

use of io.cdap.cdap.client.config.ClientConfig in project cdap by cdapio.

the class CLIMain method main.

public static void main(String[] args) {
    // disable logback logging
    Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    root.setLevel(Level.OFF);
    final PrintStream output = System.out;
    Options options = getOptions();
    CLIMainArgs cliMainArgs = CLIMainArgs.parse(args, options);
    CommandLineParser parser = new BasicParser();
    try {
        CommandLine command = parser.parse(options, cliMainArgs.getOptionTokens());
        if (command.hasOption(HELP_OPTION.getOpt())) {
            usage();
            System.exit(0);
        }
        LaunchOptions launchOptions = LaunchOptions.builder().setUri(command.getOptionValue(URI_OPTION.getOpt())).setInstanceURI(command.getOptionValue(LINK_OPTION.getOpt(), getDefaultURI().toString())).setNamespace(command.getOptionValue(NAMESPACE_OPTION.getOpt(), NamespaceId.DEFAULT.getNamespace())).setDebug(command.hasOption(DEBUG_OPTION.getOpt())).setVerifySSL(parseBooleanOption(command, VERIFY_SSL_OPTION, DEFAULT_VERIFY_SSL)).setAutoconnect(parseBooleanOption(command, AUTOCONNECT_OPTION, DEFAULT_AUTOCONNECT)).build();
        String scriptFile = command.getOptionValue(SCRIPT_OPTION.getOpt(), "");
        boolean hasScriptFile = command.hasOption(SCRIPT_OPTION.getOpt());
        String[] commandArgs = cliMainArgs.getCommandTokens();
        try {
            ClientConfig clientConfig = ClientConfig.builder().setConnectionConfig(null).setDefaultReadTimeout(parseIntegerOption(command, TIMEOUT_OPTION, 60) * 1000).setUnavailableRetryLimit(parseIntegerOption(command, RETRIES_OPTION, 0)).build();
            final CLIConfig cliConfig = new CLIConfig(clientConfig, output, new AltStyleTableRenderer());
            CLIMain cliMain = new CLIMain(launchOptions, cliConfig);
            CLI cli = cliMain.getCLI();
            if (!cliMain.tryAutoconnect(command)) {
                System.exit(0);
            }
            CLIConnectionConfig connectionConfig = new CLIConnectionConfig(cliConfig.getClientConfig().getConnectionConfig(), cliConfig.getCurrentNamespace(), null);
            cliMain.updateCLIPrompt(connectionConfig);
            if (hasScriptFile) {
                File script = cliMain.getFilePathResolver().resolvePathToFile(scriptFile);
                if (!script.exists()) {
                    output.println("ERROR: Script file '" + script.getAbsolutePath() + "' does not exist");
                    System.exit(1);
                }
                List<String> scriptLines = Files.readLines(script, Charsets.UTF_8);
                for (String scriptLine : scriptLines) {
                    output.print(cliMain.getPrompt(connectionConfig));
                    output.println(scriptLine);
                    cli.execute(scriptLine, output);
                    output.println();
                }
            } else if (commandArgs.length == 0) {
                cli.startInteractiveMode(output);
            } else {
                cli.execute(Joiner.on(" ").join(commandArgs), output);
            }
        } catch (DisconnectedException e) {
            output.printf("Couldn't reach the CDAP instance at '%s'.", e.getConnectionConfig().getURI().toString());
        } catch (Exception e) {
            e.printStackTrace(output);
        }
    } catch (ParseException e) {
        output.println(e.getMessage());
        usage();
    }
}
Also used : PrintStream(java.io.PrintStream) Options(org.apache.commons.cli.Options) CLI(io.cdap.common.cli.CLI) AltStyleTableRenderer(io.cdap.cdap.cli.util.table.AltStyleTableRenderer) Logger(ch.qos.logback.classic.Logger) URISyntaxException(java.net.URISyntaxException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) ParseException(org.apache.commons.cli.ParseException) InvalidCommandException(io.cdap.common.cli.exception.InvalidCommandException) DisconnectedException(io.cdap.cdap.client.exception.DisconnectedException) ConnectException(java.net.ConnectException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) ClientConfig(io.cdap.cdap.client.config.ClientConfig) File(java.io.File) DisconnectedException(io.cdap.cdap.client.exception.DisconnectedException)

Example 10 with ClientConfig

use of io.cdap.cdap.client.config.ClientConfig in project cdap by cdapio.

the class IntegrationTestBaseTest method testDeployApplicationInNamespace.

@Test
public void testDeployApplicationInNamespace() throws Exception {
    NamespaceId namespace = new NamespaceId("Test1");
    NamespaceMeta namespaceMeta = new NamespaceMeta.Builder().setName(namespace).build();
    getNamespaceClient().create(namespaceMeta);
    ClientConfig clientConfig = new ClientConfig.Builder(getClientConfig()).build();
    deployApplication(namespace, AllProgramsApp.class);
    // Check the default namespaces applications to see whether the application wasn't made in the default namespace
    ClientConfig defaultClientConfig = new ClientConfig.Builder(getClientConfig()).build();
    Assert.assertTrue(new ApplicationClient(defaultClientConfig).list(NamespaceId.DEFAULT).isEmpty());
    ApplicationClient applicationClient = new ApplicationClient(clientConfig);
    Assert.assertEquals(AllProgramsApp.NAME, applicationClient.list(namespace).get(0).getName());
    applicationClient.delete(namespace.app(AllProgramsApp.NAME));
    Assert.assertTrue(new ApplicationClient(clientConfig).list(namespace).isEmpty());
}
Also used : ApplicationClient(io.cdap.cdap.client.ApplicationClient) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ClientConfig(io.cdap.cdap.client.config.ClientConfig) Test(org.junit.Test)

Aggregations

ClientConfig (io.cdap.cdap.client.config.ClientConfig)22 ConnectionConfig (io.cdap.cdap.client.config.ConnectionConfig)14 IOException (java.io.IOException)10 NotFoundException (io.cdap.cdap.common.NotFoundException)6 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)6 RetryStrategy (io.cdap.cdap.common.service.RetryStrategy)6 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)6 File (java.io.File)6 RetryableException (io.cdap.cdap.api.retry.RetryableException)4 CsvTableRenderer (io.cdap.cdap.cli.util.table.CsvTableRenderer)4 ApplicationClient (io.cdap.cdap.client.ApplicationClient)4 NamespaceClient (io.cdap.cdap.client.NamespaceClient)4 BadRequestException (io.cdap.cdap.common.BadRequestException)4 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)4 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)4 BasicParser (org.apache.commons.cli.BasicParser)4 CommandLine (org.apache.commons.cli.CommandLine)4 CommandLineParser (org.apache.commons.cli.CommandLineParser)4 Options (org.apache.commons.cli.Options)3 Logger (ch.qos.logback.classic.Logger)2