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, txn_limit = 64) ⇒ ConsulConnection

Returns a new instance of ConsulConnection.



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

def initialize(url, acl_token, txn_limit=64)
  setup_consul(url, acl_token)
  @txn_limit = txn_limit
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



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

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

#delete_txn(data, recurse = false) ⇒ Object



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

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

#determine_verb(_type, recurse) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/config_kit/client.rb', line 100

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



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

def init_txn
  reset_txn_data
end

#perform_txnObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/config_kit/client.rb', line 59

def perform_txn
  begin
    txn_length = @txn_data.length
    current_index = 1
    start_index = 0
    end_index = 0
    while @txn_limit * current_index < txn_length
      start_index = @txn_limit * (current_index - 1)
      end_index = @txn_limit * current_index - 1
      txn(@txn_data[start_index..end_index])
      current_index += 1
    end
    start_index = @txn_limit * (current_index - 1)
    end_index = txn_length - 1
    txn(@txn_data[start_index..end_index])
  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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/config_kit/client.rb', line 85

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



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

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



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

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

#reset_txn_dataObject



81
82
83
# File 'lib/config_kit/client.rb', line 81

def reset_txn_data
  @txn_data = []
end

#setup_consul(url, acl_token) ⇒ Object



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

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



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

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