Trying to create large amount of interfaces #13659
-
I thought this would be an easy task but I'm really not sure what I'm doing wrong. I imported several hundred VMs into netbox. Now I need to make interfaces for those VMs. But I can't seem to get the api to work for actually creating interfaces for my VMs. response = requests.get(headers=header, url=base_url+vms+options, verify=False)
json_dict = response.json() This works as expected. I got all of the VMs that are loaded in. Great. I loop through each VM since I need to assign an interface (All VMs have one interface. There's only a dozen or 2 that have more than 1) interfaces = []
for vm in json_dict['results']:
if vm['primary_ip'] is None:
interfaces.append(json.dumps({
"name": "veth0",
"virtual_machine": vm['id']
})) This makes what I expect but when I got to try and post/put/patch all I get is "{'non_field_errors': ['Expected a list of items but got type "dict".']}" The below is my latest try. I've tried json.dumps(interfaces), just interfaces, but I can't seem to understand why I can't make the interfaces. I'm looking at the docs for creating multiple objects here: https://demo.netbox.dev/static/docs/rest-api/overview/#creating-multiple-objects response = requests.patch(headers=header,url=base_url+'/virtualization/interfaces',verify=False,data=str(interfaces))
print(response.json()) I usually use a library to interact with APIs so this is really the first time I'm interacting directly with requests. So this might be something super obvious. I also wanted to look at the docs but going to /api/docs just results in a page not found. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Firstly, to create interfaces, you have to post to Secondly, Something like this (untested):
It's |
Beta Was this translation helpful? Give feedback.
Firstly, to create interfaces, you have to post to
/api/virtualization/interfaces/
(note the trailing slash). And you need to POST not PATCH.Secondly,
data=str(interfaces)
won't work. That will give you a Python string representation - in this case of a list of strings (which is also wrong). Usejson=interfaces
instead, and get rid of the json.dumps.Something like this (untested):