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

make clouding a fog service

This commit is contained in:
Rodrigo Estebanez 2013-06-18 16:57:32 +02:00
parent 14ac44ce62
commit 045c089ee9
23 changed files with 195 additions and 441 deletions

View file

@ -38,6 +38,9 @@ module Fog
when :vcloud
require 'fog/vcloud/compute'
Fog::Vcloud::Compute.new(attributes)
when :vcloudng
require 'fog/vcloudng/compute'
Fog::Vcloudng::Compute.new(attributes)
else
if self.providers.include?(provider)
require "fog/#{provider}/compute"

View file

@ -1,10 +1,11 @@
require 'fog/core'
require 'fog/vcloudng/parser'
require 'fog/vcloudng/compute'
module Fog
module Vcloudng
VCLOUDNG_OPTIONS = [:vcloudng_username, :vcloudng_password, :vcloudng_host]
module Vcloud
extend Fog::Provider
service(:compute, 'vcloudng/compute', 'Compute')
end
end

View file

@ -1,57 +1,132 @@
require_relative './shared'
require 'fog/vcloudng'
require 'fog/compute'
class String
def to_bool
return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
return false if self == false || self.empty? || self =~ (/(false|f|no|n|0)$/i)
raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
end
end
class VcloudngParser < Fog::Parsers::Base
def extract_attributes(attributes_xml)
attributes = {}
until attributes_xml.empty?
if attributes_xml.first.is_a?(Array)
until attributes_xml.first.empty?
attribute = attributes_xml.first.shift
attributes[attribute.localname] = attribute.value
end
else
attribute = attributes_xml.shift
attributes[attribute.localname] = attribute.value
end
end
attributes
end
end
module Fog
module Vcloudng
module Compute
module Bin
end
module Compute
class Vcloudng < Fog::Service
module Defaults
PATH = '/api'
PORT = 443
SCHEME = 'https'
end
extend Fog::Vcloudng::Shared
def self.new(options={})
# Fog::Logger.deprecation("Fog::Vcloudng::Compute is deprecated, to be replaced with Compute 1.0 someday/maybe [light_black](#{caller.first})[/]")
unless @required
shared_requires
@required = true
end
check_shared_options(options)
if Fog.mocking?
Fog::Vcloudng::Compute::Mock.new(options)
else
Fog::Vcloudng::Compute::Real.new(options)
end
end
requires :vcloudng_username, :vcloudng_password, :vcloudng_host
request_path 'fog/vcloudng/requests/compute'
request :get_organizations
request :get_organization
request :get_catalog
request :get_catalog_item
request :get_vdc
request :get_vapp_template
request :instantiate_vapp_template
class Real
attr_reader :end_point
include Fog::Vcloudng::Shared::Real
include Fog::Vcloudng::Shared::Parser
def initialize(options={})
@vcloudng_password = options[:vcloudng_password]
@vcloudng_username = options[:vcloudng_username]
@connection_options = options[:connection_options] || {}
@host = options[:vcloudng_host]
@path = options[:path] || Fog::Vcloudng::Compute::Defaults::PATH
@path = options[:path] || Fog::Compute::Vcloudng::Defaults::PATH
@persistent = options[:persistent] || false
@port = options[:port] || Fog::Vcloudng::Compute::Defaults::PORT
@scheme = options[:scheme] || Fog::Vcloudng::Compute::Defaults::SCHEME
@port = options[:port] || Fog::Compute::Vcloudng::Defaults::PORT
@scheme = options[:scheme] || Fog::Compute::Vcloudng::Defaults::SCHEME
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
@end_point = "#{@scheme}://#{@host}#{@path}/"
end
def auth_token
response = @connection.request({
:expects => 200,
:headers => { 'Authorization' => "Basic #{Base64.encode64("#{@vcloudng_username}:#{@vcloudng_password}").delete("\r\n")}",
'Accept' => 'application/*+xml;version=1.5'
},
:host => @host,
:method => 'POST',
:parser => Fog::Parsers::Compute::Vcloudng::GetOrganizations.new,
:path => "/api/sessions" # curl http://example.com/api/versions | grep LoginUrl
})
response.headers['Set-Cookie']
end
def reload
@cookie = nil # verify that this makes the connection to be restored, if so use Excon::Errors::Forbidden instead of Excon::Errors::Unauthorized
@connection.reset
end
def request(params)
unless @cookie
@cookie = auth_token
end
begin
do_request(params)
# this is to know if Excon::Errors::Unauthorized really happens
#rescue Excon::Errors::Unauthorized
# @cookie = auth_token
# do_request(params)
end
end
def do_request(params)
headers = {}
if @cookie
headers.merge!('Cookie' => @cookie)
end
if params[:path]
if params[:override_path] == true
path = params[:path]
else
path = "#{@path}/#{params[:path]}"
end
else
path = "#{@path}"
end
@connection.request({
:body => params[:body],
:expects => params[:expects],
:headers => headers.merge!(params[:headers] || {}),
:host => @host,
:method => params[:method],
:parser => params[:parser],
:path => path
})
end
def default_vdc_id
if default_organization_id
@default_vdc_id ||= begin
@ -121,32 +196,30 @@ module Fog
end
end
def default_organization_id
@default_organization_id ||= begin
org_list = get_organizations.body['OrgList']
if org_list.length == 1
org_list.first['href'].split('/').last
else
nil
end
end
end
def default_organization_body
return nil unless default_organization_id
@default_organization_body ||= begin
response = get_organization(default_organization_id)
return nil unless response.respond_to? 'data'
response.data[:body]
end
end
end
class Mock
include Fog::Vcloudng::Shared::Mock
include Fog::Vcloudng::Shared::Parser
def initialize(option = {})
super
@base_url = Fog::Vcloudng::Compute::Defaults::SCHEME + "://" +
options[:vcloudng_host] +
Fog::Vcloudng::Compute::Defaults::PATH
@vcloudng_username = options[:vcloudng_username]
end
def data
self.class.data[@vcloudng_username]
end
def reset_data
self.class.data.delete(@vcloudng_username)
end
end
end

