# Path: quads/helpers.py
# def param_check(data, params, defaults={}):
#     result = []
#     # set defaults
#     for k, v in defaults.items():
#         data.setdefault(k, v)
# 
#     if data:
#         # check for missing params
#         for param in params:
#             if param not in data:
#                 result.append("Missing required parameter: %s" % param)
#             elif not data[param]:
#                 result.append("Could not parse %s parameter" % param)
#             elif data[param] == "None":
#                 data[param] = None
#             if param == "_id":
#                 data["_id"] = ObjectIdField(data[param])
# 
#     return result, data


import datetime
import ipaddress
import os
from mongoengine import (
    connect,
    Document,
    EmbeddedDocument,
    queryset_manager,
    Q,
    StringField,
    BooleanField,
    ListField,
    ReferenceField,
    DateTimeField,
    SequenceField,
    IntField,
    LongField,
    EmbeddedDocumentField,
)
from quads.helpers import param_check



connect("quads", host=os.environ.get("MONGODB_IP", "127.0.0.1"))


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

# Path: quads/helpers.py
# def param_check(data, params, defaults={}):
#     result = []
#     # set defaults
#     for k, v in defaults.items():
#         data.setdefault(k, v)
# 
#     if data:
#         # check for missing params
#         for param in params:
#             if param not in data:
#                 result.append("Missing required parameter: %s" % param)
#             elif not data[param]:
#                 result.append("Could not parse %s parameter" % param)
#             elif data[param] == "None":
#                 data[param] = None
#             if param == "_id":
#                 data["_id"] = ObjectIdField(data[param])
# 
#     return result, data


# Path: quads/model.py
import datetime
import ipaddress
import os
from mongoengine import (
    connect,
    Document,
    EmbeddedDocument,
    queryset_manager,
    Q,
    StringField,
    BooleanField,
    ListField,
    ReferenceField,
    DateTimeField,
    SequenceField,
    IntField,
    LongField,
    EmbeddedDocumentField,
)
from quads.helpers import param_check




connect("quads", host=os.environ.get("MONGODB_IP", "127.0.0.1"))


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)