Search in sources :

Example 1 with StreamsConnection

use of com.ibm.streamsx.rest.StreamsConnection in project streamsx.topology by IBMStreams.

the class StreamsOnlyConnectionTest method testBadConnections.

@Test
public void testBadConnections() throws Exception {
    assumeNotNull(System.getenv("STREAMS_REST_URL"));
    URL correctUrl = new URL(System.getenv("STREAMS_REST_URL"));
    URL badUrl = new URL(correctUrl.getProtocol(), correctUrl.getHost(), correctUrl.getPort(), "/streams/re");
    Set<Integer> okErrs = new HashSet<>();
    okErrs.add(404);
    okErrs.add(405);
    // send in wrong url
    StreamsConnection badConn = StreamsConnection.createInstance(null, null, badUrl.toExternalForm());
    badConn.allowInsecureHosts(true);
    try {
        badConn.getInstances();
    } catch (RESTException r) {
        assertTrue(r.toString(), okErrs.contains(r.getStatusCode()));
    }
    // send in url too long
    badUrl = new URL(correctUrl.getProtocol(), correctUrl.getHost(), correctUrl.getPort(), "/streams/rest/resourcesTooLong");
    badConn = StreamsConnection.createInstance(null, null, badUrl.toExternalForm());
    badConn.allowInsecureHosts(true);
    try {
        badConn.getInstances();
    } catch (RESTException r) {
        assertTrue(r.toString(), okErrs.contains(r.getStatusCode()));
    }
    // send in bad iName
    badConn = StreamsConnection.createInstance("fakeName", null, correctUrl.toExternalForm());
    badConn.allowInsecureHosts(true);
    try {
        badConn.getInstances();
    } catch (RESTException r) {
        assertEquals(r.toString(), 401, r.getStatusCode());
    }
    // send in wrong password
    badConn = StreamsConnection.createInstance(null, "badPassword", correctUrl.toExternalForm());
    badConn.allowInsecureHosts(true);
    try {
        badConn.getInstances();
    } catch (RESTException r) {
        assertEquals(r.toString(), 401, r.getStatusCode());
    }
}
Also used : RESTException(com.ibm.streamsx.rest.RESTException) StreamsConnection(com.ibm.streamsx.rest.StreamsConnection) URL(java.net.URL) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with StreamsConnection

use of com.ibm.streamsx.rest.StreamsConnection in project streamsx.topology by IBMStreams.

the class RemoteDistributedStreamsContext method setSubmissionInstance.

static void setSubmissionInstance(AppEntity entity) throws IOException {
    Instance cfgInstance = getConfigInstance(entity);
    if (cfgInstance != null) {
        StreamsConnection sc = cfgInstance.getStreamsConnection();
        boolean verify = cfgInstance.getStreamsConnection().isVerify();
        JsonObject deploy = deploy(entity.submission);
        Function<Executor, String> authenticatorO = sc.getAuthenticator();
        deploy.addProperty(ContextProperties.SSL_VERIFY, verify);
        JsonObject service;
        if (authenticatorO instanceof ICP4DAuthenticator) {
            ICP4DAuthenticator authenticator = (ICP4DAuthenticator) authenticatorO;
            service = authenticator.config(verify);
        } else if (authenticatorO instanceof StandaloneAuthenticator) {
            StandaloneAuthenticator authenticator = (StandaloneAuthenticator) authenticatorO;
            service = authenticator.config(verify);
            String buildServiceUrl = getConfigBuildServiceUrl(entity);
            if (buildServiceUrl == null) {
                buildServiceUrl = System.getenv(Util.STREAMS_BUILD_URL);
            }
            if (buildServiceUrl != null) {
                // Copy so we don't affect instance. Version of gson we
                // use lacks deepCopy() so we serialize / parse to copy.
                String json = service.toString();
                service = new JsonParser().parse(json).getAsJsonObject();
                JsonObject connInfo = service.getAsJsonObject(CONNECTION_INFO);
                if (connInfo.has(SERVICE_BUILD_ENDPOINT)) {
                    connInfo.remove(SERVICE_BUILD_ENDPOINT);
                }
                connInfo.addProperty(SERVICE_BUILD_ENDPOINT, buildServiceUrl);
            }
        } else {
            throw new IllegalStateException("Invalid Instance for Streams V5: " + cfgInstance);
        }
        deploy.add(StreamsKeys.SERVICE_DEFINITION, service);
    }
}
Also used : ICP4DAuthenticator(com.ibm.streamsx.rest.internal.ICP4DAuthenticator) Executor(org.apache.http.client.fluent.Executor) Instance(com.ibm.streamsx.rest.Instance) JsonObject(com.google.gson.JsonObject) StreamsConnection(com.ibm.streamsx.rest.StreamsConnection) StandaloneAuthenticator(com.ibm.streamsx.rest.internal.StandaloneAuthenticator) JsonParser(com.google.gson.JsonParser)

