Constructor for splunkjs.Context
Constructor for splunkjs.Context
Name | Type | Description |
---|---|---|
http | splunkjs.Http | An instance of a |
params | Object | Dictionary of optional parameters: |
init: function(http, params) {
if (!(http instanceof Http) && !params) {
// Move over the params
params = http;
http = null;
}
params = params || {};
this.scheme = params.scheme || "https";
this.host = params.host || "localhost";
this.port = params.port || 8089;
this.username = params.username || null;
this.password = params.password || null;
this.owner = params.owner;
this.app = params.app;
this.sessionKey = params.sessionKey || "";
this.authorization = params.authorization || "Splunk";
this.paths = params.paths || Paths;
this.autologin = true;
// Initialize autologin
// The reason we explicitly check to see if 'autologin'
// is actually set is because we need to distinguish the
// case of it being set to 'false', and it not being set.
// Unfortunately, in JavaScript, these are both false-y
if (params.hasOwnProperty("autologin")) {
this.autologin = params.autologin;
}
if (!http) {
// If there is no HTTP implementation set, we check what platform
// we're running on. If we're running in the browser, then we instantiate
// XdmHttp, else, we instantiate NodeHttp.
if (typeof(window) !== 'undefined') {
var XdmHttp = require('./platform/client/easyxdm_http').XdmHttp;
http = new XdmHttp(this.scheme + "://" + this.host + ":" + this.port);
}
else {
var NodeHttp = require('./platform/node/node_http').NodeHttp;
http = new NodeHttp();
}
}
// Store the HTTP implementation
this.http = http;
// Store our full prefix, which is just combining together
// the scheme with the host
this.prefix = this.scheme + "://" + this.host + ":" + this.port + "/services/json/v2";
// We perform the bindings so that every function works
// properly when it is passed as a callback.
this._headers = utils.bind(this, this._headers);
this.fullpath = utils.bind(this, this.fullpath);
this.urlify = utils.bind(this, this.urlify);
this.get = utils.bind(this, this.get);
this.del = utils.bind(this, this.del);
this.post = utils.bind(this, this.post);
this.login = utils.bind(this, this.login);
this._shouldAutoLogin = utils.bind(this, this._shouldAutoLogin);
this._requestWrapper = utils.bind(this, this._requestWrapper);
},
Perform a request
Name | Type | Description |
---|---|---|
path | String | URL to request (with any query parameters already appended and encoded) |
method | String | HTTP method (one of GET | POST | DELETE) |
headers | Object | Object of headers for this request |
body | Object | Body of parameters for this request |
callback | Function | Callback for when the request is complete: |
request: function(path, method, headers, body, callback) {
var that = this;
var request = function(callback) {
return that.http.request(
that.urlify(path),
{
method: method,
headers: that._headers(headers),
body: body,
timeout: 0
},
callback
);
};
return this._requestWrapper(request, callback);
}
});
Perform a POST request
Name | Type | Description |
---|---|---|
path | String | Path to request |
params | Object | Body parameters for this request |
callback | Function | Callback for when the request is complete: |
post: function(path, params, callback) {
var that = this;
var request = function(callback) {
return that.http.post(
that.urlify(path),
that._headers(),
params,
0,
callback
);
};
return this._requestWrapper(request, callback);
},
Perform a DELETE request
Name | Type | Description |
---|---|---|
path | String | Path to request |
params | Object | Query parameters for this request |
callback | Function | Callback for when the request is complete: |
del: function(path, params, callback) {
var that = this;
var request = function(callback) {
return that.http.del(
that.urlify(path),
that._headers(),
params,
0,
callback
);
};
return this._requestWrapper(request, callback);
},
Perform a GET request
Name | Type | Description |
---|---|---|
path | String | Path to request |
params | Object | Query parameters for this request |
callback | Function | Callback for when the request is complete: |
get: function(path, params, callback) {
var that = this;
var request = function(callback) {
return that.http.get(
that.urlify(path),
that._headers(),
params,
0,
callback
);
};
return this._requestWrapper(request, callback);
},
Login to a Splunk instance
Perform authentication to a Splunk instance and store the resulting session key.
Name | Type | Description |
---|---|---|
callback | Function | Callback to be executed when login is complete: |
login: function(callback) {
var that = this;
var url = this.paths.login;
var params = { username: this.username, password: this.password };
callback = callback || function() {};
var wrappedCallback = function(err, response) {
if (err) {
callback(err, false);
}
else {
that.sessionKey = response.data.sessionKey;
callback(null, true);
}
};
return this.http.post(
this.urlify(url),
this._headers(),
params,
0,
wrappedCallback
);
},
Convert partial paths to a fully qualified URL
Convert any partial path into a fully qualified URL.
Name | Type | Description |
---|---|---|
path | String | Partial path |
urlify: function(path) {
return this.prefix + this.fullpath(path);
},
Convert partial paths to fully qualified ones
Convert any partial path into a full path containing the full owner and app prefixes if necessary
Name | Type | Description |
---|---|---|
path | String | Partial path |
fullpath: function(path, namespace) {
namespace = namespace || {};
if (utils.startsWith(path, "/")) {
return path;
}
// If we don't have an app name (explicitly or implicitly), we default to /services/
if (!namespace.app && !this.app && namespace.sharing !== root.Sharing.SYSTEM) {
return "/services/" + path;
}
// Get the app and owner, first from the passed in namespace, then the service,
// finally defaulting to wild cards
var owner = namespace.owner || this.owner || "-";
var app = namespace.app || this.app || "-";
namespace.sharing = (namespace.sharing || "").toLowerCase();
// Modify the owner and app appropriately based on the sharing parameter
if (namespace.sharing === root.Sharing.APP || namespace.sharing === root.Sharing.GLOBAL) {
owner = "nobody";
}
else if (namespace.sharing === root.Sharing.SYSTEM) {
owner = "nobody";
app = "system";
}
return utils.trim("/servicesNS/" + owner + "/" + app + "/" + path);
},
Append Splunk-specific headers
Name | Type | Description |
---|---|---|
headers | Object | Dictionary of headers (optional) |
_headers: function (headers) {
headers = headers || {};
headers["Authorization"] = this.authorization + " " + this.sessionKey;
return headers;
},