Class: Zona::Client
- Inherits:
-
Object
- Object
- Zona::Client
- Defined in:
- zona.rb
Overview
OZones Client provides functionality to send and retrieve information from the OZones server side. It is used by CLI and API to handle the http requests to the server and basic error control on the responses.
Constant Summary
- OZONES_VERSION =
Provides current version information. Should match server’s oZones version.
"oZones 1.0\nCopyright 2002-2012, OpenNebula Project Leads (OpenNebula.org)\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may\nnot use this file except in compliance with the License. You may obtain\na copy of the License at http://www.apache.org/licenses/LICENSE-2.0\n"
Instance Method Summary (collapse)
-
- (String, Zona::Error) delete_resource(kind, id)
Deletes a resource.
-
- (String, Zone::Error) get_pool(kind)
Retrieves all elements of a kind (pool).
-
- (String, Zona::Error) get_resource(kind, id)
Retrieves a resource.
-
- (String, Zona::Error) get_resource_pool(kind, id, pool)
Retrieves a pool belonging to a specific resource.
-
- (Client) initialize(user = nil, pass = nil, endpoint_str = nil, timeout = nil, debug_flag = true)
constructor
Initialize client instance.
-
- (String, Zona::Error) post_resource(kind, tmpl_json)
Creates a resource.
-
- (String, Zona::Error) post_resource_file(kind, template)
Creates a resource from an opennebula template file.
-
- (String, Zona::Error) post_resource_str(kind, tmpl_str)
Creates a resource from an OpenNebula template string.
-
- (String, Zona::Error) put_resource(kind, id, tmpl_json)
Modifies a resource.
-
- (String, Zona::Error) put_resource_str(kind, id, tmpl_str)
Modifies a resource from an OpenNebula template string.
Constructor Details
- (Client) initialize(user = nil, pass = nil, endpoint_str = nil, timeout = nil, debug_flag = true)
Initialize client instance
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 |
# File 'zona.rb', line 65 def initialize(user=nil, pass=nil, endpoint_str=nil, timeout=nil, debug_flag=true) @debug = debug_flag @timeout = timeout # Server location if endpoint_str @endpoint = endpoint_str elsif ENV["OZONES_URL"] @endpoint = ENV["OZONES_URL"] else @endpoint = "http://localhost:6121" end # Autentication if user && pass @ozonesauth = [user, pass] elsif ENV['OZONES_AUTH'] @ozonesauth=File.read(ENV['OZONES_AUTH']).strip.split(':') end if !@ozonesauth raise "No authorization data present" end if @ozonesauth.size != 2 raise "Authorization data malformed" end end |
Instance Method Details
- (String, Zona::Error) delete_resource(kind, id)
Deletes a resource
215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'zona.rb', line 215 def delete_resource(kind, id) url = URI.parse("#{@endpoint}/#{kind}/#{id}") req = Net::HTTP::Delete.new(url.path) req.basic_auth @ozonesauth[0], @ozonesauth[1] res = Client.http_start(url, @timeout) {|http| http.request(req) } return Client.parse_error(res, kind) end |
- (String, Zone::Error) get_pool(kind)
Retrieves all elements of a kind (pool)
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'zona.rb', line 98 def get_pool(kind) url = URI.parse(@endpoint+"/" + kind) req = Net::HTTP::Get.new(url.path) req.basic_auth @ozonesauth[0], @ozonesauth[1] res = Client.http_start(url, @timeout) {|http| http.request(req) } return Client.parse_error(res, kind) end |
- (String, Zona::Error) get_resource(kind, id)
Retrieves a resource
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'zona.rb', line 181 def get_resource(kind, id) url = URI.parse("#{@endpoint}/#{kind}/#{id}") req = Net::HTTP::Get.new(url.path) req.basic_auth @ozonesauth[0], @ozonesauth[1] res = Client.http_start(url, @timeout) {|http| http.request(req) } return Client.parse_error(res, kind) end |
- (String, Zona::Error) get_resource_pool(kind, id, pool)
Retrieves a pool belonging to a specific resource
199 200 201 202 203 204 205 206 207 208 209 |
# File 'zona.rb', line 199 def get_resource_pool(kind, id, pool) url = URI.parse("#{@endpoint}/#{kind}/#{id}/#{pool}") req = Net::HTTP::Get.new(url.path) req.basic_auth @ozonesauth[0], @ozonesauth[1] res = Client.http_start(url, @timeout) {|http| http.request(req) } return Client.parse_error(res, kind) end |
- (String, Zona::Error) post_resource(kind, tmpl_json)
Creates a resource
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'zona.rb', line 133 def post_resource(kind, tmpl_json) url = URI.parse("#{@endpoint}/#{kind}") req = Net::HTTP::Post.new(url.path) req.body=tmpl_json req.basic_auth @ozonesauth[0], @ozonesauth[1] res = Client.http_start(url, @timeout) do |http| http.request(req) end return Client.parse_error(res, kind) end |
- (String, Zona::Error) post_resource_file(kind, template)
Creates a resource from an opennebula template file
115 116 117 118 |
# File 'zona.rb', line 115 def post_resource_file(kind, template) tmpl_str = File.read(template) post_resource_str(kind, tmpl_str) end |
- (String, Zona::Error) post_resource_str(kind, tmpl_str)
Creates a resource from an OpenNebula template string
124 125 126 127 |
# File 'zona.rb', line 124 def post_resource_str(kind, tmpl_str) tmpl_json = Zona.parse_template(kind, tmpl_str) post_resource(kind, tmpl_json) end |
- (String, Zona::Error) put_resource(kind, id, tmpl_json)
Modifies a resource
161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'zona.rb', line 161 def put_resource(kind, id, tmpl_json) url = URI.parse("#{@endpoint}/#{kind}/#{id}") req = Net::HTTP::Put.new(url.path) req.body=tmpl_json req.basic_auth @ozonesauth[0], @ozonesauth[1] res = Client.http_start(url, @timeout) do |http| http.request(req) end return Client.parse_error(res, kind) end |
- (String, Zona::Error) put_resource_str(kind, id, tmpl_str)
Modifies a resource from an OpenNebula template string
152 153 154 155 |
# File 'zona.rb', line 152 def put_resource_str(kind, id, tmpl_str) tmpl_json = Zona.parse_template(kind, tmpl_str) put_resource(kind, id, tmpl_json) end |