Class: Fog::Storage::HP::Directory

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/hp/models/storage/directory.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Directory

Returns a new instance of Directory.



20
21
22
23
24
# File 'lib/fog/hp/models/storage/directory.rb', line 20

def initialize(attributes = {})
    @read_acl  = []
    @write_acl = []
    super
end

Instance Method Details

#can_read?(user) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/fog/hp/models/storage/directory.rb', line 34

def can_read?(user)
  return false if @read_acl.nil?
  list_users_with_read.include?(user)
end

#can_read_write?(user) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/fog/hp/models/storage/directory.rb', line 44

def can_read_write?(user)
  can_read?(user) && can_write?(user)
end

#can_write?(user) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
# File 'lib/fog/hp/models/storage/directory.rb', line 39

def can_write?(user)
  return false if @write_acl.nil?
  list_users_with_write.include?(user)
end

#cdn_enable=(new_cdn_enable) ⇒ Object



172
173
174
175
176
177
178
179
180
# File 'lib/fog/hp/models/storage/directory.rb', line 172

def cdn_enable=(new_cdn_enable)
  @cdn_enable ||= false
  if (!service.cdn.nil? && service.cdn.enabled?)
    @cdn_enable = new_cdn_enable
  else
    # since cdn service is not activated, container cannot be cdn-enabled
    @cdn_enable = false
  end
end

#cdn_enabled?Boolean

Returns:

  • (Boolean)


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/fog/hp/models/storage/directory.rb', line 182

def cdn_enabled?
  if (!service.cdn.nil? && service.cdn.enabled?)
    begin response = service.cdn.head_container(key)
      cdn_header = response.headers.fetch('X-Cdn-Enabled', nil)
      if (!cdn_header.nil? && cdn_header == 'True')
        @cdn_enable = true
      else
        @cdn_enable = false
      end
    # If CDN endpoint is unreachable, a SocketError is raised
    rescue Fog::CDN::HP::NotFound, Excon::Errors::SocketError
      @cdn_enable = false
    end
  else
    @cdn_enable = false
  end
end

#cdn_public_ssl_urlObject



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/fog/hp/models/storage/directory.rb', line 214

def cdn_public_ssl_url
  requires :key
  @cdn_public_ssl_url ||= begin
    # return the CDN public ssl url from the appropriate uri from the header
    begin response = service.cdn.head_container(key)
      if response.headers['X-Cdn-Enabled'] == 'True'
        response.headers.fetch('X-Cdn-Ssl-Uri', nil)
      end
    rescue Fog::CDN::HP::NotFound
      nil
    end
  end
end

#cdn_public_urlObject



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/fog/hp/models/storage/directory.rb', line 200

def cdn_public_url
  requires :key
  @cdn_public_url ||= begin
    # return the CDN public url from the appropriate uri from the header
    begin response = service.cdn.head_container(key)
      if response.headers['X-Cdn-Enabled'] == 'True'
        response.headers.fetch('X-Cdn-Uri', nil)
      end
    rescue Fog::CDN::HP::NotFound
      nil
    end
  end
end

#destroyObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/fog/hp/models/storage/directory.rb', line 117

def destroy
  requires :key
  service.delete_container(key)
  # If CDN service is available, try to delete the container if it was CDN-enabled
  if cdn_enabled?
    begin
      service.cdn.delete_container(key)
    rescue Fog::CDN::HP::NotFound
      # ignore if cdn container not found
    end
  end
  true
rescue Excon::Errors::NotFound, Fog::Storage::HP::NotFound
  false
end

#filesObject



133
134
135
136
137
138
139
140
# File 'lib/fog/hp/models/storage/directory.rb', line 133

def files
  @files ||= begin
    Fog::Storage::HP::Files.new(
      :directory    => self,
      :service   => service
    )
  end
end

#grant(perm, users = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fog/hp/models/storage/directory.rb', line 60

def grant(perm, users=nil)
  # support passing in a list of users in a comma-separated list or as an Array
  if users.is_a?(String)
    user_list = users.split(',')
  else
    user_list = users
  end
  r_acl, w_acl = service.perm_to_acl(perm, user_list)
  unless r_acl.nil? || r_acl.empty?
    @read_acl = [] if @read_acl.nil?
    @read_acl = @read_acl + r_acl
    @read_acl.uniq!
  end
  unless w_acl.nil? || w_acl.empty?
    @write_acl = [] if @write_acl.nil?
    @write_acl = @write_acl + w_acl
    @write_acl.uniq!
  end
  true
end

#list_users_with_readObject



48
49
50
51
52
# File 'lib/fog/hp/models/storage/directory.rb', line 48

def list_users_with_read
  users = []
  users = @read_acl.map  {|acl| acl.split(':')[1]} unless @read_acl.nil?
  return users
end

#list_users_with_writeObject



54
55
56
57
58
# File 'lib/fog/hp/models/storage/directory.rb', line 54

def list_users_with_write
  users = []
  users = @write_acl.map  {|acl| acl.split(':')[1]} unless @write_acl.nil?
  return users
end

#metadataObject



102
103
104
105
106
107
108
109
# File 'lib/fog/hp/models/storage/directory.rb', line 102

def 
  @metadata ||= begin
    Fog::Storage::HP::Metadata.new({
      :service => service,
      :parent => self
    })
  end
