Tenant assignment broken with API #2059

Closed
opened 2025-12-29 17:21:53 +01:00 by adam · 4 comments
Owner

Originally created by @dirtycajunrice on GitHub (Oct 11, 2018).

Environment

  • Python version: 3.6.6
  • NetBox version: 2.4.5

Steps to Reproduce

Use Swagger or directly call the api to create a vlan/vrf etc while assigning it a tenant

Expected Behavior

Assign to tenant

Observed Behavior

No tenant assignment in api but creation of object / Complete failure in swagger

Examples:
Before, this would assign. This just specifies 'tenant': tenant_id.

p = requests.post(api['tenancy']['tenants'], headers=api['api']['headers'], data=tenants, verify=False)
>>> p.json()
{'id': 10985, 'name': 'DirtyCajunRice', 'slug': 'dirtycajun', 'group': None, 'description': '', 'comments': '', 'tags': [], 'created': '2018-10-11', 'last_updated': '2018-10-11T16:31:14.541213Z'}
>>> successful = {'tenants': {'api': api['tenancy']['tenants'], 'data': p.json()}}
>>> successful
{'tenants': {'api': 'https://netbox.<redacted>/api/tenancy/tenants/', 'data': {'id': 10985, 'name': 'DirtyCajunRice', 'slug': 'dirtycajun', 'group': None, 'description': '', 'comments': '', 'tags': [], 'created': '2018-10-11', 'last_updated': '2018-10-11T16:31:14.541213Z'}}}
>>> vrfs['tenant'] = successful['tenants']['data']['id']
>>> vrfs
{'name': 'CUST-21100', 'tenant': 10985, 'enforce_unique': True, 'rd': 'CUST-21100'}
>>> p = requests.post(api['ipam']['vrfs'], headers=api['api']['headers'], data=vrfs, verify=False)
>>> p.json()
{'id': 286, 'name': 'CUST-21100', 'rd': 'CUST-21100', 'tenant': None, 'enforce_unique': True, 'description': '', 'tags': [], 'display_name': 'CUST-21100 (CUST-21100)', 'created': '2018-10-11', 'last_updated': '2018-10-11T16:32:35.042343Z'}

according to swagger now you need to use a dict with name, slug, and optionally tenant_id. This seems overly verbose, and does not work regardless XD.
example using swagger:

{
  "name": "CUST-21100",
  "rd": "CUST-21100",
  "tenant": {
    "name": "DirtyCajunRice",
    "slug": "dirtycajun"
  },
  "enforce_unique": true
}

error:

{
  "tenant": [
    "Primary key must be an integer"
  ]
}
Originally created by @dirtycajunrice on GitHub (Oct 11, 2018). <!-- NOTE: This form is only for reproducible bugs. If you need assistance with NetBox installation, or if you have a general question, DO NOT open an issue. Instead, post to our mailing list: https://groups.google.com/forum/#!forum/netbox-discuss Please describe the environment in which you are running NetBox. Be sure that you are running an unmodified instance of the latest stable release before submitting a bug report. --> ### Environment * Python version: 3.6.6 * NetBox version: 2.4.5 <!-- Describe in detail the steps that someone else can take to reproduce this bug using the current stable release of NetBox (or the current beta release where applicable). --> ### Steps to Reproduce Use Swagger or directly call the api to create a vlan/vrf etc while assigning it a tenant <!-- What did you expect to happen? --> ### Expected Behavior Assign to tenant <!-- What happened instead? --> ### Observed Behavior No tenant assignment in api but creation of object / Complete failure in swagger Examples: Before, this would assign. This just specifies 'tenant': tenant_id. ``` p = requests.post(api['tenancy']['tenants'], headers=api['api']['headers'], data=tenants, verify=False) >>> p.json() {'id': 10985, 'name': 'DirtyCajunRice', 'slug': 'dirtycajun', 'group': None, 'description': '', 'comments': '', 'tags': [], 'created': '2018-10-11', 'last_updated': '2018-10-11T16:31:14.541213Z'} >>> successful = {'tenants': {'api': api['tenancy']['tenants'], 'data': p.json()}} >>> successful {'tenants': {'api': 'https://netbox.<redacted>/api/tenancy/tenants/', 'data': {'id': 10985, 'name': 'DirtyCajunRice', 'slug': 'dirtycajun', 'group': None, 'description': '', 'comments': '', 'tags': [], 'created': '2018-10-11', 'last_updated': '2018-10-11T16:31:14.541213Z'}}} >>> vrfs['tenant'] = successful['tenants']['data']['id'] >>> vrfs {'name': 'CUST-21100', 'tenant': 10985, 'enforce_unique': True, 'rd': 'CUST-21100'} >>> p = requests.post(api['ipam']['vrfs'], headers=api['api']['headers'], data=vrfs, verify=False) >>> p.json() {'id': 286, 'name': 'CUST-21100', 'rd': 'CUST-21100', 'tenant': None, 'enforce_unique': True, 'description': '', 'tags': [], 'display_name': 'CUST-21100 (CUST-21100)', 'created': '2018-10-11', 'last_updated': '2018-10-11T16:32:35.042343Z'} ``` according to swagger now you need to use a dict with name, slug, and optionally tenant_id. This seems overly verbose, and does not work regardless XD. example using swagger: ``` { "name": "CUST-21100", "rd": "CUST-21100", "tenant": { "name": "DirtyCajunRice", "slug": "dirtycajun" }, "enforce_unique": true } ``` error: ``` { "tenant": [ "Primary key must be an integer" ] } ```
adam closed this issue 2025-12-29 17:21:53 +01:00
Author
Owner

