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

Fixes for route53_records #303

Merged
merged 5 commits into from
Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 25 additions & 4 deletions lib/terraforming/resource/route53_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def tf
def tfstate
records.inject({}) do |resources, r|
record, zone_id = r[:record], r[:zone_id]
counter = r[:counter]
record_id = record_id_of(record, zone_id)

attributes = {
Expand All @@ -35,9 +36,15 @@ def tfstate
attributes["records.#"] = record.resource_records.length.to_s unless record.resource_records.empty?
attributes["ttl"] = record.ttl.to_s if record.ttl
attributes["weight"] = record.weight ? record.weight.to_s : "-1"
attributes["region"] = record.region if record.region
if record.geo_location
attributes["continent"] = record.geo_location.continent_code if record.geo_location.continent_code
attributes["country"] = record.geo_location.country_code if record.geo_location.country_code
attributes["subdivision"] = record.geo_location.subdivision_code if record.geo_location.subdivision_code
end
Copy link
Owner

Choose a reason for hiding this comment

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

Please add empty lines before if and after end.

attributes["region"] = record.region if record.region

if record.geo_location
# ...
end

attributes["set_identifier"] = record.set_identifier if record.set_identifier

attributes["set_identifier"] = record.set_identifier if record.set_identifier

resources["aws_route53_record.#{module_name_of(record)}"] = {
resources["aws_route53_record.#{module_name_of(record, counter)}"] = {
"type" => "aws_route53_record",
"primary" => {
"id" => record_id,
Expand Down Expand Up @@ -66,18 +73,32 @@ def record_sets_of(hosted_zone)
end

def records
hosted_zones.map do |hosted_zone|
to_return = hosted_zones.map do |hosted_zone|
record_sets_of(hosted_zone).map { |record| { record: record, zone_id: zone_id_of(hosted_zone) } }
end.flatten
count = {}
dups = to_return.group_by{ |record| module_name_of(record[:record], nil) }.select { |k, v| v.size > 1 }.map(&:first)
Copy link
Owner

Choose a reason for hiding this comment

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

[rubocop]

  • Space missing to the left of {.
  • Unused block argument - k. If it's necessary, use _ or _k as an argument name to indicate that it won't be used. :ref

to_return.each do |r|
module_name = module_name_of(r[:record], nil)
if dups.include?(module_name)
Copy link
Owner

Choose a reason for hiding this comment

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

[rubocop]

  • Use next to skip iteration. :ref

if count[module_name]
Copy link
Owner

Choose a reason for hiding this comment

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

[rubocop]

  • Use the return of the conditional for variable assignment and comparison.

count[module_name] = count[module_name] + 1
Copy link
Owner

Choose a reason for hiding this comment

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

[rubocop]

  • Use 2 (not -19) spaces for indentation. :ref

else
Copy link
Owner

Choose a reason for hiding this comment

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

[rubocop]

  • Align else with if.

count[module_name] = 0
Copy link
Owner

Choose a reason for hiding this comment

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

[rubocop]

  • Use 2 (not -19) spaces for indentation. :ref

end
r[:counter] = count[module_name]
end
end
to_return
end

# TODO(dtan4): change method name...
def name_of(dns_name)
dns_name.gsub(/\.\z/, "")
end

def module_name_of(record)
normalize_module_name(name_of(record.name) + "-" + record.type)
def module_name_of(record, counter)
Copy link
Owner

Choose a reason for hiding this comment

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

[method_definition_validator]

Copy link
Author

Choose a reason for hiding this comment

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

It appears as though each resource type has its own module_name_of() method, so changing how the route53_record one works should be independent of the other ones.

Copy link
Owner

Choose a reason for hiding this comment

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

[method_definition_validator]

Copy link
Owner

Choose a reason for hiding this comment

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

[method_definition_validator]

normalize_module_name(name_of(record.name) + "-" + record.type + (!counter.nil? ? "-" + counter.to_s : ""))
end

def zone_id_of(hosted_zone)
Expand Down
25 changes: 23 additions & 2 deletions lib/terraforming/template/tf/route53_record.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<% records.each do |r| -%>
<%- record, zone_id = r[:record], r[:zone_id] -%>
resource "aws_route53_record" "<%= module_name_of(record) %>" {
<%- counter = r[:counter] -%>
resource "aws_route53_record" "<%= module_name_of(record, counter) %>" {
zone_id = "<%= zone_id %>"
name = "<%= name_of(record.name.sub(/\\052/, '*')) %>"
type = "<%= record.type %>"
Expand All @@ -11,7 +12,27 @@ resource "aws_route53_record" "<%= module_name_of(record) %>" {
ttl = "<%= record.ttl %>"
<%- end -%>
<%- if record.weight -%>
weight = <%= record.weight %>
weighted_routing_policy {
weight = <%= record.weight %>
}
<%- end -%>
<%- if record.region -%>
latency_routing_policy {
region = "<%= record.region %>"
}
<%- end -%>
<%- if record.geo_location -%>
geolocation_routing_policy {
<%- if record.geo_location.continent_code -%>
continent = "<%= record.geo_location.continent_code %>"
<%- end -%>
<%- if record.geo_location.country_code -%>
country = "<%= record.geo_location.country_code %>"
<%- end -%>
<%- if record.geo_location.subdivision_code -%>
subdivision = "<%= record.geo_location.subdivision_code %>"
<%- end -%>
}
<%- end -%>
<%- if record.set_identifier -%>
set_identifier = "<%= record.set_identifier %>"
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/terraforming/resource/route53_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ module Resource
zone_id = "OPQRSTUVWXYZAB"
name = "www.fuga.net"
type = "A"
weight = 10
weighted_routing_policy {
weight = 10
Copy link
Owner

Choose a reason for hiding this comment

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

Please use 4-spaces indention, like alias.name below.

}

alias {
name = "fuga.net"
Expand Down