Sha256: 794899e509107537f34823e4419ef6e28ac8551aa3cc392900cfcb6e611891d5

Contents?: true

Size: 1.49 KB

Versions: 2

Compression:

Stored size: 1.49 KB

Contents

module Terraforming
  module Resource
    class SecurityGroup
      include Terraforming::Util

      def self.tf(client = Aws::EC2::Client.new)
        self.new(client).tf
      end

      def self.tfstate(client = Aws::EC2::Client.new)
        self.new(client).tfstate
      end

      def initialize(client)
        @client = client
      end

      def tf
        apply_template(@client, "tf/security_group")
      end

      def tfstate
        resources = security_groups.inject({}) do |result, security_group|
          attributes = {
            "description" => security_group.description,
            "egress.#" => security_group.ip_permissions_egress.length.to_s,
            "id" => security_group.group_id,
            "ingress.#" => security_group.ip_permissions.length.to_s,
            "name" => security_group.group_name,
            "owner_id" => security_group.owner_id,
            "vpc_id" => security_group.vpc_id || "",
          }
          result["aws_security_group.#{module_name_of(security_group)}"] = {
            "type" => "aws_security_group",
            "primary" => {
              "id" => security_group.group_id,
              "attributes" => attributes
            }
          }

          result
        end

        generate_tfstate(resources)
      end

      private

      def module_name_of(security_group)
        normalize_module_name(security_group.group_name)
      end

      def security_groups
        @client.describe_security_groups.security_groups
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
terraforming-0.0.3 lib/terraforming/resource/security_group.rb
terraforming-0.0.2 lib/terraforming/resource/security_group.rb