Class: ConfigKit::Client::ConsulConnection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/config_kit/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, acl_token) ⇒ ConsulConnection

Returns a new instance of ConsulConnection.



16
17
18
# File 'lib/config_kit/client.rb', line 16

def initialize(url, acl_token)
  setup_consul(url, acl_token)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/config_kit/client.rb', line 12

def config
  @config
end

#connectionObject (readonly)

Returns the value of attribute connection.



12
13
14
# File 'lib/config_kit/client.rb', line 12

def connection
  @connection
end

Instance Method Details

#create_txn(data) ⇒ Object



34
35
36
37
# File 'lib/config_kit/client.rb', line 34

def create_txn(data)
  prepare_txn_data(data,type='create')
  self
end

#delete_txn(data, recurse = false) ⇒ Object



49
50
51
52
# File 'lib/config_kit/client.rb', line 49

def delete_txn(data,recurse=false)
  prepare_txn_data(data,type='delete',recurse)
  self
end

#determine_verb(_type, recurse) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/config_kit/client.rb', line 87

def determine_verb(_type, recurse)
  verb = nil
  type = _type.to_s
  case type
  when 'read'
    if recurse
      verb = 'get-tree'
    else
      verb = 'get'
    end
  when 'update'
    verb = 'set'
  when 'create'
    verb = 'set'
  when 'delete'
    if recurse
      verb = 'delete-tree'
    else
     verb = 'delete'
    end
  else
    verb = nil
  end
  verb
end

#init_txnObject



54
55
56
# File 'lib/config_kit/client.rb', line 54

def init_txn
  reset_txn_data
end

#perform_txnObject



58
59
60
61
62
63
64
65
66
# File 'lib/config_kit/client.rb', line 58

def perform_txn
  begin
    txn(@txn_data)
  rescue => e
    raise ConfigKitTxnError.new "perform txn error:#{e.message}"
  ensure
    reset_txn_data
  end
end

#prepare_txn_data(data, type = 'read', recurse = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/config_kit/client.rb', line 72

def prepare_txn_data(data, type='read', recurse=false)
  init_txn if @txn_data.nil?
  verb = determine_verb(type, recurse)
  data.each_pair do |k,v|
    kv = {
       "Verb" => verb,
        "Key" => k.to_s,
      "Value" => v.to_s
    }

    @txn_data << {"KV" => kv}
  end
  @txn_data
end

#put!(key, value, options = nil) ⇒ Object



28
29
30
31
32
# File 'lib/config_kit/client.rb', line 28

def put!(key, value, options = nil)
  response = put(key, value, options = nil)
  raise ConfigKitUpdateError, "Config Kit Update key:#{key} error" unless response
  response
end

#read_txn(data, recurse = false) ⇒ Object



39
40
41
42
# File 'lib/config_kit/client.rb', line 39

def read_txn(data,recurse=false)
  prepare_txn_data(data,type='read',recurse)
  self
end

#reset_txn_dataObject



68
69
70
# File 'lib/config_kit/client.rb', line 68

def reset_txn_data
  @txn_data = []
end

#setup_consul(url, acl_token) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/config_kit/client.rb', line 20

def setup_consul(url, acl_token)
  @config = Diplomat.configure do |config|
    config.url = url
    config.acl_token = acl_token unless acl_token.nil?
  end
  @connection = Diplomat
end

#update_txn(data) ⇒ Object



44
45
46
47
# File 'lib/config_kit/client.rb', line 44

def update_txn(data)
  prepare_txn_data(data,type='read')
  self
end