2011-10-17 15:25:07 -04:00
|
|
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'hp'))
|
|
|
|
require 'fog/storage'
|
|
|
|
|
2011-06-15 02:13:18 -04:00
|
|
|
module Fog
|
2011-10-17 15:25:07 -04:00
|
|
|
module Storage
|
|
|
|
class HP < Fog::Service
|
2011-06-15 02:13:18 -04:00
|
|
|
|
2012-04-27 14:13:39 -04:00
|
|
|
requires :hp_secret_key, :hp_account_id, :hp_tenant_id, :hp_avl_zone
|
2012-04-11 12:41:05 -04:00
|
|
|
recognizes :hp_auth_uri, :hp_servicenet, :hp_cdn_ssl, :hp_cdn_uri, :persistent, :connection_options, :hp_use_upass_auth_style, :hp_auth_version
|
2011-06-15 02:13:18 -04:00
|
|
|
|
2011-10-17 15:25:07 -04:00
|
|
|
model_path 'fog/hp/models/storage'
|
2011-06-16 00:36:38 -04:00
|
|
|
model :directory
|
|
|
|
collection :directories
|
|
|
|
model :file
|
|
|
|
collection :files
|
2011-06-15 02:13:18 -04:00
|
|
|
|
2011-10-17 15:25:07 -04:00
|
|
|
request_path 'fog/hp/requests/storage'
|
2011-06-15 02:13:18 -04:00
|
|
|
request :delete_container
|
2011-06-15 17:59:06 -04:00
|
|
|
request :delete_object
|
2011-06-15 02:13:18 -04:00
|
|
|
request :get_container
|
|
|
|
request :get_containers
|
2011-06-15 17:59:06 -04:00
|
|
|
request :get_object
|
2011-06-15 14:33:26 -04:00
|
|
|
request :head_container
|
|
|
|
request :head_containers
|
2011-06-15 17:59:06 -04:00
|
|
|
request :head_object
|
2011-06-15 02:13:18 -04:00
|
|
|
request :put_container
|
2011-06-15 17:59:06 -04:00
|
|
|
request :put_object
|
2011-06-15 02:13:18 -04:00
|
|
|
|
|
|
|
module Utils
|
|
|
|
|
|
|
|
def cdn
|
2011-11-11 16:12:57 -05:00
|
|
|
unless @hp_cdn_uri.nil?
|
|
|
|
@cdn ||= Fog::CDN.new(
|
|
|
|
:provider => 'HP',
|
|
|
|
:hp_account_id => @hp_account_id,
|
|
|
|
:hp_secret_key => @hp_secret_key,
|
|
|
|
:hp_auth_uri => @hp_auth_uri,
|
2012-01-18 17:23:54 -05:00
|
|
|
:hp_cdn_uri => @hp_cdn_uri,
|
2012-04-16 17:18:58 -04:00
|
|
|
:hp_tenant_id => @hp_tenant_id,
|
2012-04-27 14:13:39 -04:00
|
|
|
:hp_avl_zone => @hp_avl_zone,
|
2012-04-16 17:18:58 -04:00
|
|
|
:connection_options => @connection_options
|
2011-11-11 16:12:57 -05:00
|
|
|
)
|
|
|
|
if @cdn.enabled?
|
|
|
|
@cdn
|
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
2011-07-13 18:11:08 -04:00
|
|
|
def url
|
|
|
|
"#{@scheme}://#{@host}:#{@port}#{@path}"
|
|
|
|
end
|
2011-07-13 19:25:47 -04:00
|
|
|
|
|
|
|
def acl_to_header(acl)
|
|
|
|
header = {}
|
|
|
|
case acl
|
|
|
|
when "private"
|
|
|
|
header['X-Container-Read'] = ""
|
|
|
|
header['X-Container-Write'] = ""
|
|
|
|
when "public-read"
|
|
|
|
header['X-Container-Read'] = ".r:*,.rlistings"
|
|
|
|
when "public-write"
|
|
|
|
header['X-Container-Write'] = "*"
|
|
|
|
when "public-read-write"
|
|
|
|
header['X-Container-Read'] = ".r:*,.rlistings"
|
|
|
|
header['X-Container-Write'] = "*"
|
|
|
|
end
|
|
|
|
header
|
|
|
|
end
|
2011-08-01 15:03:08 -04:00
|
|
|
|
|
|
|
def header_to_acl(read_header=nil, write_header=nil)
|
|
|
|
acl = nil
|
|
|
|
if read_header.nil? && write_header.nil?
|
|
|
|
acl = nil
|
|
|
|
elsif !read_header.nil? && read_header.include?(".r:*") && write_header.nil?
|
|
|
|
acl = "public-read"
|
|
|
|
elsif !write_header.nil? && write_header.include?("*") && read_header.nil?
|
|
|
|
acl = "public-write"
|
|
|
|
elsif !read_header.nil? && read_header.include?(".r:*") && !write_header.nil? && write_header.include?("*")
|
|
|
|
acl = "public-read-write"
|
|
|
|
end
|
|
|
|
end
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Mock
|
|
|
|
include Utils
|
2011-07-29 11:33:28 -04:00
|
|
|
def self.acls(type)
|
|
|
|
type
|
|
|
|
end
|
2011-06-15 02:13:18 -04:00
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
2011-07-29 11:33:28 -04:00
|
|
|
hash[key] = {
|
|
|
|
:acls => {
|
|
|
|
:container => {},
|
|
|
|
:object => {}
|
|
|
|
},
|
|
|
|
:containers => {}
|
|
|
|
}
|
|
|
|
end
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
require 'mime/types'
|
2011-07-11 10:46:24 -04:00
|
|
|
@hp_secret_key = options[:hp_secret_key]
|
|
|
|
@hp_account_id = options[:hp_account_id]
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
2011-07-11 10:46:24 -04:00
|
|
|
self.class.data[@hp_account_id]
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
2011-07-11 10:46:24 -04:00
|
|
|
self.class.data.delete(@hp_account_id)
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
include Utils
|
|
|
|
attr_reader :hp_cdn_ssl
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
require 'mime/types'
|
2011-10-17 15:25:07 -04:00
|
|
|
require 'multi_json'
|
2011-07-11 10:46:24 -04:00
|
|
|
@hp_secret_key = options[:hp_secret_key]
|
|
|
|
@hp_account_id = options[:hp_account_id]
|
2012-02-09 18:43:07 -05:00
|
|
|
@hp_auth_uri = options[:hp_auth_uri]
|
2011-10-21 15:51:40 -04:00
|
|
|
@hp_cdn_ssl = options[:hp_cdn_ssl]
|
2011-10-20 22:07:42 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2012-01-13 20:12:39 -05:00
|
|
|
### Set an option to use the style of authentication desired; :v1 or :v2 (default)
|
|
|
|
auth_version = options[:hp_auth_version] || :v2
|
2012-05-01 12:15:22 -04:00
|
|
|
### Pass the service name for object storage to the authentication call
|
|
|
|
options[:hp_service_type] = "Object Storage"
|
2012-01-18 17:23:54 -05:00
|
|
|
@hp_tenant_id = options[:hp_tenant_id]
|
2012-04-27 14:13:39 -04:00
|
|
|
@hp_avl_zone = options[:hp_avl_zone]
|
2011-06-15 02:13:18 -04:00
|
|
|
|
2012-01-13 20:12:39 -05:00
|
|
|
### Make the authentication call
|
|
|
|
if (auth_version == :v2)
|
|
|
|
# Call the control services authentication
|
|
|
|
credentials = Fog::HP.authenticate_v2(options, @connection_options)
|
2012-01-18 17:23:54 -05:00
|
|
|
# the CS service catalog returns the cdn endpoint
|
2012-01-19 14:50:43 -05:00
|
|
|
@hp_storage_uri = credentials[:endpoint_url]
|
2012-01-18 17:23:54 -05:00
|
|
|
@hp_cdn_uri = credentials[:cdn_endpoint_url]
|
2012-01-13 20:12:39 -05:00
|
|
|
else
|
|
|
|
# Call the legacy v1.0/v1.1 authentication
|
|
|
|
credentials = Fog::HP.authenticate_v1(options, @connection_options)
|
2012-01-18 17:23:54 -05:00
|
|
|
# the user sends in the cdn endpoint
|
2012-01-19 14:50:43 -05:00
|
|
|
@hp_storage_uri = options[:hp_auth_uri]
|
2012-01-18 17:23:54 -05:00
|
|
|
@hp_cdn_uri = options[:hp_cdn_uri]
|
2012-01-13 20:12:39 -05:00
|
|
|
end
|
2012-01-09 17:05:51 -05:00
|
|
|
|
2012-01-13 20:12:39 -05:00
|
|
|
@auth_token = credentials[:auth_token]
|
2012-01-18 17:23:54 -05:00
|
|
|
|
2012-01-19 14:50:43 -05:00
|
|
|
uri = URI.parse(@hp_storage_uri)
|
2011-06-15 02:13:18 -04:00
|
|
|
@host = options[:hp_servicenet] == true ? "snet-#{uri.host}" : uri.host
|
|
|
|
@path = uri.path
|
2011-10-20 22:07:42 -04:00
|
|
|
@persistent = options[:persistent] || false
|
2011-06-15 02:13:18 -04:00
|
|
|
@port = uri.port
|
|
|
|
@scheme = uri.scheme
|
|
|
|
Excon.ssl_verify_peer = false if options[:hp_servicenet] == true
|
2012-01-18 17:23:54 -05:00
|
|
|
|
2011-10-20 22:07:42 -04:00
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(params, parse_json = true, &block)
|
|
|
|
begin
|
|
|
|
response = @connection.request(params.merge!({
|
|
|
|
:headers => {
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'X-Auth-Token' => @auth_token
|
|
|
|
}.merge!(params[:headers] || {}),
|
|
|
|
:host => @host,
|
|
|
|
:path => "#{@path}/#{params[:path]}",
|
|
|
|
}), &block)
|
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
|
|
|
raise case error
|
|
|
|
when Excon::Errors::NotFound
|
2011-10-17 15:25:07 -04:00
|
|
|
Fog::Storage::HP::NotFound.slurp(error)
|
2011-06-15 02:13:18 -04:00
|
|
|
else
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
|
2011-10-17 15:25:07 -04:00
|
|
|
response.body = MultiJson.decode(response.body)
|
2011-06-15 02:13:18 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|