Example 3 with StreamsConnection

use of com.ibm.streamsx.rest.StreamsConnection in project streamsx.topology by IBMStreams.

the class RemoteEdgeContext method setSubmissionInstance.

static void setSubmissionInstance(AppEntity entity) throws IOException {
    Instance cfgInstance = getConfigInstance(entity);
    if (cfgInstance != null) {
        StreamsConnection sc = cfgInstance.getStreamsConnection();
        boolean verify = cfgInstance.getStreamsConnection().isVerify();
        JsonObject deploy = deploy(entity.submission);
        Function<Executor, String> authenticatorO = sc.getAuthenticator();
        deploy.addProperty(ContextProperties.SSL_VERIFY, verify);
        JsonObject service;
        if (authenticatorO instanceof ICP4DAuthenticator) {
            ICP4DAuthenticator authenticator = (ICP4DAuthenticator) authenticatorO;
            service = authenticator.config(verify);
        } else if (authenticatorO instanceof StandaloneAuthenticator) {
            StandaloneAuthenticator authenticator = (StandaloneAuthenticator) authenticatorO;
            service = authenticator.config(verify);
            String buildServiceUrl = getConfigBuildServiceUrl(entity);
            if (buildServiceUrl == null) {
                buildServiceUrl = System.getenv(Util.STREAMS_BUILD_URL);
            }
            if (buildServiceUrl != null) {
                // Copy so we don't affect instance. Version of gson we
                // use lacks deepCopy() so we serialize / parse to copy.
                String json = service.toString();
                service = new JsonParser().parse(json).getAsJsonObject();
                JsonObject connInfo = service.getAsJsonObject(CONNECTION_INFO);
                if (connInfo.has(SERVICE_BUILD_ENDPOINT)) {
                    connInfo.remove(SERVICE_BUILD_ENDPOINT);
                }
                connInfo.addProperty(SERVICE_BUILD_ENDPOINT, buildServiceUrl);
            }
        } else {
            throw new IllegalStateException("Invalid Instance for Streams V5: " + cfgInstance);
        }
        deploy.add(StreamsKeys.SERVICE_DEFINITION, service);
    }
}
Also used : ICP4DAuthenticator(com.ibm.streamsx.rest.internal.ICP4DAuthenticator) Executor(org.apache.http.client.fluent.Executor) Instance(com.ibm.streamsx.rest.Instance) JsonObject(com.google.gson.JsonObject) StreamsConnection(com.ibm.streamsx.rest.StreamsConnection) StandaloneAuthenticator(com.ibm.streamsx.rest.internal.StandaloneAuthenticator) JsonParser(com.google.gson.JsonParser)

Example 4 with StreamsConnection

use of com.ibm.streamsx.rest.StreamsConnection in project streamsx.topology by IBMStreams.

the class DistributedStreamsRestContext method postBuildAction.

