| #!/usr/bin/env python2.7 |
| from argparse import ArgumentParser |
| import json |
| import yaml |
| import logging |
| |
| from maas_client import MAASClient, MAASDispatcher, MAASOAuth |
| from maasng import list_dnsresources |
| |
| parser = ArgumentParser() |
| parser.add_argument('--address_pool', help='Path to address_pool \ |
| yaml file', required=True) |
| parser.add_argument('--debug', action='store_true', default=False) |
| |
| def maas_IPAM(): |
| args = parser.parse_args() |
| |
| handler = logging.StreamHandler() |
| |
| if args.debug: |
| log_level = logging.DEBUG |
| else: |
| log_level = logging.INFO |
| |
| LOG = logging.getLogger() |
| LOG.setLevel(log_level) |
| LOG.addHandler(handler) |
| |
| with open(args.address_pool, 'r') as f: |
| yaml_data = yaml.safe_load(f) |
| |
| # TODO: (dstremkouski) |
| # schema validator for address pool |
| address_pool = yaml_data["parameters"]["address_pool"] |
| dnsresources = list_dnsresources() |
| |
| for dnsres in dnsresources: |
| mapping_found = False |
| for net in address_pool: |
| if net == 'external': |
| continue |
| if mapping_found: |
| continue |
| for addr in address_pool[net]['pool']: |
| if dnsres["hostname"] == addr: |
| address_pool[net]['pool'][addr] = dnsres["ip_addresses"][0] |
| mapping_found = True |
| break |
| |
| return('{"address_pool": ' + json.dumps(address_pool) + '}') |
| |
| if __name__ == "__main__": |
| print maas_IPAM() |