class Cloudcost::ApiConnection

Constants

API_URL

Attributes

connection[RW]

Public Class Methods

new(api_token, options = {}) click to toggle source
# File lib/cloudcost/api_connection.rb, line 12
def initialize(api_token, options = {})
  @api_url = options[:api_url] || API_URL
  @api_token = api_token
  @connection = new_connection
end

Public Instance Methods

get_resource(resource, options = {}) click to toggle source
# File lib/cloudcost/api_connection.rb, line 18
def get_resource(resource, options = {})
  path = "v1/#{resource}"
  path += "?tag:#{options[:tag]}" if options[:tag]
  response = @connection.get(path: path, expects: [200])
  JSON.parse(response.body, symbolize_names: true)
end
get_servers(options = {}) click to toggle source
# File lib/cloudcost/api_connection.rb, line 25
def get_servers(options = {})
  servers = get_resource("servers", options)
  servers = servers.reject { |server| server[:tags].key?(options[:missing_tag].to_sym) } if options[:missing_tag]
  servers = servers.select { |server| /#{options[:name]}/.match? server[:name] } if options[:name]
  servers
end
set_server_tags(uuid, tags) click to toggle source
# File lib/cloudcost/api_connection.rb, line 32
def set_server_tags(uuid, tags)
  @connection.patch(
    path: "v1/servers/#{uuid}",
    body: { tags: tags }.to_json,
    headers: { "Content-Type": "application/json" },
    expects: [204]
  )
end

Private Instance Methods

auth_header() click to toggle source
# File lib/cloudcost/api_connection.rb, line 49
def auth_header
  { "Authorization" => "Bearer #{@api_token}" }
end
new_connection() click to toggle source
# File lib/cloudcost/api_connection.rb, line 43
def new_connection
  Excon.new(
    @api_url, headers: auth_header
  )
end