# Path: quads/model.py
# class Vlan(Document):
#     gateway = StringField()
#     ip_free = IntField()
#     ip_range = StringField()
#     netmask = StringField()
#     vlan_id = LongField()
#     meta = {"indexes": [{"fields": ["$vlan_id"]}], "strict": False}
# 
#     @staticmethod
#     def prep_data(data):
#         _fields = [
#             "gateway",
#             "ip_range",
#             "vlan_id",
#         ]
#         if "iprange" in data:
#             data["ip_range"] = data.pop("iprange")
#         if "vlanid" in data:
#             data["vlan_id"] = data.pop("vlanid")
#         ip_address = ipaddress.ip_network(data["ip_range"])
#         data["netmask"] = str(ip_address.netmask)
#         data["ip_free"] = ip_address.num_addresses - 2
#         result, data = param_check(data, _fields)
# 
#         return result, data


import logging
import argparse
import yaml
from quads.model import Vlan
#!/usr/bin/python3



logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format="%(message)s")


def main(_args):
    if _args.yaml:
        vlans = None
        try:
            with open(_args.yaml, "r") as _vlans_read:
                try:
                    vlans = yaml.safe_load(_vlans_read)
                except yaml.YAMLError:
                    logger.error("quads: Invalid YAML config: " + _args.yaml)
                    exit(1)
        except IOError as ex:
            logger.debug(ex)
            logger.error("Error reading %s" % _args.yaml)
            exit(1)
        if vlans:
            for vlan, properties in vlans.items():
                vlan_obj = Vlan.objects(vlan_id=properties["vlanid"]).first()

# Path: quads/model.py
# class Vlan(Document):
#     gateway = StringField()
#     ip_free = IntField()
#     ip_range = StringField()
#     netmask = StringField()
#     vlan_id = LongField()
#     meta = {"indexes": [{"fields": ["$vlan_id"]}], "strict": False}
# 
#     @staticmethod
#     def prep_data(data):
#         _fields = [
#             "gateway",
#             "ip_range",
#             "vlan_id",
#         ]
#         if "iprange" in data:
#             data["ip_range"] = data.pop("iprange")
#         if "vlanid" in data:
#             data["vlan_id"] = data.pop("vlanid")
#         ip_address = ipaddress.ip_network(data["ip_range"])
#         data["netmask"] = str(ip_address.netmask)
#         data["ip_free"] = ip_address.num_addresses - 2
#         result, data = param_check(data, _fields)
# 
#         return result, data


# Path: quads/tools/vlan_yaml_to_mongo.py
import logging
import argparse
import yaml
from quads.model import Vlan

#!/usr/bin/python3



logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format="%(message)s")


def main(_args):
    if _args.yaml:
        vlans = None
        try:
            with open(_args.yaml, "r") as _vlans_read:
                try:
                    vlans = yaml.safe_load(_vlans_read)
                except yaml.YAMLError:
                    logger.error("quads: Invalid YAML config: " + _args.yaml)
                    exit(1)
        except IOError as ex:
            logger.debug(ex)
            logger.error("Error reading %s" % _args.yaml)
            exit(1)
        if vlans:
            for vlan, properties in vlans.items():
                vlan_obj = Vlan.objects(vlan_id=properties["vlanid"]).first()

                result, data = Vlan.prep_data(properties)