blob: 3e910b8177c9ccdf47773a4eaaa1185a773ada65 [file] [log] [blame]
Dzmitry Stremkouskid95bd2e2018-12-03 17:35:46 +01001#!/usr/bin/env python2.7
2from argparse import ArgumentParser
3import json
4import yaml
5import logging
6
7from maas_client import MAASClient, MAASDispatcher, MAASOAuth
8from maasng import list_dnsresources
9
10parser = ArgumentParser()
11parser.add_argument('--address_pool', help='Path to address_pool \
12 yaml file', required=True)
13parser.add_argument('--debug', action='store_true', default=False)
14
15def 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
52if __name__ == "__main__":
53 print maas_IPAM()