2014-02-01 21:13:17 -05:00
|
|
|
require 'fog/hp/core'
|
2011-10-17 15:25:07 -04:00
|
|
|
|
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
|
|
|
|
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:06:41 -05:00
|
|
|
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-08 19:36:08 -04:00
|
|
|
secrets :hp_secret_key
|
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
|
2012-05-14 17:10:15 -04:00
|
|
|
model :meta
|
|
|
|
collection :metadata
|
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
|
2012-07-09 20:56:56 -04:00
|
|
|
request :attach_volume
|
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
|
2012-12-03 22:05:30 -05:00
|
|
|
request :create_persistent_server
|
2011-06-21 14:34:24 -04:00
|
|
|
request :delete_image
|
2011-11-23 15:11:51 -05:00
|
|
|
request :delete_key_pair
|
2012-05-08 11:35:37 -04:00
|
|
|
request :delete_meta
|
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
|
2012-07-09 20:56:56 -04:00
|
|
|
request :detach_volume
|
2011-12-08 17:59:18 -05:00
|
|
|
request :disassociate_address
|
2011-12-06 15:56:49 -05:00
|
|
|
request :get_address
|
2012-04-18 14:09:08 -04:00
|
|
|
request :get_console_output
|
2011-06-20 17:21:08 -04:00
|
|
|
request :get_flavor_details
|
|
|
|
request :get_image_details
|
2012-05-08 11:35:37 -04:00
|
|
|
request :get_meta
|
2012-10-03 14:39:14 -04:00
|
|
|
request :get_windows_password
|
2011-12-01 15:42:33 -05:00
|
|
|
request :get_security_group
|
2011-06-20 17:21:08 -04:00
|
|
|
request :get_server_details
|
2013-03-12 18:17:33 -04:00
|
|
|
request :get_vnc_console
|
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
|
2012-05-08 11:35:37 -04:00
|
|
|
request :list_metadata
|
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
|
2012-07-09 20:56:56 -04:00
|
|
|
request :list_server_volumes
|
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
|
2012-05-08 11:35:37 -04:00
|
|
|
request :set_metadata
|
|
|
|
request :update_meta
|
|
|
|
request :update_metadata
|
2011-06-22 12:27:34 -04:00
|
|
|
request :update_server
|
2011-06-17 14:52:26 -04:00
|
|
|
|
2012-10-03 14:39:14 -04:00
|
|
|
module Utils
|
|
|
|
|
|
|
|
# extract windows password from log
|
|
|
|
def extract_password_from_log(log_text)
|
|
|
|
encrypted_text = ""
|
|
|
|
section = []
|
|
|
|
return if log_text.nil?
|
|
|
|
log_text.each_line do |line|
|
|
|
|
case line
|
|
|
|
when /^-----BEGIN (\w+)/
|
|
|
|
section.push $1
|
|
|
|
next
|
|
|
|
when /^-----END (\w+)/
|
|
|
|
section.pop
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
case section
|
|
|
|
when ["BASE64"]
|
|
|
|
encrypted_text << line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# return the encrypted portion only
|
|
|
|
encrypted_text
|
|
|
|
end
|
|
|
|
|
|
|
|
def encrypt_using_public_key(text, public_key_data)
|
|
|
|
return if (text.nil? || public_key_data.nil?)
|
|
|
|
public_key = OpenSSL::PKey::RSA.new(public_key_data)
|
|
|
|
encrypted_text = public_key.public_encrypt(text).strip
|
|
|
|
Base64.encode64(encrypted_text)
|
|
|
|
end
|
|
|
|
|
|
|
|
def decrypt_using_private_key(encrypted_text, private_key_data)
|
|
|
|
return if (encrypted_text.nil? || private_key_data.nil?)
|
|
|
|
private_key = OpenSSL::PKey::RSA.new(private_key_data)
|
|
|
|
from_base64 = Base64.decode64(encrypted_text)
|
|
|
|
private_key.private_decrypt(from_base64).strip
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2011-06-17 14:52:26 -04:00
|
|
|
class Mock
|
2012-10-03 14:39:14 -04:00
|
|
|
include Utils
|
2011-06-17 14:52:26 -04:00
|
|
|
|
|
|
|
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={})
|
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-06-17 14:52:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
2013-01-22 17:28:33 -05:00
|
|
|
self.class.data[@hp_access_key]
|
2011-06-17 14:52:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
2013-01-22 17:28:33 -05:00
|
|
|
self.class.data.delete(@hp_access_key)
|
2011-06-17 14:52:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
2012-10-03 14:39:14 -04:00
|
|
|
include Utils
|
2013-03-27 09:52:52 -04:00
|
|
|
attr_reader :credentials
|
2011-06-17 14:52:26 -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
|
2011-07-12 18:48:12 -04:00
|
|
|
@hp_secret_key = options[:hp_secret_key]
|
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)
|
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
|
2014-02-19 07:30:59 -05:00
|
|
|
|
2012-05-01 12:15:22 -04:00
|
|
|
### Pass the service name for compute via the options hash
|
2013-09-27 19:40:08 -04: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]
|
2013-03-27 09:52:52 -04:00
|
|
|
@credentials = credentials
|
2012-01-19 19:11:03 -05:00
|
|
|
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)
|
2012-05-08 15:08:03 -04:00
|
|
|
@host = uri.host
|
2012-01-19 19:11:03 -05:00
|
|
|
@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
|
|
|
|
|
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',
|
2013-03-13 11:33:51 -04:00
|
|
|
'Accept' => 'application/json',
|
2011-06-17 14:52:26 -04:00
|
|
|
'X-Auth-Token' => @auth_token
|
|
|
|
}.merge!(params[:headers] || {}),
|
2013-08-05 15:55:02 -04:00
|
|
|
:path => "#{@path}/#{params[:path]}"
|
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
|
2013-01-18 15:06:41 -05:00
|
|
|
if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
|
|
|
|
response.body = Fog::JSON.decode(response.body)
|
2011-06-17 14:52:26 -04:00
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|