View file

@ -1,20 +0,0 @@
class VcloudngParser < Fog::Parsers::Base
def extract_attributes(attributes_xml)
attributes = {}
until attributes_xml.empty?
if attributes_xml.first.is_a?(Array)
until attributes_xml.first.empty?
attribute = attributes_xml.first.shift
attributes[attribute.localname] = attribute.value
end
else
attribute = attributes_xml.shift
attributes[attribute.localname] = attribute.value
end
end
attributes
end
end

View file

@ -16,8 +16,9 @@
module Fog
module Parsers
module Vcloudng
module Compute
module Compute
module Vcloudng
class GetCatalog < VcloudngParser

View file

@ -10,8 +10,8 @@
module Fog
module Parsers
module Vcloudng
module Compute
module Compute
module Vcloudng
class GetCatalogItem < VcloudngParser

View file

@ -26,8 +26,9 @@
module Fog
module Parsers
module Vcloudng
module Compute
module Compute
module Vcloudng
class GetNetwork < VcloudngParser

View file

@ -78,8 +78,9 @@
module Fog
module Parsers
module Vcloudng
module Compute
module Compute
module Vcloudng
class GetOrganization < VcloudngParser

View file

@ -15,8 +15,9 @@
module Fog
module Parsers
module Vcloudng
module Compute
module Compute
module Vcloudng
class GetOrganizations < VcloudngParser

View file

@ -1,7 +1,8 @@
module Fog
module Parsers
module Vcloudng
module Compute
module Compute
module Vcloudng
class GetVapp < VcloudngParser

View file