end

#metadata=(new_metadata = {}) ⇒ Object



111
112
113
114
115
# File 'lib/fog/hp/models/storage/directory.rb', line 111

def metadata=(={})
  metas = []
  .each_pair {|k,v| metas << {'key' => k, 'value' => v} }
  .load(metas)
end

#public=(new_public) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/fog/hp/models/storage/directory.rb', line 142

def public=(new_public)
  if new_public
    self.grant("pr")
  else
    self.revoke("pr")
  end
  @public = new_public
end

#public?Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
# File 'lib/fog/hp/models/storage/directory.rb', line 151

def public?
  @read_acl = [] if @read_acl.nil?
  if @read_acl.include?(".r:*")
    true
  else
    false
  end
end

#public_urlObject



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/fog/hp/models/storage/directory.rb', line 160

def public_url
  requires :key
  @public_url ||= begin
    begin response = service.head_container(key)
      # escape the key to cover for special char. in container names
      url = service.public_url(key)
    rescue Fog::Storage::HP::NotFound => err
      nil
    end
  end
end

#read_aclObject



26
27
28
# File 'lib/fog/hp/models/storage/directory.rb', line 26

def read_acl
  @read_acl || []
end

#revoke(perm, users = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fog/hp/models/storage/directory.rb', line 81

def revoke(perm, users=nil)
  # support passing in a list of users in a comma-separated list or as an Array
  if users.is_a?(String)
    user_list = users.split(',')
  else
    user_list = users
  end
  r_acl, w_acl = service.perm_to_acl(perm, user_list)
  unless r_acl.nil? || r_acl.empty?
    @read_acl = [] if @read_acl.nil?
    @read_acl = @read_acl - r_acl
    @read_acl.uniq!
  end
  unless w_acl.nil? || w_acl.empty?
    @write_acl = [] if @write_acl.nil?
    @write_acl = @write_acl - w_acl
    @write_acl.uniq!
  end
  true
end

#save(new_options = {}) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/fog/hp/models/storage/directory.rb', line 249

def save(new_options = {})
  requires :key
  options = {}
  #these are default/previous options
  meta_hash = {}
  .each { |meta| meta_hash.store(meta.key, meta.value) }
  if meta_hash
    options.merge!(meta_hash)
  end
  # write out the acls into the headers before save
  options.merge!(service.perm_acl_to_header(@read_acl, @write_acl))
  options.merge!({'X-Container-Sync-To' => self.sync_to}) unless self.sync_to.nil?
  options.merge!({'X-Container-Sync-Key' => self.sync_key}) unless self.sync_key.nil?
  options.merge!({'X-Container-Meta-Web-Index' => self.web_index}) unless self.web_index.nil?
  options.merge!({'X-Container-Meta-Web-Listings' => self.web_listings})
  options.merge!({'X-Container-Meta-Web-Listings-Css' => self.web_listings_css}) unless self.web_listings_css.nil?
  options.merge!({'X-Container-Meta-Web-Error' => self.web_error}) unless self.web_error.nil?
  # get the metadata and merge them in

  # merge user options at the end
  options.merge!(new_options)

  service.put_container(key, options)
  # Added an extra check to see if CDN is enabled for the container
  if (!service.cdn.nil? && service.cdn.enabled?)
    # If CDN available, set the container to be CDN-enabled or not based on if it is marked as cdn_enable.
    if @cdn_enable
      # check to make sure that the container exists. If yes, cdn enable it.
      begin response = service.cdn.head_container(key)
        if response.headers['X-Cdn-Enabled'] == 'False'
          service.cdn.post_container(key, {'X-Cdn-Enabled' => 'True'})
        end
      rescue Fog::CDN::HP::NotFound
        service.cdn.put_container(key)
      rescue Excon::Errors::SocketError
        # means that the CDN endpoint is unreachable
      end
    else
      # check to make sure that the container exists. If yes, cdn disable it.
      begin response = service.cdn.head_container(key)
        ### Deleting a container from CDN is much more expensive than flipping the bit to disable it
        if response.headers['X-Cdn-Enabled'] == 'True'
          service.cdn.post_container(key, {'X-Cdn-Enabled' => 'False'})
        end
      rescue Fog::CDN::HP::NotFound
        # just continue, as container is not cdn-enabled.
      rescue Excon::Errors::SocketError
        # means that the CDN endpoint is unreachable
      end
    end
  end
  true
end

#sync(target_dir, secret) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/fog/hp/models/storage/directory.rb', line 228

def sync(target_dir, secret)
  requires :key
  # do not sync if dir is same as target dir
  return false if target_dir.key == key
  begin service.head_container(key)
    if !target_dir.nil? && target_dir.is_a?(Fog::Storage::HP::Directory) && target_dir.respond_to?(:public_url) && !target_dir.public_url.nil?
      # set sync metadata on source dir
      self.sync_to = target_dir.public_url
      self.sync_key = secret
      # set sync metadata on target dir
      target_dir.sync_key = secret
      target_dir.save
      true
    else
      false
    end
  rescue Fog::Storage::HP::NotFound
    false
  end
end

#write_aclObject



30
31
32
# File 'lib/fog/hp/models/storage/directory.rb', line 30

def write_acl
  @write_acl || []
end