Class: StudioApi::File

Inherits:
ActiveResource::Base
  • Object
show all
Extended by:
StudioResource
Defined in:
lib/studio_api/file.rb

Overview

Represents overlay files which can be loaded to appliance.

Supports finding files for appliance, updating metadata, deleting, uploading and downloading.

StudioApi::File.find :all, :params => { :appliance_id => 1234 }

file = StudioApi::File.find 1234 file.owner = “root” file.path = “/etc” file.filename = “pg.conf” file.save

Examples:

Find files for appliance

Upload file Xorg.conf

File.open ("/tmp/xorg.conf) { |file|
  StudioApi::File.upload file, 1234, :path => "/etc/X11", 
                      :filename => "Xorg.conf", :permissions => "0755",
                      :owner => "root"
}

Update metadata

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StudioResource

collection_path, element_path, extended, studio_connection, studio_connection=

Class Method Details

.upload(content, appliance_id, options = {}) ⇒ StudioApi::File

Uploads file to appliance

Parameters:

  • content (String, File)

    as String or as opened File ( in this case its name is used as default for uploaded file name)

  • appliance_id (#to_i)

    id of appliance where to upload

  • options (Hash<#to_s,#to_s>) (defaults to: {})

    optional parameters, see API documentation

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/studio_api/file.rb', line 53

def self.upload ( content, appliance_id, options = {})
  request_str = "files?appliance_id=#{appliance_id.to_i}"
  options.each do |k,v|
    request_str << "&#{CGI.escape k.to_s}=#{CGI.escape v.to_s}"
  end
  rq = GenericRequest.new studio_connection
  response = rq.post request_str, :file => content
  if defined? ActiveModel #rails 3 and ActiveResource persistency
    File.new Hash.from_xml(response)["file"],true
  else
    File.new Hash.from_xml(response)["file"]
  end
end

Instance Method Details

#contentString

Downloads file to output. Allow downloading to stream or to path.

Returns:

  • (String)

    content of file



31
32
33
34
# File 'lib/studio_api/file.rb', line 31

def content
  rq = GenericRequest.new self.class.studio_connection
  rq.get "/files/#{id.to_i}/data"
end

#overwrite(content) ⇒ StudioApi::File

Overwritte file content and keep metadata ( of course without such things like size ) Immediatelly store new content

Parameters:

  • input (File, #to_s)

    new content for file as String or open file

Returns:



40
41
42
43
44
45
# File 'lib/studio_api/file.rb', line 40

def overwrite ( content )
  request_str = "/files/#{id.to_i}/data"
  rq = GenericRequest.new self.class.studio_connection
  response = rq.put request_str, :file => content
  load Hash.from_xml(response)["file"]
end