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, ca_file) ⇒ Api

Returns a new instance of Api.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dds_client/api.rb', line 15

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

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

Class Method Details

.create_from_envObject



8
9
10
11
12
13
# File 'lib/dds_client/api.rb', line 8

def self.create_from_env
  new(ENV.fetch("DDS_URL"), ENV.fetch("DDS_USERNAME"),
      ENV.fetch("DDS_PASSWORD"), ENV["DDS_CA_FILE"])
rescue KeyError => e
  raise DdsClient::ConfigError, "Set environment vars (#{e})"
end

Instance Method Details

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



29
30
31
32
33
34
35
36
37
38
# File 'lib/dds_client/api.rb', line 29

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

#heartbeatObject



40
41
42
# File 'lib/dds_client/api.rb', line 40

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

#list_containersObject



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

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