Class: Bonobo::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/bonobo/connection.rb

Overview

this class handles all connection related tasks

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConnection

here we log onto the api



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



5
6
7
# File 'lib/bonobo/connection.rb', line 5

def 
  @account_id
end

#audit_urlObject

Returns the value of attribute audit_url.



5
6
7
# File 'lib/bonobo/connection.rb', line 5

def audit_url
  @audit_url
end

#clientObject

Returns the value of attribute client.



5
6
7
# File 'lib/bonobo/connection.rb', line 5

def client
  @client
end

#endpointObject

Returns the value of attribute endpoint.



5
6
7
# File 'lib/bonobo/connection.rb', line 5

def endpoint
  @endpoint
end

Instance Method Details

#all_instancesObject

wrapper to request all operational instances



59
60
61
62
# File 'lib/bonobo/connection.rb', line 59

def all_instances
  all_instances = instances
  all_instances
end

#api16_call(query) ⇒ Object

wrapper to make any query to the 1.6 api



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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. }
  )

  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 = []) ⇒ Object

get server arrays



88
89
90
91
92
93
94
95
96
97
# 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 = []) ⇒ Object

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



77
78
79
80
81
82
83
84
85
# 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 = '') ⇒ Object

wrapper to request a specific subset of instances



65
66
67
68
69
70
71
72
73
# 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_credsObject

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



8
9
10
11
12
13
14
15
16
17
18
19
# 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