Class: CloudAssault::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
# File 'lib/cloudassault/client.rb', line 8

def initialize(options = {})
  @base_url = options[:base_url] || 'https://www.cloudassault.com/'
  @user_agent = "cloudassault/ruby/#{CloudAssault::VERSION}"
  @default_headers = {'User-Agent' => @user_agent, 'Accept' => 'application/json'}
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



6
7
8
# File 'lib/cloudassault/client.rb', line 6

def base_url
  @base_url
end

#user_idObject (readonly)

Returns the value of attribute user_id.



6
7
8
# File 'lib/cloudassault/client.rb', line 6

def user_id
  @user_id
end

Instance Method Details

#create_simple_load_test(name, desc, url, connections, minutes, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/cloudassault/client.rb', line 68

def create_simple_load_test(name, desc, url, connections, minutes, options = {})
  request = test_hash(name, desc, url, connections, minutes)
  request[:payment] = options[:payment] if (options[:payment])

  url = @base_url + "simple_load_tests"
  headers = build_headers.merge({'Content-Type' => 'application/json'})
  body = request.to_json
  CloudAssault.logger.debug "POST Request: #{url} ;; Headers: #{headers.inspect} ;; Body: #{body}" if CloudAssault.logger
  r = RestClient.post url, body, headers
  JSON.parse(r.body,:symbolize_names => true)
end

#estimate_simple_load_test(name, desc, url, connections, minutes) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/cloudassault/client.rb', line 58

def estimate_simple_load_test(name, desc, url, connections, minutes)
  request = test_hash(name, desc, url, connections, minutes)
 
  url = @base_url + "simple_load_tests/estimate"
  headers = build_headers.merge({'Content-Type' => 'application/json'})
  CloudAssault.logger.debug "POST Request: #{url} ;; Headers: #{headers.inspect}" if CloudAssault.logger
  r = RestClient.post url, request.to_json, headers
  JSON.parse(r.body,:symbolize_names => true)
end

#jobsObject



40
41
42
43
44
45
46
47
# File 'lib/cloudassault/client.rb', line 40

def jobs
  load_root if @user_id.nil?
  
  url =  @base_url + "users/#{@user_id}/jobs"
  CloudAssault.logger.debug "GET Request: #{url} ;; Headers: #{build_headers.inspect}" if CloudAssault.logger
  r = RestClient.get url, build_headers
  JSON.parse(r.body, :symbolize_names => true)
end

#load_rootObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cloudassault/client.rb', line 14

def load_root
  CloudAssault.logger.debug "GET Request: #{@base_url} ;; Headers: #{build_headers.inspect}" if CloudAssault.logger
  r = RestClient.get @base_url, build_headers
  link_arr = r.headers[:link].split(',')
  
  links = link_arr.map {|l| s=l.split(';'); {:link => s[0], :rel => s[1].split('=')[1]}}
  links.each do |l|
    if l[:rel] == 'home'
      index = l[:link].rindex('/')
      @user_id = l[:link].slice(index + 1).to_i
      return
    end
  end
end

#simple_load_test(id) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/cloudassault/client.rb', line 49

def simple_load_test(id)
  load_root if @user_id.nil?
  
  url =  @base_url + "simple_load_tests/#{id}"
  CloudAssault.logger.debug "GET Request: #{url} ;; Headers: #{build_headers.inspect}" if CloudAssault.logger
  r = RestClient.get url, build_headers
  JSON.parse(r.body, :symbolize_names => true)
end

#userObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/cloudassault/client.rb', line 29

def user
  load_root if @user_id.nil?
  
  url = @base_url + "users/#{@user_id}"
  CloudAssault.logger.debug "GET Request: #{url} ;; Headers: #{build_headers.inspect}" if CloudAssault.logger
  r = RestClient.get url, build_headers
  user = JSON.parse(r.body,:symbolize_names => true)
  user[:account_balance] = user[:account_balance].to_f
  user
end