Class: HP::Cloud::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/hpcloud/resource.rb

Direct Known Subclasses

LocalResource, ObjectStore, RemoteResource

Constant Summary collapse

@@limit =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage, fname) ⇒ Resource

Returns a new instance of Resource.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hpcloud/resource.rb', line 37

def initialize(storage, fname)
  @cstatus = CliStatus.new
  @storage = storage
  @fname = fname
  @ftype = ResourceFactory.detect_type(@fname)
  @disable_pbar = false
  @mime_type = nil
  @restart = false
  @readacl = []
  @writeacl = []
  parse()
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



30
31
32
# File 'lib/hpcloud/resource.rb', line 30

def container
  @container
end

#cstatusObject (readonly)

Returns the value of attribute cstatus.



32
33
34
# File 'lib/hpcloud/resource.rb', line 32

def cstatus
  @cstatus
end

#destinationObject (readonly)

Returns the value of attribute destination.



32
33
34
# File 'lib/hpcloud/resource.rb', line 32

def destination
  @destination
end

#fnameObject (readonly)

Returns the value of attribute fname.



30
31
32
# File 'lib/hpcloud/resource.rb', line 30

def fname
  @fname
end

#ftypeObject (readonly)

Returns the value of attribute ftype.



30
31
32
# File 'lib/hpcloud/resource.rb', line 30

def ftype
  @ftype
end

#pathObject

Returns the value of attribute path.



29
30
31
# File 'lib/hpcloud/resource.rb', line 29

def path
  @path
end

#publicObject (readonly)

Returns the value of attribute public.



31
32
33
# File 'lib/hpcloud/resource.rb', line 31

def public
  @public
end

#public_urlObject (readonly)

Returns the value of attribute public_url.



31
32
33
# File 'lib/hpcloud/resource.rb', line 31

def public_url
  @public_url
end

#readersObject (readonly)

Returns the value of attribute readers.



31
32
33
# File 'lib/hpcloud/resource.rb', line 31

def readers
  @readers
end

#restartObject (readonly)

Returns the value of attribute restart.



33
34
35
# File 'lib/hpcloud/resource.rb', line 33

def restart
  @restart
end

#writersObject (readonly)

Returns the value of attribute writers.



31
32
33
# File 'lib/hpcloud/resource.rb', line 31

def writers
  @writers
end

Instance Method Details

#closeObject



185
186
187
# File 'lib/hpcloud/resource.rb', line 185

def close()
  return not_implemented("close")
end

#container_headObject



167
168
169
170
# File 'lib/hpcloud/resource.rb', line 167

def container_head()
  @cstatus = CliStatus.new("Not supported on local object '#{@fname}'.", :not_supported)
  return false
end

#copy(from) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/hpcloud/resource.rb', line 193

def copy(from)
    if copy_all(from)
      return true
    end
    set_error(from)
    return false
end

#copy_all(from) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/hpcloud/resource.rb', line 201

