0 Replies Latest reply on Sep 4, 2019 3:32 AM by pradjini

    vSphere API always retrieves cached IP address

    pradjini Lurker

      Hi,

       

      we have a tool developed using vmware vpsher api .net sdk which basically performs various opertions below are few:

       

      1. connects to vsphere webclient,

      2. queries virtual machine specifed

      3. check power state of VM and change accordingly etc

      things are working fine, all of sudden we started facing strange problem.

       

      Problem:

      1. while the VM is in suspended state, from our tool, we power on the VM and then query for IP adress of the VM, in this case, suppose if the IP adress of the VM is changed form the time it goes to suspended state, till it is powered on, the ip adress retrivied using vpshere api ( vm.guest.ipadress) is still the old one but not the changed ipadress.

      for ex:

      Vm1 initail ipadress: 10.1.2.3

      vm1 goes to suspended state.

      vm1 ipadress is changed to 10.1.2.9

      tool will power on the vm from suspended state and when queries for IP adress it retrieves 10.1.2.3 and not the changed ip adress (10.1.2.9).

       

      for this we are calling VM. UpdateViewData() , whcih will refersh all the properties, but still finding the old ip address.

      below is the code snippet, can you please help how do i get changed IP address, or is there any better way to refersh and fetch the updated IP?

       

      code:

       

        public VirtualMachine WakeUpVirtualMachine(string vmsearch, string url, string username, string password, out VimClient vmclientinstance)

       

              {

                  var vspherenewConnection = new VsphereConnection();

                  var vmClient = vspherenewConnection.ConnectToVshereServer(url, username, password);

                  vmclientinstance = vmClient;

                  var newvm = vspherenewConnection.QueryVirtualMachines(vmsearch);

                  VirtualMachine newfoundmachine = null;

                  foreach (var foundnewvm in newvm)

                  {

                      newfoundmachine = foundnewvm;

                 }

                  if (newfoundmachine != null)

                  {            

                      var stopwatch = new Stopwatch();

                      stopwatch.Start();

                      while (stopwatch.Elapsed < TimeSpan.FromMinutes(2))

                      {

                           newfoundmachine.UpdateViewData();                                  

                          if (newfoundmachine.Guest.IpAddress != null)  

                          {                                                   

                              stopwatch.Stop();

                              stopwatch.Reset();

                              return newfoundmachine;

                          }

       

                     }

       

                }

      }