2011-06-16 00:40:51 -04:00
|
|
|
require 'fog/core/model'
|
2011-10-17 15:25:07 -04:00
|
|
|
require 'fog/hp/models/storage/files'
|
2011-06-16 00:40:51 -04:00
|
|
|
|
|
|
|
module Fog
|
2011-10-17 15:25:07 -04:00
|
|
|
module Storage
|
|
|
|
class HP
|
2011-06-16 00:40:51 -04:00
|
|
|
|
|
|
|
class Directory < Fog::Model
|
|
|
|
|
2013-03-08 11:45:25 -05:00
|
|
|
identity :key, :aliases => 'name'
|
2011-06-16 00:40:51 -04:00
|
|
|
|
2013-03-08 11:45:25 -05:00
|
|
|
attribute :bytes, :aliases => 'X-Container-Bytes-Used'
|
|
|
|
attribute :count, :aliases => 'X-Container-Object-Count'
|
|
|
|
attribute :sync_to, :aliases => 'X-Container-Sync-To'
|
|
|
|
attribute :sync_key, :aliases => 'X-Container-Sync-Key'
|
2011-06-16 00:40:51 -04:00
|
|
|
|
2012-10-16 21:48:50 -04:00
|
|
|
def initialize(attributes = {})
|
|
|
|
@read_acl = []
|
|
|
|
@write_acl = []
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_acl
|
2012-11-08 16:41:18 -05:00
|
|
|
@read_acl || []
|
2012-10-16 21:48:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def write_acl
|
2012-11-08 16:41:18 -05:00
|
|
|
@write_acl || []
|
2012-10-16 21:48:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_read?(user)
|
|
|
|
return false if @read_acl.nil?
|
|
|
|
list_users_with_read.include?(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_write?(user)
|
|
|
|
return false if @write_acl.nil?
|
|
|
|
list_users_with_write.include?(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_read_write?(user)
|
|
|
|
can_read?(user) && can_write?(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_users_with_read
|
|
|
|
users = []
|
|
|
|
users = @read_acl.map {|acl| acl.split(':')[1]} unless @read_acl.nil?
|
|
|
|
return users
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_users_with_write
|
|
|
|
users = []
|
|
|
|
users = @write_acl.map {|acl| acl.split(':')[1]} unless @write_acl.nil?
|
|
|
|
return users
|
|
|
|
end
|
|
|
|
|
2012-10-29 17:01:21 -04:00
|
|
|
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
|
2013-01-18 15:06:41 -05:00
|
|
|
r_acl, w_acl = service.perm_to_acl(perm, user_list)
|
2012-10-29 17:01:21 -04:00
|
|
|
unless r_acl.nil? || r_acl.empty?
|
2012-11-08 16:41:18 -05:00
|
|
|
@read_acl = [] if @read_acl.nil?
|
2012-10-16 21:48:50 -04:00
|
|
|
@read_acl = @read_acl + r_acl
|
|
|
|
@read_acl.uniq!
|
|
|
|
end
|
2012-10-29 17:01:21 -04:00
|
|
|
unless w_acl.nil? || w_acl.empty?
|
2012-11-08 16:41:18 -05:00
|
|
|
@write_acl = [] if @write_acl.nil?
|
2012-10-16 21:48:50 -04:00
|
|
|
@write_acl = @write_acl + w_acl
|
|
|
|
@write_acl.uniq!
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2012-10-29 17:01:21 -04:00
|
|
|
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
|
2013-01-18 15:06:41 -05:00
|
|
|
r_acl, w_acl = service.perm_to_acl(perm, user_list)
|
2012-11-08 16:41:18 -05:00
|
|
|
unless r_acl.nil? || r_acl.empty?
|
|
|
|
@read_acl = [] if @read_acl.nil?
|
2012-10-16 21:48:50 -04:00
|
|
|
@read_acl = @read_acl - r_acl
|
|
|
|
@read_acl.uniq!
|
2011-08-01 15:05:55 -04:00
|
|
|
end
|
2012-11-08 16:41:18 -05:00
|
|
|
unless w_acl.nil? || w_acl.empty?
|
|
|
|
@write_acl = [] if @write_acl.nil?
|
2012-10-16 21:48:50 -04:00
|
|
|
@write_acl = @write_acl - w_acl
|
|
|
|
@write_acl.uniq!
|
2011-07-13 19:27:58 -04:00
|
|
|
end
|
2012-10-16 21:48:50 -04:00
|
|
|
true
|
2011-07-13 19:27:58 -04:00
|
|
|
end
|
|
|
|
|
2011-06-16 00:40:51 -04:00
|
|
|
def destroy
|
|
|
|
requires :key
|
2012-12-22 18:26:21 -05:00
|
|
|
service.delete_container(key)
|
2011-11-11 16:14:28 -05:00
|
|
|
# If CDN service is available, try to delete the container if it was CDN-enabled
|
2012-01-20 21:12:02 -05:00
|
|
|
if cdn_enabled?
|
2012-02-15 00:40:23 -05:00
|
|
|
begin
|
2012-12-22 18:26:21 -05:00
|
|
|
service.cdn.delete_container(key)
|
2012-02-15 00:40:23 -05:00
|
|
|
rescue Fog::CDN::HP::NotFound
|
|
|
|
# ignore if cdn container not found
|
|
|
|
end
|
2011-06-16 00:40:51 -04:00
|
|
|
end
|
|
|
|
true
|
2011-10-24 13:35:11 -04:00
|
|
|
rescue Excon::Errors::NotFound, Fog::Storage::HP::NotFound
|
2011-06-16 00:40:51 -04:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def files
|
|
|
|
@files ||= begin
|
2011-10-17 15:25:07 -04:00
|
|
|
Fog::Storage::HP::Files.new(
|
2011-06-16 00:40:51 -04:00
|
|
|
:directory => self,
|
2012-12-22 18:26:21 -05:00
|
|
|
:service => service
|
2011-06-16 00:40:51 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def public=(new_public)
|
2011-07-13 19:27:58 -04:00
|
|
|
if new_public
|
2012-10-16 21:48:50 -04:00
|
|
|
self.grant("pr")
|
2011-07-13 19:27:58 -04:00
|
|
|
else
|
2012-10-16 21:48:50 -04:00
|
|
|
self.revoke("pr")
|
2011-07-13 19:27:58 -04:00
|
|
|
end
|
2011-06-16 00:40:51 -04:00
|
|
|
@public = new_public
|
|
|
|
end
|
|
|
|
|
2011-07-30 19:34:21 -04:00
|
|
|
def public?
|
2012-11-08 16:41:18 -05:00
|
|
|
@read_acl = [] if @read_acl.nil?
|
|
|
|
if @read_acl.include?(".r:*")
|
2012-10-16 21:48:50 -04:00
|
|
|
true
|
2011-07-30 19:34:21 -04:00
|
|
|
else
|
2012-10-16 21:48:50 -04:00
|
|
|
false
|
2011-07-30 19:34:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-16 00:40:51 -04:00
|
|
|
def public_url
|
|
|
|
requires :key
|
|
|
|
@public_url ||= begin
|
2012-12-22 18:26:21 -05:00
|
|
|
begin response = service.head_container(key)
|
2012-01-20 21:12:02 -05:00
|
|
|
# escape the key to cover for special char. in container names
|
2013-01-18 15:06:41 -05:00
|
|
|
url = service.public_url(key)
|
2012-01-20 21:12:02 -05:00
|
|
|
rescue Fog::Storage::HP::NotFound => err
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cdn_enable=(new_cdn_enable)
|
|
|
|
@cdn_enable ||= false
|
2012-12-22 18:26:21 -05:00
|
|
|
if (!service.cdn.nil? && service.cdn.enabled?)
|
2012-01-20 21:12:02 -05:00
|
|
|
@cdn_enable = new_cdn_enable
|
|
|
|
else
|
|
|
|
# since cdn service is not activated, container cannot be cdn-enabled
|
|
|
|
@cdn_enable = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cdn_enabled?
|
2012-12-22 18:26:21 -05:00
|
|
|
if (!service.cdn.nil? && service.cdn.enabled?)
|
|
|
|
begin response = service.cdn.head_container(key)
|
2012-01-20 21:12:02 -05:00
|
|
|
cdn_header = response.headers.fetch('X-Cdn-Enabled', nil)
|
|
|
|
if (!cdn_header.nil? && cdn_header == 'True')
|
|
|
|
@cdn_enable = true
|
|
|
|
else
|
|
|
|
@cdn_enable = false
|
2011-07-13 18:11:08 -04:00
|
|
|
end
|
2013-01-16 17:14:13 -05:00
|
|
|
# If CDN endpoint is unreachable, a SocketError is raised
|
|
|
|
rescue Fog::CDN::HP::NotFound, Excon::Errors::SocketError
|
2012-01-20 21:12:02 -05:00
|
|
|
@cdn_enable = false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@cdn_enable = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cdn_public_url
|
|
|
|
requires :key
|
|
|
|
@cdn_public_url ||= begin
|
|
|
|
# return the CDN public url from the appropriate uri from the header
|
2012-12-22 18:26:21 -05:00
|
|
|
begin response = service.cdn.head_container(key)
|
2012-01-20 21:12:02 -05:00
|
|
|
if response.headers['X-Cdn-Enabled'] == 'True'
|
2012-04-17 17:00:31 -04:00
|
|
|
response.headers.fetch('X-Cdn-Uri', nil)
|
|
|
|
end
|
2013-01-16 17:14:13 -05:00
|
|
|
rescue Fog::CDN::HP::NotFound
|
2012-04-17 17:00:31 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2013-01-18 15:06:41 -05:00
|
|
|
begin response = service.cdn.head_container(key)
|
2012-04-17 17:00:31 -04:00
|
|
|
if response.headers['X-Cdn-Enabled'] == 'True'
|
|
|
|
response.headers.fetch('X-Cdn-Ssl-Uri', nil)
|
2011-06-16 00:40:51 -04:00
|
|
|
end
|
2013-01-16 17:14:13 -05:00
|
|
|
rescue Fog::CDN::HP::NotFound
|
2012-01-20 21:12:02 -05:00
|
|
|
nil
|
2011-06-16 00:40:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-11 16:24:01 -04:00
|
|
|
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
|
|
|
|
|
2012-10-26 00:21:49 -04:00
|
|
|
def save(options = {})
|
2011-06-16 00:40:51 -04:00
|
|
|
requires :key
|
2012-10-16 21:48:50 -04:00
|
|
|
# write out the acls into the headers before save
|
2013-01-18 15:06:41 -05:00
|
|
|
options.merge!(service.perm_acl_to_header(@read_acl, @write_acl))
|
2013-03-11 16:24:01 -04:00
|
|
|
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?
|
2012-12-22 18:26:21 -05:00
|
|
|
service.put_container(key, options)
|
2012-01-20 21:12:02 -05:00
|
|
|
# Added an extra check to see if CDN is enabled for the container
|
2012-12-22 18:26:21 -05:00
|
|
|
if (!service.cdn.nil? && service.cdn.enabled?)
|
2012-01-20 21:12:02 -05:00
|
|
|
# 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.
|
2012-12-22 18:26:21 -05:00
|
|
|
begin response = service.cdn.head_container(key)
|
2012-01-20 21:12:02 -05:00
|
|
|
### Deleting a container from CDN is much more expensive than flipping the bit to disable it
|
2012-12-22 18:26:21 -05:00
|
|
|
service.cdn.post_container(key, {'X-CDN-Enabled' => 'True'})
|
2013-01-16 17:14:13 -05:00
|
|
|
rescue Fog::CDN::HP::NotFound
|
2012-12-22 18:26:21 -05:00
|
|
|
service.cdn.put_container(key)
|
2013-01-16 17:14:13 -05:00
|
|
|
rescue Excon::Errors::SocketError
|
|
|
|
# means that the CDN endpoint is unreachable
|
2012-01-20 21:12:02 -05:00
|
|
|
end
|
2011-06-16 00:40:51 -04:00
|
|
|
else
|
2012-01-20 21:12:02 -05:00
|
|
|
# check to make sure that the container exists. If yes, cdn disable it.
|
2012-12-22 18:26:21 -05:00
|
|
|
begin response = service.cdn.head_container(key)
|
2012-01-20 21:12:02 -05:00
|
|
|
### Deleting a container from CDN is much more expensive than flipping the bit to disable it
|
2012-12-22 18:26:21 -05:00
|
|
|
service.cdn.post_container(key, {'X-CDN-Enabled' => 'False'})
|
2013-01-16 17:14:13 -05:00
|
|
|
rescue Fog::CDN::HP::NotFound
|
2011-11-11 16:14:28 -05:00
|
|
|
# just continue, as container is not cdn-enabled.
|
2013-01-16 17:14:13 -05:00
|
|
|
rescue Excon::Errors::SocketError
|
|
|
|
# means that the CDN endpoint is unreachable
|
2011-11-11 16:14:28 -05:00
|
|
|
end
|
2011-06-16 00:40:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|