def copy_all(from)
  if ! from.valid_source()
    @cstatus = from.cstatus
    return false
  end
  if ! valid_destination(from) then return false end

  copiedfile = false
  original = File.dirname(from.path)
  from.foreach { |file|
    if (original != '.')
      filename = file.path.sub(original, '').sub(/^\//, '')
    else
      filename = file.path
    end
    return false unless set_destination(filename)
    unless copy_file(file)
      from.set_error(file)
      return false
    end
    copiedfile = true
  }

  if (copiedfile == false)
    @cstatus = CliStatus.new("No files found matching source '#{from.path}'", :not_found)
    return false
  end
  return true
end

#copy_file(from) ⇒ Object



189
190
191
# File 'lib/hpcloud/resource.rb', line 189

def copy_file(from)
  return not_implemented("copy_file")
end

#foreach(&block) ⇒ Object



231
232
233
# File 'lib/hpcloud/resource.rb', line 231

def foreach(&block)
  return
end

#get_destinationObject



235
236
237
# File 'lib/hpcloud/resource.rb', line 235

def get_destination()
  return @destination.to_s
end

#get_mime_typeObject



129
130
131
132
133
134
135
136
# File 'lib/hpcloud/resource.rb', line 129

def get_mime_type()
  return @mime_type unless @mime_type.nil?
  filename = ::File.basename(@fname)
  unless (mime_types = ::MIME::Types.of(filename)).empty?
    return mime_types.first.content_type
  end
  return 'application/octet-stream'
end

#grant(acl) ⇒ Object



249
250
251
252
# File 'lib/hpcloud/resource.rb', line 249

def grant(acl)
  @cstatus = CliStatus.new("ACLs of local objects are not supported: #{@fname}", :incorrect_usage)
  return false
end

#headObject



162
163
164
165
# File 'lib/hpcloud/resource.rb', line 162

def head()
  @cstatus = CliStatus.new("Not supported on local object '#{@fname}'.", :not_supported)
  return false
end

#is_container?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/hpcloud/resource.rb', line 76

def is_container?
  return @ftype == :container
end

#is_object_store?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/hpcloud/resource.rb', line 72

def is_object_store?
  return @ftype == :object_store
end

#is_shared?Boolean

Returns:

  • (Boolean)


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

def is_shared?
  return @ftype == :shared_resource || @ftype == :shared_directory
end

#is_valid?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/hpcloud/resource.rb', line 50

def is_valid?
  return @cstatus.is_success?
end

#isDirectoryObject



84
85
86
87
88
89
90
# File 'lib/hpcloud/resource.rb', line 84

def isDirectory()
  return @ftype == :directory ||
         @ftype == :container_directory ||
         @ftype == :shared_directory ||
         @ftype == :container ||
         @ftype == :object_store
end

#isFileObject



92
93
94
# File 'lib/hpcloud/resource.rb', line 92

def isFile()
  return @ftype == :file
end

#isLocalObject



64
65
66
# File 'lib/hpcloud/resource.rb', line 64

def isLocal()
  return ResourceFactory::is_local?(@ftype)
end

#isMultiObject



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/hpcloud/resource.rb', line 142

def isMulti()
  return true if isDirectory()
  found = false 
  foreach { |x|
    if (found == true)
      return true
    end
    found = true
  }
  return false
end

#isObjectObject



96
97
98
# File 'lib/hpcloud/resource.rb', line 96

def isObject()
  return @ftype == :object || @ftype == :shared_resource
end

#isRemoteObject



68
69
70
# File 'lib/hpcloud/resource.rb', line 68

def isRemote()
  return ResourceFactory::is_remote?(@ftype)
end

#not_implemented(value) ⇒ Object



59
60
61
62
# File 'lib/hpcloud/resource.rb', line 59

def not_implemented(value)
  @cstatus = CliStatus.new("Not implemented: #{value}")
  return false
end

#open(output = false, siz = 0) ⇒ Object



172
173
174
# File 'lib/hpcloud/resource.rb', line 172

def open(output=false, siz=0)
  return not_implemented("open")
end

#parseObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hpcloud/resource.rb', line 100

def parse
  @container = nil
  @path = nil
  if @fname.empty?
    return
  end
  if @fname[0,1] == ':'
    @container, *rest = @fname.split('/')
    @container = @container[1..-1] if @container[0,1] == ':'
    @path = rest.empty? ? '' : rest.join('/')
    unless @container.length < 257
      raise Exception.new("Valid container names must be less than 256 characters long")
    end
  else
    rest = @fname.split('/')
    @path = rest.empty? ? '' : rest.join('/')
  end
end

#readObject



176
177
178
179
# File 'lib/hpcloud/resource.rb', line 176

def read()
  not_implemented("read")
  return nil
end

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



239
240
241
242
# File 'lib/hpcloud/resource.rb', line 239

def remove(force, at=nil, after=nil)
  @cstatus = CliStatus.new("Removal of local objects is not supported: #{@fname}", :incorrect_usage)
  return false
end

#revoke(acl) ⇒ Object



254
255
256
257
# File 'lib/hpcloud/resource.rb', line 254

def revoke(acl)
  @cstatus = CliStatus.new("ACLs of local objects are not supported: #{@fname}", :incorrect_usage)
  return false
end

#set_destination(from) ⇒ Object



158
159
160
# File 'lib/hpcloud/resource.rb', line 158

def set_destination(from)
  return true
end

#set_error(from) ⇒ Object



54
55
56
57
# File 'lib/hpcloud/resource.rb', line 54

def set_error(from)
  return unless is_valid?
  @cstatus.set(from.cstatus)
end

#set_mime_type(value) ⇒ Object



125
126
127
# File 'lib/hpcloud/resource.rb', line 125

def set_mime_type(value)
  @mime_type = value.tr("'", "") unless value.nil?
end

#set_restart(value) ⇒ Object



259
260
261
# File 'lib/hpcloud/resource.rb', line 259

def set_restart(value)
  @restart = value
end

#tempurl(period = 172800) ⇒ Object



244
245
246
247
# File 'lib/hpcloud/resource.rb', line 244

def tempurl(period = 172800)
  @cstatus = CliStatus.new("Temporary URLs of local objects is not supported: #{@fname}", :incorrect_usage)
  return nil
end

#to_hashObject



119
120
121
122
123
# File 'lib/hpcloud/resource.rb', line 119

def to_hash
  hash = {}
  instance_variables.each {|var| hash[var.to_s.delete("@")] = instance_variable_get(var) }
  hash
end

#valid_destination(source) ⇒ Object



154
155
156
# File 'lib/hpcloud/resource.rb', line 154

def valid_destination(source)
  return true
end

#valid_sourceObject



138
139
140
# File 'lib/hpcloud/resource.rb', line 138

def valid_source()
  return true
end

#write(data) ⇒ Object



181
182
183
# File 'lib/hpcloud/resource.rb', line 181

def write(data)
  return not_implemented("write")
end