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

POC of config/validation errors with context. Implemented by YAML for all it's cases

This commit is contained in:
Ross McFarland
2023-07-28 17:14:01 -07:00
parent 7b2a1d4429
commit 1776d558b5
5 changed files with 97 additions and 37 deletions

View File

@@ -200,9 +200,11 @@ class Manager(object):
except KeyError:
self.log.exception('Invalid provider class')
raise ManagerException(
f'Provider {provider_name} is missing class'
f'Provider {provider_name} is missing class, {provider_config.context}'
)
_class, module, version = self._get_named_class('provider', _class)
_class, module, version = self._get_named_class(
'provider', _class, provider_config.context
)
kwargs = self._build_kwargs(provider_config)
try:
providers[provider_name] = _class(provider_name, **kwargs)
@@ -215,7 +217,7 @@ class Manager(object):
except TypeError:
self.log.exception('Invalid provider config')
raise ManagerException(
'Incorrect provider config for ' + provider_name
f'Incorrect provider config for {provider_name}, {provider_config.context}'
)
return providers
@@ -228,9 +230,11 @@ class Manager(object):
except KeyError:
self.log.exception('Invalid processor class')
raise ManagerException(
f'Processor {processor_name} is missing class'
f'Processor {processor_name} is missing class, {processor_config.context}'
)
_class, module, version = self._get_named_class('processor', _class)
_class, module, version = self._get_named_class(
'processor', _class, processor_config.context
)
kwargs = self._build_kwargs(processor_config)
try:
processors[processor_name] = _class(processor_name, **kwargs)
@@ -243,22 +247,24 @@ class Manager(object):
except TypeError:
self.log.exception('Invalid processor config')
raise ManagerException(
'Incorrect processor config for ' + processor_name
f'Incorrect processor config for {processor_name}, {processor_config.context}'
)
return processors
def _config_plan_outputs(self, plan_outputs_config):
plan_outputs = {}
for plan_output_name, plan_output_config in plan_outputs_config.items():
context = getattr(plan_output_config, 'context', None)
try:
_class = plan_output_config.pop('class')
except KeyError:
self.log.exception('Invalid plan_output class')
raise ManagerException(
f'plan_output {plan_output_name} is missing class'
)
msg = f'plan_output {plan_output_name} is missing class'
if context:
msg += f', {context}'
raise ManagerException(msg)
_class, module, version = self._get_named_class(
'plan_output', _class
'plan_output', _class, context
)
kwargs = self._build_kwargs(plan_output_config)
try:
@@ -275,9 +281,11 @@ class Manager(object):
)
except TypeError:
self.log.exception('Invalid plan_output config')
raise ManagerException(
'Incorrect plan_output config for ' + plan_output_name
)
msg = f'Incorrect plan_output config for {plan_output_name}'
if context:
msg += f', {plan_output_config.context}'
raise ManagerException(msg)
return plan_outputs
def _try_version(self, module_name, module=None, version=None):
@@ -308,7 +316,7 @@ class Manager(object):
version = self._try_version(current)
return module, version or 'n/a'
def _get_named_class(self, _type, _class):
def _get_named_class(self, _type, _class, context):
try:
module_name, class_name = _class.rsplit('.', 1)
module, version = self._import_module(module_name)
@@ -316,7 +324,10 @@ class Manager(object):
self.log.exception(
'_get_{}_class: Unable to import module %s', _class
)
raise ManagerException(f'Unknown {_type} class: {_class}')
msg = f'Unknown {_type} class: {_class}'
if context:
msg += f', {context}'
raise ManagerException(msg)
try:
return getattr(module, class_name), module_name, version
@@ -326,11 +337,14 @@ class Manager(object):
class_name,
module,
)
raise ManagerException(f'Unknown {_type} class: {_class}')
raise ManagerException(
f'Unknown {_type} class: {_class}, {context}'
)
def _build_kwargs(self, source):
# Build up the arguments we need to pass to the provider
kwargs = {}
context = getattr(source, 'context', None)
for k, v in source.items():
try:
if v.startswith('env/'):
@@ -339,10 +353,10 @@ class Manager(object):
v = environ[env_var]
except KeyError:
self.log.exception('Invalid provider config')
raise ManagerException(
'Incorrect provider config, '
'missing env var ' + env_var
)
msg = f'Incorrect provider config, missing env var {env_var}'
if context:
msg += f', {context}'
raise ManagerException(msg)
except AttributeError:
pass
kwargs[k] = v