@ -75,8 +75,9 @@
#
#module Fog
# module Parsers
# module Vcloudng
# module Compute
# module Compute
# module Vcloudng
#
#
#
# class GetVappTemplate < VcloudngParser
@ -188,8 +189,9 @@
module Fog
module Parsers
module Vcloudng
module Compute
module Compute
module Vcloudng
class GetVappTemplate < VcloudngParser

View file

@ -1,7 +1,7 @@
module Fog
module Parsers
module Vcloudng
module Compute
module Compute
module Vcloudng
class GetVdc < VcloudngParser

View file

@ -1,7 +1,8 @@
module Fog
module Parsers
module Vcloudng
module Compute
module Compute
module Vcloudng
class InstantiateVappTemplate < VcloudngParser

View file

@ -1,6 +1,6 @@
module Fog
module Vcloudng
module Compute
module Compute
class Vcloudng
class Real
require 'fog/vcloudng/parsers/compute/get_catalog'
@ -32,7 +32,7 @@ module Fog
:expects => 200,
:headers => { 'Accept' => 'application/*+xml;version=1.5' },
:method => 'GET',
:parser => Fog::Parsers::Vcloudng::Compute::GetCatalog.new,
:parser => Fog::Parsers::Compute::Vcloudng::GetCatalog.new,
:path => "catalog/#{catalog_uuid}"
)
end

View file

@ -1,6 +1,6 @@
module Fog
module Vcloudng
module Compute
module Compute
class Vcloudng
class Real
require 'fog/vcloudng/parsers/compute/get_catalog_item'
@ -36,7 +36,7 @@ module Fog
:expects => 200,
:headers => { 'Accept' => 'application/*+xml;version=1.5' },
:method => 'GET',
:parser => Fog::Parsers::Vcloudng::Compute::GetCatalogItem.new,
:parser => Fog::Parsers::Compute::Vcloudng::GetCatalogItem.new,
:path => "catalogItem/#{catalog_item_id}"
)
end

View file

@ -1,7 +1,8 @@
module Fog
module Vcloudng
module Compute
module Compute
class Vcloudng
class Real
require 'fog/vcloudng/parsers/compute/get_network'
@ -26,7 +27,7 @@ module Fog
:expects => 200,
:headers => { 'Accept' => 'application/*+xml;version=1.5' },
:method => 'GET',
:parser => Fog::Parsers::Vcloudng::Compute::GetNetwork.new,
:parser => Fog::Parsers::Compute::Vcloudng::GetNetwork.new,
:path => "network/#{network_id}"
)
end

View file

@ -1,7 +1,8 @@
module Fog
module Vcloudng
module Compute
module Compute
class Vcloudng
class Real
require 'fog/vcloudng/parsers/compute/get_organization'
@ -26,7 +27,7 @@ module Fog
:expects => 200,
:headers => { 'Accept' => 'application/*+xml;version=1.5' },
:method => 'GET',
:parser => Fog::Parsers::Vcloudng::Compute::GetOrganization.new,
:parser => Fog::Parsers::Compute::Vcloudng::GetOrganization.new,
:path => "org/#{organization_id}"
})
end

View file

@ -1,7 +1,8 @@
module Fog
module Vcloudng
module Compute
module Compute
class Vcloudng
class Real
require 'fog/vcloudng/parsers/compute/get_organizations'
@ -18,7 +19,7 @@ module Fog
:expects => 200,
:headers => { 'Accept' => 'application/*+xml;version=1.5' },
:method => 'GET',
:parser => Fog::Parsers::Vcloudng::Compute::GetOrganizations.new,
:parser => Fog::Parsers::Compute::Vcloudng::GetOrganizations.new,
:path => "org"
})
end

View file