@jeremystretch commented on GitHub (Oct 16, 2018):

As the error message indicates, the API expects to receive the integer ID of the tenant to be assigned, not an object. This is a known limitation with Swagger, which automatically generates API documentation but does not convey the differences between read and write serializer operations.

@jeremystretch commented on GitHub (Oct 16, 2018): As the error message indicates, the API expects to receive the integer ID of the tenant to be assigned, not an object. This is a known limitation with Swagger, which automatically generates API documentation but does not convey the differences between read and write serializer operations.
Author
Owner

@dirtycajunrice commented on GitHub (Oct 19, 2018):

@jeremystretch Swagger was just me checking to see if anything had changed. Per the live example above on a remote host, When giving tenant information during vrf creation, the vrf is created but not associated with the given tenant, regardless of using pk id, name, slug or any combination of them.

@dirtycajunrice commented on GitHub (Oct 19, 2018): @jeremystretch Swagger was just me checking to see if anything had changed. Per the live example above on a remote host, When giving tenant information during vrf creation, the vrf is created but not associated with the given tenant, regardless of using pk id, name, slug or any combination of them.
Author
Owner

@Preskton commented on GitHub (Nov 8, 2018):

Hi, @jeremystretch I'm currently working on a Terraform plugin to handle Netbox resources, and I've run into the above on several other objects/relationships as well. One example is, when I try to directly POST to /ipam/prefixes and try to set a VRF on a prefix, I get the same Primary key must be an integer error, regardless of how I try to assign it. Here are some payloads I've tried to pass through:

// Object w/ RD

{
  "prefix": "192.168.254.0/24",
  "vrf": {
	"rd": "pd-test-vrf-2"
  },
  "is_pool": true,
  "description": "test",
  "tags": ["test"]  
}

// Object w/ RD + Name

{
  "prefix": "192.168.254.0/24",
  "vrf": {
	"rd": "pd-test-vrf-2",
	"name": "PD Test VRF #2"
  },
  "is_pool": true,
  "description": "test",
  "tags": ["test"]  
}

// Object w/ ID

{
  "prefix": "192.168.254.0/24",
  "vrf": {
	"id": 16
  },
  "is_pool": true,
  "description": "test",
  "tags": ["test"]  
}

// ID w/o object

{
  "prefix": "192.168.254.0/24",
  "vrf": 16, 
  "is_pool": true,
  "description": "test",
  "tags": ["test"]  
}

Netbox version = v2.4.7-dev

@Preskton commented on GitHub (Nov 8, 2018): Hi, @jeremystretch I'm currently working on a Terraform plugin to handle Netbox resources, and I've run into the above on several other objects/relationships as well. One example is, when I try to directly `POST` to `/ipam/prefixes` and try to set a VRF on a prefix, I get the same `Primary key must be an integer` error, regardless of how I try to assign it. Here are some payloads I've tried to pass through: ```json // Object w/ RD { "prefix": "192.168.254.0/24", "vrf": { "rd": "pd-test-vrf-2" }, "is_pool": true, "description": "test", "tags": ["test"] } // Object w/ RD + Name { "prefix": "192.168.254.0/24", "vrf": { "rd": "pd-test-vrf-2", "name": "PD Test VRF #2" }, "is_pool": true, "description": "test", "tags": ["test"] } // Object w/ ID { "prefix": "192.168.254.0/24", "vrf": { "id": 16 }, "is_pool": true, "description": "test", "tags": ["test"] } // ID w/o object { "prefix": "192.168.254.0/24", "vrf": 16, "is_pool": true, "description": "test", "tags": ["test"] } ``` Netbox version = `v2.4.7-dev`
Author
Owner

@Preskton commented on GitHub (Nov 8, 2018):

I was able to get a tenant + tenant group to work by hand-modifying the Swagger to use a new object called TenantCreateUpdate in place of Tenant and NestedTenant. As @jeremystretch mentioned, looks like the generated Swagger for the API doesn't discriminate between the input and output models.

Example of the modification here: d7079240e2 (diff-702be237a87f80c305c9755b62bc42f9)

I'll try this same approach for Prefix shortly and report results.

@Preskton commented on GitHub (Nov 8, 2018): I was able to get a `tenant` + `tenant group` to work by hand-modifying the Swagger to use a new object called `TenantCreateUpdate` in place of `Tenant` and `NestedTenant`. As @jeremystretch mentioned, looks like the generated Swagger for the API doesn't discriminate between the input and output models. Example of the modification here: https://github.com/Preskton/go-netbox/commit/d7079240e29a660cdb681ab154c7bc6ff228b778#diff-702be237a87f80c305c9755b62bc42f9 I'll try this same approach for `Prefix` shortly and report results.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#2059