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

/^\[(.*)\]$/
@@url =

The url to communicate with consul

nil
@@prefixes =

The prefixes to use when getting keys from consul

nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix) ⇒ ConsulSource

Initialize the configuration for consul source



30
31
32
33
34
35
# File 'lib/cmdb/consul_source.rb', line 30

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

Class Method Details

.prefixesObject



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

def self.prefixes
  @@prefixes
end

.prefixes=(prefixes) ⇒ Object



21
22
23
# File 'lib/cmdb/consul_source.rb', line 21

def self.prefixes=(prefixes)
  @@prefixes = prefixes
end

.urlObject



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

def self.url
  @@url
end

.url=(url) ⇒ Object



13
14
15
# File 'lib/cmdb/consul_source.rb', line 13

def self.url=(url)
  @@url = url
end

Instance Method Details

#each_pair(&block) ⇒ Object

Not implemented for consul source



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cmdb/consul_source.rb', line 46

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])
    puts "Key: #{key}, Value: #{value}"
    block.call(dotted_key.split("#{dotted_prefix}.").last, process_value(item[:value]))
  end
rescue Diplomat::KeyNotFound
end

#get(key) ⇒ Object

Get a single key from consul



38
39
40
41
42
43
# File 'lib/cmdb/consul_source.rb', line 38

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