lib/brightbox-cli/vendor/fog/lib/fog/vcloud/compute.rb in brightbox-cli-0.17.1 vs lib/brightbox-cli/vendor/fog/lib/fog/vcloud/compute.rb in brightbox-cli-0.17.2

- old
+ new

@@ -50,24 +50,42 @@ unless @loaded reload end end + def link_up + load_unless_loaded! + self.links.find{|l| l[:rel] == 'up' } + end + + def self.has_up(item) + class_eval <<-EOS, __FILE__,__LINE__ + def #{item} + load_unless_loaded! + connection.get_#{item}(link_up[:href]) + end + EOS + end + end end end module Fog module Vcloud class Compute < Fog::Service - PATH = '/api/v1.0' + BASE_PATH = '/api' + DEFAULT_VERSION = '1.5' + SUPPORTED_VERSIONS = [ '1.5', '1.0' ] PORT = 443 SCHEME = 'https' + attr_writer :default_organization_uri + requires :vcloud_username, :vcloud_password, :vcloud_host - recognizes :vcloud_port, :vcloud_scheme, :vcloud_path, :vcloud_default_vdc + recognizes :vcloud_port, :vcloud_scheme, :vcloud_path, :vcloud_default_vdc, :vcloud_version, :vcloud_base_path recognizes :provider # remove post deprecation model_path 'fog/vcloud/models/compute' model :catalog collection :catalogs @@ -79,37 +97,35 @@ collection :networks model :server collection :servers model :task collection :tasks + model :vapp + collection :vapps model :vdc collection :vdcs + model :organization + collection :organizations request_path 'fog/vcloud/requests/compute' request :clone_vapp request :configure_network request :configure_network_ip request :configure_vapp request :configure_vm_memory request :configure_vm_name_description request :configure_vm_disks request :delete_vapp - request :get_catalog request :get_catalog_item request :get_customization_options - request :get_network request :get_network_ip request :get_network_ips request :get_network_extensions - request :get_organization - request :get_task request :get_task_list - request :get_vapp request :get_vapp_template request :get_vm_disks request :get_vm_memory - request :get_vdc request :instantiate_vapp_template request :login request :power_off request :power_on request :power_reset @@ -129,17 +145,20 @@ class << self def basic_request(*args) self.class_eval <<-EOS, __FILE__,__LINE__ def #{args[0]}(uri) - request({ - :expects => #{args[1] || 200}, - :method => '#{args[2] || 'GET'}', - :headers => #{args[3] ? args[3].inspect : '{}'}, - :body => '#{args[4] ? args[4] : ''}', - :parse => true, - :uri => uri }) + request( + { + :expects => #{args[1] || 200}, + :method => '#{args[2] || 'GET'}', + :headers => #{args[3] ? args[3].inspect : '{}'}, + :body => '#{args[4] ? args[4] : ''}', + :parse => true, + :uri => uri + } + ) end EOS end def unauthenticated_basic_request(*args) @@ -152,28 +171,31 @@ :parse => true, :uri => uri }) end EOS end - end + attr_reader :version + def initialize(options = {}) require 'builder' require 'fog/core/parser' @connections = {} @connection_options = options[:connection_options] || {} @persistent = options[:persistent] - @username = options[:vcloud_username] - @password = options[:vcloud_password] - @host = options[:vcloud_host] - @vdc_href = options[:vcloud_default_vdc] - @path = options[:vcloud_path] || Fog::Vcloud::Compute::PATH - @port = options[:vcloud_port] || Fog::Vcloud::Compute::PORT - @scheme = options[:vcloud_scheme] || Fog::Vcloud::Compute::SCHEME + @username = options[:vcloud_username] + @password = options[:vcloud_password] + @host = options[:vcloud_host] + @vdc_href = options[:vcloud_default_vdc] + @base_path = options[:vcloud_base_path] || Fog::Vcloud::Compute::BASE_PATH + @version = options[:vcloud_version] || Fog::Vcloud::Compute::DEFAULT_VERSION + @path = options[:vcloud_path] || "#{@base_path}/v#{@version}" + @port = options[:vcloud_port] || Fog::Vcloud::Compute::PORT + @scheme = options[:vcloud_scheme] || Fog::Vcloud::Compute::SCHEME end def reload @connections.each_value { |k,v| v.reset if v } end @@ -212,14 +234,21 @@ uri.to_s end end def xmlns - { "xmlns" => "http://www.vmware.com/vcloud/v1", - "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1", - "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", - "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema" } + if version == '1.0' + { "xmlns" => "http://www.vmware.com/vcloud/v1", + "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1", + "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", + "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema" } + else + { 'xmlns' => "http://www.vmware.com/vcloud/v1.5", + "xmlns:ovf" => "http://schemas.dmtf.org/ovf/envelope/1", + "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", + "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema" } + end end # If the cookie isn't set, do a get_organizations call to set it # and try the request. # If we get an Unauthorized error, we assume the token expired, re-auth and try again @@ -233,12 +262,27 @@ do_login do_request(params) end end - private + def basic_request_params(uri,*args) + { + :expects => args[0] || 200, + :method => args[1] || 'GET', + :headers => args[2] ? args[2].inspect : {}, + :body => args[3] ? args[3] : '', + :parse => true, + :uri => uri + } + end + + def base_path_url + "#{@scheme}://#{@host}:#{@port}#{@base_path}" + end + + private def ensure_parsed(uri) if uri.is_a?(String) URI.parse(uri) else uri @@ -271,10 +315,11 @@ # different hosts. @connections[host_url] ||= Fog::Connection.new(host_url, @persistent, @connection_options) # Set headers to an empty hash if none are set. headers = params[:headers] || {} + headers['Accept'] = 'application/*+xml;version=1.5' if version == '1.5' # Add our auth cookie to the headers if @cookie headers.merge!('Cookie' => @cookie) end @@ -287,25 +332,41 @@ :method => params[:method] || 'GET', :path => params[:uri].path }) # Parse the response body into a hash - #puts response.body unless response.body.empty? if params[:parse] document = Fog::ToHashDocument.new parser = Nokogiri::XML::SAX::PushParser.new(document) parser << response.body parser.finish - response.body = document.body end end response end end + def self.item_requests(*types) + types.each{|t| item_request(t) } + end + def self.item_request(type) + Fog::Vcloud::Compute::Real.class_eval <<-EOS, __FILE__,__LINE__ + def get_#{type}(uri) + Fog::Vcloud::Compute::#{type.to_s.capitalize}.new( + self.request(basic_request_params(uri)).body.merge( + :connection => self, + :collection => Fog::Vcloud::Compute::#{type.to_s.capitalize}s.new( + :connection => self + ) + ) + ) + end + EOS + end + item_requests :organization, :vdc, :network, :vapp, :server, :catalog, :task end end end