Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Tags resource #44

Open
agowa opened this issue Dec 7, 2020 · 1 comment
Open

New Tags resource #44

agowa opened this issue Dec 7, 2020 · 1 comment

Comments

@agowa
Copy link

agowa commented Dec 7, 2020

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

@AnythingOverIP
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants