netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
16.27k stars 2.59k forks source link

Unable to connect interfaces via API #2476

Closed flowdnb closed 6 years ago

flowdnb commented 6 years ago

Error exceptions.TypeError 'Interface' object is not iterable when trying to connect interfaces via api.

Environment

Steps to Reproduce

curl -X POST \
-H "Authorization: Token <TOKEN>" \
-H "Content-Type: application/json" \
-H "Accept: application/json; indent=4" \
https://netbox.fqdn/api/dcim/interface-connections/ \
--data '{ "interface_a": 114022, "interface_b": 114030, "connection_status": true }'

id of interface_a exists:

curl -X GET \
-H "Authorization: Token <TOKEN>" \
https://netbox.fqdn/api/dcim/interfaces/114022/ \
-H "accept: application/json"

{"id":114022,"device":{"id":4177,"url":"https://netbox.fqdn/api/dcim/devices/4177/","name":"API-Test-1","display_name":"API-Test-1"},"name":"1/1","form_factor":{"value":800,"label":"100BASE-TX (10/100ME)"},"enabled":true,"lag":null,"mtu":null,"mac_address":null,"mgmt_only":false,"description":"","is_connected":false,"interface_connection":null,"circuit_termination":null,"mode":null,"untagged_vlan":null,"tagged_vlans":[],"tags":[]}

id of interface_b exists:

curl -X GET \
-H "Authorization: Token <TOKEN>" \
https://netbox.fqdn/api/dcim/interfaces/114030/ \
-H "accept: application/json"

{"id":114030,"device":{"id":4178,"url":"https://netbox.fqdn/api/dcim/devices/4178/","name":"API-Test-2","display_name":"API-Test-2"},"name":"1/1","form_factor":{"value":800,"label":"100BASE-TX (10/100ME)"},"enabled":true,"lag":null,"mtu":null,"mac_address":null,"mgmt_only":false,"description":"","is_connected":false,"interface_connection":null,"circuit_termination":null,"mode":null,"untagged_vlan":null,"tagged_vlans":[],"tags":[]}

Expected Behavior

The interfaces would be connnected.

Observed Behavior

Response:

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Server Error</title>
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css">
    <meta charset="UTF-8">
</head>

<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <div class="panel panel-danger" style="margin-top: 200px">
                    <div class="panel-heading">
                        <strong>
                            <i class="fa fa-warning"></i>
                            Server Error
                        </strong>
                    </div>
                    <div class="panel-body">

                            <p>
                                There was a problem with your request. Please contact an administrator.
                            </p>

                        <hr />
                        <p>
                            The complete exception is provided below:
                        </p>
<pre><strong>&lt;type &#39;exceptions.TypeError&#39;&gt;</strong><br />
&#39;Interface&#39; object is not iterable</pre>
                        <p>
                            If further assistance is required, please post to the <a href="https://groups.google.com/forum/#!forum/netbox-discuss">NetBox mailing list</a>.
                        </p>
                        <div class="text-right">
                            <a href="/" class="btn btn-primary">Home Page</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

</html>

Exception:

Internal Server Error: /api/dcim/interface-connections/

TypeError at /api/dcim/interface-connections/
'Interface' object is not iterable

Request Method: POST
Request URL: https://netbox.fqdn/api/dcim/interface-connections/
Django Version: 1.11.11
Python Executable: /usr/bin/python2
Python Version: 2.7.5
Python Path: ['/opt/netbox/netbox', '/opt/netbox/netbox', '/usr/bin', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib/python2.7/site-packages']
Server time: Tue, 2 Oct 2018 16:39:40 +0200
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'corsheaders',
 'debug_toolbar',
 'django_filters',
 'django_tables2',
 'mptt',
 'rest_framework',
 'taggit',
 'taggit_serializer',
 'timezone_field',
 'circuits',
 'dcim',
 'ipam',
 'extras',
 'secrets',
 'tenancy',
 'users',
 'utilities',
 'virtualization',
 'drf_yasg']
