-
Notifications
You must be signed in to change notification settings - Fork 13
/
appmarket_client.rb
221 lines (173 loc) · 6.91 KB
/
appmarket_client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#------------------------------------------------------------------------------#
# Copyright 2002-2015, OpenNebula Project, OpenNebula Systems #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may #
# not use this file except in compliance with the License. You may obtain #
# a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
#------------------------------------------------------------------------------#
require 'uri'
require 'net/https'
require 'appmarket_version'
module AppMarket
class Client
def initialize(username, password, url, user_agent="Ruby")
@username = username || ENV['APPMARKET_USER']
@password = password || ENV['APPMARKET_PASSWORD']
url = url || ENV['APPMARKET_URL'] || 'http://localhost:6242/'
@uri = URI.parse(url)
@user_agent = "OpenNebula #{AppMarket::VERSION} (#{user_agent})"
@host = nil
@port = nil
if ENV['http_proxy']
uri_proxy = URI.parse(ENV['http_proxy'])
@host = uri_proxy.host
@port = uri_proxy.port
end
end
def create_appliance(body)
post('/appliance', body)
end
def get_appliances
get('/appliance')
end
def get_appliance(appliance_id)
get('/appliance/' + appliance_id)
end
def delete_appliance(appliance_id)
delete('/appliance/' + appliance_id)
end
def update_appliance(appliance_id, body)
put('/appliance/' + appliance_id, body)
end
def callback(url, result, json_body="")
uri = URI.parse(url)
post(uri.path + '/' + result, json_body)
end
def create_user(body)
post('/user', body)
end
def get_users
get('/user')
end
def get_user(user_id)
get('/user/' + user_id)
end
def enable_user(user_id)
post("/user/#{user_id}/enable", "")
end
def update_user(user_id, body)
put('/user/' + user_id, body)
end
def delete_user(user_id)
delete('/user/' + user_id)
end
private
def get(path)
req = Net::HTTP::Proxy(@host, @port)::Get.new(@uri.path + path)
do_request(req)
end
def delete(path)
req = Net::HTTP::Proxy(@host, @port)::Delete.new(@uri.path + path)
do_request(req)
end
def post(path, body)
req = Net::HTTP::Proxy(@host, @port)::Post.new(@uri.path + path)
req.body = body
do_request(req)
end
def put(path, body)
req = Net::HTTP::Proxy(@host, @port)::Put.new(@uri.path + path)
req.body = body
do_request(req)
end
def do_request(req)
if @username && @password
req.basic_auth @username, @password
end
req['User-Agent'] = @user_agent
res = AppMarket::Client::http_start(@uri, @timeout) do |http|
http.request(req)
end
res
end
# #########################################################################
# Starts an http connection and calls the block provided. SSL flag
# is set if needed.
# #########################################################################
def self.http_start(url, timeout, &block)
host = nil
port = nil
if ENV['http_proxy']
uri_proxy = URI.parse(ENV['http_proxy'])
host = uri_proxy.host
port = uri_proxy.port
end
http = Net::HTTP::Proxy(host, port).new(url.host, url.port)
if timeout
http.read_timeout = timeout.to_i
end
if url.scheme=='https'
http.use_ssl = true
http.verify_mode=OpenSSL::SSL::VERIFY_NONE
end
begin
res = http.start do |connection|
block.call(connection)
end
rescue Errno::ECONNREFUSED => e
str = "Error connecting to server (#{e.to_s}).\n"
str << "Server: #{url.host}:#{url.port}"
return AppMarket::Error.new(str,"503")
rescue Errno::ETIMEDOUT => e
str = "Error timeout connecting to server (#{e.to_s}).\n"
str << "Server: #{url.host}:#{url.port}"
return AppMarket::Error.new(str,"504")
rescue Timeout::Error => e
str = "Error timeout while connected to server (#{e.to_s}).\n"
str << "Server: #{url.host}:#{url.port}"
return AppMarket::Error.new(str,"504")
rescue SocketError => e
str = "Error timeout while connected to server (#{e.to_s}).\n"
return AppMarket::Error.new(str,"503")
rescue
return AppMarket::Error.new($!.to_s,"503")
end
if res.is_a?(Net::HTTPSuccess)
res
else
AppMarket::Error.new(res.body, res.code)
end
end
end
# #########################################################################
# The Error Class represents a generic error in the Cloud Client
# library. It contains a readable representation of the error.
# #########################################################################
class Error
attr_reader :message
attr_reader :code
# +message+ a description of the error
def initialize(message=nil, code="500")
@message=message
@code=code
end
def to_s()
@message
end
end
# #########################################################################
# Returns true if the object returned by a method of the OpenNebula
# library is an Error
# #########################################################################
def self.is_error?(value)
value.class==AppMarket::Error
end
end