2011-10-17 15:25:07 -04:00
|
|
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'hp'))
|
|
|
|
require 'fog/compute'
|
|
|
|
|
2011-06-17 14:52:26 -04:00
|
|
|
module Fog
|
2011-10-17 15:25:07 -04:00
|
|
|
module Compute
|
|
|
|
class HP < Fog::Service
|
2011-06-17 14:52:26 -04:00
|
|
|
|
2012-04-11 12:41:05 -04:00
|
|
|
requires :hp_secret_key, :hp_account_id, :hp_tenant_id
|
|
|
|
recognizes :hp_auth_uri, :hp_servicenet, :persistent, :connection_options, :hp_use_upass_auth_style, :hp_auth_version, :hp_avl_zone
|
2011-06-17 14:52:26 -04:00
|
|
|
|
2011-10-17 15:25:07 -04:00
|
|
|
model_path 'fog/hp/models/compute'
|
2011-12-07 17:55:11 -05:00
|
|
|
model :address
|
|
|
|
collection :addresses
|
2011-06-28 15:42:14 -04:00
|
|
|
model :flavor
|
|
|
|
collection :flavors
|
2011-06-28 17:29:54 -04:00
|
|
|
model :image
|
|
|
|
collection :images
|
2011-11-28 18:07:31 -05:00
|
|
|
model :key_pair
|
|
|
|
collection :key_pairs
|
2011-12-02 16:15:56 -05:00
|
|
|
model :security_group
|
|
|
|
collection :security_groups
|
2011-07-01 17:01:40 -04:00
|
|
|
model :server
|
|
|
|
collection :servers
|
2011-06-17 14:52:26 -04:00
|
|
|
|
2011-10-17 15:25:07 -04:00
|
|
|
request_path 'fog/hp/requests/compute'
|
2011-12-06 14:10:12 -05:00
|
|
|
request :allocate_address
|
2011-12-08 17:59:18 -05:00
|
|
|
request :associate_address
|
2011-11-18 12:17:29 -05:00
|
|
|
request :change_password_server
|
2011-11-03 18:31:54 -04:00
|
|
|
#request :confirm_resized_server
|
2011-06-21 14:34:24 -04:00
|
|
|
request :create_image
|
2011-11-23 13:20:36 -05:00
|
|
|
request :create_key_pair
|
2011-11-30 17:04:45 -05:00
|
|
|
request :create_security_group
|
2011-12-05 12:49:29 -05:00
|
|
|
request :create_security_group_rule
|
2011-11-23 15:11:51 -05:00
|
|
|
request :create_server
|
2011-06-21 14:34:24 -04:00
|
|
|
request :delete_image
|
2011-11-23 15:11:51 -05:00
|
|
|
request :delete_key_pair
|
2011-11-30 18:18:54 -05:00
|
|
|
request :delete_security_group
|
2011-12-05 19:04:01 -05:00
|
|
|
request :delete_security_group_rule
|
2011-06-22 12:27:34 -04:00
|
|
|
request :delete_server
|
2011-12-08 17:59:18 -05:00
|
|
|
request :disassociate_address
|
2011-12-06 15:56:49 -05:00
|
|
|
request :get_address
|
2011-06-20 17:21:08 -04:00
|
|
|
request :get_flavor_details
|
|
|
|
request :get_image_details
|
2011-12-01 15:42:33 -05:00
|
|
|
request :get_security_group
|
2011-06-20 17:21:08 -04:00
|
|
|
request :get_server_details
|
2011-12-06 17:08:41 -05:00
|
|
|
request :list_addresses
|
2011-06-20 14:49:25 -04:00
|
|
|
request :list_flavors
|
|
|
|
request :list_flavors_detail
|
2011-06-20 14:26:12 -04:00
|
|
|
request :list_images
|
|
|
|
request :list_images_detail
|
2011-11-22 14:42:29 -05:00
|
|
|
request :list_key_pairs
|
2011-11-29 17:59:46 -05:00
|
|
|
request :list_security_groups
|
2011-12-06 16:58:36 -05:00
|
|
|
request :list_server_addresses
|
|
|
|
request :list_server_private_addresses
|
|
|
|
request :list_server_public_addresses
|
2011-06-17 14:52:26 -04:00
|
|
|
request :list_servers
|
2011-06-20 12:36:12 -04:00
|
|
|
request :list_servers_detail
|
2011-06-23 19:24:33 -04:00
|
|
|
request :reboot_server
|
2011-11-02 16:29:54 -04:00
|
|
|
request :rebuild_server
|
2011-12-06 15:47:11 -05:00
|
|
|
request :release_address
|
2011-11-03 18:31:54 -04:00
|
|
|
#request :resize_server
|
|
|
|
#request :revert_resized_server
|
2011-06-23 19:24:33 -04:00
|
|
|
request :server_action
|
2011-06-22 12:27:34 -04:00
|
|
|
request :update_server
|
2011-06-17 14:52:26 -04:00
|
|
|
|
|
|
|
class Mock
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = {
|
|
|
|
:last_modified => {
|
|
|
|
:images => {},
|
2011-11-29 17:59:46 -05:00
|
|
|
:key_pairs => {},
|
|
|
|
:security_groups => {},
|
2011-12-06 14:10:12 -05:00
|
|
|
:servers => {},
|
|
|
|
:addresses => {}
|
2011-06-17 14:52:26 -04:00
|
|
|
},
|
|
|
|
:images => {},
|
2011-11-29 17:59:46 -05:00
|
|
|
:key_pairs => {},
|
2011-11-30 18:18:54 -05:00
|
|
|
:security_groups => {},
|
2011-12-06 14:10:12 -05:00
|
|
|
:servers => {},
|
|
|
|
:addresses => {}
|
2011-06-17 14:52:26 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(options={})
|
2011-07-12 18:48:12 -04:00
|
|
|
@hp_account_id = options[:hp_account_id]
|
2011-06-17 14:52:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
2011-07-12 18:48:12 -04:00
|
|
|
self.class.data[@hp_account_id]
|
2011-06-17 14:52:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
2011-07-12 18:48:12 -04:00
|
|
|
self.class.data.delete(@hp_account_id)
|
2011-06-17 14:52:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
|
|
|
def initialize(options={})
|
2011-10-17 15:25:07 -04:00
|
|
|
require 'multi_json'
|
2011-07-12 18:48:12 -04:00
|
|
|
@hp_secret_key = options[:hp_secret_key]
|
|
|
|
@hp_account_id = options[:hp_account_id]
|
2011-06-17 14:52:26 -04:00
|
|
|
@hp_servicenet = options[:hp_servicenet]
|
2011-10-20 22:07:42 -04:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2012-01-19 19:11:03 -05:00
|
|
|
### 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 compute via the options hash
|
2012-01-24 16:33:46 -05:00
|
|
|
options[:hp_service_type] = "compute"
|
2012-01-19 19:11:03 -05:00
|
|
|
@hp_tenant_id = options[:hp_tenant_id]
|
|
|
|
|
|
|
|
### 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 cdn endpoint
|
|
|
|
@hp_compute_uri = credentials[:endpoint_url]
|
|
|
|
else
|
|
|
|
# Call the legacy v1.0/v1.1 authentication
|
|
|
|
credentials = Fog::HP.authenticate_v1(options, @connection_options)
|
|
|
|
# the user sends in the cdn endpoint
|
|
|
|
@hp_compute_uri = options[:hp_auth_uri]
|
|
|
|
end
|
|
|
|
|
|
|
|
@auth_token = credentials[:auth_token]
|
|
|
|
|
|
|
|
uri = URI.parse(@hp_compute_uri)
|
|
|
|
@host = @hp_servicenet == true ? "snet-#{uri.host}" : uri.host
|
|
|
|
@path = uri.path
|
2011-10-20 22:07:42 -04:00
|
|
|
@persistent = options[:persistent] || false
|
2012-01-19 19:11:03 -05:00
|
|
|
@port = uri.port
|
|
|
|
@scheme = uri.scheme
|
|
|
|
Excon.ssl_verify_peer = false if options[:hp_servicenet] == true
|
|
|
|
|
2011-10-20 22:07:42 -04:00
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
2011-06-17 14:52:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
2012-01-19 19:11:03 -05:00
|
|
|
def request(params, parse_json = true, &block)
|
2011-06-17 14:52:26 -04:00
|
|
|
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]}",
|
|
|
|
:query => ('ignore_awful_caching' << Time.now.to_i.to_s)
|
2012-01-19 19:11:03 -05:00
|
|
|
}), &block)
|
2011-06-17 14:52:26 -04:00
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
|
|
|
raise case error
|
|
|
|
when Excon::Errors::NotFound
|
2011-10-17 15:25:07 -04:00
|
|
|
Fog::Compute::HP::NotFound.slurp(error)
|
2011-06-17 14:52:26 -04:00
|
|
|
else
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
unless response.body.empty?
|
2011-07-22 14:47:05 -04:00
|
|
|
begin
|
2011-10-17 15:25:07 -04:00
|
|
|
response.body = MultiJson.decode(response.body)
|
|
|
|
rescue MultiJson::DecodeError => error
|
2011-07-22 14:47:05 -04:00
|
|
|
response.body #### the body is not in JSON format so just return it as it is
|
|
|
|
end
|
2011-06-17 14:52:26 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|