api.rb
1.19 KB
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
module Lxd::API
def self.get host, certificate
begin
uri = URI.parse host.uri
rescue
end
con = Net::HTTP.new uri.host, uri.port ? uri.port : 8443
con.use_ssl = true
con.cert = OpenSSL::X509::Certificate.new certificate.cert
con.key = OpenSSL::PKey::RSA.new certificate.key
con.verify_mode = OpenSSL::SSL::VERIFY_NONE
resp = self.call con, Net::HTTP::Get.new('/')
return Lxd::API::V1_0.new con if resp['metadata'].include? '/1.0'
raise "unsupported api version"
end
def self.call con, req
resp = con.request req
raise "request failure: " + resp.code unless resp.code != 200
JSON.parse resp.body
end
def initialize con
@con = con
end
def call req
handle_response(Lxd::API.call @con, req)
end
def get uri
call Net::HTTP::Get.new uri
end
def put uri, data={}
request = Net::HTTP::Put.new uri
request.body = data.to_json
call request
end
def post uri, data={}
request = Net::HTTP::Post.new uri
request.body = data.to_json
call request
end
def delete uri, data={}
request = Net::HTTP::Delete.new uri
request.body = data.to_json
call request
end
end
# vim: set et ts=2 sw=2: