mirror of
https://github.com/github/octodns.git
synced 2024-05-11 05:55:00 +00:00
fix for NA continent geo target limitation on NS1
This commit is contained in:
+27
-21
@@ -350,8 +350,6 @@ class Ns1Provider(BaseProvider):
|
||||
'AS': ('ASIAPAC',),
|
||||
'EU': ('EUROPE',),
|
||||
'SA': ('SOUTH-AMERICA',),
|
||||
# TODO: what about CA, MX, and all the other NA countries?
|
||||
'NA': ('US-CENTRAL', 'US-EAST', 'US-WEST'),
|
||||
}
|
||||
|
||||
# Necessary for handling unsupported continents in _CONTINENT_TO_REGIONS
|
||||
@@ -359,6 +357,11 @@ class Ns1Provider(BaseProvider):
|
||||
'OC': {'FJ', 'NC', 'PG', 'SB', 'VU', 'AU', 'NF', 'NZ', 'FM', 'GU',
|
||||
'KI', 'MH', 'MP', 'NR', 'PW', 'AS', 'CK', 'NU', 'PF', 'PN',
|
||||
'TK', 'TO', 'TV', 'WF', 'WS'},
|
||||
'NA': {'DO', 'DM', 'BB', 'BL', 'BM', 'HT', 'KN', 'JM', 'VC', 'HN',
|
||||
'BS', 'BZ', 'PR', 'NI', 'LC', 'TT', 'VG', 'PA', 'TC', 'PM',
|
||||
'GT', 'AG', 'GP', 'AI', 'VI', 'CA', 'GD', 'AW', 'CR', 'GL',
|
||||
'CU', 'MF', 'SV', 'US', 'UM', 'MQ', 'MS', 'KY', 'MX', 'CW',
|
||||
'BQ', 'SX'}
|
||||
}
|
||||
|
||||
def __init__(self, id, api_key, retry_count=4, monitor_regions=None,
|
||||
@@ -549,42 +552,45 @@ class Ns1Provider(BaseProvider):
|
||||
|
||||
geos = set()
|
||||
|
||||
# continents are mapped (imperfectly) to regions, but what about
|
||||
# Canada/North America
|
||||
for georegion in meta.get('georegion', []):
|
||||
geos.add(self._REGION_TO_CONTINENT[georegion])
|
||||
|
||||
# Countries are easy enough to map, we just have to find their
|
||||
# continent
|
||||
#
|
||||
# NOTE: Special handling for Oceania
|
||||
# NS1 doesn't support Oceania as a region. So the Oceania countries
|
||||
# will be present in meta['country']. If all the countries in the
|
||||
# Oceania countries list are found, set the region to OC and remove
|
||||
# individual oceania country entries
|
||||
# NOTE: Some continents need special handling since NS1
|
||||
# does not supprt them as regions. These are defined under
|
||||
# _CONTINENT_TO_LIST_OF_COUNTRIES. So the countries for these
|
||||
# regions will be present in meta['country']. If all the countries
|
||||
# in _CONTINENT_TO_LIST_OF_COUNTRIES[<region>] list are found,
|
||||
# set the continent as the region and remove individual countries
|
||||
|
||||
oc_countries = set()
|
||||
special_continents = dict()
|
||||
for country in meta.get('country', []):
|
||||
# country_alpha2_to_continent_code fails for Pitcairn ('PN')
|
||||
# country_alpha2_to_continent_code fails for Pitcairn ('PN'),
|
||||
# United States Minor Outlying Islands ('UM') and
|
||||
# Sint Maarten ('SX')
|
||||
if country == 'PN':
|
||||
con = 'OC'
|
||||
elif country in ['SX', 'UM']:
|
||||
con = 'NA'
|
||||
else:
|
||||
con = country_alpha2_to_continent_code(country)
|
||||
|
||||
if con == 'OC':
|
||||
oc_countries.add(country)
|
||||
if con in self._CONTINENT_TO_LIST_OF_COUNTRIES:
|
||||
special_continents.setdefault(con, set()).add(country)
|
||||
else:
|
||||
# Adding only non-OC countries here to geos
|
||||
geos.add('{}-{}'.format(con, country))
|
||||
|
||||
if oc_countries:
|
||||
if oc_countries == self._CONTINENT_TO_LIST_OF_COUNTRIES['OC']:
|
||||
# All OC countries found, so add 'OC' to geos
|
||||
geos.add('OC')
|
||||
for continent, countries in special_continents.items():
|
||||
if countries == self._CONTINENT_TO_LIST_OF_COUNTRIES[
|
||||
continent]:
|
||||
# All countries found, so add it to geos
|
||||
geos.add(continent)
|
||||
else:
|
||||
# Partial OC countries found, just add them as-is to geos
|
||||
for c in oc_countries:
|
||||
geos.add('{}-{}'.format('OC', c))
|
||||
# Partial countries found, so just add them as-is to geos
|
||||
for c in countries:
|
||||
geos.add('{}-{}'.format(continent, c))
|
||||
|
||||
# States are easy too, just assume NA-US (CA providences aren't
|
||||
# supported by octoDNS currently)
|
||||
|
||||
@@ -1034,7 +1034,7 @@ class TestNs1ProviderDynamic(TestCase):
|
||||
rule0 = record.data['dynamic']['rules'][0]
|
||||
rule1 = record.data['dynamic']['rules'][1]
|
||||
rule0['geos'] = ['AF', 'EU']
|
||||
rule1['geos'] = ['NA']
|
||||
rule1['geos'] = ['AS']
|
||||
ret, monitor_ids = provider._params_for_A(record)
|
||||
self.assertEquals(10, len(ret['answers']))
|
||||
self.assertEquals(ret['filters'],
|
||||
@@ -1048,7 +1048,7 @@ class TestNs1ProviderDynamic(TestCase):
|
||||
},
|
||||
'iad__georegion': {
|
||||
'meta': {
|
||||
'georegion': ['US-CENTRAL', 'US-EAST', 'US-WEST'],
|
||||
'georegion': ['ASIAPAC'],
|
||||
'note': 'rule-order:1'
|
||||
}
|
||||
},
|
||||
@@ -1150,7 +1150,7 @@ class TestNs1ProviderDynamic(TestCase):
|
||||
rule0 = record.data['dynamic']['rules'][0]
|
||||
rule1 = record.data['dynamic']['rules'][1]
|
||||
rule0['geos'] = ['AF', 'EU', 'NA-US-CA']
|
||||
rule1['geos'] = ['NA', 'NA-US']
|
||||
rule1['geos'] = ['AS', 'AS-IN']
|
||||
ret, _ = provider._params_for_A(record)
|
||||
|
||||
self.assertEquals(17, len(ret['answers']))
|
||||
@@ -1210,13 +1210,13 @@ class TestNs1ProviderDynamic(TestCase):
|
||||
},
|
||||
'iad__country': {
|
||||
'meta': {
|
||||
'country': ['US'],
|
||||
'country': ['IN'],
|
||||
'note': 'rule-order:1'
|
||||
}
|
||||
},
|
||||
'iad__georegion': {
|
||||
'meta': {
|
||||
'georegion': ['US-CENTRAL', 'US-EAST', 'US-WEST'],
|
||||
'georegion': ['ASIAPAC'],
|
||||
'note': 'rule-order:1'
|
||||
}
|
||||
},
|
||||
@@ -1562,6 +1562,24 @@ class TestNs1ProviderDynamic(TestCase):
|
||||
self.assertTrue(
|
||||
'OC-{}'.format(c) in data4['dynamic']['rules'][0]['geos'])
|
||||
|
||||
# NA test cases
|
||||
# 1. Full list of countries should return 'NA' in geos
|
||||
na_countries = Ns1Provider._CONTINENT_TO_LIST_OF_COUNTRIES['NA']
|
||||
del ns1_record['regions']['lhr__country']['meta']['us_state']
|
||||
ns1_record['regions']['lhr__country']['meta']['country'] = \
|
||||
list(na_countries)
|
||||
data5 = provider._data_for_A('A', ns1_record)
|
||||
self.assertTrue('NA' in data5['dynamic']['rules'][0]['geos'])
|
||||
|
||||
# 2. Partial list of countries should return just those
|
||||
partial_na_cntry_list = list(na_countries)[:5]
|
||||
ns1_record['regions']['lhr__country']['meta']['country'] = \
|
||||
partial_na_cntry_list
|
||||
data6 = provider._data_for_A('A', ns1_record)
|
||||
for c in partial_na_cntry_list:
|
||||
self.assertTrue(
|
||||
'NA-{}'.format(c) in data6['dynamic']['rules'][0]['geos'])
|
||||
|
||||
# Test out fallback only pools and new-style notes
|
||||
ns1_record = {
|
||||
'answers': [{
|
||||
|
||||
Reference in New Issue
Block a user