From 33136931af61811dc0421af2c43b74006dfb2119 Mon Sep 17 00:00:00 2001
From: semedi <sergio.semedi@gmail.com>
Date: Mon, 26 Feb 2018 11:35:58 +0100
Subject: [PATCH] F #1757: new vcenter vm constructors, methods and small fixes

F #1757: commenting new constructors, some visual changes
---
 src/tm_mad/vcenter/cpds                       |   2 +-
 src/tm_mad/vcenter/delete                     |   5 +-
 src/tm_mad/vcenter/mvds                       |   2 +-
 src/tm_mad/vcenter/resize                     |   2 +-
 .../remotes/lib/vcenter_driver/host.rb        |   2 +-
 .../remotes/lib/vcenter_driver/importer.rb    |   2 +-
 .../lib/vcenter_driver/virtual_machine.rb     | 146 ++++++++++++++----
 src/vmm_mad/remotes/vcenter/attach_disk       |   6 +-
 src/vmm_mad/remotes/vcenter/attach_nic        |   6 +-
 src/vmm_mad/remotes/vcenter/cancel            |   2 +-
 src/vmm_mad/remotes/vcenter/deploy            |  20 +--
 src/vmm_mad/remotes/vcenter/detach_disk       |   4 +-
 src/vmm_mad/remotes/vcenter/detach_nic        |   5 +-
 src/vmm_mad/remotes/vcenter/migrate           |   2 +-
 src/vmm_mad/remotes/vcenter/poll              |   3 +-
 src/vmm_mad/remotes/vcenter/reboot            |   4 +-
 src/vmm_mad/remotes/vcenter/reconfigure       |   5 +-
 src/vmm_mad/remotes/vcenter/reset             |   4 +-
 src/vmm_mad/remotes/vcenter/restore           |   2 +-
 src/vmm_mad/remotes/vcenter/save              |   2 +-
 src/vmm_mad/remotes/vcenter/shutdown          |   2 +-
 src/vmm_mad/remotes/vcenter/snapshot_create   |   6 +-
 src/vmm_mad/remotes/vcenter/snapshot_delete   |   2 +-
 src/vmm_mad/remotes/vcenter/snapshot_revert   |   2 +-
 24 files changed, 161 insertions(+), 77 deletions(-)

diff --git a/src/tm_mad/vcenter/cpds b/src/tm_mad/vcenter/cpds
index 98003d4309..2e57c16293 100755
--- a/src/tm_mad/vcenter/cpds
+++ b/src/tm_mad/vcenter/cpds
@@ -67,7 +67,7 @@ end
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
 
     if vm.has_snapshots?
         STDERR.puts "'disk-saveas' operation is not supported for VMs with system snapshots."
diff --git a/src/tm_mad/vcenter/delete b/src/tm_mad/vcenter/delete
index bd3665cbb7..63a396b675 100755
--- a/src/tm_mad/vcenter/delete
+++ b/src/tm_mad/vcenter/delete
@@ -62,15 +62,16 @@ begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
     if !!vm_ref && !vm_ref.empty?
-        vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+        vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
     else
+        # we try to get vcenter item
         vcenter_vm = VCenterDriver::VIHelper.find_vcenter_vm_by_name(one_vm, host, vi_client)
 
         # If no VM object retrieved, raise an exception
         raise "Could not find the undeployed VM in vCenter's inventory using it's name" if !vcenter_vm
 
         vm_ref = vcenter_vm._ref
-        vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+        vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
     end
 rescue Exception => e
     vi_client.close_connection if vi_client
diff --git a/src/tm_mad/vcenter/mvds b/src/tm_mad/vcenter/mvds
index b5bb41c3ea..e8df4f5a22 100755
--- a/src/tm_mad/vcenter/mvds
+++ b/src/tm_mad/vcenter/mvds
@@ -60,7 +60,7 @@ disk = one_vm.retrieve_xmlelements("TEMPLATE/DISK[DISK_ID=#{disk_id}]").first
 
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid)
 
     vm.one_item = one_vm
 
diff --git a/src/tm_mad/vcenter/resize b/src/tm_mad/vcenter/resize
index d60332441c..d253dff9fb 100755
--- a/src/tm_mad/vcenter/resize
+++ b/src/tm_mad/vcenter/resize
@@ -58,7 +58,7 @@ begin
 
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vmid )
 
     # Cannot resize if VM has snapshots
     if vm.has_snapshots?
diff --git a/src/vmm_mad/remotes/lib/vcenter_driver/host.rb b/src/vmm_mad/remotes/lib/vcenter_driver/host.rb
index cdac561e04..b7e81318b2 100644
--- a/src/vmm_mad/remotes/lib/vcenter_driver/host.rb
+++ b/src/vmm_mad/remotes/lib/vcenter_driver/host.rb
@@ -436,7 +436,7 @@ class ClusterComputeResource
 
         vms.each do |vm_ref,info|
             begin
-                vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, @vi_client)
+                vm = VCenterDriver::VirtualMachine.new_without_id(@vi_client, vm_ref)
                 esx_host = esx_hosts[info["runtime.host"]._ref]
                 info[:esx_host_name] = esx_host[:name]
                 info[:esx_host_cpu] = esx_host[:cpu]
diff --git a/src/vmm_mad/remotes/lib/vcenter_driver/importer.rb b/src/vmm_mad/remotes/lib/vcenter_driver/importer.rb
index ae933af322..bf019f6f93 100644
--- a/src/vmm_mad/remotes/lib/vcenter_driver/importer.rb
+++ b/src/vmm_mad/remotes/lib/vcenter_driver/importer.rb
@@ -27,7 +27,7 @@ def self.import_wild(host_id, vm_ref, one_vm, template)
             raise "Could not get OpenNebula HostPool: #{hpool.message}"
         end
 
-        vcenter_vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+        vcenter_vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
         vm_name    = vcenter_vm["name"]
 
         wild     = true
diff --git a/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb b/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb
index 97acd9dd34..42621bc969 100644
--- a/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb
+++ b/src/vmm_mad/remotes/lib/vcenter_driver/virtual_machine.rb
@@ -806,7 +806,7 @@ class Template
 
 
             # Determine the location path for the template
-            vcenter_template = VCenterDriver::VirtualMachine.new_from_ref(template_ref, vi_client)
+            vcenter_template = VCenterDriver::VirtualMachine.new_without_id(vi_client, template_ref)
             item = vcenter_template.item
             folders = []
             while !item.instance_of? RbVmomi::VIM::Datacenter
@@ -857,7 +857,7 @@ class Template
             # Get the OpenNebula's template hash
             one_tmp[:one] = template_to_one(template, vcenter_uuid, template_ccr_ref, template_ccr_name, import_name, host_id)
             return one_tmp
-        rescue
+        rescue Exception => e
             return nil
         end
     end
@@ -883,9 +883,13 @@ class VirtualMachine < Template
 
     include Memoize
 
-    def initialize(item=nil, vi_client=nil)
-        @item = item
+    def initialize(vi_client, ref, one_id)
+        if (ref)
+            @item = RbVmomi::VIM::VirtualMachine.new(vi_client.vim, ref)
+        end
+
         @vi_client = vi_client
+        @vm_id = one_id
         @locking = true
         @vm_info = nil
     end
@@ -909,11 +913,11 @@ class VirtualMachine < Template
     # @return OpenNebula::VirtualMachine or XMLElement
     def one_item
         if !@one_item
-            vm_id = get_vm_id
-
-            raise "Unable to find vm_id." if vm_id.nil?
-
-            @one_item = VIHelper.one_item(OpenNebula::VirtualMachine, vm_id)
+            if @vm_id != -1
+                @one_item = VIHelper.one_item(OpenNebula::VirtualMachine, @vm_id)
+            else
+                raise "VCenterDriver::Virtualmachine: OpenNebula ID is mandatory for this vm!"
+            end
         end
 
         @one_item
@@ -1133,14 +1137,11 @@ class VirtualMachine < Template
     # @param one_item OpenNebula::VirtualMachine
     # @param vi_client VCenterDriver::VIClient
     # @return String vmware ref
-    def clone_vm(one_item, vi_client)
-        @one_item = one_item
-        @vi_client = vi_client
-
+    def clone_vm(drv_action)
         vcenter_name = get_vcenter_name
 
-        vc_template_ref = one_item['USER_TEMPLATE/VCENTER_TEMPLATE_REF']
-        vc_template = RbVmomi::VIM::VirtualMachine(vi_client.vim, vc_template_ref)
+        vc_template_ref = drv_action['USER_TEMPLATE/VCENTER_TEMPLATE_REF']
+        vc_template = RbVmomi::VIM::VirtualMachine(@vi_client.vim, vc_template_ref)
 
         ds = get_ds
 
@@ -1148,7 +1149,7 @@ class VirtualMachine < Template
         disk_move_type = :moveAllDiskBackingsAndDisallowSharing
 
         if ds.instance_of? RbVmomi::VIM::Datastore
-            use_linked_clones = one_item['USER_TEMPLATE/VCENTER_LINKED_CLONES']
+            use_linked_clones = drv_action['USER_TEMPLATE/VCENTER_LINKED_CLONES']
             if use_linked_clones && use_linked_clones.downcase == "yes"
                 # Check if all disks in template has delta disks
                 disks = vc_template.config
@@ -1169,7 +1170,7 @@ class VirtualMachine < Template
 
         # Specify vm folder in vSpere's VM and Templates view F#4823
         vcenter_vm_folder = nil
-        vcenter_vm_folder = one_item["USER_TEMPLATE/VCENTER_VM_FOLDER"]
+        vcenter_vm_folder = drv_action["USER_TEMPLATE/VCENTER_VM_FOLDER"]
         vcenter_vm_folder_object = nil
         dc = cluster.get_dc
         if !!vcenter_vm_folder && !vcenter_vm_folder.empty?
@@ -1376,16 +1377,20 @@ class VirtualMachine < Template
         xpath = "TEMPLATE/NIC[OPENNEBULA_MANAGED=\"NO\" or OPENNEBULA_MANAGED=\"no\"]"
         unmanaged_nics = one_item.retrieve_xmlelements(xpath)
 
-        if !unmanaged_nics.empty?
-            index = 0
-            self["config.hardware.device"].each_with_index do |device|
-                if is_nic?(device)
-                    # Edit capacity setting new size in KB
-                    device.macAddress = unmanaged_nics[index]["MAC"]
-                    device_change << { :device => device, :operation => :edit }
-                    index += 1
+        begin
+            if !unmanaged_nics.empty?
+                index = 0
+                self["config.hardware.device"].each_with_index do |device|
+                    if is_nic?(device)
+                        # Edit capacity setting new size in KB
+                        device.macAddress = unmanaged_nics[index]["MAC"]
+                        device_change << { :device => device, :operation => :edit }
+                        index += 1
+                    end
                 end
             end
+        rescue Exception => e
+            raise "There is a problem with your vm NICS, make sure that they are working properly. Error: #{e.message}"
         end
 
         # Save in extraconfig the key for unmanaged disks
@@ -3080,11 +3085,96 @@ class VirtualMachine < Template
         end
     end
 
-    # TODO check with uuid
-    def self.new_from_ref(ref, vi_client)
-        self.new(RbVmomi::VIM::VirtualMachine.new(vi_client.vim, ref), vi_client)
+    # STATIC MEMBERS AND CONSTRUCTORS
+    ###############################################################################################
+
+    def self.get_id(opts = {})
+        id = -1
+
+        if (opts[:name])
+                matches = opts[:name].match(/^one-(\d*)(-(.*))?$/)
+                id = matches[1] if matches
+        end
+
+        if id == -1
+            one_vm = VCenterDriver::VIHelper.find_by_ref(OpenNebula::VirtualMachinePool,
+                                                         "DEPLOY_ID",
+                                                         opts[:ref],
+                                                         opts[:vc_uuid],
+                                                         opts[:pool])
+            id = one_vm["ID"] if one_vm
+        end
+
+        return id
     end
 
+    # Try to build the vcenterdriver virtualmachine without
+    # any opennebula id or object, this constructor can find
+    # inside the opennebula pool until match
+    #
+    # @param vi_client [vi_client] the vcenterdriver client that allows the connection
+    # @param ref [String] vcenter ref to the vm
+    # @param opts [Hash] object with pairs that could contain multiple option
+    #        :vc_uuid: give the vcenter uuid directly
+    #        :name:    the vcenter vm name for extract the opennebula id
+    #
+    # @return [vcenterdriver::vm] the virtual machine
+    def self.new_from_ref(vi_client, ref, opts = {})
+        unless opts[:vc_uuid]
+            opts[:vc_uuid] = vi_client.vim.serviceContent.about.instanceUuid
+        end
+
+        opts[:ref] = ref
+
+        vm_id = VCenterDriver::VirtualMachine.get_id(opts)
+
+        self.new(vi_client, ref, vm_id)
+    end
+
+    # build a vcenterdriver virtual machine from a template
+    # this function is used to instantiate vcenter vms
+    #
+    # @param vi_client [vi_client] the vcenterdriver client that allows the connection
+    # @param drv_action [xmleleent] driver_action that contains the info
+    # @param id [int] the if of the opennebula virtual machine
+    #
+    # @return [vcenterdriver::vm] the virtual machine
+    def self.new_from_clone(vi_client, drv_action, id )
+        spawn = self.new(vi_client, nil, id).tap do |vm|
+            vm.clone_vm(drv_action)
+        end
+
+        return spawn
+    end
+
+    # build a vcenterdriver virtual machine
+    # with the opennebula object linked
+    #
+    # @param vi_client [vi_client] the vcenterdriver client that allows the connection
+    # @param ref [String] vcenter ref to the vm
+    # @param one_item [one::vm] xmlelement of opennebula
+    #
+    # @return [vcenterdriver::vm] the virtual machine
+    def self.new_one(vi_client, ref, one_item)
+        id = one_item["ID"] || one_item["VM/ID"] || -1
+
+        self.new(vi_client, ref, id).tap do |vm|
+            vm.one_item = one_item
+        end
+    end
+
+    # build a vcenterdriver virtual machine
+    # without opennebula object link, use id = -1 instead
+    #
+    # @param vi_client [vi_client] the vcenterdriver client that allows the connection
+    # @param ref [String] vcenter ref to the vm
+    #
+    # @return [vcenterdriver::vm] the virtual machine
+    def self.new_without_id(vi_client, ref)
+        self.new(vi_client, ref, -1)
+    end
+
+    ###############################################################################################
 end # class VirtualMachine
 
 end # module VCenterDriver
diff --git a/src/vmm_mad/remotes/vcenter/attach_disk b/src/vmm_mad/remotes/vcenter/attach_disk
index 6a14328332..3de43b494d 100755
--- a/src/vmm_mad/remotes/vcenter/attach_disk
+++ b/src/vmm_mad/remotes/vcenter/attach_disk
@@ -42,10 +42,10 @@ host_id = host['ID']
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
-
-    vm.one_item = drv_action.retrieve_xmlelements('VM').first
+    # Setting one_item with info with the vm_xml including DISK to be added
+    one_item = drv_action.retrieve_xmlelements("VM").first
 
