1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/brightbox/compute.rb

135 lines
4.2 KiB
Ruby
Raw Normal View History

module Fog
module Brightbox
class Compute < Fog::Service
API_URL = "https://api.gb1.brightbox.com/"
requires :brightbox_client_id, :brightbox_secret
This is a combination of 23 commits (included merges from upstream); this commit(s) include changes to enforces recognizes/requires parameters for all supported services. Comments from the included commits follow: - Added google_storage_* keys - Fixed indentations. - Factored out requires and recognizes method implementation (now relies on the requires and recognizes clause from the NamedParameters module) - Added dependency to named-parameters gem. - Added recognizes declaration to classes for all supported services to enforce parameter name checks - - passing an unrecognized key when instantiating a service object will now cause an ArgumentError to be raised. - Added NOTE - comment added - check/filter-out keys from credentials that are not required by the class being instantiated - [local|storage] properly write out file contents - Added google_storage_* keys - Fixed indentations. - added put_object_acl request (ref: https://github.com/geemus/fog/issues#issue/74) - Release 0.3.24 - remove tracker reference from README - issues is now the goto for bugs/todo - notify and gracefully skip credential-less testsa - [rackspace|storage] fixes for directory/files - [local|storage] CGI.escape file names - Release 0.3.25 - updated deps; recognized_parameters -> declared_parameters; restored options filtering if Fog.bin - Added requires/recognizes to Fog::Terremark::Ecloud - Updted to use latest named-parameters gem. - Filter out unwanted parameters when Fog.bin - Updated to latest named-parameters gem - commented out unnecessary code - fix missing "volume" parameter error when setting Fog::AWS::Volume#server to nil (in order to detach it) - documentation update for key_pairs and helper - [aws|compute] commented/documented flavors/volumes - Fixes for issue 38 and 39 Closes #96
2010-11-21 05:56:41 -05:00
recognizes :brightbox_auth_url, :brightbox_api_url
model_path 'fog/brightbox/models/compute'
model :account # Singular resource, no collection
collection :servers
model :server
collection :flavors
model :flavor
collection :images
model :image
collection :zones
model :zone
collection :cloud_ips
model :cloud_ip
collection :users
model :user
request_path 'fog/brightbox/requests/compute'
request :create_api_client
request :create_cloud_ip
request :create_image
request :create_server
request :destroy_api_client
request :destroy_cloud_ip
request :destroy_image
request :destroy_server
request :get_account
request :get_api_client
request :get_cloud_ip
request :get_image
request :get_interface
request :get_server
request :get_server_type
request :get_user
request :get_zone
request :list_api_clients
request :list_cloud_ips
request :list_images
request :list_server_types
request :list_servers
request :list_users
request :list_zones
request :map_cloud_ip
request :reset_ftp_password_account
request :resize_server
request :shutdown_server
request :snapshot_server
request :start_server
request :stop_server
request :unmap_cloud_ip
request :update_account
request :update_api_client
request :update_image
request :update_server
request :update_user
class Mock
2010-11-16 16:24:43 -05:00
def initialize(options)
@brightbox_client_id = options[:brightbox_client_id] || Fog.credentials[:brightbox_client_id]
@brightbox_secret = options[:brightbox_secret] || Fog.credentials[:brightbox_secret]
end
def request(options)
raise "Not implemented"
end
end
class Real
def initialize(options)
require "json"
# Currently authentication and api endpoints are the same but may change
@auth_url = options[:brightbox_auth_url] || Fog.credentials[:brightbox_auth_url] || API_URL
@api_url = options[:brightbox_api_url] || Fog.credentials[:brightbox_api_url] || API_URL
2010-11-16 16:24:43 -05:00
@brightbox_client_id = options[:brightbox_client_id] || Fog.credentials[:brightbox_client_id]
@brightbox_secret = options[:brightbox_secret] || Fog.credentials[:brightbox_secret]
@connection = Fog::Connection.new(@api_url)
end
def request(params)
begin
get_oauth_token if @oauth_token.nil?
response = authenticated_request(params)
rescue Excon::Errors::Unauthorized => e
get_oauth_token
response = authenticated_request(params)
end
unless response.body.empty?
response = JSON.parse(response.body)
end
end
def account
Fog::Brightbox::Compute::Account.new(get_account)
end
private
def get_oauth_token(options = {})
auth_url = options[:brightbox_auth_url] || @auth_url
connection = Fog::Connection.new(auth_url)
@authentication_body = {'client_id' => @brightbox_client_id, 'grant_type' => 'none'}.to_json
response = connection.request({
:path => "/token",
:expects => 200,
:headers => {
'Authorization' => "Basic " + Base64.encode64("#{@brightbox_client_id}:#{@brightbox_secret}").chomp,
'Content-Type' => 'application/json'
},
:method => 'POST',
:body => @authentication_body
})
@oauth_token = JSON.parse(response.body)["access_token"]
return @oauth_token
end
def authenticated_request(options)
headers = options[:headers] || {}
headers.merge!("Authorization" => "OAuth #{@oauth_token}")
options[:headers] = headers
@connection.request(options)
end
end
end
end
end