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

Fix associate_public_ip_address attr for EC2 #287

Merged
merged 1 commit into from
Dec 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method should return boolean value:

def associate_public_ip_address?(instance)
  !instance.public_ip_address.to_s.empty?
end

and both tf and tfstate should call this method.

tf:

associate_public_ip_address = <%= associate_public_ip_address?(instance) %>

tfstate:

"associate_public_ip_address?(instance).to_s


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