<commit_before>module Fog
  module Compute
    class Vsphere
      class Real
        def list_virtual_machines(options = {})
          # Listing all VM's can be quite slow and expensive.  Try and optimize
          # based on the available options we have.  These conditions are in
          # ascending order of  time to complete for large deployments.
          if options['instance_uuid'] then
            list_all_virtual_machines_by_instance_uuid(options)
          elsif options['folder'] then
            list_all_virtual_machines_in_folder(options)
          else
            list_all_virtual_machines
          end
        end

        private

        def list_all_virtual_machines_in_folder(options = {})
          # Tap gets rid of the leading empty string and "Datacenters" element
          # and returns the array.
          path_elements = options['folder'].split('/').tap { |ary| ary.shift 2 }
          # The DC name itself.
          dc_name = path_elements.shift
          # If the first path element contains "vm" this denotes the vmFolder
          # and needs to be shifted out since each DC contains only one
          # vmFolder
          path_elements.shift if path_elements[0] = 'vm'
          # Make sure @datacenters is populated (the keys are DataCenter instances)
          self.datacenters.include? dc_name or raise ArgumentError, "Could not find a Datacenter named #{dc_name}"
          # Get the datacenter managed object
          dc = @datacenters[dc_name]

          # Get the VM Folder (Group) efficiently
          vm_folder = dc.vmFolder

          # Walk the tree resetting the folder pointer as we go
          folder = path_elements.inject(vm_folder) do |current_folder, sub_folder_name|
            # JJM VIM::Folder#find appears to be quite efficient as it uses the
            # searchIndex It certainly appears to be faster than
            # VIM::Folder#inventory since that returns _all_ managed objects of
            # a certain type _and_ their properties.
            sub_folder = current_folder.find(sub_folder_name, RbVmomi::VIM::Folder)
            raise ArgumentError, "Could not descend into #{sub_folder_name}.  Please check your path." unless sub_folder
            sub_folder
          end
<commit_msg>fix list_virtual_machines when using :folder<commit_after>module Fog
  module Compute
    class Vsphere
      class Real
        def list_virtual_machines(options = {})
          # Listing all VM's can be quite slow and expensive.  Try and optimize
          # based on the available options we have.  These conditions are in
          # ascending order of  time to complete for large deployments.
          if options['instance_uuid'] then
            list_all_virtual_machines_by_instance_uuid(options)
          elsif options['folder'] then
            list_all_virtual_machines_in_folder(options)
          else
            list_all_virtual_machines
          end
        end

        private

        def list_all_virtual_machines_in_folder(options = {})
          # Tap gets rid of the leading empty string and "Datacenters" element
          # and returns the array.
          path_elements = options['folder'].split('/').tap { |ary| ary.shift 2 }
          # The DC name itself.
          dc_name = path_elements.shift
          # If the first path element contains "vm" this denotes the vmFolder
          # and needs to be shifted out since each DC contains only one
          # vmFolder
          path_elements.shift if path_elements[0] == 'vm'
          # Make sure @datacenters is populated (the keys are DataCenter instances)
          self.datacenters.include? dc_name or raise ArgumentError, "Could not find a Datacenter named #{dc_name}"
          # Get the datacenter managed object
          dc = @datacenters[dc_name]

          # Get the VM Folder (Group) efficiently
          vm_folder = dc.vmFolder

          # Walk the tree resetting the folder pointer as we go
          folder = path_elements.inject(vm_folder) do |current_folder, sub_folder_name|
            # JJM VIM::Folder#find appears to be quite efficient as it uses the
            # searchIndex It certainly appears to be faster than
            # VIM::Folder#inventory since that returns _all_ managed objects of
            # a certain type _and_ their properties.
            sub_folder = current_folder.find(sub_folder_name, RbVmomi::VIM::Folder)
            raise ArgumentError, "Could not descend into #{sub_folder_name}.  Please check your path." unless sub_folder
            sub_folder
          end

{'fraction_non_alphanumeric': 0.056578947368421055, 'fraction_numerical': 0.0008771929824561404, 'mean_word_length': 2.317090909090909, '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': '370726', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1158, 'n_words': 432}