<commit_before>require 'log4r'

module VagrantPlugins
  module VCenter
    module Action
      # This Class read the power state of the VM that Vagrant is managing.
      class ReadState
        def initialize(app, env)
          @app = app
          @logger = Log4r::Logger.new('vagrant_vcenter::action::read_state')
        end

        def call(env)
          env[:machine_state_id] = read_state(env)
          @app.call env
        end

        def read_state(env)
          if env[:machine].id.nil?
            @logger.info('VM is not created yet')
            return :not_created
          end

          # FIXME: this part needs some cleanup
          config = env[:machine].provider_config

          # FIXME: Raise a correct exception
          dc = config.vcenter_cnx.serviceInstance.find_datacenter(
               config.datacenter_name) or abort 'datacenter not found'

          root_vm_folder = dc.vmFolder

          vm = root_vm_folder.findByUuid(env[:machine].id)

          # @logger.debug("Current power state: #{vm.runtime.powerState}")
          vm_name = env[:machine].name

          if vm.runtime.powerState == 'poweredOff'
            @logger.info("VM [#{vm_name}] is stopped")
            return :stopped
          elsif vm.runtime.powerState == 'poweredOn'
            @logger.info("VM [#{vm_name}] is running")
            return :running
          elsif vm.runtime.powerState == 'suspended'
            @logger.info("VM [#{vm_name}] is suspended")
            return :suspended
          end
        end
      end
    end
  end
end<commit_msg>Avoid error message when vm cannot be found<commit_after>require 'log4r'

module VagrantPlugins
  module VCenter
    module Action
      # This Class read the power state of the VM that Vagrant is managing.
      class ReadState
        def initialize(app, env)
          @app = app
          @logger = Log4r::Logger.new('vagrant_vcenter::action::read_state')
        end

        def call(env)
          env[:machine_state_id] = read_state(env)
          @app.call env
        end

        def read_state(env)
          if env[:machine].id.nil?
            @logger.info('VM is not created yet')
            return :not_created
          end

          # FIXME: this part needs some cleanup
          config = env[:machine].provider_config

          # FIXME: Raise a correct exception
          dc = config.vcenter_cnx.serviceInstance.find_datacenter(
               config.datacenter_name) or abort 'datacenter not found'

          root_vm_folder = dc.vmFolder

          vm = root_vm_folder.findByUuid(env[:machine].id)

          # @logger.debug("Current power state: #{vm.runtime.powerState}")
          vm_name = env[:machine].name

          if vm.nil?
            # If the VM has been cloned/moved, the findByUuid call returns nil
            @logger.info("VM [#{vm_name}] state is unknown")
            return :unknown
          elsif vm.runtime.powerState == 'poweredOff'
            @logger.info("VM [#{vm_name}] is stopped")
            return :stopped
          elsif vm.runtime.powerState == 'poweredOn'
            @logger.info("VM [#{vm_name}] is running")
            return :running
          elsif vm.runtime.powerState == 'suspended'
            @logger.info("VM [#{vm_name}] is suspended")
            return :suspended
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09412114014251781, 'fraction_numerical': 0.0011876484560570072, 'mean_word_length': 1.94493006993007, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1997876', 'n_tokens_mistral': 958, 'n_tokens_neox': 946, 'n_words': 255}