From 1569d94513758bbae628930ea05730bc8e8d4b13 Mon Sep 17 00:00:00 2001 From: Dan Hanks Date: Wed, 3 Jun 2020 09:36:33 -0400 Subject: [PATCH] Add support for geo-targeting of CA provinces - For providers that support such --- octodns/record/geo.py | 11 ++++++++--- script/generate-geo-data | 4 ++-- tests/test_octodns_record_geo.py | 2 ++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/octodns/record/geo.py b/octodns/record/geo.py index ed54194..bd8ebc3 100644 --- a/octodns/record/geo.py +++ b/octodns/record/geo.py @@ -63,9 +63,14 @@ class GeoCodes(object): @classmethod def province_to_code(cls, province): - # We get to cheat on this one since we only support provinces in NA-US - if province not in geo_data['NA']['US']['provinces']: + # We cheat on this one a little since we only support provinces in NA-US, NA-CA + if (province not in geo_data['NA']['US']['provinces'] and + province not in geo_data['NA']['CA']['provinces']): cls.log.warn('country_to_code: unrecognized province "%s"', province) return - return 'NA-US-{}'.format(province) + if province in geo_data['NA']['US']['provinces']: + country = 'US' + if province in geo_data['NA']['CA']['provinces']: + country = 'CA' + return 'NA-{}-{}'.format(country, province) diff --git a/script/generate-geo-data b/script/generate-geo-data index 87a57b1..da49701 100755 --- a/script/generate-geo-data +++ b/script/generate-geo-data @@ -8,8 +8,8 @@ from pycountry_convert import country_alpha2_to_continent_code subs = defaultdict(dict) for subdivision in subdivisions: - # Route53 only supports US states, Dyn supports US states and CA provinces, but for now we'll just do US - if subdivision.country_code not in ('US'): + # Route53 only supports US states, Dyn (and others) support US states and CA provinces + if subdivision.country_code not in ('US', 'CA'): continue subs[subdivision.country_code][subdivision.code[3:]] = { 'name': subdivision.name diff --git a/tests/test_octodns_record_geo.py b/tests/test_octodns_record_geo.py index 5b7454c..35df6d5 100644 --- a/tests/test_octodns_record_geo.py +++ b/tests/test_octodns_record_geo.py @@ -77,4 +77,6 @@ class TestRecordGeoCodes(TestCase): def test_province_to_code(self): self.assertEquals('NA-US-OR', GeoCodes.province_to_code('OR')) self.assertEquals('NA-US-KY', GeoCodes.province_to_code('KY')) + self.assertEquals('NA-CA-AB', GeoCodes.province_to_code('AB')) + self.assertEquals('NA-CA-BC', GeoCodes.province_to_code('BC')) self.assertFalse(GeoCodes.province_to_code('XX'))