2012-07-30 16:01:23 -04:00
require 'fog/compute'
2013-02-28 15:57:09 -05:00
require 'fog/rackspace/service'
2012-07-30 16:01:23 -04:00
module Fog
module Compute
class RackspaceV2 < Fog :: Service
class ServiceError < Fog :: Rackspace :: Errors :: ServiceError ; end
class InternalServerError < Fog :: Rackspace :: Errors :: InternalServerError ; end
class BadRequest < Fog :: Rackspace :: Errors :: BadRequest ; end
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
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
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
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
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-02-28 15:57:09 -05:00
class Mock < Fog :: Rackspace :: Service
2013-02-20 09:14:34 -05: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-02-20 09:14:34 -05: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 )
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
def request ( params )
begin
response = @connection . request ( params . merge! ( {
:headers = > {
'Content-Type' = > 'application/json' ,
2013-03-01 17:59:59 -05:00
'Accept' = > 'application/json' ,
2013-03-01 15:48:26 -05:00
'X-Auth-Token' = > auth_token
2012-07-30 16:01:23 -04:00
} . merge! ( params [ :headers ] || { } ) ,
2013-03-15 10:11:39 -04:00
:host = > endpoint_uri . host ,
2013-02-21 14:07:29 -05:00
:path = > " #{ endpoint_uri . path } / #{ params [ :path ] } "
2012-07-30 16:01:23 -04:00
} ) )
rescue Excon :: Errors :: NotFound = > error
raise NotFound . slurp error
rescue Excon :: Errors :: BadRequest = > error
raise BadRequest . slurp error
rescue Excon :: Errors :: InternalServerError = > error
raise InternalServerError . slurp error
rescue Excon :: Errors :: HTTPStatusError = > error
raise ServiceError . slurp error
end
2013-01-31 21:50:58 -05:00
2012-07-30 16:01:23 -04:00
unless response . body . empty?
2013-01-31 21:50:58 -05:00
begin
response . body = Fog :: JSON . decode ( response . body )
rescue MultiJson :: DecodeError = > e
response . body = { }
end
2012-07-30 16:01:23 -04:00
end
response
end
2013-02-20 16:48:19 -05:00
2012-07-30 16:01:23 -04:00
def authenticate
options = {
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-02-21 14:07:29 -05:00
:rackspace_auth_url = > @rackspace_auth_url
2012-07-30 16:01:23 -04:00
}
2013-03-01 15:48:26 -05:00
super ( options )
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
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-15 12:55:40 -04:00
@rackspace_endpoint = options [ :rackspace_compute_url ] || options [ :rackspace_endpoint ]
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 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-02-20 09:14:34 -05: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