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

[hp|compute_v2] Add new HP Compute V2 provider. Add request methods for servers.

This commit is contained in:
Rupak Ganguly 2013-04-25 13:19:30 -04:00
parent 5263f25130
commit 2821685f95
8 changed files with 638 additions and 0 deletions

View file

@ -14,6 +14,17 @@ module Fog
when :gogrid when :gogrid
require 'fog/go_grid/compute' require 'fog/go_grid/compute'
Fog::Compute::GoGrid.new(attributes) Fog::Compute::GoGrid.new(attributes)
when :hp
version = attributes.delete(:version)
version = version.to_s.downcase.to_sym unless version.nil?
if version == :v2
require 'fog/hp/compute_v2'
Fog::Compute::HPV2.new(attributes)
else
Fog::Logger.deprecation "First Gen HP Cloud Compute Servers will be soon deprecated. Please use `:version => :v2` attribute to use Next Gen HP Cloud Compute Servers."
require 'fog/hp/compute'
Fog::Compute::HP.new(attributes)
end
when :new_servers when :new_servers
require 'fog/bare_metal_cloud/compute' require 'fog/bare_metal_cloud/compute'
Fog::Logger.deprecation "`new_servers` is deprecated. Please use `bare_metal_cloud` instead." Fog::Logger.deprecation "`new_servers` is deprecated. Please use `bare_metal_cloud` instead."

245
lib/fog/hp/compute_v2.rb Normal file
View file

@ -0,0 +1,245 @@
require 'fog/hp'
require 'fog/compute'
module Fog
module Compute
class HPV2 < Fog::Service
requires :hp_secret_key, :hp_tenant_id, :hp_avl_zone
recognizes :hp_auth_uri, :credentials
recognizes :hp_use_upass_auth_style, :hp_auth_version, :user_agent
recognizes :persistent, :connection_options
recognizes :hp_access_key, :hp_account_id # :hp_account_id is deprecated use hp_access_key instead
secrets :hp_secret_key
#model_path 'fog/hp/models/compute_v2'
#model :flavor
#collection :flavors
#model :image
#collection :images
#model :key_pair
#collection :key_pairs
#model :meta
#collection :metadata
#model :server
#collection :servers
request_path 'fog/hp/requests/compute_v2'
#request :allocate_address
#request :associate_address
#request :attach_volume
#request :change_password_server
#request :confirm_resized_server
#request :create_image
#request :create_key_pair
request :create_server
#request :create_persistent_server
#request :delete_image
#request :delete_key_pair
#request :delete_meta
request :delete_server
#request :detach_volume
#request :disassociate_address
#request :get_address
#request :get_console_output
#request :get_flavor_details
#request :get_image_details
#request :get_meta
#request :get_windows_password
request :get_server_details
#request :get_vnc_console
#request :list_addresses
#request :list_flavors
#request :list_flavors_detail
#request :list_images
#request :list_images_detail
#request :list_key_pairs
#request :list_metadata
#request :list_server_addresses
#request :list_server_private_addresses
#request :list_server_public_addresses
#request :list_server_volumes
request :list_servers
request :list_servers_detail
#request :reboot_server
#request :rebuild_server
#request :release_address
#request :resize_server
#request :revert_resized_server
#request :server_action
#request :set_metadata
#request :update_meta
#request :update_metadata
request :update_server
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
class Mock
include Utils
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {
:last_modified => {
:images => {},
:key_pairs => {},
:servers => {},
:addresses => {}
},
:images => {},
:key_pairs => {},
:servers => {},
:addresses => {}
}
end
end
def self.reset
@data = nil
end
def initialize(options={})
# 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
end
def data
self.class.data[@hp_access_key]
end
def reset_data
self.class.data.delete(@hp_access_key)
end
end
class Real
include Utils
attr_reader :credentials
def initialize(options={})
# 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
@hp_secret_key = options[:hp_secret_key]
@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 name for compute via the options hash
options[:hp_service_type] = "Compute"
@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]
@credentials = credentials
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 = uri.host
@path = uri.path
@persistent = options[:persistent] || false
@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',
'Accept' => '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)
}), &block)
rescue Excon::Errors::HTTPStatusError => error
raise case error
when Excon::Errors::NotFound
Fog::Compute::HP::NotFound.slurp(error)
else
error
end
end
if !response.body.empty? && parse_json && response.headers['Content-Type'] =~ %r{application/json}
response.body = Fog::JSON.decode(response.body)
end
response
end
end
end
end
end

View file

