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
|
|
|
|
2011-07-12 18:48:12 -04:00
|
|
|
requires :hp_secret_key, :hp_account_id
|
2011-10-20 22:07:42 -04:00
|
|
|
recognizes :hp_auth_uri, :hp_servicenet, :persistent, :connection_options
|
2011-06-17 14:52:26 -04:00
|
|
|
|
2011-10-17 15:25:07 -04:00
|
|
|
model_path 'fog/hp/models/compute'
|
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-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-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-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-06-22 12:27:34 -04:00
|
|
|
request :delete_server
|
2011-06-20 17:21:08 -04:00
|
|
|
request :get_flavor_details
|
|
|
|
request :get_image_details
|
|
|
|
request :get_server_details
|
2011-06-22 15:06:43 -04:00
|
|
|
request :list_addresses
|
|
|
|
request :list_private_addresses
|
|
|
|
request :list_public_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-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-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 => {},
|
|
|
|
:servers => {}
|
2011-06-17 14:52:26 -04:00
|
|
|
},
|
|
|
|
:images => {},
|
2011-11-29 17:59:46 -05:00
|
|
|
:key_pairs => {},
|
|
|
|
:security_groups => {},
|
|
|
|
:servers => {}
|
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]
|
|
|
|
@hp_auth_uri = options[:hp_auth_uri]
|
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] || {}
|
2011-06-17 14:52:26 -04:00
|
|
|
authenticate
|
|
|
|
Excon.ssl_verify_peer = false if options[:hp_servicenet] == true
|
2011-10-20 22:07:42 -04:00
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
2011-06-17 14:52:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(params)
|
|
|
|
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)
|
|
|
|
}))
|
|
|
|
rescue Excon::Errors::Unauthorized => error
|
2011-10-17 15:25:07 -04:00
|
|
|
if MultiJson.decode(response.body)['unauthorized']['message'] == 'Invalid authentication token. Please renew.'
|
2011-06-17 14:52:26 -04:00
|
|
|
authenticate
|
|
|
|
retry
|
|
|
|
else
|
|
|
|
raise error
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def authenticate
|
|
|
|
options = {
|
2011-07-12 18:48:12 -04:00
|
|
|
:hp_secret_key => @hp_secret_key,
|
|
|
|
:hp_account_id => @hp_account_id,
|
|
|
|
:hp_auth_uri => @hp_auth_uri,
|
2011-06-17 14:52:26 -04:00
|
|
|
}
|
2011-10-20 22:07:42 -04:00
|
|
|
credentials = Fog::HP.authenticate(options, @connection_options)
|
2011-06-17 14:52:26 -04:00
|
|
|
@auth_token = credentials['X-Auth-Token']
|
|
|
|
uri = URI.parse(credentials['X-Server-Management-Url'])
|
|
|
|
@host = @hp_servicenet == true ? "snet-#{uri.host}" : uri.host
|
|
|
|
@path = uri.path
|
|
|
|
@port = uri.port
|
|
|
|
@scheme = uri.scheme
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|