2014-02-01 21:13:17 -05:00
|
|
|
require 'fog/joyent/core'
|
2012-05-15 14:54:20 -04:00
|
|
|
require 'fog/joyent/errors'
|
2013-05-19 09:28:16 -04:00
|
|
|
require 'net/ssh'
|
2012-02-10 10:10:49 -05:00
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class Joyent < Fog::Service
|
2012-02-21 03:17:54 -05:00
|
|
|
requires :joyent_username
|
2012-02-10 10:10:49 -05:00
|
|
|
|
2012-02-21 03:17:54 -05:00
|
|
|
recognizes :joyent_password
|
|
|
|
recognizes :joyent_url
|
2013-05-19 09:28:16 -04:00
|
|
|
|
2012-02-21 03:17:54 -05:00
|
|
|
recognizes :joyent_keyname
|
|
|
|
recognizes :joyent_keyfile
|
2013-12-11 22:28:24 -05:00
|
|
|
recognizes :joyent_keydata
|
2013-05-19 09:28:16 -04:00
|
|
|
recognizes :joyent_keyphrase
|
2013-05-31 21:01:42 -04:00
|
|
|
recognizes :joyent_version
|
2012-02-10 10:10:49 -05:00
|
|
|
|
2013-12-19 09:27:37 -05:00
|
|
|
secrets :joyent_password, :joyent_keydata, :joyent_keyphrase
|
|
|
|
|
2012-02-10 10:10:49 -05:00
|
|
|
model_path 'fog/joyent/models/compute'
|
|
|
|
request_path 'fog/joyent/requests/compute'
|
|
|
|
|
2012-04-10 03:43:26 -04:00
|
|
|
request :list_datacenters
|
2012-02-10 10:10:49 -05:00
|
|
|
# request :get_datacenter
|
|
|
|
|
2013-12-19 09:27:37 -05:00
|
|
|
# Datacenters
|
|
|
|
collection :datacenters
|
|
|
|
model :datacenter
|
|
|
|
|
2012-02-10 10:10:49 -05:00
|
|
|
# Keys
|
|
|
|
collection :keys
|
|
|
|
model :key
|
|
|
|
|
|
|
|
request :list_keys
|
|
|
|
request :get_key
|
|
|
|
request :create_key
|
|
|
|
request :delete_key
|
|
|
|
|
|
|
|
# Images
|
|
|
|
collection :images
|
|
|
|
model :image
|
|
|
|
request :list_datasets
|
|
|
|
request :get_dataset
|
2013-12-17 14:00:35 -05:00
|
|
|
request :list_images
|
|
|
|
request :get_image
|
2012-02-10 10:10:49 -05:00
|
|
|
|
|
|
|
# Flavors
|
|
|
|
collection :flavors
|
|
|
|
model :flavor
|
|
|
|
request :list_packages
|
|
|
|
request :get_package
|
|
|
|
|
|
|
|
# Servers
|
|
|
|
collection :servers
|
|
|
|
model :server
|
|
|
|
request :list_machines
|
|
|
|
request :get_machine
|
|
|
|
request :create_machine
|
|
|
|
request :start_machine
|
|
|
|
request :stop_machine
|
|
|
|
request :reboot_machine
|
|
|
|
request :resize_machine
|
|
|
|
request :delete_machine
|
|
|
|
|
|
|
|
# Snapshots
|
|
|
|
collection :snapshots
|
|
|
|
model :snapshot
|
|
|
|
request :create_machine_snapshot
|
|
|
|
request :start_machine_from_snapshot
|
|
|
|
request :list_machine_snapshots
|
|
|
|
request :get_machine_snapshot
|
|
|
|
request :delete_machine_snapshot
|
|
|
|
request :update_machine_metadata
|
|
|
|
request :get_machine_metadata
|
|
|
|
request :delete_machine_metadata
|
|
|
|
request :delete_all_machine_metadata
|
|
|
|
|
|
|
|
# MachineTags
|
|
|
|
request :add_machine_tags
|
|
|
|
request :list_machine_tags
|
|
|
|
request :get_machine_tag
|
|
|
|
request :delete_machine_tag
|
|
|
|
request :delete_all_machine_tags
|
|
|
|
|
2013-05-31 21:01:42 -04:00
|
|
|
# Networks
|
|
|
|
collection :networks
|
|
|
|
model :network
|
|
|
|
request :list_networks
|
2012-02-28 02:14:11 -05:00
|
|
|
|
2012-02-10 10:10:49 -05:00
|
|
|
class Mock
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
self.class.data
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(options = {})
|
2014-03-25 11:09:49 -04:00
|
|
|
@joyent_username = options[:joyent_username]
|
|
|
|
@joyent_password = options[:joyent_password]
|
2012-02-10 10:10:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def request(opts)
|
|
|
|
raise "Not Implemented"
|
|
|
|
end
|
|
|
|
end # Mock
|
|
|
|
|
|
|
|
class Real
|
2013-12-17 14:00:35 -05:00
|
|
|
attr_accessor :joyent_version
|
|
|
|
attr_accessor :joyent_url
|
|
|
|
|
2012-02-10 10:10:49 -05:00
|
|
|
def initialize(options = {})
|
|
|
|
@connection_options = options[:connection_options] || {}
|
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
|
2012-02-17 22:49:58 -05:00
|
|
|
@joyent_url = options[:joyent_url] || 'https://us-sw-1.api.joyentcloud.com'
|
|
|
|
@joyent_version = options[:joyent_version] || '~6.5'
|
|
|
|
@joyent_username = options[:joyent_username]
|
2012-02-10 10:10:49 -05:00
|
|
|
|
2012-02-17 22:49:58 -05:00
|
|
|
unless @joyent_username
|
|
|
|
raise ArgumentError, "options[:joyent_username] required"
|
2012-02-10 10:10:49 -05:00
|
|
|
end
|
|
|
|
|
2013-12-11 22:28:24 -05:00
|
|
|
if options[:joyent_keyname]
|
|
|
|
@joyent_keyname = options[:joyent_keyname]
|
|
|
|
@joyent_keyphrase = options[:joyent_keyphrase]
|
|
|
|
@key_manager = Net::SSH::Authentication::KeyManager.new(nil, {
|
2013-05-19 09:28:16 -04:00
|
|
|
:keys_only => true,
|
|
|
|
:passphrase => @joyent_keyphrase
|
2013-12-11 22:28:24 -05:00
|
|
|
})
|
|
|
|
@header_method = method(:header_for_signature_auth)
|
|
|
|
|
|
|
|
if options[:joyent_keyfile]
|
2014-05-21 10:24:38 -04:00
|
|
|
if File.exist?(options[:joyent_keyfile])
|
2013-12-11 22:28:24 -05:00
|
|
|
@joyent_keyfile = options[:joyent_keyfile]
|
|
|
|
@key_manager.add(@joyent_keyfile)
|
|
|
|
else
|
|
|
|
raise ArgumentError, "options[:joyent_keyfile] provided does not exist."
|
|
|
|
end
|
|
|
|
elsif options[:joyent_keydata]
|
|
|
|
if options[:joyent_keydata].to_s.empty?
|
|
|
|
raise ArgumentError, 'options[:joyent_keydata] must not be blank'
|
|
|
|
else
|
|
|
|
@joyent_keydata = options[:joyent_keydata]
|
|
|
|
@key_manager.add_key_data(@joyent_keydata)
|
|
|
|
end
|
2012-02-10 10:10:49 -05:00
|
|
|
end
|
2012-02-17 22:49:58 -05:00
|
|
|
elsif options[:joyent_password]
|
|
|
|
@joyent_password = options[:joyent_password]
|
2012-02-28 02:14:11 -05:00
|
|
|
@header_method = method(:header_for_basic_auth)
|
2012-02-10 10:10:49 -05:00
|
|
|
else
|
2012-02-17 22:49:58 -05:00
|
|
|
raise ArgumentError, "Must provide either a joyent_password or joyent_keyname and joyent_keyfile pair"
|
2012-02-10 10:10:49 -05:00
|
|
|
end
|
|
|
|
|
2014-02-26 19:50:35 -05:00
|
|
|
@connection = Fog::XML::Connection.new(
|
2012-02-17 22:49:58 -05:00
|
|
|
@joyent_url,
|
2012-02-10 10:10:49 -05:00
|
|
|
@persistent,
|
|
|
|
@connection_options
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-05-19 09:28:16 -04:00
|
|
|
def request(opts = {})
|
|
|
|
opts[:headers] = {
|
2012-02-17 22:49:58 -05:00
|
|
|
"X-Api-Version" => @joyent_version,
|
2012-02-10 10:10:49 -05:00
|
|
|
"Content-Type" => "application/json",
|
|
|
|
"Accept" => "application/json"
|
2013-05-19 09:28:16 -04:00
|
|
|
}.merge(opts[:headers] || {}).merge(@header_method.call)
|
2012-02-10 10:10:49 -05:00
|
|
|
|
2013-05-19 09:28:16 -04:00
|
|
|
if opts[:body]
|
|
|
|
opts[:body] = Fog::JSON.encode(opts[:body])
|
2012-02-10 10:10:49 -05:00
|
|
|
end
|
|
|
|
|
2013-05-19 09:28:16 -04:00
|
|
|
response = @connection.request(opts)
|
2012-02-10 10:10:49 -05:00
|
|
|
if response.headers["Content-Type"] == "application/json"
|
2012-02-28 02:14:11 -05:00
|
|
|
response.body = json_decode(response.body)
|
2012-02-10 10:10:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
response
|
2013-08-29 15:43:14 -04:00
|
|
|
rescue Excon::Errors::HTTPStatusError => e
|
2013-12-12 14:44:51 -05:00
|
|
|
if e.response.headers["Content-Type"] == "application/json"
|
|
|
|
e.response.body = json_decode(e.response.body)
|
|
|
|
end
|
2013-05-19 09:28:16 -04:00
|
|
|
raise_if_error!(e.request, e.response)
|
2012-02-10 10:10:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2012-02-28 02:14:11 -05:00
|
|
|
def json_decode(body)
|
2012-04-25 10:31:28 -04:00
|
|
|
parsed = Fog::JSON.decode(body)
|
2012-02-28 02:14:11 -05:00
|
|
|
decode_time_attrs(parsed)
|
|
|
|
end
|
|
|
|
|
|
|
|
def header_for_basic_auth
|
2012-02-10 10:10:49 -05:00
|
|
|
{
|
2012-02-17 22:49:58 -05:00
|
|
|
"Authorization" => "Basic #{Base64.encode64("#{@joyent_username}:#{@joyent_password}").delete("\r\n")}"
|
2012-02-10 10:10:49 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-02-28 02:14:11 -05:00
|
|
|
def header_for_signature_auth
|
2012-02-10 10:10:49 -05:00
|
|
|
date = Time.now.utc.httpdate
|
2013-05-19 09:28:16 -04:00
|
|
|
|
|
|
|
# Force KeyManager to load the key(s)
|
|
|
|
@key_manager.each_identity {}
|
|
|
|
|
|
|
|
key = @key_manager.known_identities.keys.first
|
|
|
|
|
2013-05-20 19:34:10 -04:00
|
|
|
sig = if key.kind_of? OpenSSL::PKey::RSA
|
|
|
|
@key_manager.sign(key, date)[15..-1]
|
2013-05-19 09:28:16 -04:00
|
|
|
else
|
|
|
|
key = OpenSSL::PKey::DSA.new(File.read(@joyent_keyfile), @joyent_keyphrase)
|
2013-05-20 19:34:10 -04:00
|
|
|
key.sign('sha1', date)
|
2012-05-02 03:19:00 -04:00
|
|
|
end
|
2013-05-19 09:28:16 -04:00
|
|
|
|
2012-02-17 22:49:58 -05:00
|
|
|
key_id = "/#{@joyent_username}/keys/#{@joyent_keyname}"
|
2013-05-19 09:28:16 -04:00
|
|
|
key_type = key.class.to_s.split('::').last.downcase.to_sym
|
|
|
|
|
|
|
|
unless [:rsa, :dsa].include? key_type
|
|
|
|
raise Joyent::Errors::Unauthorized.new('Invalid key type -- only rsa or dsa key is supported')
|
|
|
|
end
|
|
|
|
|
2013-05-20 19:34:10 -04:00
|
|
|
signature = Base64.encode64(sig).delete("\r\n")
|
|
|
|
|
2012-02-10 10:10:49 -05:00
|
|
|
{
|
|
|
|
"Date" => date,
|
2013-05-19 09:28:16 -04:00
|
|
|
"Authorization" => "Signature keyId=\"#{key_id}\",algorithm=\"#{key_type}-sha1\" #{signature}"
|
2012-02-10 10:10:49 -05:00
|
|
|
}
|
2013-05-19 09:28:16 -04:00
|
|
|
rescue Net::SSH::Authentication::KeyManagerError => e
|
|
|
|
raise Joyent::Errors::Unauthorized.new('SSH Signing Error: :#{e.message}', e)
|
2012-02-10 10:10:49 -05:00
|
|
|
end
|
|
|
|
|
2012-02-28 02:14:11 -05:00
|
|
|
def decode_time_attrs(obj)
|
2012-02-21 04:29:36 -05:00
|
|
|
if obj.kind_of?(Hash)
|
2013-05-14 05:35:02 -04:00
|
|
|
obj["created"] = Time.parse(obj["created"]) unless obj["created"].nil? or obj["created"] == ''
|
|
|
|
obj["updated"] = Time.parse(obj["updated"]) unless obj["updated"].nil? or obj["updated"] == ''
|
2012-02-21 04:29:36 -05:00
|
|
|
elsif obj.kind_of?(Array)
|
|
|
|
obj.map do |o|
|
2012-02-28 02:14:11 -05:00
|
|
|
decode_time_attrs(o)
|
2012-02-21 04:29:36 -05:00
|
|
|
end
|
|
|
|
end
|
2012-02-28 02:14:11 -05:00
|
|
|
|
2012-02-21 04:29:36 -05:00
|
|
|
obj
|
|
|
|
end
|
2012-02-28 02:14:11 -05:00
|
|
|
|
|
|
|
def raise_if_error!(request, response)
|
|
|
|
case response.status
|
|
|
|
when 401 then
|
2013-05-06 13:03:36 -04:00
|
|
|
raise Joyent::Errors::Unauthorized.new('Invalid credentials were used', request, response)
|
2012-02-28 02:14:11 -05:00
|
|
|
when 403 then
|
2013-05-06 13:03:36 -04:00
|
|
|
raise Joyent::Errors::Forbidden.new('No permissions to the specified resource', request, response)
|
2012-02-28 02:14:11 -05:00
|
|
|
when 404 then
|
2013-05-06 13:03:36 -04:00
|
|
|
raise Joyent::Errors::NotFound.new('Requested resource was not found', request, response)
|
2012-02-28 02:14:11 -05:00
|
|
|
when 405 then
|
2013-05-06 13:03:36 -04:00
|
|
|
raise Joyent::Errors::MethodNotAllowed.new('Method not supported for the given resource', request, response)
|
2012-02-28 02:14:11 -05:00
|
|
|
when 406 then
|
2013-05-06 13:03:36 -04:00
|
|
|
raise Joyent::Errors::NotAcceptable.new('Try sending a different Accept header', request, response)
|
2012-02-28 02:14:11 -05:00
|
|
|
when 409 then
|
2013-05-06 13:03:36 -04:00
|
|
|
raise Joyent::Errors::Conflict.new('Most likely invalid or missing parameters', request, response)
|
2012-02-28 02:14:11 -05:00
|
|
|
when 414 then
|
2013-05-06 13:03:36 -04:00
|
|
|
raise Joyent::Errors::RequestEntityTooLarge.new('You sent too much data', request, response)
|
2012-02-28 02:14:11 -05:00
|
|
|
when 415 then
|
2013-05-06 13:03:36 -04:00
|
|
|
raise Joyent::Errors::UnsupportedMediaType.new('You encoded your request in a format we don\'t understand', request, response)
|
2012-02-28 02:14:11 -05:00
|
|
|
when 420 then
|
2013-05-06 13:03:36 -04:00
|
|
|
raise Joyent::Errors::PolicyNotForfilled.new('You are sending too many requests', request, response)
|
2012-02-28 02:14:11 -05:00
|
|
|
when 449 then
|
2013-05-06 13:03:36 -04:00
|
|
|
raise Joyent::Errors::RetryWith.new('Invalid API Version requested; try with a different API Version', request, response)
|
2012-02-28 02:14:11 -05:00
|
|
|
when 503 then
|
2013-05-06 13:03:36 -04:00
|
|
|
raise Joyent::Errors::ServiceUnavailable.new('Either there\'s no capacity in this datacenter, or we\'re in a maintenance window', request, response)
|
2012-02-28 02:14:11 -05:00
|
|
|
end
|
|
|
|
end
|
2012-02-10 10:10:49 -05:00
|
|
|
end # Real
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|