+    vm = VCenterDriver::VirtualMachine.new_one(vi_client, vm_ref, one_item)
     vm.attach_disk
 
 rescue Exception => e
diff --git a/src/vmm_mad/remotes/vcenter/attach_nic b/src/vmm_mad/remotes/vcenter/attach_nic
index 39ed7764ae..cd2734ea2f 100755
--- a/src/vmm_mad/remotes/vcenter/attach_nic
+++ b/src/vmm_mad/remotes/vcenter/attach_nic
@@ -41,10 +41,10 @@ host_id = host['ID']
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
-
     # Setting one_item with info with the vm_xml including NIC to be added
-    vm.one_item = drv_action.retrieve_xmlelements("VM").first
+    one_item = drv_action.retrieve_xmlelements("VM").first
+
+    vm = VCenterDriver::VirtualMachine.new_one(vi_client, vm_ref, one_item)
 
     vm.attach_nic
 
diff --git a/src/vmm_mad/remotes/vcenter/cancel b/src/vmm_mad/remotes/vcenter/cancel
index 6854ce6c61..21c95a263d 100755
--- a/src/vmm_mad/remotes/vcenter/cancel
+++ b/src/vmm_mad/remotes/vcenter/cancel
@@ -42,7 +42,7 @@ host_id = host['ID']
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vm_id)
 
     vm.one_item = drv_action.retrieve_xmlelements('VM').first
 
diff --git a/src/vmm_mad/remotes/vcenter/deploy b/src/vmm_mad/remotes/vcenter/deploy
index ba982b2b10..1414ba8cde 100755
--- a/src/vmm_mad/remotes/vcenter/deploy
+++ b/src/vmm_mad/remotes/vcenter/deploy
@@ -44,24 +44,18 @@ host_id      = drv_action["HISTORY_RECORDS/HISTORY/HID"]
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
+    one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id)
+
     if deploy_id && !deploy_id.empty?
+
         # VM is not new, we just need to reconfigure it and to power it on
-        vm = VCenterDriver::VirtualMachine.new_from_ref(deploy_id, vi_client)
-
-        # Setting one_item is optional, but it saves a couple of API calls if
-        # we already have it
-        one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id)
-        vm.one_item = one_vm
+        vm = VCenterDriver::VirtualMachine.new_one(vi_client, deploy_id, one_vm)
     else
-        # VM is new
-        vm = VCenterDriver::VirtualMachine.new
-
-        # Clone the VM from template and provide XML info
         vc_template_ref = drv_action['USER_TEMPLATE/VCENTER_TEMPLATE_REF']
-        vm.clone_vm(drv_action, vi_client)
 
-        one_vm = VCenterDriver::VIHelper.one_item(OpenNebula::VirtualMachine, vm_id)
-        vm.one_item = one_vm
+        # VM is new
+        # Clone the VM from template and provide XML info
+        vm = VCenterDriver::VirtualMachine.new_from_clone(vi_client, drv_action, vm_id)
 
         # Set reference to template disks and nics in VM template for detach ops
         vm.reference_unmanaged_devices(vc_template_ref)
diff --git a/src/vmm_mad/remotes/vcenter/detach_disk b/src/vmm_mad/remotes/vcenter/detach_disk
index e0a5f2c57b..9eb1881f23 100755
--- a/src/vmm_mad/remotes/vcenter/detach_disk
+++ b/src/vmm_mad/remotes/vcenter/detach_disk
@@ -41,7 +41,7 @@ host_id = host['ID']
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
 
     raise "vCenter doesn't allow to remove a virtual disk if it's part of a "\
           "snapshot of the virtual machine." if vm.has_snapshots?
@@ -53,4 +53,4 @@ rescue Exception => e
     exit -1
 ensure
     vi_client.close_connection if vi_client
