class LexiconClient: def __init__(self, provider_name, action, domain, name, type, content): self.lexicon_config = { "provider_name": provider_name, "action": action, "domain": domain, "name": name, "type": type, "content": content, } # print(self.lexicon_config) self.config = LexiconConfigResolver() self.config.with_env().with_dict(dict_object=self.lexicon_config) self.client = LexClient(self.config) self.auth_token = self.config.resolve("lexicon:{}:auth_token".format( self.lexicon_config["provider_name"])) def execute(self): # Check provider config before doing stuff results = "" if self.auth_token: results = self.client.execute() # print(results) if results: logger.info("✓ {}: {} Record {} -> {}".format( self.config.resolve("lexicon:provider_name"), self.config.resolve("lexicon:action").upper(), self.config.resolve("lexicon:name") + "." + self.config.resolve("lexicon:domain"), self.config.resolve("lexicon:content"), )) else: logger.error("Couldn't create Record: {}".format(results)) else: logger.error( "✗ {}: Missing auth_token. {} Record {} -> {} failed".format( self.config.resolve("lexicon:provider_name"), self.config.resolve("lexicon:action").upper(), self.config.resolve("lexicon:name") + "." + self.config.resolve("lexicon:domain"), self.config.resolve("lexicon:content"), )) results = False return results
def _test_config(self): """ This method construct a ConfigResolver suitable for tests. This will resolve any parameters required by Lexicon or the provider in the following order: * parameters that matches the ones provided by _test_parameters_overrides * parameters that matches existing environment variables at the time of test execution * parameters processed throught the lambda provided by _test_fallback_fn. See lexicon/providers/base.py for a full list of parameters available. You should not override this method. Just override `self.domain`, or use _test_parameters_overrides() to configure specific parameters for the tests. Any parameters that you expect to be passed to the provider via the cli, like --auth_username and --auth_token, will be present during the tests, with a 'placeholder_' prefix. options['auth_password'] == 'placeholder_auth_password' options['auth_username'] == 'placeholder_auth_username' options['unique_provider_option'] == 'placeholder_unique_provider_option' You can change this behavior by overriding _test_fallback_fn(). """ config = ConfigResolver() # First we load the overrides overrides = self._test_parameters_overrides() overrides['domain'] = self.domain overrides['provider_name'] = self.provider_name config.with_config_source(EngineOverrideConfigSource(overrides)) # Then we get environment variables config.with_env() # And finally we provide the fallback function config.with_config_source( FallbackConfigSource(self._test_fallback_fn())) return config
from lexicon.config import ConfigResolver from lexicon.client import Client lexicon_config = { "provider_name" : "cloudflare", # lexicon shortname for provider, see providers directory for available proviers "action": "list", # create, list, update, delete "domain": "capsulecd.com", # domain name "type": "CNAME", # specify a type for record filtering, case sensitive in some cases. "cloudflare": { # cloudflare(provider) specific configuration goes here. # if .with_env() is not used, all credentials required for authention must be specified here. } } config = ConfigResolver() config.with_env().with_dict(dict_object=lexicon_config) client = Client(config) results = client.execute() print results