Class: Cabal::Client

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

Overview

A client interface for an upstream Cabal::API

Constant Summary collapse

NotFoundError =

The requested upstream resource was not found

Class.new(StandardError)
UnauthorizedError =

The user is not authorized to use a given upstream resource

Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Create a new client instance

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :api_base (String)

    the base URL for the upstream API (required)

  • :access_key (String)

    the access key to use for authenticated API endpoints (optional)

  • :secret_key (String)

    the secret key to use for authenticated API endpoints (optional)



24
25
26
27
28
29
30
31
32
# File 'lib/cabal/client.rb', line 24

def initialize(options = {})
  @api_base = "#{options[:api_base]}/v3"
  @access_key = options[:access_key]
  @secret_key = options[:secret_key]
  @connector = Faraday.new(url: api_base) do |faraday|
    faraday.request  :url_encoded
    faraday.adapter  Faraday.default_adapter
  end
end

Instance Attribute Details

#api_baseString (readonly)

Returns the base URL for the v3 API.

Returns:

  • (String)

    the base URL for the v3 API



14
15
16
# File 'lib/cabal/client.rb', line 14

def api_base
  @api_base
end

Instance Method Details

#clustersArray<String>

Retrieves a list of cluster names from the upstream API

Returns:

  • (Array<String>)

    the names of the known clusters



36
37
38
# File 'lib/cabal/client.rb', line 36

def clusters
  get("clusters").map {|cluster| cluster['name']}
end

#get(endpoint) ⇒ Object

Does an HTTP GET against the upstream API for the provided endpoint

Parameters:

  • endpoint (String)

    the relative API path to GET

Returns:

  • (Object)

    the parsed JSON response

Raises:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cabal/client.rb', line 61

def get(endpoint)
    response = connector.get {|req|
      req.url endpoint
      req.headers['Content-Type'] = 'application/json'
      req.headers['Accept'] = 'application/json'
      req.headers['Authorization'] = authorization
    }
    case response.status
    when 200
      Oj.load(response.body)
    when 404
      raise Cabal::Client::NotFoundError
    when 401
      raise Cabal::Client::UnauthorizedError
    end
end

#private_key(cluster_name) ⇒ String

Retrieves a private SSH key from the upstream API

Parameters:

  • cluster_name (String)

    the name of the cluster

Returns:

  • (String)

    the contents of the cluster’s private key



43
44
45
# File 'lib/cabal/client.rb', line 43

def private_key(cluster_name)
  get("private-key/#{URI.encode(cluster_name)}")['private_ssh_key']
end

#public_key(cluster_name) ⇒ String

Retrieves a public SSH key from teh upstream API

Parameters:

  • cluster_name (String)

    the name of the cluster

Returns:

  • (String)

    the contents of the cluster’s public key



50
51
52
# File 'lib/cabal/client.rb', line 50

def public_key(cluster_name)
  get("key/#{URI.encode(cluster_name)}")['public_ssh_key']
end