'If register_new_installs is enabled in settings, this allows
/usr/bin/cobbler-register (part of the koan package) to add
new system records remotely if they don\'t already exist.
There is a cobbler_register snippet that helps with doing
this automatically for new installs but it can also be used
for existing installs.  See "AutoRegistration" on the Wiki.'

def register_new_system(self, info, token=None, **rest):

    enabled = self.api.settings().register_new_installs
    if (not (str(enabled) in ['1', 'y', 'yes', 'true'])):
        raise CX('registration    is    disabled    in    cobbler    settings')
    name = info.get('name', '')
    profile = info.get('profile', '')
    hostname = info.get('hostname', '')
    interfaces = info.get('interfaces', {})
    ilen = len(interfaces.keys())
    if (name == ''):
        raise CX('no    system    name    submitted')
    if (profile == ''):
        raise CX('profile    not    submitted')
    if (ilen == 0):
        raise CX('no    interfaces    submitted')
    if (ilen >= 64):
        raise CX('too    many    interfaces    submitted')
    name = info.get('name', '')
    inames = interfaces.keys()
    if self.api.find_system(name=name):
        raise CX('system    name    conflicts')
    if ((hostname != '') and self.api.find_system(hostname=hostname)):
        raise CX('hostname    conflicts')
    for iname in inames:
        mac = info['interfaces'][iname].get('mac_address', '')
        ip = info['interfaces'][iname].get('ip_address', '')
        if (ip.find('/') != (-1)):
            raise CX('no    CIDR    ips    are    allowed')
        if (mac == ''):
            raise CX(('missing    MAC    address    for    interface    %s' % iname))
        if (mac != ''):
            system = self.api.find_system(mac_address=mac)
            if (system is not None):
                raise CX(('mac    conflict:    %s' % mac))
        if (ip != ''):
            system = self.api.find_system(ip_address=ip)
            if (system is not None):
                raise CX(('ip    conflict:    %s' % ip))
    obj = self.api.new_system()
    obj.set_profile(profile)
    obj.set_name(name)
    if (hostname != ''):
        obj.set_hostname(hostname)
    obj.set_netboot_enabled(False)
    for iname in inames:
        if (info['interfaces'][iname].get('bridge', '') == 1):
            continue
        mac = info['interfaces'][iname].get('mac_address', '')
        ip = info['interfaces'][iname].get('ip_address', '')
        netmask = info['interfaces'][iname].get('netmask', '')
        if (mac == '?'):
            continue
        obj.set_mac_address(mac, iname)
        if (hostname != ''):
            obj.set_dns_name(hostname, iname)
        if ((ip != '') and (ip != '?')):
            obj.set_ip_address(ip, iname)
        if ((netmask != '') and (netmask != '?')):
            obj.set_netmask(netmask, iname)
    self.api.add_system(obj)
    return 0
