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, #entity_file_name, #initialize, #name, #save, #save_config, #skip?, #sync

Constructor Details

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

Instance Method Details

#create(entity) ⇒ Object



43
44
45
46
# File 'lib/splunk/pickaxe/objects/tags.rb', line 43

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

#entity_file_path(splunk_entity) ⇒ Object



19
20
21
22
23
24
# File 'lib/splunk/pickaxe/objects/tags.rb', line 19

def entity_file_path(splunk_entity)
  File.join(
    pickaxe_config.execution_path, entity_dir,
    entity_file_name(splunk_entity)
  )
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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/splunk/pickaxe/objects/tags.rb', line 28

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)


66
67
68
69
# File 'lib/splunk/pickaxe/objects/tags.rb', line 66

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



62
63
64
# File 'lib/splunk/pickaxe/objects/tags.rb', line 62

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

#splunk_entity_keysObject



71
72
73
# File 'lib/splunk/pickaxe/objects/tags.rb', line 71

def splunk_entity_keys
  Splunk::Pickaxe::TAGS_KEYS
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



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/splunk/pickaxe/objects/tags.rb', line 48

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