Commit 4ed4f6d3 authored by sakazuki's avatar sakazuki

add network_interface

parent d8b3075a
......@@ -28,6 +28,7 @@ require "terraforming/resource/iam_role_policy"
require "terraforming/resource/iam_user"
require "terraforming/resource/iam_user_policy"
require "terraforming/resource/network_acl"
require "terraforming/resource/network_interface"
require "terraforming/resource/rds"
require "terraforming/resource/route53_record"
require "terraforming/resource/route53_zone"
......
......@@ -89,6 +89,11 @@ module Terraforming
execute(Terraforming::Resource::NetworkACL, options)
end
desc "nif", "Network Interface"
def nif
execute(Terraforming::Resource::NetworkInterface, options)
end
desc "r53r", "Route53 Record"
def r53r
execute(Terraforming::Resource::Route53Record, options)
......
module Terraforming
module Resource
class NetworkInterface
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/network_interface")
end
def tfstate
network_interfaces.inject({}) do |resources, network_interface|
attributes = {
"attachment.#" => attachment_of(network_interface) ? "1" : "0",
"id" => network_interface.network_interface_id,
"private_ips.#" => private_ips_of(network_interface).length.to_s,
"security_groups.#" => security_groups_of(network_interface).length.to_s,
"source_dest_check" => network_interface.source_dest_check.to_s,
"subnet_id" => network_interface.subnet_id,
"tags.#" => network_interface.tag_set.length.to_s,
}
resources["aws_network_interface.#{module_name_of(network_interface)}"] = {
"type" => "aws_network_interface",
"primary" => {
"id" => network_interface.network_interface_id,
"attributes" => attributes
}
}
resources
end
end
private
def attachment_of(network_interface)
network_interface.attachment
end
def private_ips_of(network_interface)
network_interface.private_ip_addresses.map{|addr| addr.private_ip_address }
end
def security_groups_of(network_interface)
network_interface.groups.map { |group| group.group_id }
end
def module_name_of(network_interface)
network_interface.network_interface_id
end
def network_interfaces
@client.describe_network_interfaces.network_interfaces
end
end
end
end
<% network_interfaces.each do |network_interface| -%>
resource "aws_network_interface" "<%= module_name_of(network_interface) %>" {
subnet_id = "<%= network_interface.subnet_id %>"
private_ips = <%= private_ips_of(network_interface).inspect %>
security_groups = <%= security_groups_of(network_interface).inspect %>
source_dest_check = <%= network_interface.source_dest_check %>
<% if network_interface.attachment && network_interface.attachment.instance_id -%>
attachment {
instance = "<%= network_interface.attachment.instance_id %>"
device_index = <%= network_interface.attachment.device_index %>
}
<% end -%>
<% if network_interface.tag_set.length > 0 -%>
tags {
<% network_interface.tag_set.each do |tag| -%>
"<%= tag.key %>" = "<%= tag.value %>"
<% end -%>
}
<% end -%>
}
<% end -%>
......@@ -151,6 +151,13 @@ module Terraforming
it_behaves_like "CLI examples"
end
describe "nif" do
let(:klass) { Terraforming::Resource::NetworkInterface }
let(:command) { :nif }
it_behaves_like "CLI examples"
end
describe "r53r" do
let(:klass) { Terraforming::Resource::Route53Record }
let(:command) { :r53r }
......
require "spec_helper"
module Terraforming
module Resource
describe NetworkInterface do
let(:client) do
Aws::EC2::Client.new(stub_responses: true)
end
let(:network_interfaces) do
[
{
status: "available",
mac_address: "11:11:11:11:11:11",
source_dest_check: true,
vpc_id: "vpc-12345678",
description: "test network_interface",
network_interface_id: "eni-1234abcd",
private_ip_addresses: [
{
private_dns_name: "ip-1-1-1-1.ap-northeast-1.compute.internal",
private_ip_address: "1.1.1.1",
primary: true
}
],
requester_managed: false,
groups: [
],
private_dns_name: "ip-1-1-1-1.ap-northeast-1.compute.internal",
availability_zone: "ap-northeast-1a",
requester_id: "234567890123",
subnet_id: "subnet-1234abcd",
owner_id: "123456789012",
private_ip_address: "1.1.1.1",
},
{
status: "in-use",
mac_address: "22:22:22:22:22:22",
source_dest_check: false,
vpc_id: "vpc-12345678",
description: "test network_interface",
association: {
public_ip: "9.9.9.9",
association_id: "eipassoc-63446006",
public_dns_name: "ec2-9-9-9-9.ap-northeast-1.compute.amazonaws.com",
allocation_id: "eipalloc-7fe93c1a",
ip_owner_id: "123456789012"
},
network_interface_id: "eni-2345efgh",
private_ip_addresses: [
{
private_dns_name: "ip-2-2-2-2.ap-northeast-1.compute.internal",
association: {
public_ip: "9.9.9.9",
association_id: "eipassoc-63446006",
public_dns_name: "ec2-9-9-9-9.ap-northeast-1.compute.amazonaws.com",
allocation_id: "eipalloc-7fe93c1a",
ip_owner_id: "123456789012"
},
private_ip_address: "2.2.2.2",
primary: true
},
{
private_dns_name: "ip-3-3-3-3.ap-northeast-1.compute.internal",
private_ip_address: "3.3.3.3",
primary: false
},
],
requester_managed: false,
groups: [
{
group_name: "test",
group_id: "sg-12345678",
},
{
group_name: "test2",
group_id: "sg-23456789",
}
],
attachment: {
status: "attached",
device_index: 0,
attach_time: Time.parse("2015-04-01 12:34:56 UTC"),
instance_id: "i-12345678",
delete_on_termination: true,
attachment_id: "eni-attach-12345678",
instance_owner_id: "345678901234",
},
private_dns_name: "ip-2-2-2-2.ap-northeast-1.compute.internal",
availability_zone: "ap-northeast-1a",
requester_id: "234567890123",
subnet_id: "subnet-1234abcd",
owner_id: "123456789012",
private_ip_address: "2.2.2.2",
tag_set: [
{ key: "Name", value: "fuga" },
]
}
]
end
before do
client.stub_responses(:describe_network_interfaces, network_interfaces: network_interfaces)
end
describe ".tf" do
it "should generate tf" do
expect(described_class.tf(client: client)).to eq <<-EOS
resource "aws_network_interface" "eni-1234abcd" {
subnet_id = "subnet-1234abcd"
private_ips = ["1.1.1.1"]
security_groups = []
source_dest_check = true
}
resource "aws_network_interface" "eni-2345efgh" {
subnet_id = "subnet-1234abcd"
private_ips = ["2.2.2.2", "3.3.3.3"]
security_groups = ["sg-12345678", "sg-23456789"]
source_dest_check = false
attachment {
instance = "i-12345678"
device_index = 0
}
tags {
"Name" = "fuga"
}
}
EOS
end
end
describe ".tfstate" do
it "should generate tfstate" do
expect(described_class.tfstate(client: client)).to eq({
"aws_network_interface.eni-1234abcd" => {
"type" => "aws_network_interface",
"primary" => {
"id" => "eni-1234abcd",
"attributes" => {
"attachment.#" => "0",
"id" => "eni-1234abcd",
"private_ips.#" => "1",
"security_groups.#" => "0",
"source_dest_check" => "true",
"subnet_id" => "subnet-1234abcd",
"tags.#" => "0",
}
}
},
"aws_network_interface.eni-2345efgh" => {
"type" => "aws_network_interface",
"primary" => {
"id" => "eni-2345efgh",
"attributes" => {
"attachment.#" => "1",
"id" => "eni-2345efgh",
"private_ips.#" => "2",
"security_groups.#" => "2",
"source_dest_check" => "false",
"subnet_id" => "subnet-1234abcd",
"tags.#" => "1",
}
}
},
})
end
end
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment