<commit_before>require 'kelbim/policy-types'

# XXX: internal, subnets, zone

module Kelbim
  class DSL
    class Converter
      class << self
        def convert(exported, instance_names)
          self.new(exported, instance_names).convert
        end
      end # of class methods

      def initialize(exported, instance_names)
        @exported = exported
        @instance_names = instance_names
      end

      def convert
        @exported.each.map {|vpc, load_balancers|
          output_ec2(vpc, load_balancers)
        }.join("\n")
      end

      private
      def output_ec2(vpc, load_balancers)
        arg_vpc = vpc ? vpc.inspect + ' ' : ''
        load_balancers = load_balancers.map {|name, load_balancer|
          output_load_balancer(vpc, name, load_balancer)
        }.join("\n").strip

        <<-EOS
ec2 #{arg_vpc}do
  #{load_balancers}
end
        EOS
      end

      def output_load_balancer(vpc, name, load_balancer)
        name = name.inspect
        internal = (load_balancer[:scheme] == 'internal') ? ', :internal => true ' : ' '
        instances = output_instances(load_balancer[:instances]).strip
        listeners = output_listeners(load_balancer[:listeners]).strip
        health_check = output_health_check(load_balancer[:health_check]).strip

        out = <<-EOS
  load_balancer #{name}#{internal}do
    #{instances}

    #{listeners}

    #{health_check}
        EOS

        if vpc
          subnets = load_balancer[:subnets]
          subnets = subnets.map {|i| i.inspect }.join("\n,      ")

          out << "\n"
          out.concat(<<-EOS)
    subnets(
      #{subnets}
    )
          EOS
        else
        end

        out << "  end\n"
        return out
      end

      def output_instances(instances)<commit_msg>add security_groups dsl<commit_after>require 'kelbim/policy-types'

# XXX: internal, subnets, zone

module Kelbim
  class DSL
    class Converter
      class << self
        def convert(exported, instance_names)
          self.new(exported, instance_names).convert
        end
      end # of class methods

      def initialize(exported, instance_names)
        @exported = exported
        @instance_names = instance_names
      end

      def convert
        @exported.each.map {|vpc, load_balancers|
          output_ec2(vpc, load_balancers)
        }.join("\n")
      end

      private
      def output_ec2(vpc, load_balancers)
        arg_vpc = vpc ? vpc.inspect + ' ' : ''
        load_balancers = load_balancers.map {|name, load_balancer|
          output_load_balancer(vpc, name, load_balancer)
        }.join("\n").strip

        <<-EOS
ec2 #{arg_vpc}do
  #{load_balancers}
end
        EOS
      end

      def output_load_balancer(vpc, name, load_balancer)
        name = name.inspect
        internal = (load_balancer[:scheme] == 'internal') ? ', :internal => true ' : ' '
        instances = output_instances(load_balancer[:instances]).strip
        listeners = output_listeners(load_balancer[:listeners]).strip
        health_check = output_health_check(load_balancer[:health_check]).strip

        out = <<-EOS
  load_balancer #{name}#{internal}do
    #{instances}

    #{listeners}

    #{health_check}
        EOS

        if vpc
          subnets = load_balancer[:subnets]
          subnets = subnets.map {|i| i.inspect }.join("\n,      ")

          out << "\n"
          out.concat(<<-EOS)
    subnets(
      #{subnets}
    )
          EOS

          security_groups = load_balancer[:security_groups]
          security_groups = security_groups.map {|i| i.inspect }.join("\n,      ")

          out << "\n"
          out.concat(<<-EOS)
    security_groups(
      #{security_groups}
    )
          EOS
        else
        end

        out << "  end\n"
        return out
      end

      def output_instances(instances)
{'fraction_non_alphanumeric': 0.10976890756302521, 'fraction_numerical': 0.0015756302521008404, 'mean_word_length': 2.104319478402608, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 5, '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': '2459496', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1211, 'n_words': 238}