2011-04-16 20:43:30 -04:00
|
|
|
module Fog
|
2011-06-16 19:28:54 -04:00
|
|
|
module Compute
|
|
|
|
class Ninefold < Fog::Service
|
2011-04-16 20:43:30 -04:00
|
|
|
|
|
|
|
API_URL = "http://api.ninefold.com/compute/v1.0/"
|
|
|
|
|
|
|
|
requires :ninefold_compute_key, :ninefold_compute_secret
|
|
|
|
|
|
|
|
model_path 'fog/compute/models/ninefold'
|
2011-05-11 21:35:02 -04:00
|
|
|
model :server
|
|
|
|
collection :servers
|
|
|
|
model :flavor
|
|
|
|
collection :flavors
|
|
|
|
model :image
|
|
|
|
collection :images
|
2011-05-11 22:47:30 -04:00
|
|
|
model :address
|
|
|
|
collection :addresses
|
|
|
|
model :ip_forwarding_rule
|
|
|
|
collection :ip_forwarding_rules
|
2011-04-16 20:43:30 -04:00
|
|
|
|
|
|
|
request_path 'fog/compute/requests/ninefold'
|
2011-05-09 20:22:23 -04:00
|
|
|
# General list-only stuff
|
2011-05-05 18:59:50 -04:00
|
|
|
request :list_accounts
|
|
|
|
request :list_events
|
|
|
|
request :list_service_offerings
|
|
|
|
request :list_disk_offerings
|
|
|
|
request :list_capabilities
|
|
|
|
request :list_hypervisors
|
|
|
|
request :list_zones
|
|
|
|
request :list_network_offerings
|
|
|
|
request :list_resource_limits
|
2011-05-09 20:22:23 -04:00
|
|
|
# Templates
|
|
|
|
request :list_templates
|
|
|
|
# Virtual Machines
|
|
|
|
request :deploy_virtual_machine
|
|
|
|
request :destroy_virtual_machine
|
|
|
|
request :list_virtual_machines
|
|
|
|
request :reboot_virtual_machine
|
|
|
|
request :stop_virtual_machine
|
|
|
|
request :start_virtual_machine
|
|
|
|
request :change_service_for_virtual_machine
|
|
|
|
request :reset_password_for_virtual_machine
|
|
|
|
request :update_virtual_machine
|
|
|
|
# Jobs
|
|
|
|
request :list_async_jobs
|
|
|
|
request :query_async_job_result
|
|
|
|
# Networks
|
|
|
|
request :list_networks
|
2011-05-11 13:59:56 -04:00
|
|
|
# Addresses
|
|
|
|
request :associate_ip_address
|
|
|
|
request :list_public_ip_addresses
|
|
|
|
request :disassociate_ip_address
|
2011-05-11 18:59:08 -04:00
|
|
|
# NAT
|
|
|
|
request :enable_static_nat
|
|
|
|
request :disable_static_nat
|
|
|
|
request :create_ip_forwarding_rule
|
|
|
|
request :delete_ip_forwarding_rule
|
|
|
|
request :list_ip_forwarding_rules
|
2011-04-16 20:43:30 -04:00
|
|
|
|
|
|
|
class Mock
|
|
|
|
|
|
|
|
def initialize(options)
|
2011-05-29 20:03:45 -04:00
|
|
|
require "json"
|
|
|
|
|
|
|
|
@api_url = options[:ninefold_api_url] || Fog.credentials[:ninefold_api_url] || API_URL
|
|
|
|
@ninefold_compute_key = options[:ninefold_compute_key] || Fog.credentials[:ninefold_compute_key]
|
|
|
|
@ninefold_compute_secret = options[:ninefold_compute_secret] || Fog.credentials[:ninefold_compute_secret]
|
|
|
|
@connection = Fog::Connection.new(@api_url)
|
2011-04-16 20:43:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def request(options)
|
|
|
|
raise "Not implemented"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
|
|
|
def initialize(options)
|
|
|
|
require "json"
|
|
|
|
|
|
|
|
@api_url = options[:ninefold_api_url] || Fog.credentials[:ninefold_api_url] || API_URL
|
|
|
|
@ninefold_compute_key = options[:ninefold_compute_key] || Fog.credentials[:ninefold_compute_key]
|
|
|
|
@ninefold_compute_secret = options[:ninefold_compute_secret] || Fog.credentials[:ninefold_compute_secret]
|
|
|
|
@connection = Fog::Connection.new(@api_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(command, params, options)
|
2011-05-09 20:22:23 -04:00
|
|
|
params['response'] = "json"
|
2011-04-16 20:43:30 -04:00
|
|
|
req = "apiKey=#{@ninefold_compute_key}&command=#{command}&"
|
2011-05-09 20:22:23 -04:00
|
|
|
# convert params to strings for sort
|
|
|
|
req += URI.escape(params.sort_by{|k,v| k.to_s }.collect{|e| "#{e[0].to_s}=#{e[1].to_s}"}.join('&'))
|
2011-04-16 20:43:30 -04:00
|
|
|
encoded_signature = url_escape(encode_signature(req))
|
|
|
|
|
|
|
|
options = {
|
|
|
|
:expects => 200,
|
|
|
|
:method => 'GET',
|
|
|
|
:query => "#{req}&signature=#{encoded_signature}"
|
|
|
|
}.merge(options)
|
|
|
|
|
|
|
|
begin
|
|
|
|
response = @connection.request(options)
|
|
|
|
end
|
|
|
|
unless response.body.empty?
|
2011-05-05 18:59:50 -04:00
|
|
|
# Because the response is some weird xml-json thing, we need to try and mung
|
|
|
|
# the values out with a prefix, and if there is an empty data entry return an
|
|
|
|
# empty version of the expected type (if provided)
|
2011-04-16 20:43:30 -04:00
|
|
|
response = JSON.parse(response.body)
|
2011-05-05 18:59:50 -04:00
|
|
|
if options.has_key? :response_prefix
|
|
|
|
keys = options[:response_prefix].split('/')
|
|
|
|
keys.each do |k|
|
|
|
|
if response[k]
|
|
|
|
response = response[k]
|
|
|
|
elsif options[:response_type]
|
2011-05-09 20:22:23 -04:00
|
|
|
response = options[:response_type].new
|
2011-05-05 18:59:50 -04:00
|
|
|
break
|
|
|
|
else
|
|
|
|
end
|
|
|
|
end
|
|
|
|
response
|
|
|
|
else
|
|
|
|
response
|
|
|
|
end
|
2011-04-16 20:43:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def url_escape(string)
|
|
|
|
string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
|
|
|
|
'%' + $1.unpack('H2' * $1.size).join('%').upcase
|
|
|
|
end.tr(' ', '+')
|
|
|
|
end
|
|
|
|
|
|
|
|
def encode_signature(data)
|
|
|
|
Base64.encode64(OpenSSL::HMAC.digest('sha1', @ninefold_compute_secret, URI.encode(data.downcase).gsub('+', '%20'))).chomp
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|