@ -0,0 +1,147 @@
module Fog
module Compute
class HPV2
class Real
# Create a new server
#
# ==== Parameters
# * name<~String> - Name of server
# * flavor_id<~Integer> - Id of flavor for server
# * image_id<~Integer> - Id of image for server. If block_device_mapping is passed, this is ignored.
# * options<~Hash>:
# * 'availability_zone'<~String> - Availability zone where the server should be created. Defaults to 'az2'.
# * 'metadata'<~Hash> - Up to 5 key value pairs containing 255 bytes of info
# * 'min_count'<~Integer> - Number of servers to create. Defaults to 1.
# * 'max_count'<~Integer> - Max. number of servers to create. Defaults to being equal to min_count.
# * 'key_name'<~String> - Name of keypair to be used
# * 'security_groups'<~Array> - one or more security groups to be used
# * 'personality'<~Array>: Up to 5 files to customize server
# * 'file'<~Hash>:
# * 'contents'<~String> - Contents of file (10kb total of contents)
# * 'path'<~String> - Path to file (255 bytes total of path strings)
# * 'accessIPv4'<~String> - IPv4 IP address
# * 'accessIPv6'<~String> - IPv6 IP address
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'server'<~Hash>:
# * 'addresses'<~Hash>:
# * 'private'<~Array> - private and public fixed and floating ip addresses
# * 'flavor'<~Hash>
# * 'id'<~String> - id of the flavor
# * 'links'<~Array> - array of flavor links
# * 'id'<~Integer> - id of server
# * 'image'<~Hash> - id of image used to boot server
# * 'id'<~String> - id of the image
# * 'links'<~Array> - array of image links
# * 'links'<~Array> - array of server links
# * 'hostId'<~String>
# * 'metadata'<~Hash> - metadata
# * 'name'<~String> - name of server
# * 'accessIPv4'<~String> - IPv4 ip address
# * 'accessIPv6'<~String> - IPv6 ip address
# * 'progress'<~Integer> - progress through current status
# * 'status'<~String> - current server status
# * 'created'<~String> - created date time stamp
# * 'updated'<~String> - updated date time stamp
# * 'user_id'<~String> - user id
# * 'tenant_id'<~String> - tenant id
# * 'uuid'<~String> - uuid of the server
# * 'config_drive'<~String> - config drive
# * 'security_groups'<~Array of Hash>
# * 'id'<~Integer> - id of the security group
# * 'name'<~String> - name of the security group
# * 'links'<~Array> - array of security group links
# * 'key_name'<~String> - name of the keypair
# * 'adminPass'<~String> - admin password for server
def create_server(name, flavor_id, image_id, options = {})
data = {
'server' => {
'flavorRef' => flavor_id,
'imageRef' => image_id,
'name' => name
}
}
l_options = ['availability_zone', 'metadata', 'accessIPv4', 'accessIPv6', 'key_name', 'config_drive', 'user_data']
l_options.select{|o| options[o]}.each do |key|
data['server'][key] = options[key]
end
if options['personality']
data['server']['personality'] = []
for file in options['personality']
data['server']['personality'] << {
'contents' => Base64.encode64(file['contents']),
'path' => file['path']
}
end
end
min_count = options['min_count'] || 1
max_count = options['max_count'] || min_count
data['server']['min_count'] = min_count
data['server']['max_count'] = max_count
if options['security_groups']
data['server']['security_groups'] = []
for sg in options['security_groups']
data['server']['security_groups'] << {
'name' => sg
}
end
end
request(
:body => Fog::JSON.encode(data),
:expects => 202,
:method => 'POST',
:path => 'servers'
)
end
end
class Mock
def create_server(name, flavor_id, image_id, options = {})
response = Excon::Response.new
response.status = 202
if options['security_groups']
sec_group_name = options['security_groups'][0]
else
sec_group_name = "default"
end
data = {
'addresses' => { "private"=>[{"version"=>4, "addr"=>Fog::HP::Mock.ip_address}] },
'flavor' => {"id"=>"#{flavor_id}", "links"=>[{"href"=>"http://nova1:8774/admin/flavors/#{flavor_id}", "rel"=>"bookmark"}]},
'id' => Fog::Mock.random_numbers(6).to_i,
'image' => {"id"=>"#{image_id}", "links"=>[{"href"=>"http://nova1:8774/admin/images/#{image_id}", "rel"=>"bookmark"}]},
'links' => [{"href"=>"http://nova1:8774/v1.1/admin/servers/5", "rel"=>"self"}, {"href"=>"http://nova1:8774/admin/servers/5", "rel"=>"bookmark"}],
'hostId' => "123456789ABCDEF01234567890ABCDEF",
'metadata' => options['metadata'] || {},
'name' => name || "server_#{rand(999)}",
'accessIPv4' => options['accessIPv4'] || "",
'accessIPv6' => options['accessIPv6'] || "",
'progress' => 0,
'status' => 'BUILD',
'created' => "2012-01-01T13:32:20Z",
'updated' => "2012-01-01T13:32:20Z",
'user_id' => Fog::HP::Mock.user_id.to_s,
'tenant_id' => Fog::HP::Mock.user_id.to_s,
'uuid' => "95253a45-9ead-43c6-90b3-65da2ef048b3",
'config_drive' => "",
'security_groups' => [{"name"=>"#{sec_group_name}", "links"=>[{"href"=>"http://nova1:8774/v1.1/admin//os-security-groups/111", "rel"=>"bookmark"}], "id"=>111}],
'key_name' => options['key_name'] || ""
}
self.data[:last_modified][:servers][data['id']] = Time.now
self.data[:servers][data['id']] = data
response.body = { 'server' => data.merge({'adminPass' => 'password'}) }
response
end
end
end
end
end