@Override
protected void postBuildAction(JsonObject deploy, JsonObject jco, JsonObject result) throws Exception {
    if (instance == null) {
        URL instanceUrl = new URL(StreamsKeys.getStreamsInstanceURL(deploy));
        String path = instanceUrl.getPath();
        URL restUrl;
        if (path.startsWith("/streams/rest/instances/")) {
            restUrl = new URL(instanceUrl.getProtocol(), instanceUrl.getHost(), instanceUrl.getPort(), "/streams/rest/resources");
        } else {
            restUrl = new URL(instanceUrl.getProtocol(), instanceUrl.getHost(), instanceUrl.getPort(), path.replaceFirst("/streams-rest/", "/streams-resource/"));
        }
        JsonObject serviceDefinition = object(deploy, StreamsKeys.SERVICE_DEFINITION);
        String name = jstring(serviceDefinition, "service_name");
        final boolean verify = sslVerify(deploy);
        Function<Executor, String> authenticator = (name == null || name.isEmpty()) ? StandaloneAuthenticator.of(serviceDefinition) : ICP4DAuthenticator.of(serviceDefinition, verify);
        StreamsConnection conn = StreamsConnection.ofAuthenticator(restUrl.toExternalForm(), authenticator);
        if (!verify)
            conn.allowInsecureHosts(true);
        // Create the instance directly from the URL
        instance = conn.getInstance(instanceUrl.toExternalForm());
    }
    JsonArray artifacts = GsonUtilities.array(GsonUtilities.object(result, "build"), "artifacts");
    try {
        if (artifacts == null || artifacts.size() == 0)
            throw new IllegalStateException("No build artifacts produced.");
        if (artifacts.size() != 1)
            throw new IllegalStateException("Multiple build artifacts produced.");
        String location = GsonUtilities.jstring(artifacts.get(0).getAsJsonObject(), "location");
        report("Uploading bundle");
        ApplicationBundle bundle = instance.uploadBundle(new File(location));
        report("Submitting job");
        Result<Job, JsonObject> submissionResult = bundle.submitJob(jco);
        for (Entry<String, JsonElement> entry : submissionResult.getRawResult().entrySet()) {
            result.add(entry.getKey(), entry.getValue());
        }
        report("Job id:" + submissionResult.getId());
    } finally {
        if (!jboolean(deploy, KEEP_ARTIFACTS)) {
            for (JsonElement e : artifacts) {
                JsonObject artifact = e.getAsJsonObject();
                if (artifact.has("location")) {
                    new File(GsonUtilities.jstring(artifact, "location")).delete();
                }
            }
            if (result.has(SubmissionResultsKeys.BUNDLE_PATH))
                result.remove(SubmissionResultsKeys.BUNDLE_PATH);
        }
    }
}
Also used : JsonObject(com.google.gson.JsonObject) StreamsConnection(com.ibm.streamsx.rest.StreamsConnection) URL(java.net.URL) JsonArray(com.google.gson.JsonArray) GsonUtilities.jboolean(com.ibm.streamsx.topology.internal.gson.GsonUtilities.jboolean) Executor(org.apache.http.client.fluent.Executor) ApplicationBundle(com.ibm.streamsx.rest.ApplicationBundle) JsonElement(com.google.gson.JsonElement) Job(com.ibm.streamsx.rest.Job) File(java.io.File)

Example 5 with StreamsConnection

use of com.ibm.streamsx.rest.StreamsConnection in project streamsx.topology by IBMStreams.

the class StreamsConnectionSample method main.