@ -1,8 +1,9 @@
module Fog
module Vcloudng
module Compute
module Compute
class Vcloudng
class Real
require 'fog/vcloudng/parsers/compute/get_vapp'
@ -35,7 +36,7 @@ module Fog
:expects => 200,
:headers => { 'Accept' => 'application/*+xml;version=1.5' },
:method => 'GET',
:parser => Fog::Parsers::Vcloudng::Compute::GetVapp.new,
:parser => Fog::Parsers::Compute::Vcloudng::GetVapp.new,
:path => "vApp/#{vapp_id}"
)
end

View file

@ -1,8 +1,9 @@
module Fog
module Vcloudng
module Compute
module Compute
class Vcloudng
class Real
require 'fog/vcloudng/parsers/compute/get_vapp_template'
# Get details of a vapp template
@ -40,7 +41,7 @@ module Fog
:expects => 200,
:headers => { 'Accept' => 'application/*+xml;version=1.5' },
:method => 'GET',
:parser => Fog::Parsers::Vcloudng::Compute::GetVappTemplate.new,
:parser => Fog::Parsers::Compute::Vcloudng::GetVappTemplate.new,
:path => "vAppTemplate/#{vapp_template_id}"
)
end

View file

@ -1,8 +1,9 @@
module Fog
module Vcloudng
module Compute
module Compute
class Vcloudng
class Real
require 'fog/vcloudng/parsers/compute/get_vdc'
# Get details of a vdc
@ -34,7 +35,7 @@ module Fog
:expects => 200,
:headers => { 'Accept' => 'application/*+xml;version=1.5' },
:method => 'GET',
:parser => Fog::Parsers::Vcloudng::Compute::GetVdc.new,
:parser => Fog::Parsers::Compute::Vcloudng::GetVdc.new,
:path => "vdc/#{vdc_id}"
)
end

View file

@ -1,6 +1,6 @@
module Fog
module Vcloudng
module Compute
module Compute
class Vcloudng
class Real
require 'fog/vcloudng/parsers/compute/instantiate_vapp_template'
@ -25,7 +25,6 @@ module Fog
# * 'status'<~String> - 0(pending) --> 2(off) -->4(on)
def instantiate_vapp_template(vapp_name, template_id, options = {})
params = populate_uris(options.merge(vapp_name: vapp_name, template_id: template_id))
puts params
validate_uris(params)
data = generate_instantiate_vapp_template_request(params)
@ -36,7 +35,7 @@ module Fog
:headers => { 'Content-Type' => 'application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml',
'Accept' => 'application/*+xml;version=1.5' },
:method => 'POST',
:parser => Fog::Parsers::Vcloudng::Compute::InstantiateVappTemplate.new,
:parser => Fog::Parsers::Compute::Vcloudng::InstantiateVappTemplate.new,
:path => "vdc/#{params[:vdc_id]}/action/instantiateVAppTemplate"
)
end

View file

