Class: TCCE::Consul

Inherits:
Object
  • Object
show all
Defined in:
lib/tcce/consul.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, acl_token, kv_path, ca_file = nil) ⇒ Consul

Returns a new instance of Consul.

Parameters:

  • url (String)

    consul api url

  • acl_token (String)

    consul acl token

  • kv_path (String)

    consul path to object

  • ca_file (String) (defaults to: nil)

    Path to ca file



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tcce/consul.rb', line 13

def initialize(url, acl_token, kv_path, ca_file = nil)
  self.kv_path = kv_path

  Diplomat.configure do |config|
    # Set up a custom Consul URL
    config.url = url

    # Connect into consul with custom access token (ACL)
    config.acl_token = acl_token

    # Set extra Faraday configuration options
    config.options = { ssl: {
      version: :TLSv1_2,
      ca_file: ca_file
    } }
  end
end

Instance Attribute Details

#kv_pathObject

Returns the value of attribute kv_path.



7
8
9
# File 'lib/tcce/consul.rb', line 7

def kv_path
  @kv_path
end

Instance Method Details

#get(inflate = true) ⇒ String

Query the kv_path from consul

Returns:

  • (String)

    object



33
34
35
36
37
38
39
40
41
42
# File 'lib/tcce/consul.rb', line 33

def get(inflate = true)
  # Query object from consul
  consul_object = Diplomat::Kv.get kv_path
  return consul_object unless inflate

  # Inflate with gzip
  object_stream = StringIO.new consul_object
  gzip_stream = Zlib::GzipReader.new object_stream
  gzip_stream.read
end