1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/hp/cdn.rb

131 lines
4 KiB
Ruby
Raw Normal View History

2011-10-21 00:11:08 -04:00
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'hp'))
require 'fog/cdn'
module Fog
module CDN
class HP < Fog::Service
requires :hp_secret_key, :hp_account_id, :hp_tenant_id
recognizes :hp_auth_uri, :hp_cdn_uri, :persistent, :connection_options, :hp_use_upass_auth_style, :hp_auth_version
2011-10-21 00:11:08 -04:00
model_path 'fog/hp/models/cdn'
request_path 'fog/hp/requests/cdn'
request :get_containers
request :head_container
request :post_container
request :put_container
request :delete_container
2011-10-21 00:11:08 -04:00
module Utils
end
2011-10-21 00:11:08 -04:00
class Mock
include Utils
2011-10-21 00:11:08 -04:00
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {
:cdn_containers => {}
}
2011-10-21 00:11:08 -04:00
end
end
def self.reset
@data = nil
end
def initialize(options={})
@hp_account_id = options[:hp_account_id]
end
def data
self.class.data[@hp_account_id]
end
def reset_data
self.class.data.delete(@hp_account_id)
end
end
class Real
include Utils
2011-10-21 00:11:08 -04:00
def initialize(options={})
require 'multi_json'
@connection_options = options[:connection_options] || {}
### Set an option to use the style of authentication desired; :v1 or :v2 (default)
auth_version = options[:hp_auth_version] || :v2
### Pass the service type for object storage to the authentication call
options[:hp_service_type] = "hpext:cdn"
### 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]
cdn_mgmt_url = @hp_cdn_uri
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: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
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}
response.body = MultiJson.decode(response.body)
end
response
end
end
end
end
end