Class: DdsClient::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/dds_client/api.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, username, password, verify_ssl) ⇒ Api

Returns a new instance of Api.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dds_client/api.rb', line 20

def initialize(url, username, password, verify_ssl)
  url = url.chomp("/")
  token = authenticate(url, username, password, verify_ssl)

  params = {
    headers: {
      "Content-Type" => "application/json",
      "Authorization" => "Token #{token}"
    },
    ssl_verify_peer: verify_ssl
  }
  @connection = Excon.new(url, params)
end

Class Method Details

.create_from_envObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dds_client/api.rb', line 8

def self.create_from_env
  puts(ENV.fetch("DDS_VERIFY_SSL", "true"))
  new(
    ENV.fetch("DDS_URL"),
    ENV.fetch("DDS_USERNAME"),
    ENV.fetch("DDS_PASSWORD"),
    ENV.fetch("DDS_VERIFY_SSL", "true") != "false"
  )
rescue KeyError => e
  raise DdsClient::ConfigError, "Set environment vars (#{e})"
end

Instance Method Details

#authenticate(url, username, password, verify_ssl) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/dds_client/api.rb', line 34

def authenticate(url, username, password, verify_ssl)
  r = Excon.post(
    "#{url}/auth/get-token",
    headers: { "Content-Type" => "application/json" },
    body: { "username" => username, "password" => password }.to_json,
    ssl_verify_peer: verify_ssl
  )
  raise DdsClient::AuthError, "Invalid credentials" if r.status != 200
  JSON.parse(r.body)["token"]
end

#heartbeatObject



45
46
47
# File 'lib/dds_client/api.rb', line 45

def heartbeat
  JSON.parse(@connection.get(path: "/heartbeat/").body)
end

#list_containersObject



49
50
51
# File 'lib/dds_client/api.rb', line 49

def list_containers
  JSON.parse(@connection.get(path: "/containers/").body)
end