public static void main(String[] args) throws IOException {
    String userName = args[0];
    String authToken = args[1];
    String url = args[2];
    String instanceName = args[3];
    /*
         * Create the connection to the instance indicated
         */
    StreamsConnection sClient = StreamsConnection.createInstance(userName, authToken, url);
    /*
         * This option is only used to by-pass the certificate certification
         */
    if (args.length == 5 && "true".equals(args[4])) {
        sClient.allowInsecureHosts(true);
    }
    try {
        System.out.println("Instance: ");
        Instance instance = sClient.getInstance(instanceName);
        /*
             * From the Instance, get a list of jobs
             */
        List<Job> jobs = instance.getJobs();
        for (Job job : jobs) {
            System.out.println("Job: " + job.toString());
            /*
                 * For each job, get a list of operators
                 */
            List<Operator> operators = job.getOperators();
            for (Operator op : operators) {
                System.out.println("Operator: " + op.toString());
                List<Metric> metrics = op.getMetrics();
                /*
                     * For each operator, you can get a list of metrics, output
                     * ports and input ports
                     */
                for (Metric m : metrics) {
                    System.out.println("Metric: " + m.toString());
                }
                List<OutputPort> outP = op.getOutputPorts();
                for (OutputPort oport : outP) {
                    System.out.println("Output Port: " + oport.toString());
                    /*
                         * For each output port, you can get a list of metrics
                         */
                    for (Metric om : oport.getMetrics()) {
                        System.out.println("Output Port Metric: " + om.toString());
                    }
                }
                List<InputPort> inP = op.getInputPorts();
                /*
                     * For each input port, get a list of metrics
                     */
                for (InputPort ip : inP) {
                    System.out.println("Input Port: " + ip.toString());
                    for (Metric im : ip.getMetrics()) {
                        System.out.println("Input Port Metric: " + im.toString());
                    }
                }
            }
            /*
                 * For each job, get a list of processing elements
                 */
            for (ProcessingElement pe : job.getPes()) {
                System.out.println("ProcessingElement:" + pe.toString());
            }
        }
        try {
            /*
                 * Get a specific job in the instance
                 */
            instance.getJob("99999");
        } catch (RESTException e) {
            /*
                 * This shows what is available in the RESTException should
                 * something fail
                 */
            System.out.println("Status Code: " + e.getStatusCode());
            System.out.println("Message Id: " + e.getStreamsErrorMessageId());
            System.out.println("MessageAsJson: " + e.getStreamsErrorMessageAsJson().toString());
            System.out.println("Message: " + e.getMessage());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Operator(com.ibm.streamsx.rest.Operator) OutputPort(com.ibm.streamsx.rest.OutputPort) ProcessingElement(com.ibm.streamsx.rest.ProcessingElement) RESTException(com.ibm.streamsx.rest.RESTException) Instance(com.ibm.streamsx.rest.Instance) InputPort(com.ibm.streamsx.rest.InputPort) StreamsConnection(com.ibm.streamsx.rest.StreamsConnection) IOException(java.io.IOException) RESTException(com.ibm.streamsx.rest.RESTException) Metric(com.ibm.streamsx.rest.Metric) Job(com.ibm.streamsx.rest.Job)

Aggregations

StreamsConnection (com.ibm.streamsx.rest.StreamsConnection)7 Instance (com.ibm.streamsx.rest.Instance)5 JsonObject (com.google.gson.JsonObject)3 RESTException (com.ibm.streamsx.rest.RESTException)3 Executor (org.apache.http.client.fluent.Executor)3 JsonParser (com.google.gson.JsonParser)2 Job (com.ibm.streamsx.rest.Job)2 ProcessingElement (com.ibm.streamsx.rest.ProcessingElement)2 ICP4DAuthenticator (com.ibm.streamsx.rest.internal.ICP4DAuthenticator)2 StandaloneAuthenticator (com.ibm.streamsx.rest.internal.StandaloneAuthenticator)2 URL (java.net.URL)2 Test (org.junit.Test)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 ApplicationBundle (com.ibm.streamsx.rest.ApplicationBundle)1 InputPort (com.ibm.streamsx.rest.InputPort)1 Metric (com.ibm.streamsx.rest.Metric)1 Operator (com.ibm.streamsx.rest.Operator)1 OutputPort (com.ibm.streamsx.rest.OutputPort)1 GsonUtilities.jboolean (com.ibm.streamsx.topology.internal.gson.GsonUtilities.jboolean)1