Installed Middleware:
('debug_toolbar.middleware.DebugToolbarMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'utilities.middleware.ExceptionHandlingMiddleware',
 'utilities.middleware.LoginRequiredMiddleware',
 'utilities.middleware.APIVersionMiddleware',
 'extras.middleware.ObjectChangeMiddleware')

Traceback:  

File "/usr/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "/usr/lib/python2.7/site-packages/rest_framework/viewsets.py" in view
  103.             return self.dispatch(request, *args, **kwargs)

File "/usr/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  483.             response = self.handle_exception(exc)

File "/usr/lib/python2.7/site-packages/rest_framework/views.py" in handle_exception
  443.             self.raise_uncaught_exception(exc)

File "/usr/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  480.             response = handler(request, *args, **kwargs)

File "/usr/lib/python2.7/site-packages/rest_framework/mixins.py" in create
  20.         serializer.is_valid(raise_exception=True)

File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py" in is_valid
  236.                 self._validated_data = self.run_validation(self.initial_data)

File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py" in run_validation
  434.         value = self.to_internal_value(data)

File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py" in to_internal_value
  488.                 validated_value = field.run_validation(primitive_value)

File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py" in run_validation
  436.             self.run_validators(value)

File "/usr/lib/python2.7/site-packages/rest_framework/serializers.py" in run_validators
  465.         to_validate.update(value)

File "/usr/lib64/python2.7/_abcoll.py" in update
  547.             for key, value in other:

Exception Type: TypeError at /api/dcim/interface-connections/
Exception Value: 'Interface' object is not iterable
Request information:
USER: admin

GET: No GET data

POST: No POST data

FILES: No FILES data

COOKIES: No cookie data

META:
CONTENT_LENGTH = '75'
CONTENT_TYPE = 'application/json'
HTTP_ACCEPT = 'application/json; indent=4'
HTTP_AUTHORIZATION = 'Token ***'
HTTP_CONNECTION = 'close'
HTTP_HOST = '127.0.0.1:8010'
HTTP_USER_AGENT = 'curl/7.54.0'
HTTP_X_FORWARDED_HOST = 'netbox.fqdn'
HTTP_X_FORWARDED_PROTO = 'https'
HTTP_X_REAL_IP = '192.168.0.1'
PATH_INFO = u'/api/dcim/interface-connections/'
QUERY_STRING = ''
RAW_URI = '/api/dcim/interface-connections/'
REMOTE_ADDR = '127.0.0.1'
REMOTE_PORT = '56790'
REQUEST_METHOD = 'POST'
SCRIPT_NAME = u''
SERVER_NAME = '127.0.0.1'
SERVER_PORT = '8010'
SERVER_PROTOCOL = 'HTTP/1.0'
SERVER_SOFTWARE = 'gunicorn/19.6.0'
gunicorn.socket = <socket._socketobject object at 0x7f3257656280>
wsgi.errors = <gunicorn.http.wsgi.WSGIErrorsWrapper object at 0x7f3257a7b890>
wsgi.file_wrapper = ''
wsgi.input = <gunicorn.http.body.Body object at 0x7f3259e02490>
wsgi.multiprocess = True
wsgi.multithread = False
wsgi.run_once = False
wsgi.url_scheme = 'https'
wsgi.version = u'(1, 0)'
a31amit commented 6 years ago

curl -vvv -X POST \ -H "Authorization: Token $MY_TOKEN" \ -H "Content-Type: application/json" \ -H "Accept: application/json; indent=4" \ http://$MY_NETBOX/api/dcim/interface-connections/ \ --data '{ "interface_a": 7907, "interface_b": 1079, "connection_status": true }'

I tried same it worked for me. I am using netbox 2.4.3.

flowdnb commented 6 years ago

It worked after I migrated to Python3.

SesquipedalianDefenestrator commented 5 years ago

For anyone else encountering this, migrating to Python3 fixed it via coincidence/luck. It's the djangorestframework version: https://github.com/digitalocean/netbox/issues/2527