<commit_before>require_relative '../../../puppet_x/puppetlabs/aws.rb'
require 'base64'

Puppet::Type.type(:ec2_instance).provide(:v2, :parent => PuppetX::Puppetlabs::Aws) do
  confine feature: :aws
  confine feature: :retries

  mk_resource_methods
  remove_method :tags=

  def self.instances
    regions.collect do |region|
      begin
        instances = []
        subnets = Hash.new()

        subnets_response = ec2_client(region).describe_subnets()
        subnets_response.data.subnets.each do |subnet|
          subnet_name = name_from_tag(subnet)
          subnets[subnet.subnet_id] = subnet_name if subnet_name
        end

        ec2_client(region).describe_instances(filters: [
          {name: 'instance-state-name', values: ['pending', 'running', 'stopping', 'stopped']}
        ]).each do |response|
          response.data.reservations.each do |reservation|
            reservation.instances.each do |instance|
              hash = instance_to_hash(region, instance, subnets)
              instances << new(hash) if has_name?(hash)
            end
          end
        end
        instances
      rescue Timeout::Error, StandardError => e
        raise PuppetX::Puppetlabs::FetchingAWSDataError.new(region, self.resource_type.name.to_s, e.message)
      end
    end.flatten
  end

  read_only(:instance_id, :instance_type, :region, :user_data, :key_name,
            :availability_zones, :security_groups, :monitoring, :subnet,
            :ebs_optimized, :block_devices, :private_ip_address,
            :iam_instance_profile_arn, :iam_instance_profile_name)

  def self.prefetch(resources)
    instances.each do |prov|
      if resource = resources[prov.name] # rubocop:disable Lint/AssignmentInCondition
        resource.provider = prov if resource[:region] == prov.region
      end
    end
  end

  def self.instance_to_hash(region, instance, subnets)
    name = name_from_tag(instance)
    return {} unless name
    tags = {}
    subnet_name = nil
    monitoring = instance.monitoring.state == "enabled" ? true : false
    instance.tags.each do |tag|
      tags[tag.key] = tag.value unless tag.key == 'Name'
    end
    if instance.subnet_id
      subnet_name = subnets[instance.subnet_id] ? subnets[instance.subnet_id] : nil
    end

    devices = instance.block_device_mappings.collect do |mapping|
      {
        device_name: mapping.device_name,<commit_msg>Add image_id to read_only Ec2 properties<commit_after>require_relative '../../../puppet_x/puppetlabs/aws.rb'
require 'base64'

Puppet::Type.type(:ec2_instance).provide(:v2, :parent => PuppetX::Puppetlabs::Aws) do
  confine feature: :aws
  confine feature: :retries

  mk_resource_methods
  remove_method :tags=

  def self.instances
    regions.collect do |region|
      begin
        instances = []
        subnets = Hash.new()

        subnets_response = ec2_client(region).describe_subnets()
        subnets_response.data.subnets.each do |subnet|
          subnet_name = name_from_tag(subnet)
          subnets[subnet.subnet_id] = subnet_name if subnet_name
        end

        ec2_client(region).describe_instances(filters: [
          {name: 'instance-state-name', values: ['pending', 'running', 'stopping', 'stopped']}
        ]).each do |response|
          response.data.reservations.each do |reservation|
            reservation.instances.each do |instance|
              hash = instance_to_hash(region, instance, subnets)
              instances << new(hash) if has_name?(hash)
            end
          end
        end
        instances
      rescue Timeout::Error, StandardError => e
        raise PuppetX::Puppetlabs::FetchingAWSDataError.new(region, self.resource_type.name.to_s, e.message)
      end
    end.flatten
  end

  read_only(:instance_id, :instance_type, :image_id, :region, :user_data,
            :key_name, :availability_zones, :security_groups, :monitoring,
            :subnet, :ebs_optimized, :block_devices, :private_ip_address,
            :iam_instance_profile_arn, :iam_instance_profile_name)

  def self.prefetch(resources)
    instances.each do |prov|
      if resource = resources[prov.name] # rubocop:disable Lint/AssignmentInCondition
        resource.provider = prov if resource[:region] == prov.region
      end
    end
  end

  def self.instance_to_hash(region, instance, subnets)
    name = name_from_tag(instance)
    return {} unless name
    tags = {}
    subnet_name = nil
    monitoring = instance.monitoring.state == "enabled" ? true : false
    instance.tags.each do |tag|
      tags[tag.key] = tag.value unless tag.key == 'Name'
    end
    if instance.subnet_id
      subnet_name = subnets[instance.subnet_id] ? subnets[instance.subnet_id] : nil
    end

    devices = instance.block_device_mappings.collect do |mapping|
      {
        device_name: mapping.device_name,
{'fraction_non_alphanumeric': 0.10478170478170479, 'fraction_numerical': 0.002702702702702703, 'mean_word_length': 2.966199505358615, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2542038', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1519, 'n_words': 303}