def main() -> None: """Main function of Lexicon.""" # Dynamically determine all the providers available and gather command line arguments. parsed_args = generate_cli_main_parser().parse_args() log_level = logging.getLevelName(parsed_args.log_level) logging.basicConfig(stream=sys.stdout, level=log_level, format="%(message)s") logger.debug("Arguments: %s", parsed_args) # In the CLI context, will get configuration interactively: # * from the command line # * from the environment variables # * from lexicon configuration files found in given --config-dir (default is current dir) config = ConfigResolver() config.with_args(parsed_args).with_env().with_config_dir(parsed_args.config_dir) client = Client(config) results = client.execute() action = config.resolve("lexicon:action") if not action: raise ValueError("Parameter action is not set.") handle_output(results, parsed_args.output, action)
def delete_dns_record(self, record): """ Delete a record from the domain. """ lexicon_config = self._get_base_config() lexicon_config['domain'] = record['domain'] lexicon_config['action'] = 'delete' lexicon_config['name'] = record['name'] lexicon_config['type'] = record['type'] config = ConfigResolver() config.with_dict(dict_object=lexicon_config) client = Client(config) result = False try: result = client.execute() # Invalidate cache for the domain-cname pair cache.delete(f"{record['domain']}-{record['type']}") except Exception as e: # pylint: disable=broad-except # This ugly checking of the exception message is needed # as the library only throws an instance of the Exception class. if 'Record identifier could not be found' in str(e): result = True else: raise return result
def main(): parsed_args = MainParser().parse_args() log_level = logging.getLevelName(parsed_args.log_level) logging.basicConfig(stream=sys.stdout, level=log_level, format='%(message)s') logger.debug('Arguments: %s', parsed_args) client = Client(vars(parsed_args)) results = client.execute() handle_output(results, parsed_args.output)
def add_dns_record(self, record): """ Add a DNS record to the domain. """ lexicon_config = self._get_base_config() lexicon_config['domain'] = record['domain'] lexicon_config['action'] = 'create' lexicon_config['type'] = record['type'] lexicon_config['name'] = record['name'] lexicon_config['content'] = record['value'] lexicon_config['ttl'] = record['ttl'] config = ConfigResolver() config.with_dict(dict_object=lexicon_config) client = Client(config) result = client.execute() return result
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 list_dns_records(self, record): """ List all records of a domain name for a given type. """ cached_result = cache.get(f"{record['domain']}-{record['type']}") if cached_result: return cached_result lexicon_config = self._get_base_config() lexicon_config['domain'] = record['domain'] lexicon_config['action'] = 'list' lexicon_config['type'] = record['type'] config = ConfigResolver() config.with_dict(dict_object=lexicon_config) client = Client(config) result = client.execute() cache.set(f"{record['domain']}-{record['type']}", result) return result
def main(): """Main function of Lexicon.""" # Dynamically determine all the providers available and gather command line arguments. parsed_args = generate_cli_main_parser().parse_args() log_level = logging.getLevelName(parsed_args.log_level) logging.basicConfig(stream=sys.stdout, level=log_level, format='%(message)s') logger.debug('Arguments: %s', parsed_args) # In the CLI context, will get configuration interactively: # * from the command line # * from the environment variables # * from lexicon configuration files in working directory config = ConfigResolver() config.with_args(parsed_args).with_env().with_config_dir(os.getcwd()) client = Client(config) results = client.execute() handle_output(results, parsed_args.output)
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