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
119
120
121
|
# File 'lib/simple_stack/hypervisor.rb', line 119
def delete(url)
http_call { HTTParty.delete(url, http_options) }
end
|
#get(url) ⇒ Object
72
73
74
|
# File 'lib/simple_stack/hypervisor.rb', line 72
def get(url)
http_call { HTTParty.get(url, http_options) }
end
|
#get_stream(url, io) ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/simple_stack/hypervisor.rb', line 76
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
|
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
123
124
125
126
127
128
|
# File 'lib/simple_stack/hypervisor.rb', line 123
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
|
#import(opts = {}) ⇒ Object
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/simple_stack/hypervisor.rb', line 61
def import(opts={})
file = File.open(opts[:from], "rb")
response = post_stream("#{url}/guests", file)
entity_path = response["location"].sub(/^\//, "").sub(/\/$/, "")
entity_url = "#{connection.url}/#{entity_path}"
SimpleStack::Guest.new self, guests.reload, entity_url
ensure
file.close rescue nil
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
130
131
132
|
# File 'lib/simple_stack/hypervisor.rb', line 130
def inspect
"#<#{self.class} url=#{url}>"
end
|
#post(url, body) ⇒ Object
90
91
92
|
# File 'lib/simple_stack/hypervisor.rb', line 90
def post(url, body)
http_call { HTTParty.post(url, http_options.merge(:body => JSON.dump(body))) }
end
|
#post_stream(url, io) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/simple_stack/hypervisor.rb', line 98
def post_stream(url, io)
uri = URI.parse url
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.path, {})
.each_pair do |k, v|
request.add_field(k, v)
end
request.body_stream = io
request.content_length = io.size
response = http.request(request)
if response.code.to_i >= 400
parsed_response = JSON.load(response.body) rescue response.body
raise SimpleStack::Exception.factory(parsed_response)
end
response
end
|
#put(url, body) ⇒ Object
94
95
96
|
# File 'lib/simple_stack/hypervisor.rb', line 94
def put(url, body)
http_call { HTTParty.put(url, http_options.merge(:body => JSON.dump(body))) }
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
|