Class: Splunk::Pickaxe::Config

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

Constant Summary collapse

SHARING_DEFAULT =
'app'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, environment, execution_path) ⇒ Config

Returns a new instance of Config.



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
64
65
66
67
68
69
70
71
# File 'lib/splunk/pickaxe/config.rb', line 31

def initialize(config, environment, execution_path)
  raise "Environment [#{environment}] is not configured" unless config['environments'].has_key?(environment)

  @execution_path = execution_path
  @environment = environment

  env_config = config['environments'][environment]

  if env_config.is_a?(String)
    # Support this for now to be passive but we will remove it later
    puts "Your .pickaxe.yml is using a deprecated config format. Check https://github.com/cerner/splunk-pickaxe#backwards-compatibility for details"
    @emails = config['emails']
    @url = env_config
    @env_config = { 'url' => @url, 'emails' => @emails }

    # Convert namespace config hash to hash with symbols for keys
    namespace_config = config['namespace'].each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v; }
    @namespace = Splunk.namespace(namespace_config)
  elsif env_config.is_a?(Hash)
    raise "url config is required for environment [#{environment}]" unless env_config.has_key?('url')
    @url = env_config['url']
    @emails = env_config.has_key?('emails') ? env_config['emails'] : config['emails']
    @env_config = env_config

    # If the environment config has namespace use it otherwise fallback to root config
    if env_config.has_key?('namespace')
      raise "Environment config must have a 'namespace / app' config" unless env_config['namespace'].key?('app')
      namespace_config = env_config['namespace']
      namespace_config['sharing'] = SHARING_DEFAULT unless namespace_config.has_key?('sharing')
    else
      raise "Config must have a 'namespace / app' config" unless config['namespace'].key?('app')
      namespace_config = config['namespace']
    end

    # Convert namespace config hash to hash with symbols for keys
    @namespace = Splunk.namespace(namespace_config.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v; })
  else
    raise "Unexepcted value for environment [#{environment}] config. Expected String or Hash, saw #{config['environments'][environment]}"
  end

end

Instance Attribute Details

#emailsObject (readonly)

Returns the value of attribute emails.



29
30
31
# File 'lib/splunk/pickaxe/config.rb', line 29

def emails
  @emails
end

#env_configObject (readonly)

Returns the value of attribute env_config.



29
30
31
# File 'lib/splunk/pickaxe/config.rb', line 29

def env_config
  @env_config
end

#environmentObject (readonly)

Returns the value of attribute environment.



29
30
31
# File 'lib/splunk/pickaxe/config.rb', line 29

def environment
  @environment
end

#execution_pathObject (readonly)

Returns the value of attribute execution_path.



29
30
31
# File 'lib/splunk/pickaxe/config.rb', line 29

def execution_path
  @execution_path
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



29
30
31
# File 'lib/splunk/pickaxe/config.rb', line 29

def namespace
  @namespace
end

#urlObject (readonly)

Returns the value of attribute url.



29
30
31
# File 'lib/splunk/pickaxe/config.rb', line 29

def url
  @url
end

Class Method Details

.load(environment, execution_path) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/splunk/pickaxe/config.rb', line 21

def self.load(environment, execution_path)
  config_path = File.join(execution_path, CONFIG_FILE)
  raise "Unable to load config file [#{config_path}]" unless File.exist? config_path

  # Merges DEFAULTS with yaml config
  Config.new deep_merge(DEFAULTS, YAML.load_file(config_path)), environment, execution_path
end