From aeb98ec8ab8a4079e253eefc18b72c55f07471e3 Mon Sep 17 00:00:00 2001 From: Chao Li Date: Wed, 13 Jun 2018 00:20:24 +0800 Subject: [PATCH] vim.rb: enhance the logic of vim.rev. (#139) * vim.rb: enhance the logic of vim.rev. 1. No matter if rev is specified when calls RbVmomi.connect, vim.rev should be real version that effects current VIM connection. Without this fix, say, vSphere is 6.5, and we give 7.0 to RbVmomi.connect, then returned vim.rev will be 7.0. Even if we give 100.0, obviously 100.0 is not a reasonable vSphere version, returned vim.rev will be 100.0. With this fix, returned vim.rev will be min value of vSphere version and specified rev. 2. Enhanced version comparison. Without this fix, version comparison is done by string comparison, as a result "100.0" is smaller than "6.0". vSphere version has reached to 8.x, once it reaches to "10.0", this bug will be triggered. With this fix, a version number is split into parts by ".", then compare each part from left to right, so that "10.0" is bigger than "6.0". * Revert "vim.rb: enhance the logic of vim.rev." This reverts commit 4efb0befa312304bd68a990b5749313aa2b577ec. * vim.rb: enhance the logic of vim.rev. 1. No matter if rev is specified when calls RbVmomi.connect, vim.rev should be real version that effects current VIM connection. Without this fix, say, vSphere is 6.5, and we give 7.0 to RbVmomi.connect, then returned vim.rev will be 7.0. Even if we give 100.0, obviously 100.0 is not a reasonable vSphere version, returned vim.rev will be 100.0. With this fix, returned vim.rev will be min value of vSphere version and specified rev. 2. Enhanced version comparison. Without this fix, version comparison is done by string comparison, as a result "100.0" is smaller than "6.0". vSphere version has reached to 8.x, once it reaches to "10.0", this bug will be triggered. With this fix, a version number is split into parts by ".", then compare each part from left to right, so that "10.0" is bigger than "6.0". --- lib/rbvmomi/vim.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/rbvmomi/vim.rb b/lib/rbvmomi/vim.rb index f4ef9141..c80e6db0 100644 --- a/lib/rbvmomi/vim.rb +++ b/lib/rbvmomi/vim.rb @@ -39,8 +39,7 @@ def self.connect opts opts[:port] ||= (opts[:ssl] ? 443 : 80) opts[:path] ||= '/sdk' opts[:ns] ||= 'urn:vim25' - rev_given = opts[:rev] != nil - opts[:rev] = '6.5' unless rev_given + opts[:rev] = '6.5' if opts[:rev].nil? opts[:debug] = (!ENV['RBVMOMI_DEBUG'].empty? rescue false) unless opts.member? :debug conn = new(opts).tap do |vim| @@ -61,10 +60,8 @@ def self.connect opts vim.serviceContent.sessionManager.Login :userName => opts[:user], :password => opts[:password] end end - unless rev_given - rev = vim.serviceContent.about.apiVersion - vim.rev = [rev, '6.5'].min - end + rev = vim.serviceContent.about.apiVersion + vim.rev = [rev, opts[:rev]].min { |a, b| Gem::Version.new(a) <=> Gem::Version.new(b) } end at_exit { conn.close }