Class: RequestHelper

Inherits:
Object
  • Object
show all
Includes:
CookieJar
Defined in:
lib/request_helper.rb

Instance Method Summary collapse

Methods included from CookieJar

#get_cookies

Constructor Details

#initialize(conf) ⇒ RequestHelper

Returns a new instance of RequestHelper.



9
10
11
12
13
14
# File 'lib/request_helper.rb', line 9

def initialize(conf)
  @server = conf[:server] || "localhost"
  @username = conf[:username]
  @password = conf[:password]
  @cookies = get_cookies
end

Instance Method Details

#create_vm(options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/request_helper.rb', line 16

def create_vm(options)
  options = options.dup
  update_owner_field(options)
  handle_multi_instances(options)
  handle_custom_request(options)
  handle_location_specs(options)

  begin
    response = RestClient.post("http://#{@server}/vm",
                               {:vm_request => options},
                               {:cookies => @cookies, :content_type => 'application/json', :accept => :json})
    result = JSON.parse(response)
    if options[:json]
      puts JSON.pretty_generate(result)
    else
      puts result['message']
    end
  rescue => e
    puts e.inspect
  end
end

#perform_action(options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/request_helper.rb', line 38

def perform_action(options)
  action = options['action']
  hosts = options['hosts'] 
  begin
    response = RestClient.post("http://#{@server}/vm/#{action}",
                               {:id => hosts.first},
                               {:cookies => @cookies, :content_type => 'application/json', :accept => :json})
    result = JSON.parse(response)
    if options[:json]
      puts JSON.pretty_generate(result)
    else
      puts result['message']
    end
  rescue => e
    puts e.inspect
  end
end