Class: Consul::Migrate::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



23
24
25
# File 'lib/consul/migrate/client.rb', line 23

def initialize(options = {})
  @options = CLIENT_DEFAULTS.merge(symbolize_keys(options))
end

Instance Attribute Details

#optionsObject (readonly)

Make options readable to CLI



10
11
12
# File 'lib/consul/migrate/client.rb', line 10

def options
  @options
end

Instance Method Details

#base_urlObject



15
16
17
# File 'lib/consul/migrate/client.rb', line 15

def base_url
  "http://#{bind_client}:#{port}"
end

#bind_clientObject



12
# File 'lib/consul/migrate/client.rb', line 12

def bind_client; @options[:bind_client]; end

#export_acls(dest) ⇒ Object

Export ACLs into a file



55
56
57
58
59
60
61
62
63
# File 'lib/consul/migrate/client.rb', line 55

def export_acls(dest)
  json = get_acl_list

  File.open(dest, 'w') { |file|
    file.write(json)
  }

  return true
end

#get_acl_listObject

GET /v1/acl/list



28
29
30
31
32
33
34
35
36
# File 'lib/consul/migrate/client.rb', line 28

def get_acl_list
  uri = URI("#{base_url}/v1/acl/list")
  uri.query = URI.encode_www_form(http_params)
  response = Net::HTTP.get_response(uri)

  fail(Error, response.body) unless response.kind_of? Net::HTTPSuccess

  response.body
end

#http_paramsObject



19
20
21
# File 'lib/consul/migrate/client.rb', line 19

def http_params
  { :token => @options[:acl_token] }
end

#import_acls(file) ⇒ Object

Import ACLs from a file Returns array of IDs that were inserted into consul’s ACL



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/consul/migrate/client.rb', line 67

def import_acls(file)
  f = File.read(file)
  data_hash = JSON.parse(f)

  result = []
  data_hash.each do |k, v|
    h = JSON.parse(put_acl(k))
    result.push(h)
  end

  return result
end

#portObject



13
# File 'lib/consul/migrate/client.rb', line 13

def port;        @options[:port];        end

#put_acl(acl_hash) ⇒ Object

PUT /v1/acl/create



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/consul/migrate/client.rb', line 39

def put_acl(acl_hash)
  uri = URI("#{base_url}/v1/acl/create")
  uri.query = URI.encode_www_form(http_params)
  req = Net::HTTP::Put.new(uri.request_uri)
  req.body = acl_hash.to_json

  response = Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(req)
  end

  fail(Error, response.body) unless response.kind_of? Net::HTTPSuccess

  response.body
end