Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Fix associate_public_ip_address attr for EC2
Browse files Browse the repository at this point in the history
  • Loading branch information
diwaniuk committed Dec 4, 2016
1 parent d78824c commit 6ff9782
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
6 changes: 5 additions & 1 deletion lib/terraforming/resource/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/terraforming/template/tf/ec2.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
Expand Down
24 changes: 8 additions & 16 deletions spec/lib/terraforming/resource/ec2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -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
}
]
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 6ff9782

Please sign in to comment.