Skip to content

Commit

Permalink
Job model, ability to find by ID.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcom committed Sep 23, 2012
1 parent bcf081e commit e0dbfd6
Show file tree
Hide file tree
Showing 12 changed files with 297 additions and 30 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -101,3 +101,7 @@ The API client raises an exception when a non-2XX [response codes](http://docs.s
* Token Auth
* Resource creation handling, blocking & polling options, with a timeout.
* Build console into gem (bin/)
* Search all models in addition to getting by ID
* Support search paramters (or, validate support)
* Strat for acknowledge, dispatch, history, etc.
* XML parsing -- detect nodes with children, detect types?
8 changes: 5 additions & 3 deletions lib/splunker/models/finders.rb
Expand Up @@ -2,6 +2,7 @@ module Splunker::Models
module Finders
module ClassMethods
def where(options={})
raise NotImplemented, "Not yet implemented!"
find(:all, options)
end

Expand All @@ -19,12 +20,13 @@ def find(*arguments)
end

def find_all(options)
raise NotImplemented, "Not yet implemented!"
self.client.get @service_path, options
end

def find_by_id(object_id, options)
object_path = assemble_path("#{@service_path}/#{escape_object_id(id)}")
self.client.get object_path
def find_by_id(object_id, options={})
object_path = "#{@service_path}/#{escape_object_id(object_id)}"
self.new(self.client.get(object_path))
end
end

Expand Down
8 changes: 8 additions & 0 deletions lib/splunker/models/search/job.rb
@@ -0,0 +1,8 @@
module Splunker::Models
module Search
class Job < Splunker::Models::Resource
# http://docs.splunk.com/Documentation/Splunk/latest/RESTAPI/RESTsearch#search.2Fjobs.2F.7Bsearch_id.7D
service_path "jobs"
end
end
end
12 changes: 0 additions & 12 deletions lib/splunker/models/search/jobs.rb

This file was deleted.

6 changes: 0 additions & 6 deletions lib/splunker/models/search/results.rb

This file was deleted.

5 changes: 2 additions & 3 deletions lib/splunker/models/search/saved.rb
Expand Up @@ -3,15 +3,14 @@ module Search
class Saved < Splunker::Models::Resource
# http://docs.splunk.com/Documentation/Splunk/4.3.4/RESTAPI/RESTsearch#saved.2Fsearches.2F.7Bname.7D
service_path "saved/searches"
# service_path
# service_path/id

# Dispatch these?
=begin Can these actions be standardized?
def acknowledge; end
def dispatch; end
def history; end
def scheduled_times; end
def suppress; end
=end
end
end
end
14 changes: 9 additions & 5 deletions lib/splunker/models/xml_processor.rb
Expand Up @@ -44,12 +44,16 @@ def nested_hash(name_array, text, hash)
end

def top_node(xml_doc)
begin
if xml_doc.xpath("/xmlns:feed/xmlns:entry/*").size > 0
return xml_doc.xpath("/xmlns:feed/xmlns:entry")
# Handle the various types of XML structure
["/xmlns:feed/xmlns:entry",
"/xmlns:entry"].each do |path|
begin
if xml_doc.xpath("#{path}/*").size > 0
return xml_doc.xpath(path)
end
rescue Nokogiri::XML::XPath::SyntaxError => e
# Ignore...
end
rescue Nokogiri::XML::XPath::SyntaxError => e
# Ignore...
end

xml_doc
Expand Down
211 changes: 211 additions & 0 deletions spec/fixtures/job_mysearch.xml
@@ -0,0 +1,211 @@
<entry
xmlns="http://www.w3.org/2005/Atom"
xmlns:s="http://dev.splunk.com/ns/rest"
xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
<title>search index</title>
<id>https://localhost:8089/services/search/jobs/mysearch</id>
<updated>2011-07-07T20:49:58.000-07:00</updated>
<link href="/services/search/jobs/mysearch" rel="alternate"/>
<published>2011-07-07T20:49:57.000-07:00</published>
<link href="/services/search/jobs/mysearch/search.log" rel="search.log"/>
<link href="/services/search/jobs/mysearch/events" rel="events"/>
<link href="/services/search/jobs/mysearch/results" rel="results"/>
<link href="/services/search/jobs/mysearch/results_preview" rel="results_preview"/>
<link href="/services/search/jobs/mysearch/timeline" rel="timeline"/>
<link href="/services/search/jobs/mysearch/summary" rel="summary"/>
<link href="/services/search/jobs/mysearch/control" rel="control"/>
<author>
<name>admin</name>
</author>
<content type="text/xml">
<s:dict>
<s:key name="cursorTime">1969-12-31T16:00:00.000-08:00</s:key>
<s:key name="delegate"></s:key>
<s:key name="diskUsage">2174976</s:key>
<s:key name="dispatchState">DONE</s:key>
<s:key name="doneProgress">1.00000</s:key>
<s:key name="dropCount">0</s:key>
<s:key name="earliestTime">2011-07-07T11:18:08.000-07:00</s:key>
<s:key name="eventAvailableCount">287</s:key>
<s:key name="eventCount">287</s:key>
<s:key name="eventFieldCount">6</s:key>
<s:key name="eventIsStreaming">1</s:key>
<s:key name="eventIsTruncated">0</s:key>
<s:key name="eventSearch">search index</s:key>
<s:key name="eventSorting">desc</s:key>
<s:key name="isDone">1</s:key>
<s:key name="isFailed">0</s:key>
<s:key name="isFinalized">0</s:key>
<s:key name="isPaused">0</s:key>
<s:key name="isPreviewEnabled">0</s:key>
<s:key name="isRealTimeSearch">0</s:key>
<s:key name="isRemoteTimeline">0</s:key>
<s:key name="isSaved">0</s:key>
<s:key name="isSavedSearch">0</s:key>
<s:key name="isZombie">0</s:key>
<s:key name="keywords">index</s:key>
<s:key name="label"></s:key>
<s:key name="latestTime">1969-12-31T16:00:00.000-08:00</s:key>
<s:key name="numPreviews">0</s:key>
<s:key name="priority">5</s:key>
<s:key name="remoteSearch">litsearch index | fields keepcolorder=t "host" "index" "linecount" "source" "sourcetype" "splunk_server"</s:key>
<s:key name="reportSearch"></s:key>
<s:key name="resultCount">287</s:key>
<s:key name="resultIsStreaming">1</s:key>
<s:key name="resultPreviewCount">287</s:key>
<s:key name="runDuration">1.004000</s:key>
<s:key name="scanCount">287</s:key>
<s:key name="sid">mysearch</s:key>
<s:key name="statusBuckets">0</s:key>
<s:key name="ttl">516</s:key>
<s:key name="performance">
<s:dict>
<s:key name="command.fields">
<s:dict>
<s:key name="duration_secs">0.004</s:key>
<s:key name="invocations">4</s:key>
<s:key name="input_count">287</s:key>
<s:key name="output_count">287</s:key>
</s:dict>
</s:key>
<s:key name="command.search">
<s:dict>
<s:key name="duration_secs">0.089</s:key>
<s:key name="invocations">4</s:key>
<s:key name="input_count">0</s:key>
<s:key name="output_count">287</s:key>
</s:dict>
</s:key>
<s:key name="command.search.fieldalias">
<s:dict>
<s:key name="duration_secs">0.002</s:key>
<s:key name="invocations">2</s:key>
<s:key name="input_count">287</s:key>
<s:key name="output_count">287</s:key>
</s:dict>
</s:key>
<s:key name="command.search.index">
<s:dict>
<s:key name="duration_secs">0.005</s:key>
<s:key name="invocations">4</s:key>
</s:dict>
</s:key>
<s:key name="command.search.kv">
<s:dict>
<s:key name="duration_secs">0.002</s:key>
<s:key name="invocations">2</s:key>
</s:dict>
</s:key>
<s:key name="command.search.lookups">
<s:dict>
<s:key name="duration_secs">0.002</s:key>
<s:key name="invocations">2</s:key>
<s:key name="input_count">287</s:key>
<s:key name="output_count">287</s:key>
</s:dict>
</s:key>
<s:key name="command.search.rawdata">
<s:dict>
<s:key name="duration_secs">0.083</s:key>
<s:key name="invocations">2</s:key>
</s:dict>
</s:key>
<s:key name="command.search.tags">
<s:dict>
<s:key name="duration_secs">0.004</s:key>
<s:key name="invocations">4</s:key>
<s:key name="input_count">287</s:key>
<s:key name="output_count">287</s:key>
</s:dict>
</s:key>
<s:key name="command.search.typer">
<s:dict>
<s:key name="duration_secs">0.004</s:key>
<s:key name="invocations">4</s:key>
<s:key name="input_count">287</s:key>
<s:key name="output_count">287</s:key>
</s:dict>
</s:key>
<s:key name="dispatch.createProviderQueue">
<s:dict>
<s:key name="duration_secs">0.059</s:key>
<s:key name="invocations">1</s:key>
</s:dict>
</s:key>
<s:key name="dispatch.evaluate">
<s:dict>
<s:key name="duration_secs">0.037</s:key>
<s:key name="invocations">1</s:key>
</s:dict>
</s:key>
<s:key name="dispatch.evaluate.search">
<s:dict>
<s:key name="duration_secs">0.036</s:key>
<s:key name="invocations">1</s:key>
</s:dict>
</s:key>
<s:key name="dispatch.fetch">
<s:dict>
<s:key name="duration_secs">0.092</s:key>
<s:key name="invocations">5</s:key>
</s:dict>
</s:key>
<s:key name="dispatch.readEventsInResults">
<s:dict>
<s:key name="duration_secs">0.110</s:key>
<s:key name="invocations">1</s:key>
</s:dict>
</s:key>
<s:key name="dispatch.stream.local">
<s:dict>
<s:key name="duration_secs">0.089</s:key>
<s:key name="invocations">4</s:key>
</s:dict>
</s:key>
<s:key name="dispatch.timeline">
<s:dict>
<s:key name="duration_secs">0.359</s:key>
<s:key name="invocations">5</s:key>
</s:dict>
</s:key>
</s:dict>
</s:key>
<s:key name="messages">
<s:dict/>
</s:key>
<s:key name="request">
<s:dict>
<s:key name="id">mysearch</s:key>
<s:key name="search">search index</s:key>
</s:dict>
</s:key>
<s:key name="eai:acl">
<s:dict>
<s:key name="perms">
<s:dict>
<s:key name="read">
<s:list>
<s:item>admin</s:item>
</s:list>
</s:key>
<s:key name="write">
<s:list>
<s:item>admin</s:item>
</s:list>
</s:key>
</s:dict>
</s:key>
<s:key name="owner">admin</s:key>
<s:key name="modifiable">true</s:key>
<s:key name="sharing">global</s:key>
<s:key name="app">search</s:key>
<s:key name="can_write">true</s:key>
</s:dict>
</s:key>
<s:key name="searchProviders">
<s:list>
<s:item>vgenovese-mbp15.splunk.com</s:item>
</s:list>
</s:key>
</s:dict>
</content>
29 changes: 29 additions & 0 deletions spec/unit/models/finders_spec.rb
@@ -0,0 +1,29 @@
require 'spec_helper'

describe Splunker::Models::Finders do
let(:resource) do
Splunker::Models::Search::Saved
end

let(:search_fixture) do
Nokogiri::XML(fixture("saved_search.xml"))
end

before(:each) do
object_path = "saved/searches/MyFinderTestSavedSearch"
resource.client.stub(:get).with(object_path).and_return(search_fixture)
end

it "should find_by_id" do
a = resource.find_by_id("MyFinderTestSavedSearch")
a.should be_a(Splunker::Models::Search::Saved)
a.title.should eq("MySavedSearch")
end

it "should find by id by default" do
a = resource.find("MyFinderTestSavedSearch")
a.should be_a(Splunker::Models::Search::Saved)
a.title.should eq("MySavedSearch")
end
end

1 change: 0 additions & 1 deletion spec/unit/models/resource_spec.rb
@@ -1,7 +1,6 @@
require 'spec_helper'

describe Splunker::Models::Resource do
include Splunker::Models

let(:model) do
xml = Nokogiri::XML(fixture("saved_search.xml"))
Expand Down
25 changes: 25 additions & 0 deletions spec/unit/models/search/job_spec.rb
@@ -0,0 +1,25 @@
require 'spec_helper'

describe Splunker::Models::Search::Job do
let(:resource) do
Splunker::Models::Search::Job
end

let(:job_fixture) do
Nokogiri::XML(fixture("job_mysearch.xml"))
end

# Covered in finders_spec, but I'm feeling redundant this morning.
context "searching" do
before(:each) do
object_path = "jobs/MyFinderTestJob"
resource.client.stub(:get).with(object_path).and_return(job_fixture)
end

it "should find by id by default" do
a = resource.find("MyFinderTestJob")
a.should be_a(Splunker::Models::Search::Job)
a.title.should eq("search index")
end
end
end
4 changes: 4 additions & 0 deletions spec/unit/models/search/saved_spec.rb
@@ -0,0 +1,4 @@
require 'spec_helper'

describe Splunker::Models::Search::Saved do
end

0 comments on commit e0dbfd6

Please sign in to comment.