-end
\ No newline at end of file
+end
diff --git a/src/vmm_mad/remotes/vcenter/detach_nic b/src/vmm_mad/remotes/vcenter/detach_nic
index cc5c4e4070..aebede02c8 100755
--- a/src/vmm_mad/remotes/vcenter/detach_nic
+++ b/src/vmm_mad/remotes/vcenter/detach_nic
@@ -41,10 +41,9 @@ host_id = host['ID']
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
-
     # Setting one_item with info with the vm_xml including NIC to be added
-    vm.one_item = drv_action.retrieve_xmlelements("VM").first
+    one_item = drv_action.retrieve_xmlelements("VM").first
+    vm = VCenterDriver::VirtualMachine.new_one(vi_client, vm_ref, one_item)
 
     vm.detach_nic
 
diff --git a/src/vmm_mad/remotes/vcenter/migrate b/src/vmm_mad/remotes/vcenter/migrate
index 59c4fb38ab..c063a7bf8a 100755
--- a/src/vmm_mad/remotes/vcenter/migrate
+++ b/src/vmm_mad/remotes/vcenter/migrate
@@ -54,7 +54,7 @@ begin
     dst_host.info
 
     # required vcenter objects
-    vc_vm = VCenterDriver::VirtualMachine.new_from_ref(vm["/VM/DEPLOY_ID"], vi_client)
+    vc_vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm["/VM/DEPLOY_ID"])
     vc_host = VCenterDriver::ClusterComputeResource.new_from_ref(dst_host["/HOST/TEMPLATE/VCENTER_CCR_REF"],vi_client).item
 
     config = {:cluster => vc_host }
diff --git a/src/vmm_mad/remotes/vcenter/poll b/src/vmm_mad/remotes/vcenter/poll
index c258b3c381..872c0b98be 100755
--- a/src/vmm_mad/remotes/vcenter/poll
+++ b/src/vmm_mad/remotes/vcenter/poll
@@ -32,6 +32,7 @@ require 'vcenter_driver'
 
 vm_ref          = ARGV[0]
 vc_cluster_name = ARGV[1]
+vm_id           = ARGV[2]
 
 host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name)
 host_id = host['ID']
@@ -39,7 +40,7 @@ host_id = host['ID']
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vi_client)
 
     vm.monitor_poll_vm
 
diff --git a/src/vmm_mad/remotes/vcenter/reboot b/src/vmm_mad/remotes/vcenter/reboot
index 84503f6aec..0685d6009f 100755
--- a/src/vmm_mad/remotes/vcenter/reboot
+++ b/src/vmm_mad/remotes/vcenter/reboot
@@ -38,7 +38,7 @@ host_id = host['ID']
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
 
     vm.reboot
 
@@ -50,4 +50,4 @@ rescue Exception => e
     exit -1
 ensure
     vi_client.close_connection if vi_client
-end
\ No newline at end of file
+end
diff --git a/src/vmm_mad/remotes/vcenter/reconfigure b/src/vmm_mad/remotes/vcenter/reconfigure
index 51cdff66d5..eabe53f369 100755
--- a/src/vmm_mad/remotes/vcenter/reconfigure
+++ b/src/vmm_mad/remotes/vcenter/reconfigure
@@ -31,6 +31,7 @@ require 'vcenter_driver'
 
 vm_ref          = ARGV[0]
 vc_cluster_name = ARGV[-1]
+vm_id           = ARGV[-2]
 
 host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, vc_cluster_name)
 host_id = host['ID']
@@ -38,7 +39,7 @@ host_id = host['ID']
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new(vi_client, vm_ref, vm_id)
 
     vm.regenerate_context
 
@@ -50,4 +51,4 @@ rescue Exception => e
     exit -1
 ensure
     vi_client.close_connection if vi_client
-end
\ No newline at end of file
+end
diff --git a/src/vmm_mad/remotes/vcenter/reset b/src/vmm_mad/remotes/vcenter/reset
index 01d1f208dd..2194210c12 100755
--- a/src/vmm_mad/remotes/vcenter/reset
+++ b/src/vmm_mad/remotes/vcenter/reset
@@ -38,7 +38,7 @@ host_id = host['ID']
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
 
     vm.reset
 
