Class: Visor::Image::Client
- Inherits:
-
Object
- Object
- Visor::Image::Client
- Includes:
- Common::Exception, Common::Util
- Defined in:
- lib/image/client.rb
Overview
In the examples presented in this page, we will consider that the VIS server is listening in the 10.0.0.1 address and port 4568. We will also use a sample user account, with access_key “foo” and secret_key “bar”.
The programming API for the VISOR Image System (VIS). This class supports all image metadata and files operations through a programming interface.
After Instantiate a VIS Client object, its possible to directly interact with the VIS server. This API conforms to the tenets of the VIS server REST API Visor Image System server.
Instance Attribute Summary collapse
-
#access_key ⇒ Object
readonly
Returns the value of attribute access_key.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
Instance Method Summary collapse
-
#delete_by_query(query) ⇒ Hash
Removes images that match a specific query.
-
#delete_image(id) ⇒ Hash
Removes an image record based on its _id and returns its metadata.
-
#get_image(id) ⇒ Binary
Retrieves the file of the image with the given id.
-
#get_images(query = {}) ⇒ Array
Retrieves brief metadata of all public and user’s private images.
-
#get_images_detail(query = {}) ⇒ Array
Retrieves detailed metadata of all public and user’s private images.
-
#head_image(id) ⇒ Hash
Retrieves detailed image metadata of the image with the given id.
-
#initialize(opts = {}) ⇒ Visor::Image::Client
constructor
Initializes a new VIS programming client.
-
#post_image(meta, file = nil) ⇒ Hash
Register a new image on VISOR with the given metadata and optionally upload its file, or provide a :location parameter containing the full path to the already existing image file, stored somewhere.
-
#put_image(id, meta, file = nil) ⇒ Hash
Updates an image record with the given metadata and optionally upload its file, or provide a :location parameter containing the full path to the already existing image file, stored somewhere.
Constructor Details
#initialize(opts = {}) ⇒ Visor::Image::Client
Initializes a new VIS programming client. VIS server settings (host and port address) and user’s credentials should be provided for initialization or ignored (where settings will be loaded from the local VISOR configuration file).
42 43 44 45 46 47 48 |
# File 'lib/image/client.rb', line 42 def initialize(opts = {}) configs = Common::Config.load_config :visor_image @host = opts[:host] || configs[:bind_host] || '0.0.0.0' @port = opts[:port] || configs[:bind_port] || 4568 @access_key = opts[:access_key] || configs[:access_key] @secret_key = opts[:secret_key] || configs[:secret_key] end |
Instance Attribute Details
#access_key ⇒ Object (readonly)
Returns the value of attribute access_key.
21 22 23 |
# File 'lib/image/client.rb', line 21 def access_key @access_key end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
21 22 23 |
# File 'lib/image/client.rb', line 21 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
21 22 23 |
# File 'lib/image/client.rb', line 21 def port @port end |
#secret_key ⇒ Object (readonly)
Returns the value of attribute secret_key.
21 22 23 |
# File 'lib/image/client.rb', line 21 def secret_key @secret_key end |
Instance Method Details
#delete_by_query(query) ⇒ Hash
Removes images that match a specific query.
524 525 526 527 528 529 530 531 532 |
# File 'lib/image/client.rb', line 524 def delete_by_query(query) result = [] images = get_images(query) images.each do |image| req = Net::HTTP::Delete.new("/images/#{image[:_id]}") result << do_request(req) end result end |
#delete_image(id) ⇒ Hash
Removes an image record based on its _id and returns its metadata. If the image have some registered image file, that file is also deleted on its source store.
467 468 469 470 |
# File 'lib/image/client.rb', line 467 def delete_image(id) req = Net::HTTP::Delete.new("/images/#{id}") do_request(req) end |
#get_image(id) ⇒ Binary
Retrieves the file of the image with the given id.
The file is yielded in streaming chunks, so its possible to receive a big file without buffering it all in memory.
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/image/client.rb', line 184 def get_image(id) req = Net::HTTP::Get.new("/images/#{id}") prepare_headers(req) Net::HTTP.start(host, port) do |http| http.request(req) do |res| assert_response(res) res.read_body { |chunk| yield chunk } end end end |
#get_images(query = {}) ⇒ Array
Retrieves brief metadata of all public and user’s private images. Options for filtering the returned results can be passed in.
129 130 131 132 133 |
# File 'lib/image/client.rb', line 129 def get_images(query = {}) str = build_query(query) req = Net::HTTP::Get.new("/images#{str}") do_request(req) end |
#get_images_detail(query = {}) ⇒ Array
Filtering and querying works the same as with #get_images. The only difference is the number
Retrieves detailed metadata of all public and user’s private images.
of disclosed attributes.
156 157 158 159 160 |
# File 'lib/image/client.rb', line 156 def get_images_detail(query = {}) str = build_query(query) req = Net::HTTP::Get.new("/images/detail#{str}") do_request(req) end |
#head_image(id) ⇒ Hash
Retrieves detailed image metadata of the image with the given id.
84 85 86 87 88 89 |
# File 'lib/image/client.rb', line 84 def head_image(id) path = "/images/#{id}" req = Net::HTTP::Head.new(path) res = do_request(req, false) (res) end |
#post_image(meta, file = nil) ⇒ Hash
If the :location parameter is passed, you can not pass an image file and the other way around too.
Register a new image on VISOR with the given metadata and optionally upload its file, or provide a :location parameter containing the full path to the already existing image file, stored somewhere.
The image file is streamed to the server in chunks, which in turn also buffers chunks as they arrive, avoiding buffering large files in memory in both clients and server.
292 293 294 295 296 297 298 299 300 301 |
# File 'lib/image/client.rb', line 292 def post_image(, file = nil) req = Net::HTTP::Post.new('/images') (, req) if file req['Content-Type'] = 'application/octet-stream' req['Transfer-Encoding'] = 'chunked' req.body_stream = File.open(File. file) end do_request(req) end |
#put_image(id, meta, file = nil) ⇒ Hash
Only images with status set to ‘locked’ or ‘error’ can be updated with an image data file.
Updates an image record with the given metadata and optionally upload its file, or provide a :location parameter containing the full path to the already existing image file, stored somewhere.
The image file is streamed to the server in chunks, which in turn also buffers chunks as they arrive, avoiding buffering large files in memory in both clients and server.
the image file was already stored in the local filesystem backend, thus it is not needed to upload the file, but rather just register that the image file is there.
# wanted image _id
id = "7583d669-8a65-41f1-b8ae-eb34ff6b322f"
# metadata update
update = {:format => 'iso', :store => 'file', :location => 'file:///Users/server/debian-6.0.4-amd64.iso'}
# update the image metadata with file values
client.put_image(id, update)
# returns:
{
:_id => "7583d669-8a65-41f1-b8ae-eb34ff6b322f",
:uri => "http://10.0.0.1:4568/images/7583d669-8a65-41f1-b8ae-eb34ff6b322f",
:name => "CentOS 6.2",
:architecture => "i386",
:access => "private",
:status => "available",
:format => "iso",
:size => 729808896,
:store => "file",
:location => "file:///home/foo/downloads/CentOS-6.2-i386-LiveCD.iso",
:created_at => "2012-06-15 21:01:21 +0100",
:updated_at => "2012-06-15 21:12:27 +0100",
:checksum => "1b8441b6f4556be61c16d9750da42b3f",
:owner => "foo"
}
419 420 421 422 423 424 425 426 427 428 |
# File 'lib/image/client.rb', line 419 def put_image(id, , file = nil) req = Net::HTTP::Put.new("/images/#{id}") (, req) if if file req['Content-Type'] = 'application/octet-stream' req['Transfer-Encoding'] = 'chunked' req.body_stream = File.open(File. file) end do_request(req) end |