Class: HP::Cloud::SharedResource

Inherits:
RemoteResource show all
Defined in:
lib/hpcloud/shared_resource.rb

Constant Summary

Constants inherited from RemoteResource

RemoteResource::CONTAINER_META, RemoteResource::DEFAULT_STORAGE_MAX_SIZE, RemoteResource::DEFAULT_STORAGE_PAGE_LENGTH, RemoteResource::DEFAULT_STORAGE_SEGMENT_SIZE, RemoteResource::OBJECT_META

Instance Attribute Summary collapse

Attributes inherited from RemoteResource

#etag, #headers, #modified, #size, #synckey, #syncto, #type

Attributes inherited from Resource

#container, #cstatus, #destination, #fname, #ftype, #path, #public, #public_url, #readers, #restart, #writers

Instance Method Summary collapse

Methods inherited from RemoteResource

VALID_CONTAINER_META, VALID_OBJECT_META, #cdn_public_ssl_url, #cdn_public_url, #close, #get_destination, #grant, #has_same_account, #open, #parse_container_headers, #parse_object_headers, #printable_container_headers, #printable_headers, #printable_object_headers, #revoke, #set_destination, #set_metadata, #tempurl, #valid_destination, #valid_metadata_key?, #valid_source, #write

Methods inherited from Resource

#close, #copy, #copy_all, #get_destination, #get_mime_type, #grant, #initialize, #isDirectory, #isFile, #isLocal, #isMulti, #isObject, #isRemote, #is_container?, #is_object_store?, #is_shared?, #is_valid?, #not_implemented, #open, #revoke, #set_destination, #set_error, #set_mime_type, #set_restart, #tempurl, #to_hash, #valid_destination, #valid_source, #write

Constructor Details

This class inherits a constructor from HP::Cloud::Resource

Instance Attribute Details

#countObject

Returns the value of attribute count.



25
26
27
# File 'lib/hpcloud/shared_resource.rb', line 25

def count
  @count
end

Instance Method Details

#container_headObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hpcloud/shared_resource.rb', line 51

def container_head
  begin
    return true unless @size.nil?
    @size = 0
    @directory = @storage.shared_directories.get(@container)
    if @directory.nil?
      @cstatus = CliStatus.new("Cannot find container '#{@container}'.", :not_found)
      return false
    end
    @count = @directory.count.to_i
    @size = @directory.bytes.to_i
  rescue Fog::Storage::HP::NotFound => error
p self
    @cstatus = CliStatus.new("Cannot find container '#{@container}'.", :not_found)
    return false
  rescue Excon::Errors::Forbidden => e
    resp = ErrorResponse.new(e)
    @cstatus  = CliStatus.new(resp.error_string, :permission_denied)
    return false
  rescue Fog::HP::Errors::Forbidden => error
    @cstatus  = CliStatus.new("Permission denied trying to access '#{@fname}'.", :permission_denied)
    return false
  rescue Exception => error
    @cstatus = CliStatus.new("Error reading '#{@fname}': " + error.to_s, :general_error)
    return false
  end
  return true
end

#copy_file(from) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/hpcloud/shared_resource.rb', line 121

def copy_file(from)
  result = true
  if from.isLocal()
    return false unless from.open
    options = { 'Content-Type' => from.get_mime_type() }
    @storage.put_shared_object(@container, @destination, {}, options) {
      from.read().to_s
    }
    result = false unless from.close()
  else
    begin
      @storage.put_shared_object(@container, @destination, nil, {'X-Copy-From' => "/#{from.container}/#{from.path}" })
    rescue Fog::Storage::HP::NotFound => e
      @cstatus = CliStatus.new("The specified object does not exist.", :not_found)
      result = false
    end
  end
  return result
end

#foreach(&block) ⇒ Object

Add the capability to iterate through all the matching files for copy. Use different regular expressions for a directory where we want to recursively copy things vs a regular file



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hpcloud/shared_resource.rb', line 94

def foreach(&block)
  return false unless container_head()
  case @ftype
  when :shared_directory
    regex = "^" + path + ".*"
  else
    regex = "^" + path + '$'
  end
  @directory.files.each { |x|
    name = x.key.to_s
    if ! name.match(regex).nil?
      yield ResourceFactory.create(@storage, @container + '/' + name)
    end
  }
end

#get_sizeObject



84
85
86
87
# File 'lib/hpcloud/shared_resource.rb', line 84

def get_size()
  return 0 unless container_head()
  return @size
end

#headObject



47
48
49
# File 'lib/hpcloud/shared_resource.rb', line 47

def head
  return container_head()
end

#object_headObject



80
81
82
# File 'lib/hpcloud/shared_resource.rb', line 80

def object_head
  return container_head()
end

#parseObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hpcloud/shared_resource.rb', line 27

def parse()
  @container = nil
  @path = nil
  if @fname.empty?
    return
  end
  #
  # Extract container given the expected input in the form:
  # https://domain_and_port/version/tenant_id/container/object/name/with/slashes.txt
  # where the container for that shared object is:
  # https://domain_and_port/version/tenant_id/container
  # and the path for the object is:
  # object/name/with/slashes.txt
  @container = @fname.match(/http[s]*:\/\/[^\/]*\/[^\/]*\/[^\/]*\/[^\/]*/).to_s
  @path = @fname.gsub(@container, '')
  @path = @path.gsub(/^\/*/, '')
  @lname = @fname
  @sname = @path
end

#readObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/hpcloud/shared_resource.rb', line 110

def read
  begin
    @storage.get_shared_object(@fname) { |chunk, one, two|
      yield chunk
    }
  rescue Fog::Storage::HP::NotFound => e
    @cstatus = CliStatus.new("The specified object does not exist.", :not_found)
    result = false
  end
end

#remove(force, at = nil, after = nil) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/hpcloud/shared_resource.rb', line 141

def remove(force, at=nil, after=nil)
  unless at.nil?
    @cstatus = CliStatus.new("The at option is only supported for objects.", :incorrect_usage)
    return false
  end
  unless after.nil?
    @cstatus = CliStatus.new("The after option is only supported for objects.", :incorrect_usage)
    return false
  end

  if @path.empty?
    @cstatus = CliStatus.new("Removal of shared containers is not supported.", :not_supported)
    return false
  end
  begin
    return false unless container_head()
    @storage.delete_shared_object(@container + '/' + @path)
  rescue Fog::Storage::HP::NotFound => error
    @cstatus = CliStatus.new("You don't have an object named '#{@fname}'.", :not_found)
    return false
  rescue Excon::Errors::Forbidden => error
    @cstatus = CliStatus.new("Permission denied for '#{@fname}.", :permission_denied)
    return false
  rescue Exception => e
    @cstatus = CliStatus.new("Exception removing '#{@fname}': " + e.to_s, :general_error)
    return false
  end
  return true
end