<commit_before>module Vcloud
  class Vm < Entity

    attr_reader :vcloud_attributes

    def initialize(vcloud_attributes, vapp)
      @vcloud_attributes = vcloud_attributes
      @fog_interface = FogServiceInterface.new
      @vapp = vapp
    end

    def customize(vm_config)
      configure_network_interfaces vm_config[:network_connections]
      if hardware_config = vm_config[:hardware_config]
        update_cpu_count(hardware_config[:cpu])
        update_memory_size_in_mb(hardware_config[:memory])
      end
      add_extra_disks(vm_config[:extra_disks])
      update_metadata(vm_config[:metadata])
      configure_guest_customization_section(
            @vapp.name,
            vm_config[:bootstrap]
            )
    end

    def update_memory_size_in_mb(new_memory)
      return if new_memory.nil?
      return if new_memory.to_i < 64
      unless memory.to_i == new_memory.to_i
        @fog_interface.put_memory(id, new_memory)
      end
    end

    def memory
      memory_item = virtual_hardware_section.detect { |i| i[:'rasd:ResourceType'] == '4' }
      memory_item[:'rasd:VirtualQuantity']
    end

    def cpu
      cpu_item = virtual_hardware_section.detect { |i| i[:'rasd:ResourceType'] == '3' }
      cpu_item[:'rasd:VirtualQuantity']
    end

    def update_cpu_count(new_cpu_count)
      return if new_cpu_count.nil?
      return if new_cpu_count.to_i == 0
      unless cpu.to_i == new_cpu_count.to_i
        @fog_interface.put_cpu(id, new_cpu_count)
      end
    end

    def update_metadata(metadata)
      return if metadata.nil?
      metadata.each do |k, v|
        @fog_interface.put_vapp_metadata_value(@vapp.id, k, v)
        @fog_interface.put_vapp_metadata_value(id, k, v)
      end
    end

    def add_extra_disks(extra_disks)
      vm = FogModelInterface.new.get_vm_by_href(@vcloud_attributes[:href])
      if extra_disks
        extra_disks.each do |extra_disk|
          Vcloud.logger.info("adding a disk of size #{extra_disk[:size]}MB into VM #{id}")
          vm.disks.create(extra_disk[:size])
        end
      end
    end

    def configure_network_interfaces(networks_config)
      return unless networks_config
      section = {PrimaryNetworkConnectionIndex: 0}
      section[:NetworkConnection] = networks_config.compact.each_with_index.map do |network, i|
        connection = {
            network: network[:name],
            needsCustomization: true,
            NetworkConnectionIndex: i,
            IsConnected: true
        }
        ip_address = network[:ip_address]
        connection[:IpAddress] = ip_address unless ip_address.nil?
        connection[:IpAddressAllocationMode] = ip_address ? 'MANUAL' : 'DHCP'
        connection
      end
      @fog_interface.put_network_connection_system_section_vapp(id, section)
    end

    def configure_guest_customization_section(name, bootstrap_config)
      if bootstrap_config.nil? or bootstrap_config[:script_path].nil?
        interpolated_preamble = ''
      else
        interpolated_preamble = generate_preamble(
            bootstrap_config[:script_path],
            bootstrap_config[:vars]
        )
      end
      @fog_interface.put_guest_customization_section(id, name, interpolated_preamble)<commit_msg>Dan/Sneha making extra_disks details available to preamble<commit_after>module Vcloud
  class Vm < Entity

    attr_reader :vcloud_attributes

    def initialize(vcloud_attributes, vapp)
      @vcloud_attributes = vcloud_attributes
      @fog_interface = FogServiceInterface.new
      @vapp = vapp
    end

    def customize(vm_config)
      configure_network_interfaces vm_config[:network_connections]
      if hardware_config = vm_config[:hardware_config]
        update_cpu_count(hardware_config[:cpu])
        update_memory_size_in_mb(hardware_config[:memory])
      end
      add_extra_disks(vm_config[:extra_disks])
      update_metadata(vm_config[:metadata])
      configure_guest_customization_section(
            @vapp.name,
            vm_config[:bootstrap],
            vm_config[:extra_disks]
            )
    end

    def update_memory_size_in_mb(new_memory)
      return if new_memory.nil?
      return if new_memory.to_i < 64
      unless memory.to_i == new_memory.to_i
        @fog_interface.put_memory(id, new_memory)
      end
    end

    def memory
      memory_item = virtual_hardware_section.detect { |i| i[:'rasd:ResourceType'] == '4' }
      memory_item[:'rasd:VirtualQuantity']
    end

    def cpu
      cpu_item = virtual_hardware_section.detect { |i| i[:'rasd:ResourceType'] == '3' }
      cpu_item[:'rasd:VirtualQuantity']
    end

    def update_cpu_count(new_cpu_count)
      return if new_cpu_count.nil?
      return if new_cpu_count.to_i == 0
      unless cpu.to_i == new_cpu_count.to_i
        @fog_interface.put_cpu(id, new_cpu_count)
      end
    end

    def update_metadata(metadata)
      return if metadata.nil?
      metadata.each do |k, v|
        @fog_interface.put_vapp_metadata_value(@vapp.id, k, v)
        @fog_interface.put_vapp_metadata_value(id, k, v)
      end
    end

    def add_extra_disks(extra_disks)
      vm = FogModelInterface.new.get_vm_by_href(@vcloud_attributes[:href])
      if extra_disks
        extra_disks.each do |extra_disk|
          Vcloud.logger.info("adding a disk of size #{extra_disk[:size]}MB into VM #{id}")
          vm.disks.create(extra_disk[:size])
        end
      end
    end

    def configure_network_interfaces(networks_config)
      return unless networks_config
      section = {PrimaryNetworkConnectionIndex: 0}
      section[:NetworkConnection] = networks_config.compact.each_with_index.map do |network, i|
        connection = {
            network: network[:name],
            needsCustomization: true,
            NetworkConnectionIndex: i,
            IsConnected: true
        }
        ip_address = network[:ip_address]
        connection[:IpAddress] = ip_address unless ip_address.nil?
        connection[:IpAddressAllocationMode] = ip_address ? 'MANUAL' : 'DHCP'
        connection
      end
      @fog_interface.put_network_connection_system_section_vapp(id, section)
    end

    def configure_guest_customization_section(name, bootstrap_config, extra_disks)
      if bootstrap_config.nil? or bootstrap_config[:script_path].nil?
        interpolated_preamble = ''
      else
        preamble_vars = bootstrap_config[:vars].merge(extra_disks)
        interpolated_preamble = generate_preamble(
            bootstrap_config[:script_path],
            preamble_vars,
        )
      end
      @fog_interface.put_guest_customization_section(id, name, interpolated_preamble)
{'fraction_non_alphanumeric': 0.08294162244124337, 'fraction_numerical': 0.0018195602729340408, 'mean_word_length': 2.9639423076923075, 'pattern_counts': {'":': 0, '<': 7, '<?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': '1628071', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2044, 'n_words': 361}