# File lib/fog/vcloud/compute.rb, line 147 def basic_request(*args) self.class_eval " 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 } ) end ", __FILE__,__LINE__ end
# File lib/fog/vcloud/compute.rb, line 180 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] @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
# File lib/fog/vcloud/compute.rb, line 164 def unauthenticated_basic_request(*args) self.class_eval " def #{args[0]}(uri) unauthenticated_request({ :expects => #{args[1] || 200}, :method => '#{args[2] || 'GET'}', :headers => #{args[3] ? args[3].inspect : '{}'}, :parse => true, :uri => uri }) end ", __FILE__,__LINE__ end
# File lib/fog/vcloud/compute.rb, line 279 def base_path_url "#{@scheme}://#{@host}:#{@port}#{@base_path}" end
# File lib/fog/vcloud/compute.rb, line 268 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
# File lib/fog/vcloud/requests/compute/clone_vapp.rb, line 21 def clone_vapp(vdc_uri, vapp_uri, options = {}) unless options.has_key?(:poweron) options[:poweron] = "false" end validate_clone_vapp_options(options) request( :body => generate_clone_vapp_request(vapp_uri, options), :expects => 202, :headers => {'Content-Type' => 'application/vnd.vmware.vcloud.cloneVAppParams+xml'}, :method => 'POST', :uri => vdc_uri + '/action/clonevapp', :parse => true ) end
# File lib/fog/vcloud/requests/compute/configure_network.rb, line 13 def configure_network(network_uri, network_data) validate_network_data(network_data) request( :body => generate_configure_network_request(network_data), :expects => 200, :headers => {'Content-Type' => 'application/vnd.vmware.vcloud.networkService+xml'}, :method => 'PUT', :uri => network_uri, :parse => true ) end
# File lib/fog/vcloud/requests/compute/configure_network_ip.rb, line 18 def configure_network_ip(network_ip_uri, network_ip_data) validate_network_ip_data(network_ip_data) request( :body => generate_configure_network_ip_request(network_ip_data), :expects => 200, :headers => {'Content-Type' => 'application/vnd.vmware.vcloud.ip+xml' }, :method => 'PUT', :uri => network_ip_uri, :parse => true ) end
# File lib/fog/vcloud/requests/compute/configure_node.rb, line 22 def configure_node(node_uri, node_data) validate_node_data(node_data, true) request( :body => generate_configure_node_request(node_data), :expects => 200, :headers => {'Content-Type' => 'application/vnd.vmware.vcloud.nodeService+xml'}, :method => 'PUT', :uri => node_uri, :parse => true ) end
# File lib/fog/vcloud/requests/compute/configure_vapp.rb, line 96 def configure_vapp(vapp_uri, vapp_data) validate_vapp_data(vapp_data) request( :body => generate_configure_vapp_request(vapp_uri, vapp_data), :expects => 202, :headers => {'Content-Type' => 'application/vnd.vmware.vcloud.vApp+xml' }, :method => 'PUT', :uri => vapp_uri, :parse => true ) end
# File lib/fog/vcloud/requests/compute/configure_vm_disks.rb, line 80 def configure_vm_disks(vm_href, disk_data) disk_href = vm_href + '/virtualHardwareSection/disks' body = generate_configure_vm_disks_request(disk_href, disk_data) request( :body => body, :expects => 202, :headers => {'Content-Type' => 'application/vnd.vmware.vcloud.rasdItem+xml' }, :method => 'PUT', :uri => disk_href, :parse => true ) end
# File lib/fog/vcloud/requests/compute/configure_vm_memory.rb, line 6 def configure_vm_memory(vm_data) edit_uri = vm_data.select {|k,v| k == :Link && v[:rel] == 'edit'} edit_uri = edit_uri.kind_of?(Array) ? edit_uri.flatten[1][:href] : edit_uri[:Link][:href] body = "<?xml version="1.0" encoding="UTF-8"?> <Item xmlns="http://www.vmware.com/vcloud/v1" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:vcloud="http://www.vmware.com/vcloud/v1" vcloud:href="https://vcd01.esx.dev.int.realestate.com.au/api/v1.0/vApp/vm-329805878/virtualHardwareSection/memory" vcloud:type="application/vnd.vmware.vcloud.rasdItem+xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2.22.0/CIM_ResourceAllocationSettingData.xsd http://www.vmware.com/vcloud/v1 http://vcd01.esx.dev.int.realestate.com.au/api/v1.0/schema/master.xsd"> <rasd:AllocationUnits>byte * 2^20</rasd:AllocationUnits> <rasd:Description>Memory Size</rasd:Description> <rasd:ElementName>#{vm_data[:'rasd:VirtualQuantity']} MB of memory</rasd:ElementName> <rasd:InstanceID>5</rasd:InstanceID> <rasd:Reservation>0</rasd:Reservation> <rasd:ResourceType>4</rasd:ResourceType> <rasd:VirtualQuantity>#{vm_data[:'rasd:VirtualQuantity']}</rasd:VirtualQuantity> <rasd:Weight>5120</rasd:Weight> <Link rel="edit" type="application/vnd.vmware.vcloud.rasdItem+xml" href="https://vcd01.esx.dev.int.realestate.com.au/api/v1.0/vApp/vm-329805878/virtualHardwareSection/memory"/> </Item> " request( :body => body, :expects => 202, :headers => {'Content-Type' => vm_data[:"vcloud_type"] }, :method => 'PUT', :uri => edit_uri, :parse => true ) end
# File lib/fog/vcloud/requests/compute/configure_vm_name_description.rb, line 6 def configure_vm_name_description(edit_href, name, description) body = "<?xml version="1.0" encoding="UTF-8"?> <VApp xmlns="http://www.vmware.com/vcloud/v1" name="#{name}" type="application/vnd.vmware.vcloud.vApp+xml"> <Description>#{description}</Description> </VApp> " request( :body => body, :expects => 202, :headers => {'Content-Type' => "application/vnd.vmware.vcloud.vApp+xml"}, :method => 'PUT', :uri => edit_href, :parse => true ) end
# File lib/fog/vcloud/compute.rb, line 203 def default_organization_uri @default_organization_uri ||= begin unless @login_results do_login end case @login_results.body[:Org] when Array @login_results.body[:Org].first[:href] when Hash @login_results.body[:Org][:href] else nil end end end
# File lib/fog/vcloud/compute.rb, line 219 def default_vdc_href @vdc_href end
login handles the auth, but we just need the Set-Cookie header from that call.
# File lib/fog/vcloud/compute.rb, line 225 def do_login @login_results = login @cookie = @login_results.headers['Set-Cookie'] end
# File lib/fog/vcloud/compute.rb, line 230 def ensure_unparsed(uri) if uri.is_a?(String) uri else uri.to_s end end
# File lib/fog/vcloud/requests/compute/clone_vapp.rb, line 13 def generate_clone_vapp_request(uri, options) xml = Builder::XmlMarkup.new xml.CloneVAppParams(xmlns.merge!(:name => options[:name], :deploy => "true", :powerOn => options[:poweron])) { xml.VApp( :href => uri, :type => "application/vnd.vmware.vcloud.vApp+xml", :xmlns => "http://www.vmware.com/vcloud/v0.8") } end
# File lib/fog/vcloud/requests/compute/configure_vapp.rb, line 18 def generate_configure_vapp_request(vapp_uri, vapp_data) rasd_xmlns = { "xmlns" => "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" } xml = Nokogiri::XML(request( :uri => vapp_uri).body) xml.root['name'] = vapp_data[:name] #cpu xml.at("//xmlns:ResourceType[.='3']/..", rasd_xmlns).at('.//xmlns:VirtualQuantity', rasd_xmlns).content = vapp_data[:cpus] #memory xml.at("//xmlns:ResourceType[.='4']/..", rasd_xmlns).at('.//xmlns:VirtualQuantity', rasd_xmlns).content = vapp_data[:memory] #disks real_disks = xml.xpath("//xmlns:ResourceType[ .='17']/..", rasd_xmlns) real_disk_numbers = real_disks.map { |disk| disk.at('.//xmlns:AddressOnParent', rasd_xmlns).content } disk_numbers = vapp_data[:disks].map { |vdisk| vdisk[:number].to_s } if vapp_data[:disks].length < real_disks.length #Assume we're removing a disk remove_disk_numbers = real_disk_numbers - disk_numbers remove_disk_numbers.each do |number| if result = xml.at("//xmlns:ResourceType[ .='17']/../xmlns:AddressOnParent[.='#{number}']/..", rasd_xmlns) result.remove end end elsif vapp_data[:disks].length > real_disks.length add_disk_numbers = disk_numbers - real_disk_numbers add_disk_numbers.each do |number| new_disk = real_disks.first.dup new_disk.at('.//xmlns:AddressOnParent', rasd_xmlns).content = -1 new_disk.at('.//xmlns:VirtualQuantity', rasd_xmlns).content = vapp_data[:disks].detect { |disk| disk[:number].to_s == number.to_s }[:size] real_disks.first.parent << new_disk end end #puts xml.root.to_s xml.root.to_s #builder = Builder::XmlMarkup.new #builder.Vapp(:href => vapp_uri.to_s, # :type => 'application/vnd.vmware.vcloud.vApp+xml', # :name => vapp_data[:name], # :status => 2, # :size => 0, # :xmlns => 'http://www.vmware.com/vcloud/v0.8', # :"xmlns:xsi" => 'http://www.w3.org/2001/XMLSchema-instance', # :"xmlns:xsd" => 'http://www.w3.org/2001/XMLSchema') { # #builder.VirtualHardwareSection(:xmlns => 'http://schemas.dmtf.org/ovf/envelope/1') { # builder.Section(:"xsi:type" => "q2:VirtualHardwareSection_Type", :xmlns => "http://schemas.dmtf.org/ovf/envelope/1", :"xmlns:q2" => "http://www.vmware.com/vcloud/v0.8") { # builder.Info('Virtual Hardware') # builder.Item(:xmlns => 'http://schemas.dmtf.org/ovf/envelope/1') { # #builder.Item { # builder.InstanceID(1, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData') # builder.ResourceType(3, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData') # builder.VirtualQuantity(vapp_data[:cpus], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData') # } # builder.Item(:xmlns => 'http://schemas.dmtf.org/ovf/envelope/1') { # #builder.Item { # builder.InstanceID(2, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData') # builder.ResourceType(4, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData') # builder.VirtualQuantity(vapp_data[:memory], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData') # } # vapp_data[:disks].each do |disk_data| # #builder.Item(:xmlns => 'http://schemas.dmtf.org/ovf/envelope/1') { # builder.Item { # builder.AddressOnParent(disk_data[:number], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData') # builder.HostResource(disk_data[:resource], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData') # builder.InstanceID(9, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData') # builder.ResourceType(17, :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData') # builder.VirtualQuantity(disk_data[:size], :xmlns => 'http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData') # } # end # # } #} end
# File lib/fog/vcloud/requests/compute/configure_vm_disks.rb, line 39 def generate_configure_vm_disks_request(href, disk_data) xmlns = { "xmlns:rasd" => "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData", "xmlns" => "http://www.vmware.com/vcloud/v1" } # Get the XML from the API, parse it. xml = Nokogiri::XML(request( :uri => href).body) #xml.root['name'] = vapp_data[:name] #disks real_disks = xml.xpath("//rasd:ResourceType[ .='17']/..", xmlns) real_disk_numbers = real_disks.map { |disk| disk.at('.//rasd:AddressOnParent', xmlns).content } disk_numbers = disk_data.map { |vdisk| vdisk[:"rasd:AddressOnParent"].to_s } if disk_data.length < real_disks.length #Assume we're removing a disk remove_disk_numbers = real_disk_numbers - disk_numbers remove_disk_numbers.each do |number| if result = xml.at("//rasd:ResourceType[ .='17']/../rasd:AddressOnParent[.='#{number}']/..", xmlns) result.remove end end elsif disk_data.length > real_disks.length add_disk_numbers = disk_numbers - real_disk_numbers add_disk_numbers.each do |number| new_disk = real_disks.first.dup new_disk.at('.//rasd:AddressOnParent', xmlns).content = number.to_i #-1 new_disk.at('.//rasd:HostResource', xmlns)["vcloud:capacity"] = disk_data.detect { |disk| disk[:'rasd:AddressOnParent'].to_s == number.to_s }[:'rasd:HostResource'][:vcloud_capacity].to_s # nokogiri bug? shouldn't need to add this explicitly. new_disk.at('.//rasd:HostResource', xmlns)["xmlns:vcloud"] = xmlns['xmlns'] new_disk.at('.//rasd:InstanceID', xmlns).content = (2000 + number.to_i).to_s new_disk.at('.//rasd:ElementName', xmlns).content = "Hard disk #{number.to_i + 1}" real_disks.first.parent << new_disk end end xml.to_s end
# File lib/fog/vcloud/requests/compute/get_vm_disks.rb, line 6 def get_vm_disks(href) request( :expects => 200, :uri => href, :parse => true#false#true ) end
# File lib/fog/vcloud/requests/compute/get_vm_memory.rb, line 6 def get_vm_memory(href, parse = true) request( :expects => 200, :uri => href, :parse => parse ) end
# File lib/fog/vcloud/requests/compute/instantiate_vapp_template.rb, line 68 def instantiate_vapp_template options = {} validate_instantiate_vapp_template_options options request( :body => generate_instantiate_vapp_template_request(options), :expects => 201, :headers => {'Content-Type' => 'application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml'}, :method => 'POST', :uri => options[:vdc_uri] + '/action/instantiateVAppTemplate', :parse => true ) end
# File lib/fog/vcloud/requests/compute/login.rb, line 8 def login headers = { 'Authorization' => authorization_header } uri = if version == '1.0' "#{base_url}/login" else "#{base_path_url}/sessions" end unauthenticated_request({ :expects => 200, :headers => headers, :method => 'POST', :parse => true, :uri => uri }) end
# File lib/fog/vcloud/compute.rb, line 199 def reload @connections.each_value { |k,v| v.reset if v } 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
# File lib/fog/vcloud/compute.rb, line 255 def request(params) unless @cookie do_login end begin do_request(params) rescue Excon::Errors::Unauthorized => e do_login do_request(params) end end
# File lib/fog/vcloud/requests/compute/undeploy.rb, line 5 def undeploy(vapp_uri, save_state = false) # builder = Builder::XmlMarkup.new # builder.UndeployVAppParams(:xmlns => 'http://www.vmware.com/vcloud/v1', # :saveState => save_state) {} builder = "<UndeployVAppParams saveState=\"#{save_state.to_s}\" xmlns=\"http://www.vmware.com/vcloud/v1\"/>" request( :body => builder, :expects => 202, :headers => {'Content-Type' => 'application/vnd.vmware.vcloud.undeployVAppParams+xml' }, :method => 'POST', :uri => vapp_uri + '/action/undeploy', :parse => true ) end
# File lib/fog/vcloud/requests/compute/clone_vapp.rb, line 6 def validate_clone_vapp_options(options) valid_opts = [:name, :poweron] unless valid_opts.all? { |opt| options.has_key?(opt) } raise ArgumentError.new("Required data missing: #{(valid_opts - options.keys).map(&:inspect).join(", ")}") end end
# File lib/fog/vcloud/requests/compute/configure_network.rb, line 6 def validate_network_data(network_data, configure=false) valid_opts = [:id, :href, :name, :address, :broadcast, :gateway] unless valid_opts.all? { |opt| network_data.has_key?(opt) } raise ArgumentError.new("Required data missing: #{(valid_opts - network_data.keys).map(&:inspect).join(", ")}") end end
# File lib/fog/vcloud/compute.rb, line 238 def xmlns 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