Class: ConfigKit::Client

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

Defined Under Namespace

Classes: ConfigKitCreateError, ConfigKitReadError, ConfigKitTxnError, ConfigKitUpdateError, ConsulConnection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, acl_token) ⇒ Client

Returns a new instance of Client.



127
128
129
130
131
# File 'lib/config_kit/client.rb', line 127

def initialize(url, acl_token)
  @url = url
  @acl_token = acl_token
  @connection = ConsulConnection.new(@url, @acl_token)
end

Instance Attribute Details

#acl_tokenObject (readonly)

Returns the value of attribute acl_token.



9
10
11
# File 'lib/config_kit/client.rb', line 9

def acl_token
  @acl_token
end

#optsObject (readonly)

Returns the value of attribute opts.



9
10
11
# File 'lib/config_kit/client.rb', line 9

def opts
  @opts
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/config_kit/client.rb', line 9

def url
  @url
end

Instance Method Details

#atom_update(key, value) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/config_kit/client.rb', line 144

def atom_update(key,value)
  begin
    retries ||= 0
    modify_idx = @connection.get(key, modify_index: true)
    response = @connection.put!(key, value, cas: modify_idx)
  rescue ConfigKitUpdateError => e
    if (retries += 1) < 3
      #
      # TODO: 
      # 1. Need to log
      # 2. Need to delay and retry refactor
      #
      sleep(0.5)
      retry
    end
  end
end

#create(key, value) ⇒ Object



133
134
135
136
137
# File 'lib/config_kit/client.rb', line 133

def create(key,value)
  response = @connection.put(key, value, cas: 0)
  raise ConfigKitCreateError, "Config Kit create #{key} error" unless response
  response
end

#create_txn(data) ⇒ Object



192
193
194
# File 'lib/config_kit/client.rb', line 192

def create_txn(data)
  @connection.create_txn(data)
end

#delete(key) ⇒ Object

TODO: enhancement



187
188
189
190
# File 'lib/config_kit/client.rb', line 187

def delete(key)
  response = @connection.delete(url, recurse: true)
  response
end

#delete_txn(data) ⇒ Object



204
205
206
# File 'lib/config_kit/client.rb', line 204

def delete_txn(data)
  @connection.delete_txn(data)
end

#init_txnObject



208
209
210
# File 'lib/config_kit/client.rb', line 208

def init_txn
  @connection.init_txn
end

#perform_txnObject



212
213
214
# File 'lib/config_kit/client.rb', line 212

def perform_txn
  @connection.perform_txn
end

#read(key, convert_to_hash = true, recurse = true) ⇒ Object

TODO: enhancement



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/config_kit/client.rb', line 165

def read(key, convert_to_hash=true, recurse=true)
  begin
    ConfigKit.logger.debug "getting key: #{key}"
    response = @connection.get(key, convert_to_hash: convert_to_hash, recurse: recurse)
    response
  rescue Diplomat::KeyNotFound => e
    return nil
  rescue Faraday::ConnectionFailed => e
    raise ConfigKitReadError, "config server #{@url} is not avaliable. #{e.message}"
  rescue Diplomat::UnknownStatus => e
    raise ConfigKitReadError, "Unknown error #{e.message}."
  rescue Exception => e
    ConfigKit.logger.debug e.backtrace.join("\n ")
    return nil
  end

end

#read_txn(data) ⇒ Object



196
197
198
# File 'lib/config_kit/client.rb', line 196

def read_txn(data)
  @connection.read_txn(data)
end

#update(key, value) ⇒ Object



139
140
141
142
# File 'lib/config_kit/client.rb', line 139

def update(key, value)
  response = @connection.put(key, value)
  raise ConfigKitUpdateError, "Config Kit update #{key} error" unless response
end

#update_txn(data) ⇒ Object



200
201
202
# File 'lib/config_kit/client.rb', line 200

def update_txn(data)
  @connection.update_txn(data)
end