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());
}
}
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);
}
}
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);
}
}
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);
}
}
}
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();
}
}
Aggregations