Class: EC2Launcher::Route53

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2launcher/route53.rb

Instance Method Summary collapse

Constructor Details

#initialize(route53, hosted_zone_id, logger = nil) ⇒ Route53

Returns a new instance of Route53.

Parameters:

  • route53 (AWS::Route53)

    Initialized Route53 object

  • hosted_zone_id (String)

    Zone ID

  • (Log4r::Logger)


25
26
27
28
29
30
# File 'lib/ec2launcher/route53.rb', line 25

def initialize(route53, hosted_zone_id, logger = nil)
  @log = logger

  @route53 = route53
  @hosted_zone_id = hosted_zone_id
end

Instance Method Details

#create_record(name, value, type = "A", ttl = 300) ⇒ Object

Creates a new DNS record in Route53. Deletes any existing record with the same name and record type.

Parameters:

  • name (String)

    Name of the record

  • value (String)

    Value for the DNS record

  • type (String) (defaults to: "A")

    Type of DNS record: A, CNAME, etc. Defaults to ‘A’.

  • ttl (Integer) (defaults to: 300)

    TTL in seconds. Defaults to 300.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ec2launcher/route53.rb', line 39

def create_record(name, value, type = "A", ttl = 300)
  # Delete existing record because you can't update records
  delete_record_by_name(name, type, false)

  # Create new record
  begin
    @route53.client.change_resource_record_sets({
      :hosted_zone_id => @hosted_zone_id, 
      :change_batch => {
        :changes => [ 
          {
            :action => "CREATE", 
            :resource_record_set => { 
              :name => name, 
              :type => type,
              :ttl => ttl, 
              :resource_records => [ { :value => value } ]
            }
          }
        ]
      }
    })
  rescue StandardError => bang
    @log.error "Error creating A record from Route53: #{bang}"
  end
end

#delete_record(name, type, ttl, value, log_errors = true) ⇒ Object

Deletes a DNS record from Route53.

Parameters:

  • name (String)

    Name of the record

  • type (String)

    Type of DNS record: A, CNAME, etc.

  • ttl (Integer)

    TTL in seconds

  • value (String)

    Value for the DNS record

  • log_errors (Boolean) (defaults to: true)

    Log errors or not. False quietly ignores errors.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ec2launcher/route53.rb', line 90

def delete_record(name, type, ttl, value, log_errors = true)
  begin
    @route53.client.change_resource_record_sets({
      :hosted_zone_id => @hosted_zone_id, 
      :change_batch => {
        :changes => [ 
          {
            :action => "DELETE", 
            :resource_record_set => { 
              :name => name, 
              :type => type,
              :ttl => ttl, 
              :resource_records => [ { :value => value } ]
            }
          }
        ]
      }
    })
  rescue StandardError => bang
    @log.error "Error deleting A record from Route53: #{bang}" if log_errors
  end
end

#delete_record_by_name(hostname, record_type = "A", log_errors = true) ⇒ Object

Deletes a record by hostname, if it exists.

Parameters:

  • hostname (String)

    Name of the record

  • record_type (String) (defaults to: "A")

    Type of DNS record: A, CNAME, etc.

  • log_errors (Boolean) (defaults to: true)

    Log errors or not. False quietly ignores errors.



72
73
74
75
76
77
78
79
80
# File 'lib/ec2launcher/route53.rb', line 72

def delete_record_by_name(hostname, record_type = "A", log_errors = true)
  # Search for the record
  record = find_record(hostname, record_type)
  if record
    delete_record(record.name, record.type, record.ttl, record.value, log_errors)
  else
    @log.warn "Route53 '#{record_type}' record for '#{hostname}' not found!" if log_errors
  end
end

#find_record(name, type = 'A') ⇒ EC2Launcher::Route53Record

Searches for a record with the specified name and type.

required information about a Route53 entry or nil if not found.

Parameters:

  • name (String)

    Name of the record

  • type (String) (defaults to: 'A')

    Type of DNS record: A, CNAME, etc.

Returns:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/ec2launcher/route53.rb', line 120

def find_record(name, type = 'A')
  # Find the record
  response = @route53.client.list_resource_record_sets({
    :hosted_zone_id => @hosted_zone_id,
    :start_record_name => name,
    :start_record_type => type,
    :max_items => 1
  })

  record = nil
  if response && response.data
    if response.data[:resource_record_sets] && response.data[:resource_record_sets].size > 0
      response_record = response.data[:resource_record_sets][0]
      if (response_record[:name] == name || response_record[:name] == "#{name}.") && response_record[:type] == type
        record = Route53Record.new(response_record[:name], response_record[:type], response_record[:ttl], response_record[:resource_records][0][:value])
      end
    end
  end

  record
end