Class: Awful::Route53

Inherits:
Cli show all
Defined in:
lib/awful/route53.rb

Instance Method Summary collapse

Methods inherited from Cli

#initialize

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#alias(name, target) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/awful/route53.rb', line 127

def alias(name, target)
  if options[:hosted_zone_id]
    dns_name = target
    hosted_zone_id = options[:hosted_zone_id]
  else
    dns_name, hosted_zone_id = send("get_#{options[:resource]}_dns", target)
  end
  action = options[:delete] ? 'DELETE' : 'UPSERT'
  params = {
    hosted_zone_id: get_zone_by_name(get_domain(name)),
    change_batch: {
      changes: [
        {
          action: action,
          resource_record_set: {
            name:           name,
            type:           options[:type],
            set_identifier: options[:set_identifier],
            weight:         options[:weight],
            alias_target: {
              hosted_zone_id:         hosted_zone_id,
              dns_name:               dns_name,
              evaluate_target_health: false
            }
          }
        }
      ]
    }
  }
  route53.change_resource_record_sets(params).output do |response|
    puts YAML.dump(stringify_keys(response.change_info.to_hash))
  end
end

#change(id) ⇒ Object



190
191
192
193
194
# File 'lib/awful/route53.rb', line 190

def change(id)
  route53.get_change(id: id).change_info.output do |info|
    puts YAML.dump(stringify_keys(info.to_hash))
  end
end

#cname(name, target) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/awful/route53.rb', line 163

def cname(name, target)
  params = {
    hosted_zone_id: get_zone_by_name(get_domain(name)),
    change_batch: {
      changes: [
        {
          action: 'UPSERT',
          resource_record_set: {
            name: name,
            type: 'CNAME',
            resource_records: [
              {
                value: target
              }
            ],
            ttl: options[:ttl]
          }
        }
      ]
    }
  }
  route53.change_resource_record_sets(params).output do |response|
    puts YAML.dump(stringify_keys(response.change_info.to_hash))
  end
end

#dump(name) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/awful/route53.rb', line 95

def dump(name)
  zone = name.split('.').last(2).join('.')
  name += '.' unless name.end_with?('.') # output will have dot at end
  params = {
    hosted_zone_id:    get_zone_by_name(zone),
    start_record_name: name,
    start_record_type: options[:type],
    max_items:         options[:max_items]
  }
  route53.list_resource_record_sets(params).resource_record_sets.select do |record|
    record.name == name
  end.output do |records|
    records.each do |record|
      puts YAML.dump(stringify_keys(record.to_hash))
    end
  end
end

#ls(name = /./) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/awful/route53.rb', line 49

def ls(name = /./)
  route53.list_hosted_zones.hosted_zones.select do |zone|
    zone.name.match(name)
  end.output do |list|
    if options[:long]
      print_table list.map { |z| [z.name, z.id, z.resource_record_set_count, z.config.comment] }
    else
      puts list.map(&:name)
    end
  end
end

#records(zone) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/awful/route53.rb', line 66

def records(zone)
  ## match on given record types
  include_type = options[:type] ? ->(type) { options[:type].include?(type) } : ->(_) { true }

  ## match on given record names
  names = options.fetch('name', []).map { |name| name.gsub(/([^\.])$/, '\1.') } # append . to names if missing
  include_name = options[:name] ? ->(name) { names.include?(name) } : ->(_) { true }

  route53.list_resource_record_sets(
    hosted_zone_id: get_zone_by_name(zone),
    max_items:      options[:max_items]
  ).resource_record_sets.select do |rrset|
    include_type.call(rrset.type) and include_name.call(rrset.name)
  end.output do |records|
    if options[:long]
      print_table records.map { |r|
        dns_name = r.alias_target.nil? ? [] : ['ALIAS ' + r.alias_target.dns_name]
        values = r.resource_records.map(&:value)
        [r.name, r.type, (dns_name + values).join(', ')]
      }
    else
      puts records.map(&:name)
    end
  end
end