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

EnvVar: Integrating review feedback and finishing tests

This commit is contained in:
Phelps Williams
2020-07-17 12:09:20 -07:00
parent 4d006e94a2
commit 0a342aa6c2
2 changed files with 25 additions and 23 deletions

View File

@@ -40,7 +40,7 @@ class EnvVarSource(BaseSource):
variable: USER
# The TXT record name to embed the value found at the above
# environment variable
record: deployuser
name: deployuser
# The TTL of the TXT record (optional, default 60)
ttl: 3600
@@ -62,42 +62,37 @@ class EnvVarSource(BaseSource):
DEFAULT_TTL = 60
def __init__(self, id, variable, record, ttl=DEFAULT_TTL):
def __init__(self, id, variable, name, ttl=DEFAULT_TTL):
self.log = logging.getLogger('{}[{}]'.format(
self.__class__.__name__, id))
self.log.debug('__init__: id=%s, variable=%s, record=%s, '
'ttl=%d', id, variable, record, ttl)
self.log.debug('__init__: id=%s, variable=%s, name=%s, '
'ttl=%d', id, variable, name, ttl)
super(EnvVarSource, self).__init__(id)
self.envvar = variable
self.record = record
self.name = name
self.ttl = ttl
self.value = None
def _read_variable(self):
self.value = os.environ.get(self.envvar)
if self.value is None:
value = os.environ.get(self.envvar)
if value is None:
raise EnvironmentVariableNotFoundException(self.envvar)
self.log.debug('_read_variable: successfully loaded var=%s val=%s',
self.envvar, self.value)
self.envvar, value)
return value
def populate(self, zone, target=False, lenient=False):
self.log.debug('populate: name=%s, target=%s, lenient=%s', zone.name,
target, lenient)
# if target:
# TODO: Environment Variable Source cannot act as a target,
# throw exception?
# return
before = len(zone.records)
self._read_variable()
value = self._read_variable()
# We don't need to worry about conflicting records here because the
# manager will deconflict sources on our behalf.
payload = {'ttl': self.ttl, 'type': 'TXT', 'values': [self.value]}
record = Record.new(zone, self.record, payload, source=self,
payload = {'ttl': self.ttl, 'type': 'TXT', 'values': [value]}
record = Record.new(zone, self.name, payload, source=self,
lenient=lenient)
zone.add_record(record, lenient=lenient)

View File

@@ -18,17 +18,24 @@ class TestEnvVarSource(TestCase):
self.assertEquals(msg, text_type(ctx.exception))
with patch.dict('os.environ', {envvar: 'testvalue'}):
source._read_variable()
self.assertEquals(source.value, 'testvalue')
value = source._read_variable()
self.assertEquals(value, 'testvalue')
def test_populate(self):
envvar = 'TEST_VAR'
value = 'somevalue'
record = 'testrecord'
source = EnvVarSource('testid', envvar, record)
zone = Zone('unit.tests.', [])
name = 'testrecord'
zone_name = 'unit.tests.'
source = EnvVarSource('testid', envvar, name)
zone = Zone(zone_name, [])
with patch.dict('os.environ', {envvar: value}):
source.populate(zone)
# TODO: Validate zone and record
self.assertEquals(1, len(zone.records))
record = list(zone.records)[0]
self.assertEquals(name, record.name)
self.assertEquals('{}.{}'.format(name, zone_name), record.fqdn)
self.assertEquals('TXT', record._type)
self.assertEquals(1, len(record.values))
self.assertEquals(value, record.values[0])