powerbox icon indicating copy to clipboard operation
powerbox copied to clipboard

New Tags resource

Open agowa opened this issue 4 years ago • 1 comments

We recently updated netbox to v2.9.8 and one of our scripts broke because there apparently the api changed and contains now a new resource specifically for tags:

POST /api/tenancy/tenants/?format=json HTTP/1.1
Host: netbox
Authorization: token myTokenString
User-Agent: PowerShell
Content-Type: application/json
Connection: close

{"slug":"BAR111","tags":["MyTagName"],"custom_fields":{"customer_id":"111"},"group":1,"name":"Bar111"}

Error response:

HTTP/1.1 400 Bad Request
Date: Sat, 05 Dec 2020 04:08:45 GMT
Content-Type: application/json
Connection: close
Server: gunicorn/20.0.4
Vary: Accept, Cookie, Origin
Allow: GET, POST, HEAD, OPTIONS
API-Version: 2.9
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
X-Frame-Options: SAMEORIGIN

{"tags":[["Related objects must be referenced by numeric ID or by dictionary of attributes. Received an unrecognized value: MyTagName"]]}

new:

POST /api/tenancy/tenants/?format=json HTTP/1.1
Host: netbox
Authorization: token myTokenString
User-Agent: PowerShell
Content-Type: application/json
Connection: close

{"slug":"BAR111","tags":[92],"custom_fields":{"customer_id":"111"},"group":1,"name":"Bar111"}

The difference is that tags is now a int array instead of a string array. Where the provided number ("92") is the id of the tag resource. The swagger documentation of netbox is apparently incorrect on that part, as it says it's an array of objects with mandatory name and slug.

To get all tags and there id: Get-nbObject -APIUrl "https://netbox" -Resource "extras/tags" | ft

agowa avatar Dec 07 '20 01:12 agowa

When I have to play with tags, I ended up doing the following:

#Loading available Tags: $nbtags = Get-nbObject -Resource 'extras/tags' #Replacing array values by ID $site.site_tags = @($site.site_tags | forEach-Object { $tag = $_ ($nbtags | Where-Object {$.name -like "$tag"}).id }) #Removing nulls in array if any $site.site_tags = $site.Site_tags.Where({ $null -ne $ }) $values= [PScustomObject]@{ tags = $site.site_tags } Set-nbSite -id $nbsite.id -Object $updates -Patch

thefreakquency avatar Apr 09 '21 18:04 thefreakquency