2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/hp'
|
2011-10-21 00:11:08 -04:00
|
|
|
require 'fog/cdn'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module CDN
|
|
|
|
class HP < Fog::Service
|
|
|
|
|
2013-01-22 17:28:33 -05:00
|
|
|
requires :hp_secret_key, :hp_tenant_id, :hp_avl_zone
|
2013-01-18 15:06:41 -05:00
|
|
|
recognizes :hp_auth_uri, :hp_cdn_uri
|
|
|
|
recognizes :hp_use_upass_auth_style, :hp_auth_version, :user_agent
|
|
|
|
recognizes :persistent, :connection_options
|
2013-01-22 17:28:33 -05:00
|
|
|
recognizes :hp_access_key, :hp_account_id # :hp_account_id is deprecated use hp_access_key instead
|
2011-06-15 02:10:54 -04:00
|
|
|
|
2012-06-22 12:38:00 -04:00
|
|
|
secrets :hp_secret_key
|
2011-10-21 00:11:08 -04:00
|
|
|
|
|
|
|
model_path 'fog/hp/models/cdn'
|
|
|
|
|
|
|
|
request_path 'fog/hp/requests/cdn'
|
|
|
|
request :get_containers
|
2011-10-21 15:50:35 -04:00
|
|
|
request :head_container
|
|
|
|
request :post_container
|
|
|
|
request :put_container
|
2011-10-21 16:39:55 -04:00
|
|
|
request :delete_container
|
2011-10-21 00:11:08 -04:00
|
|
|
|
2011-10-21 17:43:17 -04:00
|
|
|
module Utils
|
2012-03-21 16:59:08 -04:00
|
|
|
|
2011-10-21 17:43:17 -04:00
|
|
|
end
|
|
|
|
|
2011-10-21 00:11:08 -04:00
|
|
|
class Mock
|
2011-10-21 17:43:17 -04:00
|
|
|
include Utils
|
2011-10-21 00:11:08 -04:00
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
2012-04-09 16:02:04 -04:00
|
|
|
hash[key] = {
|
|
|
|
:cdn_containers => {}
|
|
|
|
}
|
2011-10-21 00:11:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(options={})
|
2013-01-22 17:28:33 -05:00
|
|
|
# deprecate hp_account_id
|
|
|
|
if options[:hp_account_id]
|
|
|
|
Fog::Logger.deprecation(":hp_account_id is deprecated, please use :hp_access_key instead.")
|
|
|
|
@hp_access_key = options.delete(:hp_account_id)
|
|
|
|
end
|
|
|
|
@hp_access_key = options[:hp_access_key]
|
|
|
|
unless @hp_access_key
|
|
|
|
raise ArgumentError.new("Missing required arguments: hp_access_key. :hp_account_id is deprecated, please use :hp_access_key instead.")
|
|
|
|
end
|
2011-10-21 00:11:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
2013-01-22 17:28:33 -05:00
|
|
|
self.class.data[@hp_access_key]
|
2011-10-21 00:11:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
2013-01-22 17:28:33 -05:00
|
|
|
self.class.data.delete(@hp_access_key)
|
2011-10-21 00:11:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
2011-10-21 17:43:17 -04:00
|
|
|
include Utils
|
2011-10-21 00:11:08 -04:00
|
|
|
|
|
|
|
def initialize(options={})
|
2013-01-22 17:28:33 -05:00
|
|
|
# deprecate hp_account_id
|
|
|
|
if options[:hp_account_id]
|
|
|
|
Fog::Logger.deprecation(":hp_account_id is deprecated, please use :hp_access_key instead.")
|
|
|
|
options[:hp_access_key] = options.delete(:hp_account_id)
|
|
|
|
end
|
|
|
|
@hp_access_key = options[:hp_access_key]
|
|
|
|
unless @hp_access_key
|
|
|
|
raise ArgumentError.new("Missing required arguments: hp_access_key. :hp_account_id is deprecated, please use :hp_access_key instead.")
|
|
|
|
end
|
|
|
|
@hp_secret_key = options[:hp_secret_key]
|
2011-10-21 00:11:08 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2012-01-18 17:26:48 -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] = "CDN"
|
2013-01-22 17:28:33 -05:00
|
|
|
@hp_tenant_id = options[:hp_tenant_id]
|
2012-01-18 17:26:48 -05:00
|
|
|
|
|
|
|
### Make the authentication call
|
|
|
|
if (auth_version == :v2)
|
|
|
|
# Call the control services authentication
|
|
|
|
credentials = Fog::HP.authenticate_v2(options, @connection_options)
|
|
|
|
### When using the v2 CS authentication, the CDN Mgmt comes from the service catalog
|
|
|
|
@hp_cdn_uri = credentials[:endpoint_url]
|
2012-02-09 17:59:07 -05:00
|
|
|
cdn_mgmt_url = @hp_cdn_uri
|
2012-01-18 17:26:48 -05:00
|
|
|
else
|
|
|
|
# Call the legacy v1.0/v1.1 authentication
|
|
|
|
credentials = Fog::HP.authenticate_v1(options, @connection_options)
|
|
|
|
# In case of legacy authentication systems, the user can pass the CDN Mgmt Uri
|
|
|
|
@hp_cdn_uri = options[:hp_cdn_uri] || "https://region-a.geo-1.cdnmgmt.hpcloudsvc.com/v1.0"
|
|
|
|
# In case of legacy authentication systems, the :cdn_endpoint_url will carry the cdn storage url
|
|
|
|
cdn_mgmt_url = "#{@hp_cdn_uri}#{URI.parse(credentials[:cdn_endpoint_url]).path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
@auth_token = credentials[:auth_token]
|
2011-10-21 00:11:08 -04:00
|
|
|
@enabled = false
|
|
|
|
@persistent = options[:persistent] || false
|
2012-01-18 17:26:48 -05:00
|
|
|
|
2012-01-18 17:36:29 -05:00
|
|
|
if cdn_mgmt_url
|
|
|
|
uri = URI.parse(cdn_mgmt_url)
|
|
|
|
@host = uri.host
|
|
|
|
@path = uri.path.chomp("/")
|
|
|
|
@port = uri.port
|
|
|
|
@scheme = uri.scheme
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
|
|
|
@enabled = true
|
|
|
|
end
|
2011-10-21 00:11:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def enabled?
|
|
|
|
@enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@cdn_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
|
2012-02-15 00:40:23 -05:00
|
|
|
Fog::CDN::HP::NotFound.slurp(error)
|
2011-10-21 00:11:08 -04:00
|
|
|
else
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
|
2012-04-25 10:31:28 -04:00
|
|
|
response.body = Fog::JSON.decode(response.body)
|
2011-10-21 00:11:08 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|