mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Merge pull request #642 from bensie/route53-elb
[aws|dns] Add support for aliasing records to Elastic Load Balancers
This commit is contained in:
commit
0bf1bf2677
10 changed files with 236 additions and 199 deletions
|
@ -89,7 +89,7 @@ module Fog
|
|||
@persistent = options[:persistent] || true
|
||||
@port = options[:port] || 443
|
||||
@scheme = options[:scheme] || 'https'
|
||||
@version = options[:version] || '2010-10-01'
|
||||
@version = options[:version] || '2011-05-05'
|
||||
|
||||
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}#{@path}", @persistent, @connection_options)
|
||||
end
|
||||
|
|
|
@ -9,6 +9,7 @@ module Fog
|
|||
@resource_record = []
|
||||
@resource_record_set = {}
|
||||
@resource_record_set['ResourceRecords'] = []
|
||||
@alias_target = {}
|
||||
@response = {}
|
||||
@response['ResourceRecordSets'] = []
|
||||
@section = :resource_record_set
|
||||
|
@ -21,6 +22,11 @@ module Fog
|
|||
@resource_record_set[name] = value
|
||||
when 'Value'
|
||||
@resource_record_set['ResourceRecords'] << value
|
||||
when 'AliasTarget'
|
||||
@resource_record_set[name] = @alias_target
|
||||
@alias_target = {}
|
||||
when 'HostedZoneId', 'DNSName'
|
||||
@alias_target[name] = value
|
||||
when 'ResourceRecordSet'
|
||||
@response['ResourceRecordSets'] << @resource_record_set
|
||||
@resource_record_set = {}
|
||||
|
@ -31,9 +37,9 @@ module Fog
|
|||
elsif @section == :main
|
||||
case name
|
||||
when 'MaxItems'
|
||||
@response[name]= value.to_i
|
||||
@response[name] = value.to_i
|
||||
when 'IsTruncated', 'NextRecordName', 'NextRecordType'
|
||||
@response[name]= value
|
||||
@response[name] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,8 +16,11 @@ module Fog
|
|||
# * action<~String> - 'CREATE' or 'DELETE'
|
||||
# * name<~String> - This must be a fully-specified name, ending with a final period
|
||||
# * type<~String> - A | AAAA | CNAME | MX | NS | PTR | SOA | SPF | SRV | TXT
|
||||
# * ttl<~Integer> -
|
||||
# * resource_record<~String>
|
||||
# * ttl<~Integer> - Time-to-live value - omit if using an alias record
|
||||
# * resource_record<~String> - Omit if using an alias record
|
||||
# * alias_target<~Hash> - Information about the domain to which you are redirecting traffic (Alias record sets only)
|
||||
# * dns_name<~String> - The Elastic Load Balancing domain to which you want to reroute traffic
|
||||
# * hosted_zone_id<~String> - The ID of the hosted zone that contains the Elastic Load Balancing domain to which you want to reroute traffic
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
|
@ -34,38 +37,50 @@ module Fog
|
|||
zone_id = zone_id.sub('/hostedzone/', '')
|
||||
|
||||
optional_tags = ''
|
||||
options.each { |option, value|
|
||||
options.each do |option, value|
|
||||
case option
|
||||
when :comment
|
||||
optional_tags+= "<Comment>#{value}</Comment>"
|
||||
optional_tags += "<Comment>#{value}</Comment>"
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
#build XML
|
||||
if change_batch.count > 0
|
||||
|
||||
changes= "<ChangeBatch>#{optional_tags}<Changes>"
|
||||
changes = "<ChangeBatch>#{optional_tags}<Changes>"
|
||||
|
||||
change_batch.each { |change_item|
|
||||
change_batch.each do |change_item|
|
||||
action_tag = %Q{<Action>#{change_item[:action]}</Action>}
|
||||
name_tag = %Q{<Name>#{change_item[:name]}</Name>}
|
||||
type_tag = %Q{<Type>#{change_item[:type]}</Type>}
|
||||
ttl_tag = %Q{<TTL>#{change_item[:ttl]}</TTL>}
|
||||
resource_records= change_item[:resource_records]
|
||||
|
||||
# TTL must be omitted if using an alias record
|
||||
ttl_tag = ''
|
||||
ttl_tag += %Q{<TTL>#{change_item[:ttl]}</TTL>} unless change_item[:alias_target]
|
||||
|
||||
resource_records = change_item[:resource_records] || []
|
||||
resource_record_tags = ''
|
||||
resource_records.each { |record|
|
||||
resource_record_tags+= %Q{<ResourceRecord><Value>#{record}</Value></ResourceRecord>}
|
||||
}
|
||||
resource_tag= %Q{<ResourceRecords>#{resource_record_tags}</ResourceRecords>}
|
||||
|
||||
change_tags = %Q{<Change>#{action_tag}<ResourceRecordSet>#{name_tag}#{type_tag}#{ttl_tag}#{resource_tag}</ResourceRecordSet></Change>}
|
||||
changes+= change_tags
|
||||
}
|
||||
|
||||
changes+= '</Changes></ChangeBatch>'
|
||||
resource_records.each do |record|
|
||||
resource_record_tags += %Q{<ResourceRecord><Value>#{record}</Value></ResourceRecord>}
|
||||
end
|
||||
|
||||
body = %Q{<?xml version="1.0" encoding="UTF-8"?><ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2010-10-01/">#{changes}</ChangeResourceRecordSetsRequest>}
|
||||
# ResourceRecords must be omitted if using an alias record
|
||||
resource_tag = ''
|
||||
resource_tag += %Q{<ResourceRecords>#{resource_record_tags}</ResourceRecords>} if resource_records.any?
|
||||
|
||||
alias_target_tag = ''
|
||||
if change_item[:alias_target]
|
||||
alias_target_tag += %Q{<AliasTarget><HostedZoneId>#{change_item[:alias_target][:hosted_zone_id]}</HostedZoneId><DNSName>#{change_item[:alias_target][:dns_name]}</DNSName></AliasTarget>}
|
||||
end
|
||||
|
||||
change_tags = %Q{<Change>#{action_tag}<ResourceRecordSet>#{name_tag}#{type_tag}#{ttl_tag}#{resource_tag}#{alias_target_tag}</ResourceRecordSet></Change>}
|
||||
changes += change_tags
|
||||
end
|
||||
|
||||
changes += '</Changes></ChangeBatch>'
|
||||
end
|
||||
|
||||
body = %Q{<?xml version="1.0" encoding="UTF-8"?><ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2011-05-05/">#{changes}</ChangeResourceRecordSetsRequest>}
|
||||
request({
|
||||
:body => body,
|
||||
:parser => Fog::Parsers::DNS::AWS::ChangeResourceRecordSets.new,
|
||||
|
|
|
@ -12,7 +12,7 @@ module Fog
|
|||
# * options<~Hash>
|
||||
# * caller_ref<~String> - unique string that identifies the request & allows failed
|
||||
# calls to be retried without the risk of executing the operation twice
|
||||
# * comment<~Integer> -
|
||||
# * comment<~String> -
|
||||
#
|
||||
# ==== Returns
|
||||
# * response<~Excon::Response>:
|
||||
|
@ -33,18 +33,18 @@ module Fog
|
|||
|
||||
optional_tags = ''
|
||||
if options[:caller_ref]
|
||||
optional_tags+= "<CallerReference>#{options[:caller_ref]}</CallerReference>"
|
||||
optional_tags += "<CallerReference>#{options[:caller_ref]}</CallerReference>"
|
||||
else
|
||||
#make sure we have a unique call reference
|
||||
caller_ref = "ref-#{rand(1000000).to_s}"
|
||||
optional_tags+= "<CallerReference>#{caller_ref}</CallerReference>"
|
||||
optional_tags += "<CallerReference>#{caller_ref}</CallerReference>"
|
||||
end
|
||||
if options[:comment]
|
||||
optional_tags+= "<HostedZoneConfig><Comment>#{options[:comment]}</Comment></HostedZoneConfig>"
|
||||
optional_tags += "<HostedZoneConfig><Comment>#{options[:comment]}</Comment></HostedZoneConfig>"
|
||||
end
|
||||
|
||||
request({
|
||||
:body => %Q{<?xml version="1.0" encoding="UTF-8"?><CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2010-10-01/"><Name>#{name}</Name>#{optional_tags}</CreateHostedZoneRequest>},
|
||||
:body => %Q{<?xml version="1.0" encoding="UTF-8"?><CreateHostedZoneRequest xmlns="https://route53.amazonaws.com/doc/2011-05-05/"><Name>#{name}</Name>#{optional_tags}</CreateHostedZoneRequest>},
|
||||
:parser => Fog::Parsers::DNS::AWS::CreateHostedZone.new,
|
||||
:expects => 201,
|
||||
:method => 'POST',
|
||||
|
|
|
@ -29,14 +29,14 @@ module Fog
|
|||
def list_hosted_zones(options = {})
|
||||
|
||||
parameters = {}
|
||||
options.each { |option, value|
|
||||
options.each do |option, value|
|
||||
case option
|
||||
when :marker
|
||||
parameters[option] = value
|
||||
when :max_items
|
||||
parameters[:maxitems] = value
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
request({
|
||||
:query => parameters,
|
||||
|
|
|
@ -21,6 +21,9 @@ module Fog
|
|||
# * 'Name'<~String> -
|
||||
# * 'Type'<~String> -
|
||||
# * 'TTL'<~Integer> -
|
||||
# * 'AliasTarget'<~Hash> -
|
||||
# * 'HostedZoneId'<~String> -
|
||||
# * 'DNSName'<~String> -
|
||||
# * 'ResourceRecords'<~Array>
|
||||
# * 'Value'<~String> -
|
||||
# * 'IsTruncated'<~String> -
|
||||
|
@ -35,14 +38,14 @@ module Fog
|
|||
zone_id = zone_id.sub('/hostedzone/', '')
|
||||
|
||||
parameters = {}
|
||||
options.each { |option, value|
|
||||
options.each do |option, value|
|
||||
case option
|
||||
when :type, :name
|
||||
parameters[option]= "#{value}"
|
||||
parameters[option] = "#{value}"
|
||||
when :max_items
|
||||
parameters['maxitems']= "#{value}"
|
||||
parameters['maxitems'] = "#{value}"
|
||||
end
|
||||
end
|
||||
}
|
||||
|
||||
request({
|
||||
:query => parameters,
|
||||
|
|
|
@ -3,42 +3,19 @@ Shindo.tests('Fog::DNS[:aws] | DNS requests', ['aws', 'dns']) do
|
|||
@org_zone_count = 0
|
||||
@zone_id = ''
|
||||
@change_id = ''
|
||||
@new_records =[]
|
||||
@new_records = []
|
||||
@domain_name = generate_unique_domain
|
||||
|
||||
# NOTE: can't use generate_unique_domain() as we do in other DNS provider
|
||||
# test suites as AWS charges $1/mth for each domain, even if it exists
|
||||
# on AWS for only the time that this test suite runs!!
|
||||
# http://aws.amazon.com/route53/pricing/
|
||||
@test_domain = 'test-343246324434.com'
|
||||
@elb_connection = Fog::AWS::ELB.new
|
||||
@r53_connection = Fog::DNS[:aws]
|
||||
|
||||
tests( 'success') do
|
||||
|
||||
test('see if test domain already exists') do
|
||||
pending if Fog.mocking?
|
||||
|
||||
@zone_id = nil
|
||||
|
||||
response = Fog::DNS[:aws].list_hosted_zones()
|
||||
if response.status == 200
|
||||
@hosted_zones = response.body['HostedZones']
|
||||
end
|
||||
|
||||
#go through zones for this account
|
||||
@hosted_zones.each { |zone|
|
||||
domain = zone['Name']
|
||||
if domain.chomp == @test_domain
|
||||
@zone_id = zone['Id']
|
||||
end
|
||||
}
|
||||
|
||||
@zone_id.nil?
|
||||
end
|
||||
tests('success') do
|
||||
|
||||
test('get current zone count') do
|
||||
pending if Fog.mocking?
|
||||
|
||||
@org_zone_count= 0
|
||||
response = Fog::DNS[:aws].list_hosted_zones()
|
||||
response = @r53_connection.list_hosted_zones
|
||||
if response.status == 200
|
||||
@hosted_zones = response.body['HostedZones']
|
||||
@org_zone_count = @hosted_zones.count
|
||||
|
@ -52,10 +29,10 @@ Shindo.tests('Fog::DNS[:aws] | DNS requests', ['aws', 'dns']) do
|
|||
|
||||
result = false
|
||||
|
||||
response = Fog::DNS[:aws].create_hosted_zone( @test_domain)
|
||||
response = @r53_connection.create_hosted_zone(@domain_name)
|
||||
if response.status == 201
|
||||
|
||||
zone= response.body['HostedZone']
|
||||
zone = response.body['HostedZone']
|
||||
change_info = response.body['ChangeInfo']
|
||||
ns_servers = response.body['NameServers']
|
||||
|
||||
|
@ -81,11 +58,11 @@ Shindo.tests('Fog::DNS[:aws] | DNS requests', ['aws', 'dns']) do
|
|||
pending if Fog.mocking?
|
||||
|
||||
result = false
|
||||
response = Fog::DNS[:aws].get_change(@change_id)
|
||||
response = @r53_connection.get_change(@change_id)
|
||||
if response.status == 200
|
||||
status = response.body['Status']
|
||||
if (status == 'PENDING') or (status == 'INSYNC')
|
||||
result= true
|
||||
result = true
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -97,7 +74,7 @@ Shindo.tests('Fog::DNS[:aws] | DNS requests', ['aws', 'dns']) do
|
|||
|
||||
result = false
|
||||
|
||||
response = Fog::DNS[:aws].get_hosted_zone( @zone_id)
|
||||
response = @r53_connection.get_hosted_zone(@zone_id)
|
||||
if response.status == 200
|
||||
zone = response.body['HostedZone']
|
||||
zone_id = zone['Id']
|
||||
|
@ -107,7 +84,7 @@ Shindo.tests('Fog::DNS[:aws] | DNS requests', ['aws', 'dns']) do
|
|||
|
||||
# AWS returns domain with a dot at end - so when compare, remove dot
|
||||
|
||||
if (zone_id == @zone_id) and (name.chop == @test_domain) and (caller_ref.length > 0) and
|
||||
if (zone_id == @zone_id) and (name.chop == @domain_name) and (caller_ref.length > 0) and
|
||||
(ns_servers.count > 0)
|
||||
result = true
|
||||
end
|
||||
|
@ -121,12 +98,12 @@ Shindo.tests('Fog::DNS[:aws] | DNS requests', ['aws', 'dns']) do
|
|||
|
||||
result = false
|
||||
|
||||
response = Fog::DNS[:aws].list_hosted_zones()
|
||||
response = @r53_connection.list_hosted_zones
|
||||
if response.status == 200
|
||||
|
||||
zones= response.body['HostedZones']
|
||||
if (zones.count > 0)
|
||||
zone= zones[0]
|
||||
zone = zones[0]
|
||||
zone_id = zone['Id']
|
||||
zone_name= zone['Name']
|
||||
caller_ref = zone['CallerReference']
|
||||
|
@ -145,18 +122,16 @@ Shindo.tests('Fog::DNS[:aws] | DNS requests', ['aws', 'dns']) do
|
|||
test("add a A resource record") {
|
||||
pending if Fog.mocking?
|
||||
|
||||
result = false
|
||||
|
||||
# create an A resource record
|
||||
host = 'www.' + @test_domain
|
||||
host = 'www.' + @domain_name
|
||||
ip_addrs = ['1.2.3.4']
|
||||
resource_record = { :name => host, :type => 'A', :ttl => 3600, :resource_records => ip_addrs }
|
||||
resource_record_set = resource_record.merge( :action => 'CREATE')
|
||||
resource_record_set = resource_record.merge(:action => 'CREATE')
|
||||
|
||||
change_batch = []
|
||||
change_batch << resource_record_set
|
||||
options = { :comment => 'add A record to domain'}
|
||||
response = Fog::DNS[:aws].change_resource_record_sets( @zone_id, change_batch, options)
|
||||
response = @r53_connection.change_resource_record_sets(@zone_id, change_batch, options)
|
||||
if response.status == 200
|
||||
change_id = response.body['Id']
|
||||
status = response.body['Status']
|
||||
|
@ -169,18 +144,16 @@ Shindo.tests('Fog::DNS[:aws] | DNS requests', ['aws', 'dns']) do
|
|||
test("add a CNAME resource record") {
|
||||
pending if Fog.mocking?
|
||||
|
||||
result = false
|
||||
|
||||
# create a CNAME resource record
|
||||
host = 'mail.' + @test_domain
|
||||
value = ['www.' + @test_domain]
|
||||
host = 'mail.' + @domain_name
|
||||
value = ['www.' + @domain_name]
|
||||
resource_record = { :name => host, :type => 'CNAME', :ttl => 3600, :resource_records => value }
|
||||
resource_record_set = resource_record.merge( :action => 'CREATE')
|
||||
resource_record_set = resource_record.merge(:action => 'CREATE')
|
||||
|
||||
change_batch = []
|
||||
change_batch << resource_record_set
|
||||
options = { :comment => 'add CNAME record to domain'}
|
||||
response = Fog::DNS[:aws].change_resource_record_sets( @zone_id, change_batch, options)
|
||||
response = @r53_connection.change_resource_record_sets( @zone_id, change_batch, options)
|
||||
if response.status == 200
|
||||
change_id = response.body['Id']
|
||||
status = response.body['Status']
|
||||
|
@ -193,18 +166,55 @@ Shindo.tests('Fog::DNS[:aws] | DNS requests', ['aws', 'dns']) do
|
|||
test("add a MX resource record") {
|
||||
pending if Fog.mocking?
|
||||
|
||||
result = false
|
||||
|
||||
# create a MX resource record
|
||||
host = @test_domain
|
||||
value = ['7 mail.' + @test_domain]
|
||||
host = @domain_name
|
||||
value = ['7 mail.' + @domain_name]
|
||||
resource_record = { :name => host, :type => 'MX', :ttl => 3600, :resource_records => value }
|
||||
resource_record_set = resource_record.merge( :action => 'CREATE')
|
||||
|
||||
change_batch = []
|
||||
change_batch << resource_record_set
|
||||
options = { :comment => 'add MX record to domain'}
|
||||
response = Fog::DNS[:aws].change_resource_record_sets( @zone_id, change_batch, options)
|
||||
response = @r53_connection.change_resource_record_sets( @zone_id, change_batch, options)
|
||||
if response.status == 200
|
||||
change_id = response.body['Id']
|
||||
status = response.body['Status']
|
||||
@new_records << resource_record
|
||||
end
|
||||
|
||||
response.status == 200
|
||||
}
|
||||
|
||||
test("add an ALIAS resource record") {
|
||||
pending if Fog.mocking?
|
||||
|
||||
# create a load balancer
|
||||
@elb_connection.create_load_balancer(["us-east-1a"], "fog", [{"Protocol" => "HTTP", "LoadBalancerPort" => "80", "InstancePort" => "80"}])
|
||||
|
||||
elb_response = @elb_connection.describe_load_balancers("fog")
|
||||
elb = elb_response.body["DescribeLoadBalancersResult"]["LoadBalancerDescriptions"].first
|
||||
hosted_zone_id = elb["CanonicalHostedZoneNameID"]
|
||||
dns_name = elb["DNSName"]
|
||||
|
||||
# create an ALIAS record
|
||||
host = @domain_name
|
||||
alias_target = {
|
||||
:hosted_zone_id => hosted_zone_id,
|
||||
:dns_name => dns_name
|
||||
}
|
||||
resource_record = { :name => host, :type => 'A', :alias_target => alias_target }
|
||||
resource_record_set = resource_record.merge(:action => 'CREATE')
|
||||
|
||||
change_batch = []
|
||||
change_batch << resource_record_set
|
||||
options = { :comment => 'add ALIAS record to domain'}
|
||||
|
||||
puts "Hosted Zone ID (ELB): #{hosted_zone_id}"
|
||||
puts "DNS Name (ELB): #{dns_name}"
|
||||
puts "Zone ID for Route 53: #{@zone_id}"
|
||||
|
||||
sleep 120
|
||||
response = @r53_connection.change_resource_record_sets(@zone_id, change_batch, options)
|
||||
if response.status == 200
|
||||
change_id = response.body['Id']
|
||||
status = response.body['Status']
|
||||
|
@ -218,7 +228,7 @@ Shindo.tests('Fog::DNS[:aws] | DNS requests', ['aws', 'dns']) do
|
|||
pending if Fog.mocking?
|
||||
|
||||
# get resource records for zone
|
||||
response = Fog::DNS[:aws].list_resource_record_sets( @zone_id)
|
||||
response = @r53_connection.list_resource_record_sets( @zone_id)
|
||||
if response.status == 200
|
||||
record_sets= response.body['ResourceRecordSets']
|
||||
num_records= record_sets.count
|
||||
|
@ -238,7 +248,7 @@ Shindo.tests('Fog::DNS[:aws] | DNS requests', ['aws', 'dns']) do
|
|||
change_batch << resource_record_set
|
||||
}
|
||||
options = { :comment => 'remove records from domain'}
|
||||
response = Fog::DNS[:aws].change_resource_record_sets( @zone_id, change_batch, options)
|
||||
response = @r53_connection.change_resource_record_sets(@zone_id, change_batch, options)
|
||||
if response.status != 200
|
||||
result = false
|
||||
break
|
||||
|
@ -250,7 +260,10 @@ Shindo.tests('Fog::DNS[:aws] | DNS requests', ['aws', 'dns']) do
|
|||
test("delete hosted zone #{@zone_id}") {
|
||||
pending if Fog.mocking?
|
||||
|
||||
response = Fog::DNS[:aws].delete_hosted_zone( @zone_id)
|
||||
# cleanup the ELB as well
|
||||
@elb_connection.delete_load_balancer("fog")
|
||||
|
||||
response = @r53_connection.delete_hosted_zone(@zone_id)
|
||||
|
||||
response.status == 200
|
||||
}
|
||||
|
@ -258,16 +271,16 @@ Shindo.tests('Fog::DNS[:aws] | DNS requests', ['aws', 'dns']) do
|
|||
end
|
||||
|
||||
|
||||
tests( 'failure') do
|
||||
tests('failure') do
|
||||
tests('create hosted zone using invalid domain name').raises(Excon::Errors::BadRequest) do
|
||||
pending if Fog.mocking?
|
||||
response = Fog::DNS[:aws].create_hosted_zone('invalid-domain')
|
||||
response = @r53_connection.create_hosted_zone('invalid-domain')
|
||||
end
|
||||
|
||||
tests('get hosted zone using invalid ID').raises(Excon::Errors::Forbidden) do
|
||||
pending if Fog.mocking?
|
||||
zone_id = 'dummy-id'
|
||||
response = Fog::DNS[:aws].get_hosted_zone(zone_id)
|
||||
response = @r53_connection.get_hosted_zone(zone_id)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue