Class: Splunk::Pickaxe::Tags

Inherits:
Objects
  • Object
show all
Defined in:
lib/splunk/pickaxe/objects/tags.rb

Instance Attribute Summary

Attributes inherited from Objects

#environment, #pickaxe_config, #service

Instance Method Summary collapse

Methods inherited from Objects

#config, #entity_file_extensions, #initialize, #name, #skip?, #sync

Constructor Details

This class inherits a constructor from Splunk::Pickaxe::Objects

Instance Method Details

#create(entity) ⇒ Object



36
37
38
39
# File 'lib/splunk/pickaxe/objects/tags.rb', line 36

def create(entity)
  # Create and update are the same thing. Pass in no known fields
  update [], entity
end

#entity_dirObject



15
16
17
# File 'lib/splunk/pickaxe/objects/tags.rb', line 15

def entity_dir
  DIR
end

#find(entity) ⇒ Object

Tags do not follow the typical conventions that other splunk resources do so we have to change the find/create/update methods



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/splunk/pickaxe/objects/tags.rb', line 21

def find(entity)
  # Either return the entity or nil if it doesn't exist

  response = service.request(method: :GET, resource: splunk_resource + [name(entity)])
  # Parse out fields
  atom_feed = Splunk::AtomFeed.new(response.body)
  atom_feed.entries.map { |e| e['title'] }
rescue Splunk::SplunkHTTPError => e
  if e.code == 404
    nil
  else
    raise e
  end
end

#needs_update?(splunk_entity, entity) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/splunk/pickaxe/objects/tags.rb', line 59

def needs_update?(splunk_entity, entity)
  # Compares the fields in our config vs whats in splunk
  splunk_config(entity).uniq.sort != splunk_entity.uniq.sort
end

#splunk_config(entity) ⇒ Object



55
56
57
# File 'lib/splunk/pickaxe/objects/tags.rb', line 55

def splunk_config(entity)
  entity['fields']
end

#splunk_resourceObject



11
12
13
# File 'lib/splunk/pickaxe/objects/tags.rb', line 11

def splunk_resource
  %w[search tags]
end

#update(splunk_entity, entity) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/splunk/pickaxe/objects/tags.rb', line 41

def update(splunk_entity, entity)
  # what we want - whats there = what we need to create/update
  (splunk_config(entity) - splunk_entity).each do |field|
    response = service.request(method: :POST, resource: splunk_resource + [name(entity)], body: { add: field })
    raise "Failed to add field to tag [#{response.code}] - [#{response.body}]" unless response.is_a? Net::HTTPSuccess
  end

  # whats there - what we want = what we need to remove
  (splunk_entity - splunk_config(entity)).each do |field|
    response = service.request(method: :POST, resource: splunk_resource + [name(entity)], body: { delete: field })
    raise "Failed to delete field from tag [#{response.code}] - [#{response.body}]" unless response.is_a? Net::HTTPSuccess
  end
end