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

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

  mk_resource_methods
  remove_method :tags=

  def self.instances()
    regions.collect do |region|
      gateways = []
      ec2_client(region).describe_vpn_gateways.each do |response|
        response.data.vpn_gateways.each do |gateway|
          hash = gateway_to_hash(region, gateway)
          if hash[:name]
            gateways << new(hash) unless (gateway.state == "deleting" or gateway.state == "deleted")
          end
        end
      end
      gateways
    end.flatten
  end

  read_only(:vpc, :region, :type, :region, :availability_zone)

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

  def self.gateway_to_hash(region, gateway)
    attached = gateway.vpc_attachments.detect { |vpc| vpc.state == 'attached' }
    if attached
      vpc_id = attached.vpc_id
      vpc_response = ec2_client(region).describe_vpcs(
        vpc_ids: [vpc_id]
      )
      vpc = vpc_response.data.vpcs.first
      vpc_name_tag = vpc.tags.detect { |tag| tag.key == 'Name' }
      vpc_name = vpc_name_tag ? vpc_name_tag.value : nil
    else
      vpc_name = nil
      vpc_id = nil<commit_msg>Filter out gateways in the query, speeding up the response<commit_after>require_relative '../../../puppet_x/puppetlabs/aws.rb'
require 'retries'

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

  mk_resource_methods
  remove_method :tags=

  def self.instances()
    regions.collect do |region|
      gateways = []
      ec2_client(region).describe_vpn_gateways(filters: [
        {:name => 'state', :values => ['pending', 'available']}
      ]).each do |response|
        response.data.vpn_gateways.each do |gateway|
          hash = gateway_to_hash(region, gateway)
          if hash[:name]
            gateways << new(hash)
          end
        end
      end
      gateways
    end.flatten
  end

  read_only(:vpc, :region, :type, :region, :availability_zone)

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

  def self.gateway_to_hash(region, gateway)
    attached = gateway.vpc_attachments.detect { |vpc| vpc.state == 'attached' }
    if attached
      vpc_id = attached.vpc_id
      vpc_response = ec2_client(region).describe_vpcs(
        vpc_ids: [vpc_id]
      )
      vpc = vpc_response.data.vpcs.first
      vpc_name_tag = vpc.tags.detect { |tag| tag.key == 'Name' }
      vpc_name = vpc_name_tag ? vpc_name_tag.value : nil
    else
      vpc_name = nil
      vpc_id = nil
{'fraction_non_alphanumeric': 0.11216216216216217, 'fraction_numerical': 0.002702702702702703, 'mean_word_length': 2.8206451612903227, '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': '2757685', 'n_tokens_mistral': 1050, 'n_tokens_neox': 1033, 'n_words': 202}