Class: SimpleStack::Hypervisor
- Inherits:
-
Object
- Object
- SimpleStack::Hypervisor
show all
- Includes:
- Cacheable
- Defined in:
- lib/simple_stack/hypervisor.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Cacheable
#cacheable?, #cached_attributes, #reload
Constructor Details
#initialize(connection, type, options) ⇒ Hypervisor
Returns a new instance of Hypervisor.
7
8
9
10
11
12
13
|
# File 'lib/simple_stack/hypervisor.rb', line 7
def initialize(connection, type, options)
self.connection = connection
self.type = type
self.host = options[:host]
self.username = options[:username]
self.password = options[:password]
end
|
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
5
6
7
|
# File 'lib/simple_stack/hypervisor.rb', line 5
def connection
@connection
end
|
#host ⇒ Object
Returns the value of attribute host.
5
6
7
|
# File 'lib/simple_stack/hypervisor.rb', line 5
def host
@host
end
|
#password ⇒ Object
Returns the value of attribute password.
5
6
7
|
# File 'lib/simple_stack/hypervisor.rb', line 5
def password
@password
end
|
#type ⇒ Object
Returns the value of attribute type.
5
6
7
|
# File 'lib/simple_stack/hypervisor.rb', line 5
def type
@type
end
|
#username ⇒ Object
Returns the value of attribute username.
5
6
7
|
# File 'lib/simple_stack/hypervisor.rb', line 5
def username
@username
end
|
Instance Method Details
#delete(url) ⇒ Object
89
90
91
|
# File 'lib/simple_stack/hypervisor.rb', line 89
def delete(url)
http_call { HTTParty.delete(url, http_options) }
end
|
#get(url) ⇒ Object
49
50
51
|
# File 'lib/simple_stack/hypervisor.rb', line 49
def get(url)
http_call { HTTParty.get(url, http_options) }
end
|
#get_stream(url, io) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/simple_stack/hypervisor.rb', line 53
def get_stream(url, io)
uri = URI.parse url
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.path)
.each_pair do |k, v|
request.add_field(k, v)
end
http.request(request) do |res|
res.read_body {|chunk| io.write chunk }
end
end
|
#guests ⇒ Object
45
46
47
|
# File 'lib/simple_stack/hypervisor.rb', line 45
def guests
cached_attributes[:guests] ||= SimpleStack::Collection.new self, self, "#{url}/guests", SimpleStack::Guest
end
|
19
20
21
22
23
24
25
26
|
# File 'lib/simple_stack/hypervisor.rb', line 19
def
{
"x-simplestack-version" => "1.0",
"x-simplestack-token" => connection.token,
"x-simplestack-hypervisor-token" => token,
"Content-Type" => "application/json"
}
end
|
#http_call ⇒ Object
93
94
95
96
97
98
|
# File 'lib/simple_stack/hypervisor.rb', line 93
def http_call
response = yield
return SimpleStack::GracefulObject.new if response.code == 501 && connection.graceful_degradation
raise SimpleStack::Exception.factory(response.parsed_response) if response.code >= 400
response
end
|
#http_options ⇒ Object
28
29
30
31
32
33
34
35
|
# File 'lib/simple_stack/hypervisor.rb', line 28
def http_options
{
:timeout => connection.timeout,
:read_timeout => connection.read_timeout,
:no_follow => true,
:headers =>
}
end
|
#info ⇒ Object
41
42
43
|
# File 'lib/simple_stack/hypervisor.rb', line 41
def info
cached_attributes[:info] ||= self.get url
end
|
#inspect ⇒ Object
100
101
102
|
# File 'lib/simple_stack/hypervisor.rb', line 100
def inspect
"#<#{self.class} url=#{url}>"
end
|
#post(url, body) ⇒ Object
67
68
69
|
# File 'lib/simple_stack/hypervisor.rb', line 67
def post(url, body)
http_call { HTTParty.post(url, http_options.merge(:body => JSON.dump(body))) }
end
|
#put(url, body) ⇒ Object
71
72
73
|
# File 'lib/simple_stack/hypervisor.rb', line 71
def put(url, body)
http_call { HTTParty.put(url, http_options.merge(:body => JSON.dump(body))) }
end
|
#put_stream(url, io) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/simple_stack/hypervisor.rb', line 75
def put_stream(url, io)
uri = URI.parse url
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Put.new(uri.path, {})
.each_pair do |k, v|
request.add_field(k, v)
end
request.body_stream = io
request.content_length = io.size
http.request(request)
end
|
#token ⇒ Object
37
38
39
|
# File 'lib/simple_stack/hypervisor.rb', line 37
def token
Base64.encode64("#{username}:#{password}").split("\n").join("")
end
|
#url ⇒ Object
15
16
17
|
# File 'lib/simple_stack/hypervisor.rb', line 15
def url
"#{connection.url}/#{type}/#{host}"
end
|