Creates a configuration file.
Creates a configuration file.
Name | Type | Description |
---|---|---|
filename | String | A name for this configuration file. |
callback | Function | A function to call with the new configuration file: |
var configurations = service.configurations();
configurations.create("myprops", function(err, newFile) {
console.log("CREATED");
});
create: function(filename, callback) {
// If someone called us with the default style of (params, callback),
// lets make it work
if (utils.isObject(filename)) {
filename = filename["__conf"];
}
callback = callback || function() {};
var that = this;
var req = this.post("", {__conf: filename}, function(err, response) {
if (err) {
callback(err);
}
else {
var entity = new root.ConfigurationFile(that.service, filename);
entity.fetch(function() {
if (req.wasAborted) {
return; // aborted, so ignore
}
else {
callback.apply(null, arguments);
}
});
}
});
return req;
},
Creates/Updates a configuration file and stanza.
Name | Type | Description |
---|---|---|
filename | String | A name for this configuration file to be created/updated. |
stanzaName | String | A name for the stanza to be created/updated. |
keyValueMap | String | A key-value map of properties to be put under the stanza. |
callback | Function | A function to call with the new configuration file. |
createAsync: async function (filename, stanzaName, keyValueMap, callback) {
callback = callback || function() {};
var that = this;
// 1. Check if the file exists
var configFile = await this.getConfFile(filename);
// 2. If the file doesn't exist, create a new file
if(configFile == undefined) {
that.create( { __conf: filename });
configFile = new root.ConfigurationFile( that.service, filename );
configFile = await configFile.fetchAsync();
}
// 3. Check if the stanza exists
var configStanza = await this.getStanza(configFile, stanzaName);
// 4. If the stanza doesn't exist, create a new stanza with given keyValueMap
if(configStanza == undefined) {
configFile.create(stanzaName, keyValueMap, function (err, newStanza) {
callback();
});
}
// 5. If the stanza exists, update it with the keyValueMap
else {
configStanza.update(keyValueMap, (err, updatedStanza) => {
callback();
});
}
},
});
Indicates whether to call fetch
after an entity has been created. By default, the entity is not fetched because the endpoint returns (echoes) the new entity.
fetchOnEntityCreation: true,
Fetch a configuration file.
Name | Type | Description |
---|---|---|
file | String | A name for configuration file. |
if exists or null
getConfFile: async function(filename) {
var that = this;
// 1. Fetch files list
var response = await this.get("", {__conf: filename}, null, true);
// 2. Filter the files
var files = response
&& response.body
&& response.body.entry
&& response.body.entry.filter(f => f.name === filename);
// 3. Check if the file exists
if(files && files.length == 0) {
return null;
}
// 4. Create a local instance
var configurationFile = new root.ConfigurationFile(that.service, filename);
// 5. Load the file content
var fetchedFile = await configurationFile.fetchAsync();
return fetchedFile;
},
Fetch a configuration stanza.
Name | Type | Description |
---|---|---|
file | String | A configuration file. |
stanza | String | A configuration stanza. |
if exists or null
getStanza: async function(file, stanza) {
// 1. check if the stanza exists
var fetchedStanza = file.item(stanza);
if(fetchedStanza == undefined) {
return null;
}
else {
return fetchedStanza;
}
},
Constructor for splunkjs.Service.Configurations
.
Name | Type | Description |
---|---|---|
service | splunkjs.Service | A |
namespace | Object | Namespace information: |
A new splunkjs.Service.Configurations
instance.
init: function(service, namespace) {
if (!namespace || namespace.owner === "-" || namespace.app === "-") {
throw new Error("Configurations requires a non-wildcard owner/app");
}
this._super(service, this.path(), namespace);
},
Creates a local instance of a configuration file.
Name | Type | Description |
---|---|---|
props | Object | The properties for this configuration file. |
A new splunkjs.Service.ConfigurationFile
instance.
instantiateEntity: function(props) {
return new root.ConfigurationFile(this.service, props.name, this.namespace);
},
Retrieves the REST endpoint path for this resource (with no namespace).
path: function() {
return Paths.properties;
},
Performs a relative DELETE request on an endpoint's path, combined with the parameters and a relative path if specified.
Name | Type | Description |
---|---|---|
relpath | String | A relative path to append to the endpoint path. |
params | Object | A dictionary of entity-specific parameters to add to the query string. |
callback | Function | A function to call when the request is complete: |
// Will make a request to {service.prefix}/search/jobs/123456
var endpoint = new splunkjs.Service.Endpoint(service, "search/jobs/12345");
endpoint.delete("", {}, function() { console.log("DELETED"))});
del: function(relpath, params, callback) {
var url = this.qualifiedPath;
// If we have a relative path, we will append it with a preceding
// slash.
if (relpath) {
url = url + "/" + relpath;
}
return this.service.del(
url,
params,
callback
);
}
});
Refreshes the resource by fetching the object from the server and loading it.
Name | Type | Description |
---|---|---|
options | Object | A dictionary of collection filtering and pagination options: |
callback | Function | A function to call when the object is retrieved: |
fetch: function(options, callback) {
if (!callback && utils.isFunction(options)) {
callback = options;
options = {};
}
callback = callback || function() {};
options = options || {};
if (!options.count) {
options.count = 0;
}
var that = this;
var req = that.get("", options, function(err, response) {
if (err) {
callback(err);
}
else {
that._load(response.data);
callback(null, that);
}
});
return req;
},
It's an asynchronous version of fetch(options, callback) function.
Refreshes the resource by fetching the object from the server and loading it.
Name | Type | Description |
---|---|---|
options | Object | A dictionary of collection filtering and pagination options: |
fetchAsync: async function(options) {
options = options || {};
if (!options.count) {
options.count = 0;
}
var that = this;
var response = await that.get("", options, null, true);
that._load(response.body);
return that;
},
Performs a relative GET request on an endpoint's path, combined with the parameters and a relative path if specified.
Name | Type | Description |
---|---|---|
relpath | String | A relative path to append to the endpoint path. |
params | Object | A dictionary of entity-specific parameters to add to the query string. |
callback | Function | A function to call when the request is complete: |
// Will make a request to {service.prefix}/search/jobs/123456/results?offset=1
var endpoint = new splunkjs.Service.Endpoint(service, "search/jobs/12345");
endpoint.get("results", {offset: 1}, function() { console.log("DONE"))});
get: function(relpath, params, callback, isAsync) {
var url = this.qualifiedPath;
// If we have a relative path, we will append it with a preceding
// slash.
if (relpath) {
url = url + "/" + relpath;
}
return this.service.get(
url,
params,
callback,
isAsync
);
},
Returns a specific entity from the collection.
Name | Type | Description |
---|---|---|
id | String | The name of the entity to retrieve. |
namespace | Object | Namespace information: |
var apps = service.apps();
apps.fetch(function(err, apps) {
var app = apps.item("search");
console.log("Search App Found: " + !!app);
// `app` is an Application object.
});
item: function(id, namespace) {
if (utils.isEmpty(namespace)) {
namespace = null;
}
if (!id) {
throw new Error("Must suply a non-empty name.");
}
if (namespace && (namespace.app === '-' || namespace.owner === '-')) {
throw new Error("When searching for an entity, wildcards are not allowed in the namespace. Please refine your search.");
}
var fullPath = null;
if (this._entitiesByName.hasOwnProperty(id)) {
var entities = this._entitiesByName[id];
if (entities.length === 1 && !namespace) {
// If there is only one entity with the
// specified name and the user did not
// specify a namespace, then we just
// return it
return entities[0];
}
else if (entities.length === 1 && namespace) {
// If we specified a namespace, then we
// only return the entity if it matches
// the full path
fullPath = this.service.fullpath(entities[0].path(), namespace);
if (entities[0].qualifiedPath === fullPath) {
return entities[0];
}
else {
return null;
}
}
else if (entities.length > 1 && !namespace) {
// If there is more than one entity and we didn't
// specify a namespace, then we return an error
// saying the match is ambiguous
throw new Error("Ambiguous match for name '" + id + "'");
}
else {
// There is more than one entity, and we do have
// a namespace, so we try and find it
for(var i = 0; i < entities.length; i++) {
var entity = entities[i];
fullPath = this.service.fullpath(entities[i].path(), namespace);
if (entity.qualifiedPath === fullPath) {
return entity;
}
}
}
}
else {
return null;
}
},
Retrieves the links information for this collection, which is the URI of the resource relative to the management port of a Splunk instance.
The links information.
links: function() {
return this._links;
},
Retrieves a list of all entities in the collection.
Name | Type | Description |
---|---|---|
callback | Function | A function to call with the list of entities: |
var apps = service.apps();
apps.fetch(function(err, apps) {
var appList = apps.list();
console.log(appList.length);
});
list: function(callback) {
callback = callback || function() {};
return utils.clone(this._entities);
}
});
Retrieves the author information for this collection.
The author.
paging: function() {
return this._paging;
},
Performs a relative POST request on an endpoint's path, combined with the parameters and a relative path if specified.
Name | Type | Description |
---|---|---|
relpath | String | A relative path to append to the endpoint path. |
params | Object | A dictionary of entity-specific parameters to add to the body. |
callback | Function | A function to call when the request is complete: |
// Will make a request to {service.prefix}/search/jobs/123456/control
var endpoint = new splunkjs.Service.Endpoint(service, "search/jobs/12345");
endpoint.post("control", {action: "cancel"}, function() { console.log("CANCELLED"))});
post: function(relpath, params, callback) {
var url = this.qualifiedPath;
// If we have a relative path, we will append it with a preceding
// slash.
if (relpath) {
url = url + "/" + relpath;
}
return this.service.post(
url,
params,
callback
);
},
Retrieves the current properties for this resource.
The properties.
properties: function() {
return this._properties;
},
Retrieves the current full state (properties and metadata) of this resource.
The current full state of this resource.
state: function() {
return this._state;
}
});
Retrieves the updated time for this collection.
The updated time.
updated: function() {
return this._updated;
},