Class: EC2Launcher::Route53
- Inherits:
-
Object
- Object
- EC2Launcher::Route53
- Defined in:
- lib/ec2launcher/route53.rb
Instance Method Summary collapse
-
#create_record(name, value, type = "A", ttl = 300) ⇒ Object
Creates a new DNS record in Route53.
-
#delete_record(name, type, ttl, value, log_errors = true) ⇒ Object
Deletes a DNS record from Route53.
-
#delete_record_by_name(hostname, record_type = "A", log_errors = true) ⇒ Object
Deletes a record by hostname, if it exists.
-
#find_record(name, type = 'A') ⇒ EC2Launcher::Route53Record
Searches for a record with the specified name and type.
-
#initialize(route53, hosted_zone_id, logger = nil) ⇒ Route53
constructor
A new instance of Route53.
Constructor Details
#initialize(route53, hosted_zone_id, logger = nil) ⇒ Route53
Returns a new instance of Route53.
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.
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.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ec2launcher/route53.rb', line 93 def delete_record(name, type, ttl, value, log_errors = true) delete_result = 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 delete_result = false end delete_result end |
#delete_record_by_name(hostname, record_type = "A", log_errors = true) ⇒ Object
Deletes a record by hostname, if it exists.
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ec2launcher/route53.rb', line 72 def delete_record_by_name(hostname, record_type = "A", log_errors = true) # Search for the record delete_result = true record = find_record(hostname, record_type) if record delete_record(record.name, record.type, record.ttl, record.value, log_errors) else delete_result = false @log.warn "Route53 '#{record_type}' record for '#{hostname}' not found!" if log_errors end delete_result 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.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ec2launcher/route53.rb', line 126 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 |