Class: Visor::Meta::Server

Inherits:
Sinatra::Base
  • Object
show all
Includes:
Common::Config, Common::Exception
Defined in:
lib/meta/server.rb

Overview

The VISoR Meta Server class. This class supports all image metadata manipulation operations through the VISoR REST API implemented along the following routes.

After initialize the Server its possible to directly interact with the meta backend.

Instance Method Summary collapse

Instance Method Details

#delete('/images/: id') ⇒ JSON

Delete an image metadata and returns it.

Parameters:

  • id (String)

    The image _id to delete.

Returns:

  • (JSON)

    The already deleted image detailed metadata.

Raises:

  • (HTTP Error 404)

    If image not found.



270
271
272
273
274
275
276
277
278
279
# File 'lib/meta/server.rb', line 270

delete '/images/:id' do
  begin
    image = DB.delete_image(params[:id])
    {image: image}.to_json
  rescue NotFound => e
    json_error 404, e.message
  rescue => e
    json_error 500, e.message
  end
end

#get('/images') ⇒ JSON

Get brief information about all public images.

{ "images": [{
    "_id":<_id>,
    "uri":<uri>,
    "name":<name>,
    "architecture":<architecture>,
    "type":<type>,
    "format":<format>,
    "store":<type>,
    "size":<size>,
    "created_at":<creation timestamp>
    }, ...]}

The following options can be passed as query parameters, plus any other additional image attribute not defined in the schema.

Parameters:

  • name (String)

    The image name.

  • architecture (String)

    The image architecture.

  • type (String)

    The image type.

  • format (String)

    The image format.

  • store (String)

    The image store.

  • size (Fixnum)

    The image size.

  • created_at (Date)

    The image creation timestamp.

  • sort (String)

    (‘_id’) The image attribute to sort results.

  • dir (String)

    (‘asc’) The sorting order (‘asc’/‘desc’).

Returns:

  • (JSON)

    The public images brief metadata.

Raises:

  • (HTTP Error 404)

    If there is no public images.



101
102
103
104
105
106
107
108
109
110
# File 'lib/meta/server.rb', line 101

get '/images' do
  begin
    images = DB.get_public_images(true, params)
    {images: images}.to_json
  rescue NotFound => e
    json_error 404, e.message
  rescue => e
    json_error 500, e.message
  end
end

#get('/images/detail') ⇒ JSON

Get detailed information about all public images.

{"images": [{
    "_id":<_id>,
    "uri":<uri>,
    "name":<name>,
    "architecture":<architecture>,
    "access":<access>,
    "status":<status>,
    "size":<size>,
    "type":<type>,
    "format":<format>,
    "store":<store>,
    "created_at":<timestamp>
    "updated_at":<timestamp>,
    "kernel":<associated kernel>,
    "ramdisk":<associated ramdisk>,
    ...
    }, ...]}

The following options can be passed as query parameters, plus any other additional image attribute not defined in the schema.

Parameters:

  • name (String)

    The image name.

  • architecture (String)

    The image architecture.

  • access (String)

    The image access permission.

  • type (String)

    The image type.

  • format (String)

    The image format.

  • store (String)

    The image store.

  • size (Fixnum)

    The image size.

  • created_at (Date)

    The image creation timestamp.

  • updated_at (Date)

    The image update timestamp.

  • kernel (String)

    The image associated kernel image’s _id.

  • ramdisk (String)

    The image associated kernel image’s _id.

  • sort (String)

    (_id) The image attribute to sort results.

  • dir (String)

    (‘asc’) The sorting order (‘asc’/‘desc’).

Returns:

  • (JSON)

    The public images detailed metadata.

Raises:

  • (HTTP Error 404)

    If there is no public images.



156
157
158
159
160
161
162
163
164
165
# File 'lib/meta/server.rb', line 156

get '/images/detail' do
  begin
    images = DB.get_public_images(false, params)
    {images: images}.to_json
  rescue NotFound => e
    json_error 404, e.message
  rescue => e
    json_error 500, e.message
  end
end

#get('/images/: id') ⇒ JSON

Get detailed information about a specific image.

{"image": {
    "_id":<_id>,
    "uri":<uri>,
    "name":<name>,
    "architecture":<architecture>,
    "access":<access>,
    "status":<status>,
    "size":<size>,
    "type":<type>,
    "format":<format>,
    "store":<type>,
    "created_at":<creation timestamp>
    "updated_at":<update timestamp>,
    "kernel":<associated kernel>,
    "ramdisk":<associated ramdisk>,
    ...
}}

Parameters:

  • id (String)

    The wanted image _id.

Returns:

  • (JSON)

    The image detailed metadata.

Raises:

  • (HTTP Error 404)

    If image not found.



196
197
198
199
200
201
202
203
204
205
# File 'lib/meta/server.rb', line 196

get '/images/:id' do |id|
  begin
    image = DB.get_image(id)
    {image: image}.to_json
  rescue NotFound => e
    json_error 404, e.message
  rescue => e
    json_error 500, e.message
  end
end

#post('/images') ⇒ JSON

Create a new image metadata and returns it.

Parameters:

  • http-body (JSON)

    The image metadata.

Returns:

  • (JSON)

    The already created image detailed metadata.

Raises:

  • (HTTP Error 400)

    Image metadata validation errors.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/meta/server.rb', line 218

post '/images' do
  begin
    meta  = JSON.parse(request.body.read, @parse_opts)
    vis_address = request.user_agent ? request.user_agent.split(' - ').last : nil
    image = DB.post_image(meta[:image], :vis_address => vis_address)
    {image: image}.to_json
  rescue NotFound => e
    json_error 404, e.message
  rescue ArgumentError => e
    json_error 400, e.message
  rescue => e
    json_error 500, e.message
  end
end

#put('/images/: id') ⇒ JSON

Update an existing image metadata and return it.

Parameters:

  • id (String)

    The wanted image _id.

  • http-body (JSON)

    The image metadata.

Returns:

  • (JSON)

    The already updated image detailed metadata.

Raises:

  • (HTTP Error 400)

    Image metadata update validation errors.



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/meta/server.rb', line 245

put '/images/:id' do |id|
  begin
    meta  = JSON.parse(request.body.read, @parse_opts)
    image = DB.put_image(id, meta[:image])
    {image: image}.to_json
  rescue NotFound => e
    json_error 404, e.message
  rescue ArgumentError => e
    json_error 400, e.message
  rescue => e
    json_error 500, e.message
  end
end