Class: CMDB::ConsulSource

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdb/consul_source.rb

Constant Summary collapse

ARRAY_VALUE =

Regular expression to match array values

/^\[(.*)\]$/

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix) ⇒ ConsulSource

Initialize the configuration for consul source



33
34
35
36
37
38
# File 'lib/cmdb/consul_source.rb', line 33

def initialize(prefix)
  Diplomat.configure do |config|
    config.url = self.class.url
  end
  @prefix = prefix
end

Class Attribute Details

.prefixesObject

Returns the value of attribute prefixes.



24
25
26
# File 'lib/cmdb/consul_source.rb', line 24

def prefixes
  @prefixes
end

.urlObject

Returns the value of attribute url.



16
17
18
# File 'lib/cmdb/consul_source.rb', line 16

def url
  @url
end

Instance Method Details

#each_pair(&_block) ⇒ Object

Not implemented for consul source



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cmdb/consul_source.rb', line 51

def each_pair(&_block)
  prefix = @prefix || ''
  all = Diplomat::Kv.get(prefix, recurse: true)
  all.each do |item|
    dotted_prefix = prefix.split('/').join('.')
    dotted_key = item[:key].split('/').join('.')
    key = dotted_prefix == '' ? dotted_key : dotted_key.split("#{dotted_prefix}.").last
    value = process_value(item[:value])
    yield(key, value)
  end
rescue Diplomat::KeyNotFound
  CMDB.log.warn exc.message
end

#get(key) ⇒ Object

Get a single key from consul



41
42
43
44
45
46
47
48
# File 'lib/cmdb/consul_source.rb', line 41

def get(key)
  value = Diplomat::Kv.get(dot_to_slash(key))
  process_value(value)
rescue TypeError
  puts 'hi'
rescue Diplomat::KeyNotFound
  nil
end