Class: Splunk::Pickaxe::Objects

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

Direct Known Subclasses

Alerts, Dashboards, EventTypes, FieldExtractions, Reports, Tags

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, environment, pickaxe_config) ⇒ Objects

Returns a new instance of Objects.



12
13
14
15
16
# File 'lib/splunk/pickaxe/objects.rb', line 12

def initialize(service, environment, pickaxe_config)
  @service = service
  @environment = environment
  @pickaxe_config = pickaxe_config
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



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

def environment
  @environment
end

#pickaxe_configObject (readonly)

Returns the value of attribute pickaxe_config.



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

def pickaxe_config
  @pickaxe_config
end

#serviceObject (readonly)

Returns the value of attribute service.



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

def service
  @service
end

Instance Method Details

#config(file_path) ⇒ Object



64
65
66
# File 'lib/splunk/pickaxe/objects.rb', line 64

def config(file_path)
  YAML.load_file file_path
end

#create(entity) ⇒ Object



68
69
70
71
# File 'lib/splunk/pickaxe/objects.rb', line 68

def create(entity)
  entity_collection = Splunk::Collection.new service, splunk_resource
  entity_collection.create(name(entity), splunk_config(entity))
end

#entity_dirObject



119
120
121
122
# File 'lib/splunk/pickaxe/objects.rb', line 119

def entity_dir
  # Must be implemented by child class
  nil
end

#entity_file_extensionsObject



110
111
112
# File 'lib/splunk/pickaxe/objects.rb', line 110

def entity_file_extensions
  ['.yml', '.yaml']
end

#find(entity) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/splunk/pickaxe/objects.rb', line 77

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

  Splunk::Entity.new service, service.namespace, splunk_resource, name(entity)
rescue Splunk::SplunkHTTPError => e
  if e.code == 404
    nil
  else
    raise e
  end
end

#name(entity) ⇒ Object



102
103
104
# File 'lib/splunk/pickaxe/objects.rb', line 102

def name(entity)
  entity['name']
end

#needs_update?(splunk_entity, entity) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
# File 'lib/splunk/pickaxe/objects.rb', line 89

def needs_update?(splunk_entity, entity)
  splunk_config(entity).each do |k, v|
    return true if splunk_entity[k] != v
  end

  false
end

#skip?(entity) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/splunk/pickaxe/objects.rb', line 97

def skip?(entity)
  return false unless entity.key?('envs')
  !entity['envs'].include?(environment)
end

#splunk_config(entity) ⇒ Object



106
107
108
# File 'lib/splunk/pickaxe/objects.rb', line 106

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

#splunk_resourceObject



114
115
116
117
# File 'lib/splunk/pickaxe/objects.rb', line 114

def splunk_resource
  # Must be implemented by child class
  nil
end

#syncObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/splunk/pickaxe/objects.rb', line 18

def sync
  puts "Syncing all #{entity_dir.capitalize}"

  dir = File.join(pickaxe_config.execution_path, entity_dir)

  unless Dir.exist? dir
    puts "The directory #{dir} does not exist. Not syncing #{entity_dir.capitalize}"
    return
  end

  Dir.entries(dir).each do |entity_file|
    entity_path = File.join(dir, entity_file)

    next unless File.file?(entity_path) && entity_file_extensions.any? { |ext| entity_path.end_with?(ext) }

    entity = config(entity_path)
    entity_name = name(entity)

    puts "- #{entity_name}"

    # Check if we should skip this entity
    if skip? entity
      puts '  Skipping'
      next
    end

    splunk_entity = find entity

    if splunk_entity.nil?
      # Entity does not exist create it
      puts '  Creating ...'
      create entity
      puts '  Created!'
    else
      # Entity exists check if it needs an update
      if needs_update? splunk_entity, entity
        puts '  Updating ...'
        update splunk_entity, entity
        puts '  Updated!'
      else
        puts '  Up to date!'
      end
    end
  end
end

#update(splunk_entity, entity) ⇒ Object



73
74
75
# File 'lib/splunk/pickaxe/objects.rb', line 73

def update(splunk_entity, entity)
  splunk_entity.update(splunk_config(entity))
end