1
0
mirror of https://github.com/github/octodns.git synced 2024-05-11 05:55:00 +00:00

Add GeoCodes.parse and move Move Dyn dynamnic over to use it

This commit is contained in:
Ross McFarland
2018-12-10 14:18:50 -08:00
parent a452a0eb00
commit a169d50fcf
4 changed files with 36 additions and 6 deletions

View File

@@ -18,6 +18,7 @@ from threading import Lock
from uuid import uuid4
from ..record import Record, Update
from ..record.geo import GeoCodes
from .base import BaseProvider
@@ -1109,7 +1110,7 @@ class DynProvider(BaseProvider):
criteria_type = 'always'
try:
for geo in rule.data['geos']:
geo = new.geo_parse(geo)
geo = GeoCodes.geo_parse(geo)
pprint(geo)
criteria_type = 'geoip'
if geo['subdivision_code']:

View File

@@ -568,11 +568,6 @@ class _DynamicMixin(object):
return reasons
@classmethod
def geo_parse(cls, code):
match = cls.geo_re.match(code)
return match.groupdict()
def __init__(self, zone, name, data, *args, **kwargs):
super(_DynamicMixin, self).__init__(zone, name, data, *args,
**kwargs)

View File

@@ -33,3 +33,20 @@ class GeoCodes(object):
reasons.append('{}unknown province code "{}"'.format(prefix, code))
return reasons
@classmethod
def parse(cls, code):
pieces = code.split('-')
try:
country_code = pieces[1]
except IndexError:
country_code = None
try:
province_code = pieces[2]
except IndexError:
province_code = None
return {
'continent_code': pieces[0],
'country_code': country_code,
'province_code': province_code,
}

View File

@@ -51,3 +51,20 @@ class TestRecordGeoCodes(TestCase):
# Bad province code, good continent and country
self.assertEquals(['xyz unknown province code "NA-US-XX"'],
GeoCodes.validate('NA-US-XX', prefix))
def test_parse(self):
self.assertEquals({
'continent_code': 'NA',
'country_code': None,
'province_code': None,
}, GeoCodes.parse('NA'))
self.assertEquals({
'continent_code': 'NA',
'country_code': 'US',
'province_code': None,
}, GeoCodes.parse('NA-US'))
self.assertEquals({
'continent_code': 'NA',
'country_code': 'US',
'province_code': 'CA',
}, GeoCodes.parse('NA-US-CA'))