Replies: 1 comment
-
It is possible to PATCH existing data but I believe you are right that tags needs to be a list of all the tags you want on the object, so it is two reasons why you need to fetch the current object first before changing it.
So you won't want to call PATCH if the tag is already present to not create unnecessary changelog entries when nothing has changed and when updating the thing it needs is a list of the tag id numbers, not the name strings, so a summary of the way I've done it in perl is:
```perl
# get a hash of the canonical netbox tags from '/extras/tags/'
# as %netbox_tags similar to how device tags
# make hash of existing device tags array by name string
my %device_tags = map { $_->{name} => $_->{id} } @{ $netbox_object->{tags} }
# Check if the thing we want to add already exists
unless ( exists( $device_tags{"new_tag_string"} ) ) {
# create new array with the id number of the new tag
my @new_tags = ( $netbox_tags->{"new_tag_string"}{id} );
# add all the existing %device_tags id values to @new_tags
***@***.***_tags, map { $device_tags{$_} } keys %device_tags ) if %device_tags;
# PATCH $netbox_object->{url} with { "tags": @new_tags }
}
```
—
Mark Tinberg ***@***.***>
Division of Information Technology-Network Services
University of Wisconsin-Madison
…________________________________
From: pisaniej ***@***.***>
Sent: Wednesday, February 2, 2022 8:01 AM
To: netbox-community/netbox ***@***.***>
Cc: Subscribed ***@***.***>
Subject: [netbox-community/netbox] adding an additional tag to an object via REST API (Discussion #8519)
Is it possible to patch an existing object to add an additional tag?
If I understand correctly, a PATCH will need the existing tags along with new tag otherwise only the new tag will be added.
Right now I'm making a GET call to /virtualization/virtual-machines/?name={name}, so that the server's id is returned, and then I'm making a PATCH call to /virtualization/virtual-machines/{id}/.
(Additionally, is there a way to simplify the process above so that I'm only making one call on PATCH if the server's id is unknown to me).
Thanks
—
Reply to this email directly, view it on GitHub<#8519>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AAS7UM55TISBKBBIGEL7SVTUZE2NLANCNFSM5NMEATSQ>.
Triage notifications on the go with GitHub Mobile for iOS<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Is it possible to patch an existing object to add an additional tag?
If I understand correctly, a
PATCH
will need the existing tags along with new tag otherwise only the new tag will be added.Right now I'm making a
GET
call to/virtualization/virtual-machines/?name={name}
, so that the server's id is returned, and then I'm making aPATCH
call to/virtualization/virtual-machines/{id}/
.(Additionally, is there a way to simplify the process above so that I'm only making one call on
PATCH
if the server's id is unknown to me).Thanks
Beta Was this translation helpful? Give feedback.
All reactions