@@ -50,4 +50,4 @@ rescue Exception => e
     exit -1
 ensure
     vi_client.close_connection if vi_client
-end
\ No newline at end of file
+end
diff --git a/src/vmm_mad/remotes/vcenter/restore b/src/vmm_mad/remotes/vcenter/restore
index 1a746ada8b..3d9fd90932 100755
--- a/src/vmm_mad/remotes/vcenter/restore
+++ b/src/vmm_mad/remotes/vcenter/restore
@@ -38,7 +38,7 @@ host_id = host['ID']
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
 
     vm.poweron
 
diff --git a/src/vmm_mad/remotes/vcenter/save b/src/vmm_mad/remotes/vcenter/save
index c57391d6e4..a8796c7639 100755
--- a/src/vmm_mad/remotes/vcenter/save
+++ b/src/vmm_mad/remotes/vcenter/save
@@ -57,7 +57,7 @@ end
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
 
     vm.suspend
 
diff --git a/src/vmm_mad/remotes/vcenter/shutdown b/src/vmm_mad/remotes/vcenter/shutdown
index bd037cb920..1e19f4866f 100755
--- a/src/vmm_mad/remotes/vcenter/shutdown
+++ b/src/vmm_mad/remotes/vcenter/shutdown
@@ -62,7 +62,7 @@ begin
         vm_ref = vcenter_vm._ref
     end
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
 
     vm.shutdown #Undeploy, Poweroff or Terminate
 
diff --git a/src/vmm_mad/remotes/vcenter/snapshot_create b/src/vmm_mad/remotes/vcenter/snapshot_create
index 7370a1c6bb..fe4b144bab 100755
--- a/src/vmm_mad/remotes/vcenter/snapshot_create
+++ b/src/vmm_mad/remotes/vcenter/snapshot_create
@@ -44,11 +44,9 @@ snap_name = drv_action["VM/TEMPLATE/SNAPSHOT[ACTIVE='YES']/NAME"]
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
 
     one_vm = drv_action.retrieve_xmlelements("VM").first
-
-    vm.one_item = one_vm
+    vm = VCenterDriver::VirtualMachine.new_one(vi_client, vm_ref, one_vm)
 
     persistent_disks = one_vm.retrieve_xmlelements("TEMPLATE/DISK[PERSISTENT=YES]")
 
@@ -69,4 +67,4 @@ rescue Exception => e
     exit -1
 ensure
     vi_client.close_connection if vi_client
-end
\ No newline at end of file
+end
diff --git a/src/vmm_mad/remotes/vcenter/snapshot_delete b/src/vmm_mad/remotes/vcenter/snapshot_delete
index 2db38311dd..ed840b60f7 100755
--- a/src/vmm_mad/remotes/vcenter/snapshot_delete
+++ b/src/vmm_mad/remotes/vcenter/snapshot_delete
@@ -39,7 +39,7 @@ host_id = host['ID']
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
 
     vm.delete_snapshot(snap_id)
 
diff --git a/src/vmm_mad/remotes/vcenter/snapshot_revert b/src/vmm_mad/remotes/vcenter/snapshot_revert
index ec360a49bb..81abc96501 100755
--- a/src/vmm_mad/remotes/vcenter/snapshot_revert
+++ b/src/vmm_mad/remotes/vcenter/snapshot_revert
@@ -39,7 +39,7 @@ host_id = host['ID']
 begin
     vi_client = VCenterDriver::VIClient.new_from_host(host_id)
 
-    vm = VCenterDriver::VirtualMachine.new_from_ref(vm_ref, vi_client)
+    vm = VCenterDriver::VirtualMachine.new_without_id(vi_client, vm_ref)
 
     vm.revert_snapshot(snap_id)