Class: Fog::Compute::VcloudDirector::Medias

Inherits:
Collection show all
Defined in:
lib/fog/vcloud_director/models/compute/medias.rb

Instance Attribute Summary

Attributes inherited from Fog::Collection

#service

Instance Method Summary collapse

Methods inherited from Collection

#all, #get, #get_by_name, #get_everyone, #index

Methods inherited from Fog::Collection

#clear, #destroy, #initialize, #inspect, #load, model, #model, #new, #reload, #table, #to_json

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

This class inherits a constructor from Fog::Collection

Instance Method Details

#create(name, io, image_type = 'iso') ⇒ Media

Parameters:

  • name (String)

    The name of the entity.

  • io (#read)

    The input object to read from.

  • image_type (String) (defaults to: 'iso')

    Media image type. One of: iso, floppy.

Returns:



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
# File 'lib/fog/vcloud_director/models/compute/medias.rb', line 17

def create(name, io, image_type='iso')
  requires :vdc

  response = service.post_upload_media(vdc.id, name, image_type, io.size)
  service.add_id_from_href!(response.body)
  media = new(response.body)

  # Perhaps this would be better implemented as media#upload.

  file = response.body[:Files][:File].first
  file[:Link] = [file[:Link]] if file[:Link].is_a?(Hash)
  link = file[:Link].detect {|l| l[:rel] == 'upload:default'}

  headers = {
    'Content-Length' => io.size,
    'Content-Type' => 'application/octet-stream',
    'x-vcloud-authorization' => service.vcloud_token
  }
  chunker = lambda do
    # to_s will convert the nil received after everything is read to
    # the final empty chunk.
    io.read(Excon.defaults[:chunk_size]).to_s
  end
  Excon.put(
    link[:href],
    :expects => 200,
    :headers => headers,
    :request_block => chunker)

  service.process_task(response.body[:Tasks][:Task])

  media.reload
  media
end