class Bonobo::Connection

this class handles all connection related tasks

Attributes

account_id[RW]
audit_url[RW]
client[RW]
endpoint[RW]

Public Class Methods

new() click to toggle source

here we log onto the api

# File lib/bonobo/connection.rb, line 22
def initialize
  Log.debug 'Logging into Api 1.5 right_api_client'
  creds = load_creds

  @client = RightApi::Client.new(
    email: creds[:user],
    password: creds[:pass],
    account_id: creds[:account],
    api_url: creds[:api_url],
    timeout: 60,
    enable_retry: true
  )

  Log.info 'Connected to account: ' + @account_id
rescue
  puts 'Error: connection couldnt be establishhed'
end

Public Instance Methods

all_instances() click to toggle source

wrapper to request all operational instances

# File lib/bonobo/connection.rb, line 59
def all_instances
  all_instances = instances
  all_instances
end
api16_call(query) click to toggle source

wrapper to make any query to the 1.6 api

# File lib/bonobo/connection.rb, line 41
def api16_call(query)
  url = 'https://' + @endpoint + query
  response = RestClient::Request.execute(
    method: :get,
    url: url,
    cookies: @client.cookies,
    headers: { 'X-Api_Version' => '1.6',
               'X-Account' => @client.account_id }
  )

  result = JSON.parse(response)
  Log.debug result.size.to_s + ' instances matched our query : ' + query
  result
rescue RestClient::ExceptionWithResponse => err
  p err.inspect
end
by_array(arrays = []) click to toggle source

get server arrays

# File lib/bonobo/connection.rb, line 88
def by_array(arrays = [])
  instances = all_instances
  # select only array members
  instances.select! do |i|
    i['links']['incarnator']['kind'] == 'cm#server_array' &&
      arrays.any? { |array| i['links']['incarnator']['name'].include?(array) }
  end

  instances
end
by_tag(tags = []) click to toggle source

this function filters out instances based on an array of tags tags should be an Array.

# File lib/bonobo/connection.rb, line 77
def by_tag(tags = [])
  binding.pry
  t = tags.join('&tag=')
  filter = "tag=#{t}"
  results = instances(filter)

  Log.warn 'Tag query returned no results: ' + tags.join(' ') if results.empty?
  results
end
instances(extra_filters = '') click to toggle source

wrapper to request a specific subset of instances

# File lib/bonobo/connection.rb, line 65
def instances(extra_filters = '')
  filters_list = 'state=operational&' + extra_filters
  filters = CGI.escape(filters_list)

  query = '/api/instances?view=full&filter=' + filters

  instances = api16_call(query)
  instances
end
load_creds() click to toggle source

We simply load up the creds file and set up account and api_url

# File lib/bonobo/connection.rb, line 8
def load_creds
  creds = YAML.load_file("#{ENV['HOME']}/.rest_connection/rest_api_config.yaml")

  # Extract specific details
  creds[:account] = File.basename(creds[:api_url])
  creds[:api_url] = 'https://' + URI.parse(creds[:api_url]).host

  @account_id = creds[:account]
  @endpoint = URI.parse(creds[:api_url]).host

  creds
end