2013-06-18 10:57:32 -04:00
|
|
|
require 'fog/vcloudng'
|
|
|
|
require 'fog/compute'
|
2013-06-20 13:36:34 -04:00
|
|
|
require 'fog/vcloudng/requests/compute/helper'
|
2013-06-11 09:06:30 -04:00
|
|
|
|
2013-06-18 10:57:32 -04:00
|
|
|
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
|
2013-07-02 06:49:47 -04:00
|
|
|
|
|
|
|
def extract_link(attributes_xml)
|
|
|
|
response = {}
|
|
|
|
link_attrs = extract_attributes(attributes_xml)
|
|
|
|
response[:type] = link_attrs["type"]
|
|
|
|
response[:rel] = link_attrs["rel"]
|
|
|
|
response[:href] = link_attrs["href"]
|
|
|
|
if response[:type] && response[:rel]
|
|
|
|
short_type = response[:type].scan(/.*\.(.*)\+/).first.first
|
|
|
|
snake_case_short_type = short_type.gsub(/([A-Z])/) { '_' + $1.downcase }
|
|
|
|
response[:method_name] = response[:rel] + '_' + snake_case_short_type
|
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
2013-06-18 10:57:32 -04:00
|
|
|
end
|
2013-06-11 09:06:30 -04:00
|
|
|
|
2013-07-08 14:31:37 -04:00
|
|
|
class NonLoaded
|
|
|
|
end
|
2013-06-11 09:06:30 -04:00
|
|
|
|
2013-06-18 10:57:32 -04:00
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class Vcloudng < Fog::Service
|
|
|
|
|
2013-06-11 09:06:30 -04:00
|
|
|
module Defaults
|
|
|
|
PATH = '/api'
|
|
|
|
PORT = 443
|
|
|
|
SCHEME = 'https'
|
|
|
|
end
|
2013-06-18 10:57:32 -04:00
|
|
|
|
2013-07-05 08:35:23 -04:00
|
|
|
module Errors
|
|
|
|
class ServiceError < Fog::Errors::Error; end
|
|
|
|
class Task < ServiceError; end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-06-18 10:57:32 -04:00
|
|
|
requires :vcloudng_username, :vcloudng_password, :vcloudng_host
|
|
|
|
|
2013-06-18 11:29:42 -04:00
|
|
|
secrets :vcloudng_password
|
|
|
|
|
|
|
|
model_path 'fog/vcloudng/models/compute'
|
|
|
|
model :organization
|
|
|
|
collection :organizations
|
2013-06-18 12:56:19 -04:00
|
|
|
model :catalog
|
|
|
|
collection :catalogs
|
2013-06-18 14:49:56 -04:00
|
|
|
model :catalog_item
|
|
|
|
collection :catalog_items
|
2013-06-18 14:51:12 -04:00
|
|
|
model :vdc
|
|
|
|
collection :vdcs
|
2013-06-20 13:36:34 -04:00
|
|
|
model :vapp
|
|
|
|
collection :vapps
|
|
|
|
model :task
|
|
|
|
collection :tasks
|
2013-06-24 07:46:30 -04:00
|
|
|
model :vm
|
|
|
|
collection :vms
|
|
|
|
model :vm_customization
|
|
|
|
collection :vm_customizations
|
2013-06-24 11:04:55 -04:00
|
|
|
model :network
|
|
|
|
collection :networks
|
2013-06-28 06:49:11 -04:00
|
|
|
model :disk
|
|
|
|
collection :disks
|
2013-07-01 11:27:52 -04:00
|
|
|
model :vm_network
|
|
|
|
collection :vm_networks
|
2013-07-03 07:10:31 -04:00
|
|
|
model :tag # this is called metadata in vcloud
|
|
|
|
collection :tags
|
2013-06-18 11:29:42 -04:00
|
|
|
|
2013-06-18 10:57:32 -04:00
|
|
|
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
|
2013-06-20 13:36:34 -04:00
|
|
|
request :get_vapp
|
2013-06-24 07:46:30 -04:00
|
|
|
request :get_vms
|
2013-06-18 10:57:32 -04:00
|
|
|
request :instantiate_vapp_template
|
2013-06-20 13:36:34 -04:00
|
|
|
request :get_task
|
|
|
|
request :get_tasks_list
|
2013-06-24 07:46:30 -04:00
|
|
|
request :get_vm_customization
|
2013-06-28 09:03:38 -04:00
|
|
|
request :put_vm_customization
|
2013-06-24 11:04:55 -04:00
|
|
|
request :get_network
|
2013-06-26 08:23:54 -04:00
|
|
|
request :get_vm_cpu
|
|
|
|
request :put_vm_cpu
|
2013-06-26 10:19:39 -04:00
|
|
|
request :get_vm_memory
|
|
|
|
request :put_vm_memory
|
2013-06-27 12:44:27 -04:00
|
|
|
request :get_vm_disks
|
|
|
|
request :put_vm_disks
|
2013-07-01 11:27:52 -04:00
|
|
|
request :get_vm_network
|
|
|
|
request :put_vm_network
|
2013-07-03 07:10:31 -04:00
|
|
|
request :get_vm_metadata
|
|
|
|
request :post_vm_metadata
|
|
|
|
request :put_vm_metadata_value
|
|
|
|
request :delete_vm_metadata
|
2013-07-05 08:35:23 -04:00
|
|
|
request :post_vm_poweron
|
2013-07-02 06:49:47 -04:00
|
|
|
request :get_request
|
|
|
|
request :get_href
|
2013-06-18 10:57:32 -04:00
|
|
|
|
2013-07-09 07:31:43 -04:00
|
|
|
class Model < Fog::Model
|
|
|
|
def initialize(attrs={})
|
|
|
|
super(attrs)
|
|
|
|
lazy_load_attrs = self.class.attributes - attributes.keys
|
|
|
|
lazy_load_attrs.each do |attr|
|
|
|
|
attributes[attr]= NonLoaded if attributes[attr].nil?
|
|
|
|
make_lazy_load_method(attr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_lazy_load_method(attr)
|
|
|
|
self.class.instance_eval do
|
|
|
|
define_method(attr) do
|
|
|
|
reload if attributes[attr] == NonLoaded and !@inspecting
|
|
|
|
attributes[attr]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
@inspecting = true
|
|
|
|
out = super
|
|
|
|
@inspecting = false
|
|
|
|
out
|
|
|
|
end
|
|
|
|
end
|
2013-07-09 08:19:21 -04:00
|
|
|
|
|
|
|
class Collection < Fog::Collection
|
|
|
|
def all(lazy_load=true)
|
|
|
|
lazy_load ? index : get_everyone
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(item_id)
|
|
|
|
item = get_by_id(item_id)
|
|
|
|
return nil unless item
|
|
|
|
new(item)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_by_name(item_name)
|
|
|
|
item_found = item_list.detect{|item| item[:name] == item_name }
|
|
|
|
return nil unless item_found
|
|
|
|
get(item_found[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
|
|
|
load(item_list)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_everyone
|
2013-07-09 08:38:11 -04:00
|
|
|
items = item_list.map {|item| get_by_id(item[:id]) }
|
2013-07-09 08:19:21 -04:00
|
|
|
load(items)
|
|
|
|
end
|
|
|
|
end
|
2013-06-11 09:06:30 -04:00
|
|
|
|
|
|
|
class Real
|
2013-06-20 13:36:34 -04:00
|
|
|
include Fog::Compute::Helper
|
|
|
|
|
2013-06-12 13:51:13 -04:00
|
|
|
attr_reader :end_point
|
2013-06-11 09:06:30 -04:00
|
|
|
|
2013-06-20 13:36:34 -04:00
|
|
|
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::Compute::Vcloudng::Defaults::PATH
|
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
@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::ToHashDocument.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
|
|
|
|
})
|
2013-07-04 07:24:25 -04:00
|
|
|
rescue => @e
|
|
|
|
raise @e unless @e.class.to_s =~ /^Excon::Errors/
|
|
|
|
puts @e.response.status
|
|
|
|
puts CGI::unescapeHTML(@e.response.body)
|
|
|
|
raise @e
|
2013-06-20 13:36:34 -04:00
|
|
|
end
|
2013-07-01 11:27:52 -04:00
|
|
|
|
2013-07-05 13:10:43 -04:00
|
|
|
def process_task(response_body)
|
|
|
|
task = make_task_object(response_body)
|
2013-07-05 09:21:40 -04:00
|
|
|
wait_and_raise_unless_success(task)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def make_task_object(task_response)
|
|
|
|
task_response[:id] = task_response[:href].split('/').last
|
|
|
|
tasks.new(task_response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def wait_and_raise_unless_success(task)
|
|
|
|
task.wait_for { non_running? }
|
|
|
|
raise Errors::Task.new "status: #{task.status}, error: #{task.error}" unless task.success?
|
|
|
|
end
|
|
|
|
|
2013-07-08 10:57:40 -04:00
|
|
|
def add_id_from_href!(data={})
|
|
|
|
data[:id] = data[:href].split('/').last
|
|
|
|
end
|
|
|
|
|
2013-06-11 09:06:30 -04:00
|
|
|
end
|
|
|
|
|
2013-06-12 13:52:15 -04:00
|
|
|
|
2013-06-11 09:06:30 -04:00
|
|
|
class Mock
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|