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

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

Instance Method Summary collapse

Methods inherited from Collection

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

Instance Method Details

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



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

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].find {|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