lib/terraforming/resource/ec2.rb in terraforming-0.9.1 vs lib/terraforming/resource/ec2.rb in terraforming-0.10.0
- old
+ new
@@ -23,28 +23,28 @@
instances.inject({}) do |resources, instance|
in_vpc = in_vpc?(instance)
block_devices = block_devices_of(instance)
attributes = {
- "ami"=> instance.image_id,
- "associate_public_ip_address"=> "true",
- "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,
+ "ami" => instance.image_id,
+ "associate_public_ip_address" => "true",
+ "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,
"ephemeral_block_device.#" => "0", # Terraform 0.6.1 cannot fetch this field from AWS
- "id"=> instance.instance_id,
- "instance_type"=> instance.instance_type,
+ "id" => instance.instance_id,
+ "instance_type" => instance.instance_type,
"monitoring" => monitoring_state(instance).to_s,
- "private_dns"=> instance.private_dns_name,
- "private_ip"=> instance.private_ip_address,
- "public_dns"=> instance.public_dns_name,
- "public_ip"=> instance.public_ip_address,
- "root_block_device.#"=> root_block_devices_in(block_devices, instance).length.to_s,
- "security_groups.#"=> in_vpc ? "0" : instance.security_groups.length.to_s,
- "source_dest_check"=> instance.source_dest_check.to_s,
- "tenancy"=> instance.placement.tenancy,
- "vpc_security_group_ids.#"=> in_vpc ? instance.security_groups.length.to_s : "0",
+ "private_dns" => instance.private_dns_name,
+ "private_ip" => instance.private_ip_address,
+ "public_dns" => instance.public_dns_name,
+ "public_ip" => instance.public_ip_address,
+ "root_block_device.#" => root_block_devices_in(block_devices, instance).length.to_s,
+ "security_groups.#" => in_vpc ? "0" : instance.security_groups.length.to_s,
+ "source_dest_check" => instance.source_dest_check.to_s,
+ "tenancy" => instance.placement.tenancy,
+ "vpc_security_group_ids.#" => in_vpc ? instance.security_groups.length.to_s : "0",
}
placement_group = instance.placement.group_name
attributes["placement_group"] = placement_group unless placement_group.empty?
@@ -70,12 +70,12 @@
def block_device_ids_of(instance)
instance.block_device_mappings.map { |bdm| bdm.ebs.volume_id }
end
def block_devices_of(instance)
- return [] unless instance.block_device_mappings.length > 0
- @client.describe_volumes(volume_ids: block_device_ids_of(instance)).volumes
+ return [] if instance.block_device_mappings.empty?
+ @client.describe_volumes(volume_ids: block_device_ids_of(instance)).map(&:volumes).flatten
end
def block_device_mapping_of(instance, volume_id)
instance.block_device_mappings.select { |bdm| bdm.ebs.volume_id == volume_id }[0]
end
@@ -90,19 +90,21 @@
# NOTE(dtan4):
# Original logic is here:
# https://github.com/hashicorp/terraform/blob/281e4d3e67f66daab9cdb1f7c8b6f602d949e5ee/builtin/providers/aws/resource_aws_instance.go#L481-L501
#
def in_vpc?(instance)
- vpc_security_groups_of(instance).length > 0 ||
- (instance.subnet_id && instance.subnet_id != "" && instance.security_groups.length == 0)
+ !vpc_security_groups_of(instance).empty? ||
+ (instance.subnet_id && instance.subnet_id != "" && instance.security_groups.empty?)
end
def monitoring_state(instance)
%w(enabled pending).include?(instance.monitoring.state)
end
def instances
- @client.describe_instances.reservations.map(&:instances).flatten.reject { |instance| instance.state.name == "terminated" }
+ @client.describe_instances.map(&:reservations).flatten.map(&:instances).flatten.reject do |instance|
+ instance.state.name == "terminated"
+ end
end
def module_name_of(instance)
normalize_module_name(name_from_tag(instance, instance.instance_id))
end