From 6ff9782f19bd3001aae520b18c04bdb015a2d53c Mon Sep 17 00:00:00 2001 From: Daniel Iwaniuk Date: Fri, 18 Nov 2016 13:45:19 +0100 Subject: [PATCH] Fix associate_public_ip_address attr for EC2 --- lib/terraforming/resource/ec2.rb | 6 +++++- lib/terraforming/template/tf/ec2.erb | 2 +- spec/lib/terraforming/resource/ec2_spec.rb | 24 ++++++++-------------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/lib/terraforming/resource/ec2.rb b/lib/terraforming/resource/ec2.rb index 730fd011..adab638b 100644 --- a/lib/terraforming/resource/ec2.rb +++ b/lib/terraforming/resource/ec2.rb @@ -26,7 +26,7 @@ def tfstate attributes = { "ami" => instance.image_id, - "associate_public_ip_address" => "true", + "associate_public_ip_address" => associate_public_ip?(instance).to_s, "availability_zone" => instance.placement.availability_zone, "ebs_block_device.#" => ebs_block_devices_in(block_devices, instance).length.to_s, "ebs_optimized" => instance.ebs_optimized.to_s, @@ -96,6 +96,10 @@ def in_vpc?(instance) (instance.subnet_id && instance.subnet_id != "" && instance.security_groups.empty?) end + def associate_public_ip?(instance) + !instance.public_ip_address.to_s.empty? + end + def monitoring_state(instance) %w(enabled pending).include?(instance.monitoring.state) end diff --git a/lib/terraforming/template/tf/ec2.erb b/lib/terraforming/template/tf/ec2.erb index 1fbb977c..a4e17b93 100644 --- a/lib/terraforming/template/tf/ec2.erb +++ b/lib/terraforming/template/tf/ec2.erb @@ -15,7 +15,7 @@ resource "aws_instance" "<%= module_name_of(instance) %>" { <%- else -%> security_groups = <%= instance.security_groups.map { |sg| sg.group_name }.inspect %> <%- end -%> - associate_public_ip_address = true + associate_public_ip_address = <%= associate_public_ip?(instance) %> private_ip = "<%= instance.private_ip_address %>" <%- if instance.source_dest_check -%> source_dest_check = <%= instance.source_dest_check %> diff --git a/spec/lib/terraforming/resource/ec2_spec.rb b/spec/lib/terraforming/resource/ec2_spec.rb index d9b53dda..99655ed9 100644 --- a/spec/lib/terraforming/resource/ec2_spec.rb +++ b/spec/lib/terraforming/resource/ec2_spec.rb @@ -178,7 +178,7 @@ module Resource image_id: "ami-9012ijkl", state: { code: 16, name: "running" }, private_dns_name: "ip-10-0-0-102.ap-northeast-1.compute.internal", - public_dns_name: "ec2-54-12-0-2.ap-northeast-1.compute.amazonaws.com", + public_dns_name: "", state_transition_reason: "", key_name: "hoge-key", ami_launch_index: 0, @@ -190,7 +190,7 @@ module Resource subnet_id: "", vpc_id: "vpc-9012ijkl", private_ip_address: "10.0.0.102", - public_ip_address: "54.12.0.2", + public_ip_address: "", architecture: "x86_64", root_device_type: "ebs", root_device_name: "/dev/sda1", @@ -225,21 +225,13 @@ module Resource attach_time: Time.parse("2015-03-12 01:23:45 UTC"), delete_on_termination: true }, - association: { - public_ip: "54.12.0.2", - public_dns_name: "ec2-54-12-0-2.ap-northeast-1.compute.amazonaws.com", - ip_owner_id: "amazon" - }, + association: nil, private_ip_addresses: [ { private_ip_address: "10.0.0.102", private_dns_name: "ip-10-0-6-102.ap-northeast-1.compute.internal", primary: true, - association: { - public_ip: "54.12.0.2", - public_dns_name: "ec2-54-12-0-2.ap-northeast-1.compute.amazonaws.com", - ip_owner_id: "amazon" - } + association: nil } ] } @@ -377,7 +369,7 @@ module Resource monitoring = true key_name = "hoge-key" security_groups = ["default"] - associate_public_ip_address = true + associate_public_ip_address = false private_ip = "10.0.0.102" source_dest_check = true @@ -458,7 +450,7 @@ module Resource "id" => "i-9012ijkl", "attributes" => { "ami" => "ami-9012ijkl", - "associate_public_ip_address" => "true", + "associate_public_ip_address" => "false", "availability_zone" => "ap-northeast-1b", "ebs_block_device.#" => "0", "ebs_optimized" => "false", @@ -468,8 +460,8 @@ module Resource "monitoring" => "true", "private_dns" => "ip-10-0-0-102.ap-northeast-1.compute.internal", "private_ip" => "10.0.0.102", - "public_dns" => "ec2-54-12-0-2.ap-northeast-1.compute.amazonaws.com", - "public_ip" => "54.12.0.2", + "public_dns" => "", + "public_ip" => "", "root_block_device.#" => "0", "security_groups.#" => "1", "source_dest_check" => "true",