2013-06-14 01:55:06 -04:00
require 'fog/rackspace'
2012-07-30 16:01:23 -04:00
require 'fog/compute'
module Fog
module Compute
class RackspaceV2 < Fog :: Service
2013-04-15 10:31:13 -04:00
include Fog :: Rackspace :: Errors
2012-07-30 16:01:23 -04:00
class ServiceError < Fog :: Rackspace :: Errors :: ServiceError ; end
class InternalServerError < Fog :: Rackspace :: Errors :: InternalServerError ; end
class BadRequest < Fog :: Rackspace :: Errors :: BadRequest ; end
2013-03-26 10:14:34 -04:00
class InvalidStateException < :: RuntimeError
attr_reader :desired_state
attr_reader :current_state
def initialize ( desired_state , current_state )
@desired_state = desired_state
@current_state = current_state
end
end
class InvalidServerStateException < InvalidStateException
def to_s
2013-04-17 11:18:47 -04:00
" Server should have transitioned to ' #{ desired_state } ' not ' #{ current_state } ' "
2013-03-26 10:14:34 -04:00
end
end
class InvalidImageStateException < InvalidStateException
def to_s
2013-04-17 11:18:47 -04:00
" Image should have transitioned to ' #{ desired_state } ' not ' #{ current_state } ' "
2013-03-26 10:14:34 -04:00
end
2013-04-22 18:16:32 -04:00
end
2013-03-26 10:14:34 -04:00
2012-07-30 16:50:16 -04:00
DFW_ENDPOINT = 'https://dfw.servers.api.rackspacecloud.com/v2'
ORD_ENDPOINT = 'https://ord.servers.api.rackspacecloud.com/v2'
LON_ENDPOINT = 'https://lon.servers.api.rackspacecloud.com/v2'
2012-07-30 16:01:23 -04:00
requires :rackspace_username , :rackspace_api_key
recognizes :rackspace_endpoint
recognizes :rackspace_auth_url
recognizes :rackspace_auth_token
2013-02-20 09:14:34 -05:00
recognizes :rackspace_region
2013-02-20 16:48:19 -05:00
recognizes :rackspace_compute_url
2012-07-30 16:01:23 -04:00
model_path 'fog/rackspace/models/compute_v2'
2012-07-30 18:22:16 -04:00
model :server
collection :servers
2012-07-30 16:50:16 -04:00
model :flavor
collection :flavors
model :image
collection :images
2013-01-31 22:18:37 -05:00
model :attachment
2012-08-28 17:10:47 -04:00
collection :attachments
2013-01-31 22:17:51 -05:00
model :network
collection :networks
2013-07-31 15:12:43 -04:00
model :key_pair
collection :key_pairs
2012-07-30 16:01:23 -04:00
request_path 'fog/rackspace/requests/compute_v2'
2012-07-30 18:22:16 -04:00
request :list_servers
request :get_server
request :create_server
request :update_server
request :delete_server
request :change_server_password
request :reboot_server
request :rebuild_server
request :resize_server
request :confirm_resize_server
request :revert_resize_server
2013-04-13 23:53:26 -04:00
request :rescue_server
request :unrescue_server
2012-12-20 18:42:04 -05:00
request :list_addresses
2012-12-21 11:13:25 -05:00
request :list_addresses_by_network
2012-07-30 18:22:16 -04:00
2012-12-11 09:35:06 -05:00
request :create_image
2012-07-30 16:50:16 -04:00
request :list_images
2013-08-26 19:42:14 -04:00
request :list_images_detail
2012-07-30 16:50:16 -04:00
request :get_image
2012-12-11 09:35:06 -05:00
request :delete_image
2012-07-30 16:50:16 -04:00
request :list_flavors
2013-08-26 14:59:18 -04:00
request :list_flavors_detail
2012-07-30 16:50:16 -04:00
request :get_flavor
2012-07-30 16:01:23 -04:00
2012-08-28 17:10:47 -04:00
request :attach_volume
request :get_attachment
request :list_attachments
request :delete_attachment
2013-01-31 22:18:37 -05:00
2013-01-02 16:39:44 -05:00
request :list_metadata
request :set_metadata
request :update_metadata
request :get_metadata_item
request :set_metadata_item
request :delete_metadata_item
2012-08-28 17:10:47 -04:00
2013-01-31 21:49:29 -05:00
request :list_networks
request :get_network
request :create_network
request :delete_network
2013-07-17 03:18:27 -04:00
request :list_keypairs
request :create_keypair
request :delete_keypair
request :get_keypair
2013-02-28 15:57:09 -05:00
class Mock < Fog :: Rackspace :: Service
2013-05-05 14:38:54 -04:00
include Fog :: Rackspace :: MockData
2013-01-11 16:06:58 -05:00
2012-12-12 09:56:16 -05:00
def initialize ( options )
2013-01-11 16:06:58 -05:00
@rackspace_api_key = options [ :rackspace_api_key ]
2012-12-12 09:56:16 -05:00
end
2013-01-11 16:06:58 -05:00
2012-07-30 16:01:23 -04:00
def request ( params )
Fog :: Mock . not_implemented
end
2013-01-11 16:06:58 -05:00
def response ( params = { } )
body = params [ :body ] || { }
status = params [ :status ] || 200
headers = params [ :headers ] || { }
response = Excon :: Response . new ( :body = > body , :headers = > headers , :status = > status )
if params . has_key? ( :expects ) && ! [ * params [ :expects ] ] . include? ( response . status )
raise ( Excon :: Errors . status_error ( params , response ) )
else response
end
end
2012-07-30 16:01:23 -04:00
end
2013-02-28 15:57:09 -05:00
class Real < Fog :: Rackspace :: Service
2013-05-05 14:38:54 -04:00
2012-07-30 16:01:23 -04:00
def initialize ( options = { } )
@rackspace_api_key = options [ :rackspace_api_key ]
@rackspace_username = options [ :rackspace_username ]
@rackspace_auth_url = options [ :rackspace_auth_url ]
2013-03-15 10:11:39 -04:00
setup_custom_endpoint ( options )
2012-07-30 16:01:23 -04:00
@rackspace_must_reauthenticate = false
@connection_options = options [ :connection_options ] || { }
2013-02-21 14:07:29 -05:00
authenticate
2012-07-30 16:01:23 -04:00
2013-02-20 16:48:19 -05:00
deprecation_warnings ( options )
2013-05-05 14:38:54 -04:00
2012-07-30 16:01:23 -04:00
@persistent = options [ :persistent ] || false
2013-03-01 15:46:14 -05:00
@connection = Fog :: Connection . new ( endpoint_uri . to_s , @persistent , @connection_options )
2013-02-20 16:48:19 -05:00
end
2012-07-30 16:01:23 -04:00
2013-09-24 13:02:10 -04:00
def request ( params , parse_json = true )
super
2013-06-24 11:13:57 -04:00
rescue Excon :: Errors :: NotFound = > error
2013-08-01 15:31:51 -04:00
raise NotFound . slurp ( error , self )
2013-06-24 11:13:57 -04:00
rescue Excon :: Errors :: BadRequest = > error
2013-08-01 15:31:51 -04:00
raise BadRequest . slurp ( error , self )
2013-06-24 11:13:57 -04:00
rescue Excon :: Errors :: InternalServerError = > error
2013-08-01 15:31:51 -04:00
raise InternalServerError . slurp ( error , self )
2013-06-24 11:13:57 -04:00
rescue Excon :: Errors :: HTTPStatusError = > error
2013-08-01 15:31:51 -04:00
raise ServiceError . slurp ( error , self )
2012-07-30 16:01:23 -04:00
end
2013-05-05 14:38:54 -04:00
2013-06-21 16:11:17 -04:00
def authenticate ( options = { } )
super ( {
2013-02-28 15:57:09 -05:00
:rackspace_api_key = > @rackspace_api_key ,
2012-07-30 16:01:23 -04:00
:rackspace_username = > @rackspace_username ,
2013-05-30 13:52:37 -04:00
:rackspace_auth_url = > @rackspace_auth_url ,
:connection_options = > @connection_options
2013-06-21 16:11:17 -04:00
} )
2013-02-28 15:57:09 -05:00
end
2013-02-20 09:14:34 -05:00
2013-02-28 15:57:09 -05:00
def service_name
:cloudServersOpenStack
end
2013-08-01 15:31:51 -04:00
def request_id_header
" X-Compute-Request-Id "
end
2013-02-28 15:57:09 -05:00
def region
@rackspace_region
end
def endpoint_uri ( service_endpoint_url = nil )
@uri = super ( @rackspace_endpoint || service_endpoint_url , :rackspace_compute_url )
end
2013-03-01 17:47:22 -05:00
private
2013-03-15 10:11:39 -04:00
def setup_custom_endpoint ( options )
2013-03-20 14:25:38 -04:00
@rackspace_endpoint = Fog :: Rackspace . normalize_url ( options [ :rackspace_compute_url ] || options [ :rackspace_endpoint ] )
2013-03-15 12:55:40 -04:00
if v2_authentication?
case @rackspace_endpoint
when DFW_ENDPOINT
@rackspace_endpoint = nil
@rackspace_region = :dfw
when ORD_ENDPOINT
@rackspace_endpoint = nil
@rackspace_region = :ord
when LON_ENDPOINT
@rackspace_endpoint = nil
@rackspace_region = :lon
else
# we are actually using a custom endpoint
@rackspace_region = options [ :rackspace_region ] || :dfw
end
2013-03-15 17:38:33 -04:00
else
#if we are using auth1 and the endpoint is not set, default to DFW_ENDPOINT for historical reasons
@rackspace_endpoint || = DFW_ENDPOINT
2013-03-15 10:11:39 -04:00
end
end
2013-03-01 17:47:22 -05:00
def deprecation_warnings ( options )
Fog :: Logger . deprecation ( " The :rackspace_endpoint option is deprecated. Please use :rackspace_compute_url for custom endpoints " ) if options [ :rackspace_endpoint ]
2013-03-15 12:55:40 -04:00
if [ DFW_ENDPOINT , ORD_ENDPOINT , LON_ENDPOINT ] . include? ( @rackspace_endpoint ) && v2_authentication?
2013-03-15 10:11:39 -04:00
regions = @identity_service . service_catalog . display_service_regions ( service_name )
Fog :: Logger . deprecation ( " Please specify region using :rackspace_region rather than :rackspace_endpoint. Valid regions for :rackspace_region are #{ regions } . " )
2013-03-01 17:47:22 -05:00
end
end
2013-03-15 10:11:39 -04:00
def append_tenant_v1 ( credentials )
2012-07-30 16:01:23 -04:00
account_id = credentials [ 'X-Server-Management-Url' ] . match ( / .* \/ ([ \ d]+)$ / ) [ 1 ]
2013-05-05 14:38:54 -04:00
2013-02-28 15:57:09 -05:00
endpoint = @rackspace_endpoint || credentials [ 'X-Server-Management-Url' ] || DFW_ENDPOINT
2013-02-20 16:48:19 -05:00
@uri = URI . parse ( endpoint )
@uri . path = " #{ @uri . path } / #{ account_id } "
2013-02-28 15:57:09 -05:00
end
def authenticate_v1 ( options )
credentials = Fog :: Rackspace . authenticate ( options , @connection_options )
2013-03-15 10:11:39 -04:00
append_tenant_v1 credentials
2013-03-01 15:48:26 -05:00
@auth_token = credentials [ 'X-Auth-Token' ]
2012-07-30 16:01:23 -04:00
end
end
end
end
end