class Cabal::Client

A client interface for an upstream Cabal::API

Constants

NotFoundError

The requested upstream resource was not found

UnauthorizedError

The user is not authorized to use a given upstream resource

Attributes

api_base[R]

@return [String] the base URL for the v3 API

Public Class Methods

new(options = {}) click to toggle source

Create a new client instance @param options [Hash] @option options [String] :api_base the base URL for the upstream API

(required)

@option options [String] :access_key the access key to use for

authenticated API endpoints (optional)

@option options [String] :secret_key the secret key to use for

authenticated API endpoints (optional)
# 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

Public Instance Methods

clusters() click to toggle source

Retrieves a list of cluster names from the upstream API @return [Array<String>] the names of the known clusters

# File lib/cabal/client.rb, line 36
def clusters
  get("clusters").map {|cluster| cluster['name']}
end
get(endpoint) click to toggle source

Does an HTTP GET against the upstream API for the provided endpoint @param endpoint [String] the relative API path to GET @return [Object] the parsed JSON response @raise [Cabal::Client::NotFoundError] if the upstream API returns a

404 status

@raise [Cabal::Client::UnauthorizedError] if the upstream API retirns

a 401 status
# 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) click to toggle source

Retrieves a private SSH key from the upstream API @param cluster_name [String] the name of the cluster @return [String] the contents of the cluster's private key

# 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) click to toggle source

Retrieves a public SSH key from teh upstream API @param cluster_name [String] the name of the cluster @return [String] the contents of the cluster's public key

# File lib/cabal/client.rb, line 50
def public_key(cluster_name)
  get("key/#{URI.encode(cluster_name)}")['public_ssh_key']
end

Private Instance Methods

access_key() click to toggle source
# File lib/cabal/client.rb, line 79
def access_key
  @access_key
end
authorization() click to toggle source
# File lib/cabal/client.rb, line 87
def authorization
  "#{access_key}:#{secret_key}"
end
connector() click to toggle source
# File lib/cabal/client.rb, line 91
def connector
  @connector
end
secret_key() click to toggle source
# File lib/cabal/client.rb, line 83
def secret_key
  @secret_key
end