Search in sources :

Example 1 with ApplicationBundle

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

the class AppBundleTest method testAppBundles.

@Test
public void testAppBundles() throws Exception {
    String namespace = "App.NS_" + (int) (Math.random() * 10000.0);
    String name = "AppBundleTest" + System.currentTimeMillis();
    final String appName = namespace + "::" + name;
    File bundle = createBundle(namespace, name);
    Instance instance = getInstance();
    Result<Job, JsonObject> result = instance.submitJob(bundle, null);
    verifyJob(result, appName, null);
    String jobName = "ABJN_" + (int) (Math.random() * 10000.0) + "_" + System.currentTimeMillis();
    JobConfig jc = new JobConfig();
    jc.setJobName(jobName);
    JsonObject overlay = new JsonObject();
    overlay.add("jobConfig", new Gson().toJsonTree(jc));
    JsonArray a = new JsonArray();
    a.add(overlay);
    JsonObject jco = new JsonObject();
    jco.add("jobConfigOverlays", a);
    result = instance.submitJob(bundle, jco);
    verifyJob(result, appName, jobName);
    final ApplicationBundle appBundle = instance.uploadBundle(bundle);
    assertNotNull(appBundle);
    verifyJob(appBundle.submitJob(null), appName, null);
    jobName = "ABJN2_" + (int) (Math.random() * 10000.0) + "_" + System.currentTimeMillis();
    jc.setJobName(jobName);
    overlay.add("jobConfig", new Gson().toJsonTree(jc));
    verifyJob(appBundle.submitJob(jco), appName, jobName);
}
Also used : JsonArray(com.google.gson.JsonArray) Instance(com.ibm.streamsx.rest.Instance) ApplicationBundle(com.ibm.streamsx.rest.ApplicationBundle) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) Job(com.ibm.streamsx.rest.Job) File(java.io.File) JobConfig(com.ibm.streamsx.topology.jobconfig.JobConfig) Test(org.junit.Test)

Example 2 with ApplicationBundle

use of com.ibm.streamsx.rest.ApplicationBundle 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)

Aggregations

JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 ApplicationBundle (com.ibm.streamsx.rest.ApplicationBundle)2 Job (com.ibm.streamsx.rest.Job)2 File (java.io.File)2 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 Instance (com.ibm.streamsx.rest.Instance)1 StreamsConnection (com.ibm.streamsx.rest.StreamsConnection)1 GsonUtilities.jboolean (com.ibm.streamsx.topology.internal.gson.GsonUtilities.jboolean)1 JobConfig (com.ibm.streamsx.topology.jobconfig.JobConfig)1 URL (java.net.URL)1 Executor (org.apache.http.client.fluent.Executor)1 Test (org.junit.Test)1