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), splunk_config(entity))
end

#entity_dirObject



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

def entity_dir
  # Must be implemented by child class
  nil
end

#entity_file_extensionsObject



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

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

#entity_file_name(entity) ⇒ Object



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

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

#entity_file_pathObject



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

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



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

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

#needs_update?(splunk_entity, entity) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  false
end

#save(overwrite) ⇒ Object



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

def save(overwrite)
  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 }
end

#save_config(splunk_entity, overwrite) ⇒ Object



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

def save_config(splunk_entity, overwrite)
  file_path = entity_file_path splunk_entity

  puts "- #{splunk_entity.name}"
  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

#skip?(entity) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#splunk_config(entity) ⇒ Object



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

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

#splunk_entity_keysObject



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

def splunk_entity_keys
  # Must be implemented by child class
  nil
end

#splunk_resourceObject



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

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(splunk_config(entity))
end