2013-01-29 15:25:06 -05:00
|
|
|
require 'fog/digitalocean'
|
|
|
|
require 'fog/compute'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class DigitalOcean < Fog::Service
|
|
|
|
|
|
|
|
requires :digitalocean_api_key
|
|
|
|
requires :digitalocean_client_id
|
|
|
|
|
|
|
|
recognizes :digitalocean_api_url
|
|
|
|
|
|
|
|
model_path 'fog/digitalocean/models/compute'
|
|
|
|
model :server
|
|
|
|
collection :servers
|
|
|
|
model :flavor
|
|
|
|
collection :flavors
|
|
|
|
model :image
|
|
|
|
collection :images
|
|
|
|
model :region
|
|
|
|
collection :regions
|
2013-03-25 17:56:30 -04:00
|
|
|
model :ssh_key
|
|
|
|
collection :ssh_keys
|
2013-01-29 15:25:06 -05:00
|
|
|
|
|
|
|
request_path 'fog/digitalocean/requests/compute'
|
|
|
|
request :list_servers
|
|
|
|
request :list_images
|
|
|
|
request :list_regions
|
|
|
|
request :list_flavors
|
|
|
|
request :get_server_details
|
|
|
|
request :create_server
|
|
|
|
request :destroy_server
|
2013-02-06 15:23:33 -05:00
|
|
|
request :reboot_server
|
2013-02-06 15:35:53 -05:00
|
|
|
request :power_cycle_server
|
2013-02-26 14:09:45 -05:00
|
|
|
request :power_off_server
|
|
|
|
request :power_on_server
|
|
|
|
request :shutdown_server
|
2013-03-04 06:39:57 -05:00
|
|
|
request :list_ssh_keys
|
|
|
|
request :create_ssh_key
|
2013-03-25 17:56:30 -04:00
|
|
|
request :get_ssh_key
|
|
|
|
request :destroy_ssh_key
|
2013-01-29 15:25:06 -05:00
|
|
|
|
|
|
|
# request :digitalocean_resize
|
|
|
|
|
|
|
|
class Mock
|
|
|
|
|
|
|
|
def self.data
|
|
|
|
@data ||= Hash.new do |hash, key|
|
2013-03-18 07:47:21 -04:00
|
|
|
hash[key] = {
|
|
|
|
:servers => [],
|
|
|
|
:ssh_keys => []
|
|
|
|
}
|
2013-01-29 15:25:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
@digitalocean_api_key = options[:digitalocean_api_key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
|
|
|
self.class.data[@digitalocean_api_key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data.delete(@digitalocean_api_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
@digitalocean_api_key = options[:digitalocean_api_key]
|
|
|
|
@digitalocean_client_id = options[:digitalocean_client_id]
|
|
|
|
@digitalocean_api_url = options[:digitalocean_api_url] || \
|
|
|
|
"https://api.digitalocean.com"
|
|
|
|
@connection = Fog::Connection.new(@digitalocean_api_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(params)
|
|
|
|
params[:query] ||= {}
|
|
|
|
params[:query].merge!(:api_key => @digitalocean_api_key)
|
|
|
|
params[:query].merge!(:client_id => @digitalocean_client_id)
|
|
|
|
|
2013-07-10 20:41:12 -04:00
|
|
|
response = retry_event_lock { parse @connection.request(params) }
|
2013-01-29 15:25:06 -05:00
|
|
|
|
|
|
|
unless response.body.empty?
|
|
|
|
if response.body['status'] != 'OK'
|
2013-07-10 20:41:12 -04:00
|
|
|
case response.body['error_message']
|
|
|
|
when /No Droplets Found/
|
|
|
|
raise Fog::Errors::NotFound.new
|
|
|
|
else
|
|
|
|
raise Fog::Errors::Error.new response.body.to_s
|
|
|
|
end
|
2013-01-29 15:25:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
2013-07-10 20:41:12 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def parse(response)
|
|
|
|
return response if response.body.empty?
|
|
|
|
response.body = Fog::JSON.decode(response.body)
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
def retry_event_lock
|
|
|
|
count = 0
|
|
|
|
reponse = nil
|
|
|
|
while count < 5
|
|
|
|
response = yield
|
|
|
|
|
|
|
|
if response.body && response.body['error_message'] =~ /There is already a pending event for the droplet/
|
|
|
|
count += 1
|
|
|
|
sleep count ** 3
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
2013-01-29 15:25:06 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|