Class: Splunk::Pickaxe::Objects

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, environment, pickaxe_config) ⇒ Objects

Returns a new instance of Objects.



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

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.



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

def environment
  @environment
end

#pickaxe_configObject (readonly)

Returns the value of attribute pickaxe_config.



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

def pickaxe_config
  @pickaxe_config
end

#serviceObject (readonly)

Returns the value of attribute service.



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

def service
  @service
end

Instance Method Details

#config(file_path) ⇒ Object



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

def config(file_path)
  template = File.read(file_path)
  yaml_contents = ERBWithBinding::render_from_hash(template, pickaxe_config.env_config)
  YAML.safe_load(yaml_contents, [], [], true)
end

#create(entity) ⇒ Object



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

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

#entity_dirObject



155
156
157
158
# File 'lib/splunk/pickaxe/objects.rb', line 155

def entity_dir
  # Must be implemented by child class
  nil
end

#entity_file_extensionsObject



142
143
144
# File 'lib/splunk/pickaxe/objects.rb', line 142

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

#entity_file_name(entity) ⇒ Object



138
139
140
# File 'lib/splunk/pickaxe/objects.rb', line 138

def entity_file_name(entity)
  "#{entity.name}.yml".gsub(/[^a-z0-9_\-. ]/i, '')
end

#entity_file_pathObject



165
166
167
168
# File 'lib/splunk/pickaxe/objects.rb', line 165

def entity_file_path
  # Must be implemented by child class
  nil
end

#find(entity) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/splunk/pickaxe/objects.rb', line 80

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

Saved Splunk object’s name



130
131
132
# File 'lib/splunk/pickaxe/objects.rb', line 130

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

#needs_update?(splunk_entity, entity) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  false
end

#remove_pickaxe_config(config) ⇒ Object



146
147
148
# File 'lib/splunk/pickaxe/objects.rb', line 146

def remove_pickaxe_config config
  config.select{|key, value| !key.start_with?('pickaxe') }
end

#save(overwrite, local_save) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/splunk/pickaxe/objects.rb', line 92

def save(overwrite, local_save)
  puts "Saving all #{entity_dir.capitalize}"

  dir = File.join(pickaxe_config.execution_path, entity_dir)
  Dir.mkdir dir unless Dir.exist? dir

  Splunk::Collection.new(service, splunk_resource)
                    .map { |e| save_config e, overwrite, local_save }
end

#save_config(splunk_entity, overwrite, local_save) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/splunk/pickaxe/objects.rb', line 102

def save_config(splunk_entity, overwrite, local_save)
  file_path = entity_file_path splunk_entity

  if local_save
    if File.exist?(file_path)
      puts "- #{splunk_entity.name}"
      write_to_file(file_path, overwrite, splunk_entity)
    end
  else
    puts "- #{splunk_entity.name}"
    write_to_file(file_path, overwrite, splunk_entity)
  end
end

#skip?(entity) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
# File 'lib/splunk/pickaxe/objects.rb', line 124

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

#splunk_config(entity) ⇒ Object



134
135
136
# File 'lib/splunk/pickaxe/objects.rb', line 134

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

#splunk_entity_keysObject



160
161
162
163
# File 'lib/splunk/pickaxe/objects.rb', line 160

def splunk_entity_keys
  # Must be implemented by child class
  nil
end

#splunk_resourceObject



150
151
152
153
# File 'lib/splunk/pickaxe/objects.rb', line 150

def splunk_resource
  # Must be implemented by child class
  nil
end

#syncObject



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
63
# File 'lib/splunk/pickaxe/objects.rb', line 19

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



76
77
78
# File 'lib/splunk/pickaxe/objects.rb', line 76

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

#write_to_file(file_path, overwrite, splunk_entity) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/splunk/pickaxe/objects.rb', line 170

def write_to_file(file_path, overwrite, splunk_entity)
  if overwrite || !File.exist?(file_path)
    overwritten = overwrite && File.exist?(file_path)

    File.write(file_path, {
      'name' => splunk_entity.name,
      'config' => splunk_entity_keys
                    .map { |k| { k => splunk_entity.fetch(k) } }
                    .reduce({}) { |memo, setting| memo.update(setting) }
    }.to_yaml)
    puts overwritten ? '  Overwritten' : '  Created'
  else
    puts '  Already exists'
  end
end