Esempio n. 1
0
def upload(data):
    name = None
    rest = uploader.Rest(BASE_URL, USERNAME, SECRET, DEBUG)

    # Upload device first and get name back
    for rec in data:
        if not 'macaddress' in rec:
            devindex = data.index(rec)
    rec = data[devindex]
    if DUPLICATE_SERIALS:
        result = rest.post_multinodes(rec)
    else:
        result = rest.post_device(rec)
    try:
        name = result['msg'][2]
    except:
        pass

    # upload IPs and MACs
    for rec in data:
        if not 'macaddress' in rec:
            pass
        elif 'ipaddress' in rec:
            if name and 'device' in rec:
                rec['device'] = name
            rest.post_ip(rec)
        elif 'port_name' in rec:
            if name and 'device' in rec:
                rec['device'] = name
            rest.post_mac(rec)
Esempio n. 2
0
def remove_stale_ips(ips, name):
    rest = uploader.Rest(debug)
    fetched_ips = rest.get_device_by_name(name)
    ips_to_remove = set(fetched_ips) - set(ips)
    if ips_to_remove:
        print '\n[*] IPs to remove: %s' % ips_to_remove
        for ip in ips_to_remove:
            rest.delete_ip(ip)
Esempio n. 3
0
def upload(data):
    ips = []
    name = None
    dev_id = None
    rest = uploader.Rest(base_url, username, secret, debug)
    if mac_lookup:
        dev_id = find_devid_by_mac(data, rest)

    # get hdd parts if any
    hdd_parts = []
    for rec in data:
        if 'hdd_parts' in rec:
            for part in rec['hdd_parts']:
                hdd_parts.append(part)
            data.remove(rec)

    # Upload device first and get name back
    devindex = None
    for rec in data:
        if 'macaddress' not in rec:
            devindex = data.index(rec)
    if devindex != None:
        rec = data[devindex]
        if mac_lookup and dev_id and not duplicate_serials:
            rec.update({'device_id': dev_id})
            result, scode = rest.put_device(rec)
            if scode != 200:
                print '\n[!] Error! Could not upload device: %s\n' % str(rec)
                return
        elif duplicate_serials:
            if mac_lookup and dev_id:
                rec.update({'device_id': dev_id})
                result, scode = rest.put_device(rec)
                if scode != 200:
                    print '\n[!] Error! Could not upload device: %s\n' % str(
                        rec)
                    return
            else:
                result, scode = rest.post_multinodes(rec)
                if scode != 200:
                    print '\n[!] Error! Could not upload devices: %s\n' % str(
                        rec)
                    return
        else:
            result, scode = rest.post_device(rec)
            if scode != 200:
                print '\n[!] Error! Could not upload device: %s\n' % str(rec)
                return

        try:
            name = result['msg'][2]
        except IndexError:
            print '\n[!] Error! Could not get device name from response: %s\n' % str(
                result)
            return

    # upload IPs and MACs
    for rec in data:
        if 'ipaddress' in rec:
            ip = rec['ipaddress']
            if ip:
                ips.append(ip)
            if name and 'device' in rec:
                rec['device'] = name
            rest.post_ip(rec)
        elif 'port_name' in rec:
            if name and 'device' in rec:
                rec['device'] = name
            rest.post_mac(rec)

    # remove unused IPs
    if REMOVE_STALE_IPS and name:
        remove_stale_ips(ips, name)

    # upload hdd_parts if any
    if hdd_parts:
        for part in hdd_parts:
            rest.post_parts(part)