2013-01-18 15:08:27 -05:00
|
|
|
require 'fog/hp'
|
2012-07-05 13:05:50 -04:00
|
|
|
|
|
|
|
module Fog
|
2013-01-14 17:51:41 -05:00
|
|
|
module HP
|
|
|
|
class BlockStorage < Fog::Service
|
2012-07-05 13:05:50 -04:00
|
|
|
|
2013-01-22 17:28:33 -05:00
|
|
|
requires :hp_secret_key, :hp_tenant_id, :hp_avl_zone
|
2013-03-27 09:52:52 -04:00
|
|
|
recognizes :hp_auth_uri, :credentials, :hp_service_type
|
2013-01-18 15:08:27 -05:00
|
|
|
recognizes :persistent, :connection_options
|
|
|
|
recognizes :hp_use_upass_auth_style, :hp_auth_version, :user_agent
|
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
|
2013-01-18 15:08:27 -05:00
|
|
|
|
|
|
|
secrets :hp_secret_key
|
2012-07-05 13:05:50 -04:00
|
|
|
|
2013-01-22 17:28:33 -05:00
|
|
|
model_path 'fog/hp/models/block_storage'
|
2012-07-11 16:59:54 -04:00
|
|
|
model :volume
|
|
|
|
collection :volumes
|
2012-12-04 02:18:10 -05:00
|
|
|
collection :bootable_volumes
|
2012-07-05 13:05:50 -04:00
|
|
|
|
2012-08-09 16:44:59 -04:00
|
|
|
model :snapshot
|
|
|
|
collection :snapshots
|
|
|
|
|
2012-07-05 13:05:50 -04:00
|
|
|
request_path 'fog/hp/requests/block_storage'
|
2012-07-09 11:53:23 -04:00
|
|
|
request :create_volume
|
2012-07-05 17:03:14 -04:00
|
|
|
request :delete_volume
|
2012-11-14 15:08:24 -05:00
|
|
|
request :get_bootable_volume_details
|
2012-07-05 16:41:24 -04:00
|
|
|
request :get_volume_details
|
2012-11-14 15:08:24 -05:00
|
|
|
request :list_bootable_volumes
|
2012-07-05 13:05:50 -04:00
|
|
|
request :list_volumes
|
|
|
|
|
2012-08-02 18:01:18 -04:00
|
|
|
request :create_snapshot
|
2012-08-02 19:12:54 -04:00
|
|
|
request :delete_snapshot
|
2012-08-02 19:03:37 -04:00
|
|
|
request :get_snapshot_details
|
2012-11-14 15:08:24 -05:00
|
|
|
request :list_snapshots
|
2012-08-02 14:33:03 -04:00
|
|
|
|
2012-07-05 13:05:50 -04:00
|
|
|
module Utils
|
|
|
|
|
2012-07-11 19:20:53 -04:00
|
|
|
def compute
|
|
|
|
@compute ||= Fog::Compute.new(
|
|
|
|
:provider => 'HP',
|
2013-01-22 17:28:33 -05:00
|
|
|
:hp_access_key => @hp_access_key,
|
2012-07-11 19:20:53 -04:00
|
|
|
:hp_secret_key => @hp_secret_key,
|
|
|
|
:hp_auth_uri => @hp_auth_uri,
|
|
|
|
:hp_tenant_id => @hp_tenant_id,
|
|
|
|
:hp_avl_zone => @hp_avl_zone,
|
2013-04-04 19:08:02 -04:00
|
|
|
:credentials => @credentials,
|
2012-07-11 19:20:53 -04:00
|
|
|
:connection_options => @connection_options
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2012-07-05 13:05:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Mock
|
|
|
|
include Utils
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = {
|
2012-08-02 14:33:03 -04:00
|
|
|
:volumes => {},
|
|
|
|
:snapshots => {}
|
2012-07-05 13:05:50 -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
|
2012-07-05 13:05:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
2013-01-22 17:28:33 -05:00
|
|
|
self.class.data[@hp_access_key]
|
2012-07-05 13:05:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
2013-01-22 17:28:33 -05:00
|
|
|
self.class.data.delete(@hp_access_key)
|
2012-07-05 13:05:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
include Utils
|
2013-03-27 09:52:52 -04:00
|
|
|
attr_reader :credentials
|
2012-07-05 13:05:50 -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
|
2012-07-05 13:05:50 -04:00
|
|
|
@hp_secret_key = options[:hp_secret_key]
|
2012-07-18 14:23:01 -04:00
|
|
|
@hp_auth_uri = options[:hp_auth_uri]
|
2012-07-05 13:05:50 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
|
|
|
### Set an option to use the style of authentication desired; :v1 or :v2 (default)
|
2013-09-30 11:58:29 -04:00
|
|
|
### A symbol is required, we should ensure that the value is loaded as a symbol
|
2013-10-07 05:29:31 -04:00
|
|
|
auth_version = options[:hp_auth_version] || :v2
|
|
|
|
auth_version = auth_version.to_s.downcase.to_sym
|
|
|
|
|
2012-07-05 13:05:50 -04:00
|
|
|
### Pass the service name for object storage to the authentication call
|
2013-09-27 19:40:08 -04:00
|
|
|
options[:hp_service_type] ||= "Block Storage"
|
2012-07-05 13:05:50 -04:00
|
|
|
@hp_tenant_id = options[:hp_tenant_id]
|
|
|
|
@hp_avl_zone = options[:hp_avl_zone]
|
|
|
|
|
|
|
|
### Make the authentication call
|
|
|
|
if (auth_version == :v2)
|
|
|
|
# Call the control services authentication
|
|
|
|
credentials = Fog::HP.authenticate_v2(options, @connection_options)
|
|
|
|
# the CS service catalog returns the block storage endpoint
|
|
|
|
@hp_block_uri = credentials[:endpoint_url]
|
2013-03-27 09:52:52 -04:00
|
|
|
@credentials = credentials
|
2012-07-05 13:05:50 -04:00
|
|
|
else
|
|
|
|
# Call the legacy v1.0/v1.1 authentication
|
|
|
|
credentials = Fog::HP.authenticate_v1(options, @connection_options)
|
|
|
|
# the user sends in the block storage endpoint
|
|
|
|
@hp_block_uri = options[:hp_auth_uri]
|
|
|
|
end
|
|
|
|
|
|
|
|
@auth_token = credentials[:auth_token]
|
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
|
|
|
|
uri = URI.parse(@hp_block_uri)
|
|
|
|
@host = uri.host
|
|
|
|
@path = uri.path
|
|
|
|
@port = uri.port
|
|
|
|
@scheme = uri.scheme
|
|
|
|
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(params, parse_json = true, &block)
|
|
|
|
begin
|
|
|
|
response = @connection.request(params.merge!({
|
|
|
|
:headers => {
|
|
|
|
'Content-Type' => 'application/json',
|
2013-03-13 11:33:51 -04:00
|
|
|
'Accept' => 'application/json',
|
2012-07-05 13:05:50 -04:00
|
|
|
'X-Auth-Token' => @auth_token
|
|
|
|
}.merge!(params[:headers] || {}),
|
2013-03-21 13:01:34 -04:00
|
|
|
:path => "#{@path}/#{params[:path]}"
|
2012-07-05 13:05:50 -04:00
|
|
|
}), &block)
|
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
|
|
|
raise case error
|
|
|
|
when Excon::Errors::NotFound
|
2013-01-14 17:51:41 -05:00
|
|
|
Fog::HP::BlockStorage::NotFound.slurp(error)
|
2012-07-05 13:05:50 -04:00
|
|
|
else
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
|
2013-01-18 15:08:27 -05:00
|
|
|
response.body = Fog::JSON.decode(response.body)
|
2012-07-05 13:05:50 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|