Class: VSphereCloud::FileProvider

Inherits:
Object
  • Object
show all
Includes:
RetryBlock
Defined in:
lib/cloud/vsphere/file_provider.rb

Instance Method Summary collapse

Methods included from RetryBlock

#retry_block, #retry_with_timeout

Constructor Details

#initialize(rest_client, vcenter_host) ⇒ FileProvider

Returns a new instance of FileProvider.



5
6
7
8
# File 'lib/cloud/vsphere/file_provider.rb', line 5

def initialize(rest_client, vcenter_host)
  @vcenter_host = vcenter_host
  @rest_client = rest_client
end

Instance Method Details

#fetch_file(datacenter_name, datastore_name, path) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cloud/vsphere/file_provider.rb', line 10

def fetch_file(datacenter_name, datastore_name, path)
  retry_block do
    url ="https://#{@vcenter_host}/folder/#{path}?dcPath=#{URI.escape(datacenter_name)}" +
      "&dsName=#{URI.escape(datastore_name)}"

    response = @rest_client.get(url)

    if response.code < 400
      response.body
    elsif response.code == 404
      nil
    else
      raise "Could not fetch file: #{url}, status code: #{response.code}"
    end
  end
end

#upload_file(datacenter_name, datastore_name, path, contents) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cloud/vsphere/file_provider.rb', line 27

def upload_file(datacenter_name, datastore_name, path, contents)
  retry_block do
    url = "https://#{@vcenter_host}/folder/#{path}?dcPath=#{URI.escape(datacenter_name)}" +
      "&dsName=#{URI.escape(datastore_name)}"

    response = @rest_client.put(
      url,
      contents,
      { 'Content-Type' => 'application/octet-stream', 'Content-Length' => contents.length })

    unless response.code < 400
      raise "Could not upload file: #{url}, status code: #{response.code}"
    end
  end
end