Dzmitry Stremkouski | d95bd2e | 2018-12-03 17:35:46 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
| 2 | from argparse import ArgumentParser |
| 3 | import json |
| 4 | import yaml |
| 5 | import logging |
| 6 | |
| 7 | from maas_client import MAASClient, MAASDispatcher, MAASOAuth |
| 8 | from maasng import list_dnsresources |
| 9 | |
| 10 | parser = ArgumentParser() |
| 11 | parser.add_argument('--address_pool', help='Path to address_pool \ |
| 12 | yaml file', required=True) |
| 13 | parser.add_argument('--debug', action='store_true', default=False) |
| 14 | |
| 15 | def maas_IPAM(): |
| 16 | args = parser.parse_args() |
| 17 | |
| 18 | handler = logging.StreamHandler() |
| 19 | |
| 20 | if args.debug: |
| 21 | log_level = logging.DEBUG |
| 22 | else: |
| 23 | log_level = logging.INFO |
| 24 | |
| 25 | LOG = logging.getLogger() |
| 26 | LOG.setLevel(log_level) |
| 27 | LOG.addHandler(handler) |
| 28 | |
| 29 | with open(args.address_pool, 'r') as f: |
| 30 | yaml_data = yaml.safe_load(f) |
| 31 | |
| 32 | # TODO: (dstremkouski) |
| 33 | # schema validator for address pool |
| 34 | address_pool = yaml_data["parameters"]["address_pool"] |
| 35 | dnsresources = list_dnsresources() |
| 36 | |
| 37 | for dnsres in dnsresources: |
| 38 | mapping_found = False |
| 39 | for net in address_pool: |
| 40 | if net == 'external': |
| 41 | continue |
| 42 | if mapping_found: |
| 43 | continue |
| 44 | for addr in address_pool[net]['pool']: |
| 45 | if dnsres["hostname"] == addr: |
| 46 | address_pool[net]['pool'][addr] = dnsres["ip_addresses"][0] |
| 47 | mapping_found = True |
| 48 | break |
| 49 | |
| 50 | return('{"address_pool": ' + json.dumps(address_pool) + '}') |
| 51 | |
| 52 | if __name__ == "__main__": |
| 53 | print maas_IPAM() |