@ -1,316 +0,0 @@
class String
def to_bool
return true if self == true || self =~ (/(true|t|yes|y|1)$/i)
return false if self == false || self.empty? || self =~ (/(false|f|no|n|0)$/i)
raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
end
end
module Fog
module Vcloudng
module Shared
# Commond methods shared by Real and Mock
module Common
def default_organization_id
@default_organization_id ||= begin
org_list = get_organizations.body['OrgList']
if org_list.length == 1
org_list.first['href'].split('/').last
else
nil
end
end
end
def default_organization_body
return nil unless default_organization_id
@default_organization_body ||= begin
response = get_organization(default_organization_id)
return nil unless response.respond_to? 'data'
response.data[:body]
end
end
end
module Parser
def parse(data)
case data['type']
when 'application/vnd.vmware.vcloud.vApp+xml'
servers.new(data.merge!(:service => self))
else
data
end
end
end
module Real
include Common
# private
def auth_token
response = @connection.request({
:expects => 200,
:headers => { 'Authorization' => "Basic #{Base64.encode64("#{@vcloudng_username}:#{@vcloudng_password}").delete("\r\n")}",
'Accept' => 'application/*+xml;version=1.5'
},
:host => @host,
:method => 'POST',
:parser => Fog::Parsers::Vcloudng::Compute::GetOrganizations.new,
:path => "/api/sessions" # curl http://example.com/api/versions | grep LoginUrl
})
response.headers['Set-Cookie']
end
def reload
@connection.reset
end
def request(params)
unless @cookie
@cookie = auth_token
end
begin
do_request(params)
rescue Excon::Errors::Unauthorized
@cookie = auth_token
do_request(params)
end
end
def do_request(params)
headers = {}
if @cookie
headers.merge!('Cookie' => @cookie)
end
if params[:path]
if params[:override_path] == true
path = params[:path]
else
path = "#{@path}/#{params[:path]}"
end
else
path = "#{@path}"
end
@connection.request({
:body => params[:body],
:expects => params[:expects],
:headers => headers.merge!(params[:headers] || {}),
:host => @host,
:method => params[:method],
:parser => params[:parser],
:path => path
})
end
end
module Mock
include Common
def self.mock_data
{
:organizations =>
[
{
:info => {
:name => "Boom Inc.",
:id => 1
},
:vdcs => [
{ :id => 21,
:name => "Boomstick",
:storage => { :used => 105, :allocated => 200 },
:cpu => { :allocated => 10000 },
:memory => { :allocated => 20480 },
:networks => [
{ :id => 31,
:name => "1.2.3.0/24",
:subnet => "1.2.3.0/24",
:gateway => "1.2.3.1",
:netmask => "255.255.255.0",
:fencemode => "isolated"
},
{ :id => 32,
:name => "4.5.6.0/24",
:subnet => "4.5.6.0/24",
:gateway => "4.5.6.1",
:netmask => "255.255.255.0",
:fencemode => "isolated"
},
],
:vms => [
{ :id => 41,
:name => "Broom 1"
},
{ :id => 42,
:name => "Broom 2"
},
{ :id => 43,
:name => "Email!"
}
],
:public_ips => [
{ :id => 51,
:name => "99.1.2.3"
},
{ :id => 52,
:name => "99.1.2.4"
},
{ :id => 53,
:name => "99.1.9.7"
}
]
},
{ :id => 22,
:storage => { :used => 40, :allocated => 150 },
:cpu => { :allocated => 1000 },
:memory => { :allocated => 2048 },
:name => "Rock-n-Roll",
:networks => [
{ :id => 33,
:name => "7.8.9.0/24",
:subnet => "7.8.9.0/24",
:gateway => "7.8.9.1",
:netmask => "255.255.255.0",
:fencemode => "isolated"
}
],
:vms => [
{ :id => 44,
:name => "Master Blaster"
}
],
:public_ips => [
{ :id => 54,
:name => "99.99.99.99"
}
]
}
]
}
]
}
end
def self.error_headers
{"X-Powered-By"=>"ASP.NET",
"Date"=> Time.now.to_s,
"Content-Type"=>"text/html",
"Content-Length"=>"0",
"Server"=>"Microsoft-IIS/7.0",
"Cache-Control"=>"private"}
end
def self.unathorized_status
401
end
def self.headers(body, content_type)
{"X-Powered-By"=>"ASP.NET",
"Date"=> Time.now.to_s,
"Content-Type"=> content_type,
"Content-Length"=> body.to_s.length,
"Server"=>"Microsoft-IIS/7.0",
"Set-Cookie"=>"vcloud-token=ecb37bfc-56f0-421d-97e5-bf2gdf789457; path=/",
"Cache-Control"=>"private"}
end
def self.status
200
end
def initialize(options={})
self.class.instance_eval '
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = Fog::Vcloudng::Compute::Mock.mock_data
end
end'
self.class.instance_eval '
def self.reset
@data = nil
end
def self.reset_data(keys=data.keys)
for key in [*keys]
data.delete(key)
end
end'
end
end
def check_shared_options(options)
cloud_option_keys = options.keys.select { |key| key.to_s =~ /^vcloudng_.*/ }
unless cloud_option_keys.length == 0 || cloud_option_keys.length == 3
raise ArgumentError.new("vcloudng_username, vcloudng_password and vcloudng_host required to access vcloud")
end
end
def shared_requires
#require 'fog/vcloudng/models/compute/address'
#require 'fog/vcloudng/models/compute/addresses'
#require 'fog/vcloudng/models/compute/network'
#require 'fog/vcloudng/models/compute/networks'
#require 'fog/vcloudng/models/compute/server'
#require 'fog/vcloudng/models/compute/servers'
#require 'fog/vcloudng/models/compute/image'
#require 'fog/vcloudng/models/compute/images'
#require 'fog/vcloudng/models/compute/task'
#require 'fog/vcloudng/models/compute/tasks'
#require 'fog/vcloudng/models/compute/vdc'
#require 'fog/vcloudng/models/compute/vdcs'
#require 'fog/vcloudng/models/compute/internetservice'
#require 'fog/vcloudng/models/compute/internetservices'
#require 'fog/vcloudng/models/compute/nodeservice'
#require 'fog/vcloudng/models/compute/nodeservices'
#require 'fog/vcloudng/parsers/compute/get_internet_services'
#require 'fog/vcloudng/parsers/compute/get_network_ips'
#require 'fog/vcloudng/parsers/compute/get_node_services'
#require 'fog/vcloudng/parsers/compute/get_public_ips'
#require 'fog/vcloudng/parsers/compute/get_tasks_list'
#require 'fog/vcloudng/parsers/compute/get_keys_list'
#require 'fog/vcloudng/parsers/compute/internet_service'
#require 'fog/vcloudng/parsers/compute/node_service'
#require 'fog/vcloudng/parsers/compute/public_ip'
#require 'fog/vcloudng/parsers/compute/task'
#require 'fog/vcloudng/requests/compute/add_internet_service'
#require 'fog/vcloudng/requests/compute/add_node_service'
#require 'fog/vcloudng/requests/compute/create_internet_service'
#require 'fog/vcloudng/requests/compute/delete_internet_service'
#require 'fog/vcloudng/requests/compute/delete_public_ip'
#require 'fog/vcloudng/requests/compute/delete_node_service'
#require 'fog/vcloudng/requests/compute/delete_vapp'
#require 'fog/vcloudng/requests/compute/deploy_vapp'
require 'fog/vcloudng/requests/compute/get_catalog'
require 'fog/vcloudng/requests/compute/get_catalog_item'
#require 'fog/vcloudng/requests/compute/get_internet_services'
require 'fog/vcloudng/requests/compute/get_network'
#require 'fog/vcloudng/requests/compute/get_network_ips'
#require 'fog/vcloudng/requests/compute/get_node_services'
require 'fog/vcloudng/requests/compute/get_organization'
require 'fog/vcloudng/requests/compute/get_organizations'
#require 'fog/vcloudng/requests/compute/get_public_ip'
#require 'fog/vcloudng/requests/compute/get_public_ips'
#require 'fog/vcloudng/requests/compute/get_task'
#require 'fog/vcloudng/requests/compute/get_tasks_list'
#require 'fog/vcloudng/requests/compute/get_keys_list'
require 'fog/vcloudng/requests/compute/get_vapp'
require 'fog/vcloudng/requests/compute/get_vapp_template'
require 'fog/vcloudng/requests/compute/get_vdc'
require 'fog/vcloudng/requests/compute/instantiate_vapp_template'
#require 'fog/vcloudng/requests/compute/configure_vapp'
#require 'fog/vcloudng/requests/compute/power_off'
#require 'fog/vcloudng/requests/compute/power_on'
#require 'fog/vcloudng/requests/compute/power_reset'
#require 'fog/vcloudng/requests/compute/power_shutdown'
end
end
end
end