View file

@ -0,0 +1,43 @@
module Fog
module Compute
class HPV2
class Real
# Delete an existing server
#
# ==== Parameters
# * id<~Integer> - Id of server to delete
#
def delete_server(server_id)
request(
:expects => 204,
:method => 'DELETE',
:path => "servers/#{server_id}"
)
end
end
class Mock
def delete_server(server_id)
response = Excon::Response.new
if server = list_servers_detail.body['servers'].detect {|_| _['id'] == server_id}
if server['status'] == 'BUILD'
response.status = 409
raise(Excon::Errors.status_error({:expects => 202}, response))
else
self.data[:last_modified][:servers].delete(server_id)
self.data[:servers].delete(server_id)
response.status = 204
end
response
else
raise Fog::Compute::HP::NotFound
end
end
end
end
end
end

View file

@ -0,0 +1,52 @@
module Fog
module Compute
class HPV2
class Real
# Get details about a server
#
# ==== Parameters
# * server_id<~Integer> - Id of server to get details for
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'server'<~Hash>:
# * 'addresses'<~Hash>:
# * 'public'<~Array> - public address strings
# * 'private'<~Array> - private address strings
# * 'flavorId'<~Integer> - Id of servers current flavor
# * 'hostId'<~String>
# * 'id'<~Integer> - Id of server
# * 'imageId'<~Integer> - Id of image used to boot server
# * 'metadata'<~Hash> - metadata
# * 'name<~String> - Name of server
# * 'progress'<~Integer> - Progress through current status
# * 'status'<~String> - Current server status
def get_server_details(server_id)
request(
:expects => [200, 203],
:method => 'GET',
:path => "servers/#{server_id}"
)
end
end
class Mock
def get_server_details(server_id)
response = Excon::Response.new
if server = list_servers_detail.body['servers'].detect {|_| _['id'] == server_id}
response.status = [200, 203][rand(1)]
response.body = { 'server' => server }
response
else
raise Fog::Compute::HP::NotFound
end
end
end
end
end
end

View file

@ -0,0 +1,42 @@
module Fog
module Compute
class HPV2
class Real
# List all servers (IDs and names only)
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'servers'<~Array>:
# * 'id'<~Integer> - UUId of server
# * 'name'<~String> - Name of server
# * 'links'<~Array> - array of server links
def list_servers
request(
:expects => 200,
:method => 'GET',
:path => 'servers'
)
end
end
class Mock
def list_servers
response = Excon::Response.new
data = list_servers_detail.body['servers']
servers = []
for server in data
servers << server.reject { |key, value| !['id', 'name', 'links', 'uuid'].include?(key) }
end
response.status = 200
response.body = { 'servers' => servers }
response
end
end
end
end
end

View file

@ -0,0 +1,56 @@
module Fog
module Compute
class HPV2
class Real
# List all servers details
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Hash>:
# * 'servers'<~Array>:
# * 'id'<~Integer> - UUId of server
# * 'name<~String> - Name of server
# * 'imageId'<~Integer> - Id of image used to boot server
# * 'flavorId'<~Integer> - Id of servers current flavor
# * 'hostId'<~String>
# * 'status'<~String> - Current server status
# * 'progress'<~Integer> - Progress through current status
# * 'addresses'<~Hash>:
# * 'public'<~Array> - public address strings
# * 'private'<~Array> - private address strings
# * 'metadata'<~Hash> - metadata
def list_servers_detail
request(
:expects => 200,
:method => 'GET',
:path => 'servers/detail'
)
end
end
class Mock
def list_servers_detail
response = Excon::Response.new
servers = self.data[:servers].values
for server in servers
case server['status']
when 'BUILD'
if Time.now - self.data[:last_modified][:servers][server['id']] > Fog::Mock.delay * 2
server['status'] = 'ACTIVE'
end
end
end
response.status = 200
response.body = { 'servers' => servers }
response
end
end
end
end
end

View file

@ -0,0 +1,42 @@
module Fog
module Compute
class HPV2
class Real
# Update an existing server
#
# ==== Parameters
# # server_id<~Integer> - Id of server to update
# * options<~Hash>:
# * adminPass<~String> - New admin password for server
# * name<~String> - New name for server
def update_server(server_id, options = {})
request(
:body => Fog::JSON.encode({ 'server' => options }),
:expects => 200,
:method => 'PUT',
:path => "servers/#{server_id}"
)
end
end
class Mock
def update_server(server_id, options)
response = Excon::Response.new
if server = list_servers_detail.body['servers'].detect {|_| _['id'] == server_id}
if options['name']
server['name'] = options['name']
end
response.status = 200
response
else
raise Fog::Compute::HP::NotFound
end
end
end
end
end
end