示例#1
0
def add_device():
    new_device_data = general_utils.read_device_details_from_request_body()
    mac = new_device_data['mac']
    if len(mac) == 0:
        return jsonify(status="Error", reason="Must Supply MAC Address")
    if not validators.mac_address(mac):
        return jsonify(status="Error", reason="Invalid MAC Address")
    ip = new_device_data['ip']
    if len(ip) > 0 and not validators.ip_address(ip):
        return jsonify(status="Error", reason="Invalid IP Address")
    hostname = new_device_data['hostname']
    if len(hostname) > 0 and not validators.hostname(hostname):
        return jsonify(status="Error", reason="Invalid Hostname")

    existing_devices = db.find_device_by_mac(mac)

    if len(existing_devices) == 0:
        try:
            db.add_device(new_device_data)

            email_body = render_template('emails/addDevice.html',
                                         deviceInfo=new_device_data,
                                         serverUrl=app.config['SERVER_URL'])
            email.email_user("New Device Detected", email_body)
            return jsonify(status="Success", reason="Device added")
        except Exception as e:
            traceback.print_exc()
            return jsonify(status="Error",
                           reason='Error adding device:' + str(e))
    else:
        # device already exists
        existing_device = existing_devices[0]
        existing_device['hostname'] = hostname
        existing_device['ip'] = ip
        existing_device['last_seen'] = datetime.datetime.now()

        try:
            db.update_device(existing_device)
            return jsonify(status="Success", reason="Device updated")
        except Exception as e:
            traceback.print_exc()
            return jsonify(status="Error",
                           reason='Error updating device:' + str(e))
示例#2
0
def test_returns_true_on_valid_domain(value):
    assert hostname(value)
示例#3
0
def test_returns_false_on_invalid_hostname(value):
    assert isinstance(hostname(value), ValidationFailure)