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

added virtual machine support and security group support

This commit is contained in:
bdorry 2011-09-07 10:00:25 -04:00
parent 19300ae771
commit 8ac261a0cf
22 changed files with 485 additions and 8 deletions

View file

@ -4,24 +4,48 @@ module Fog
module Compute
class Cloudstack < Fog::Service
class BadRequest < Fog::Errors::Error; end
requires :cloudstack_api_key, :cloudstack_secret_access_key, :host
recognizes :host, :port, :path, :scheme, :persistent
request_path 'fog/compute/requests/cloudstack'
request :authorize_security_group_ingress
request :create_account
request :create_domain
request :create_security_group
request :create_snapshot
request :create_user
request :delete_account
request :delete_domain
request :delete_security_group
request :delete_snapshot
request :delete_user
request :deploy_virtual_machine
request :destroy_virtual_machine
request :disable_user
request :enable_user
request :generate_usage_records
request :get_vm_password
request :list_accounts
request :list_alerts
request :list_async_jobs
request :list_capacity
request :list_capabilities
request :list_clusters
request :list_configurations
request :list_disk_offerings
request :list_capacity
request :list_domains
request :list_domain_children
request :list_events
@ -49,8 +73,21 @@ module Fog
request :list_volumes
request :list_zones
request :update_user
request :query_async_job_result
request :reboot_virtual_machine
request :register_user_keys
request :revoke_security_group_ingress
request :start_virtual_machine
request :stop_virtual_machine
request :update_account
request :update_domain
request :update_user
request :update_virtual_machine
class Mock
def self.data
@ -103,11 +140,25 @@ module Fog
params.merge!({'signature' => signature})
response = @connection.request({
:query => params,
:method => 'GET',
:expects => 200
})
begin
response = @connection.request({
:query => params,
:method => 'GET',
:expects => 200
})
rescue Excon::Errors::HTTPStatusError => error
error_response = JSON.parse(error.response.body)
error_code = error_response.values.first['errorcode']
error_text = error_response.values.first['errortext']
case error_code
when 431
raise Fog::Compute::Cloudstack::BadRequest.new(error_text)
else
raise Fog::Compute::Cloudstack::Error.new(error_text)
end
end
unless response.body.empty?
response = JSON.parse(response.body)

View file

@ -0,0 +1,22 @@
module Fog
module Compute
class Cloudstack
class Real
# Creates an account.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/authorizeSecurityGroupIngress.html]
def authorize_security_group_ingress(options={})
options.merge!(
'command' => 'authorizeSecurityGroupIngress'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Creates an account.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/createSecurityGroup.html]
def create_security_group(options={})
options.merge!(
'command' => 'createSecurityGroup'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Creates a snapshot for an account that already exists.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/createSnapshot.html]
def create_snapshot(options={})
options.merge!(
'command' => 'createSnapshot'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Creates an account.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/deleteSecurityGroup.html]
def delete_security_group(options={})
options.merge!(
'command' => 'deleteSecurityGroup'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Deletes a specified user.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/deleteSnapshot.html]
def delete_snapshot(options={})
options.merge!(
'command' => 'deleteSnapshot'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,28 @@
module Fog
module Compute
class Cloudstack
class Real
# Creates and automatically starts a virtual machine based on a service offering, disk offering, and template.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/deployVirtualMachine.html]
def deploy_virtual_machine(options={})
options.merge!(
'command' => 'deployVirtualMachine'
)
if ( securitygroupids = options.delete('securitygroupids') ).is_a?(Array)
options.merge!('securitygroupids' => securitygroupnames.join(','))
end
if ( securitygroupnames = options.delete('securitygroupnames') ).is_a?(Array)
options.merge!('securitygroupnames' => securitygroupnames.join(','))
end
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Updates account information for the authenticated user.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/destroyVirtualMachine.html]
def destroy_virtual_machine(options={})
options.merge!(
'command' => 'destroyVirtualMachine'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,28 @@
module Fog
module Compute
class Cloudstack
class Real
# Lists all available networks.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/generateUsageRecords.html]
def generate_usage_records(options={})
options.merge!(
'command' => 'generateUsageRecords'
)
if startdate = options.delete('startdate')
options.merge!('startdate' => startdate.strftime('%Y-%m-%d'))
end
if enddate = options.delete('enddate')
options.merge!('enddate' => enddate.strftime('%Y-%m-%d'))
end
request(options)
end
end
end
end
end

View file

@ -0,0 +1,21 @@
module Fog
module Compute
class Cloudstack
class Real
# Returns an encrypted password for the VM
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/getVMPassword.html]
def get_vm_password(id)
options = {
'command' => 'getVMPassword',
'id' => id
}
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Lists configurations and provides detailed account information for listed configurations.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/listAccounts.html]
def list_capabilities(options={})
options.merge!(
'command' => 'listCapabilities'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Lists domains and provides detailed information for listed domains.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/listDomains.html]
def list_capacity(options={})
options.merge!(
'command' => 'listCapacity'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Lists configurations and provides detailed account information for listed configurations.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/listAccounts.html]
def list_clusters(options={})
options.merge!(
'command' => 'listClusters'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Lists configurations and provides detailed account information for listed configurations.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/listAccounts.html]
def list_configurations(options={})
options.merge!(
'command' => 'listConfigurations'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Creates a domain.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/queryAsyncJobResult.html]
def query_async_job_result(options={})
options.merge!(
'command' => 'queryAsyncJobResult'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Updates account information for the authenticated user.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/rebootVirtualMachine.html]
def reboot_virtual_machine(options={})
options.merge!(
'command' => 'rebootVirtualMachine'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Enables a user account.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/registerUserKeys.html]
def register_user_keys(options={})
options.merge!(
'command' => 'registerUserKeys'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,21 @@
module Fog
module Compute
class Cloudstack
class Real
# Creates an account.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/authorizeSecurityGroupIngress.html]
def revoke_security_group_ingress(options={})
options.merge!(
'command' => 'revokeSecurityGroupIngress'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Updates account information for the authenticated user.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/startVirtualMachine.html]
def start_virtual_machine(options={})
options.merge!(
'command' => 'startVirtualMachine'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Updates account information for the authenticated user.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/stopVirtualMachine.html]
def stop_virtual_machine(options={})
options.merge!(
'command' => 'stopVirtualMachine'
)
request(options)
end
end
end
end
end

View file

@ -0,0 +1,20 @@
module Fog
module Compute
class Cloudstack
class Real
# Updates account information for the authenticated user.
#
# {CloudStack API Reference}[http://download.cloud.com/releases/2.2.0/api_2.2.4/global_admin/updateAccount.html]
def update_virtual_machine(options={})
options.merge!(
'command' => 'updateVirtualMachine'
)
request(options)
end
end
end
end
end

View file

@ -1,4 +1,5 @@
require 'fog/core'
require 'uri'
module Fog
module Cloudstack
@ -10,10 +11,15 @@ module Fog
DIGEST = OpenSSL::Digest::Digest.new('sha1')
def self.escape(string)
CGI::escape(string)
string = CGI::escape(string)
string = string.gsub("+","%20")
string
end
def self.signed_params(key,params)
# remove empty attributes, cloudstack will not takem them into account when verifying signature
params.reject!{|k,v| v.nil? || v.to_s == ''}
query = params.to_a.sort.collect{|c| "#{c[0]}=#{escape(c[1].to_s)}"}.join('&').downcase
signed_string = Base64.encode64(OpenSSL::HMAC.digest(DIGEST,key,query)).strip