fix(#212): prevent server tags to be override.

Add a param `--replace-tags` to override all tags, default behavior is to append new tags.
This commit is contained in:
Cyril Levis 2022-03-28 14:37:21 +02:00
parent ce83364ff9
commit dd4d935fb2
No known key found for this signature in database
GPG key ID: 6DB88737C11F5A48
2 changed files with 10 additions and 2 deletions

View file

@ -45,6 +45,7 @@ def get_config():
help="Command to output hostname, used as Device's name in netbox")
p.add_argument('--device.tags', default=r'',
help='tags to use for a host')
p.add_argument('--preserve-tags', action='store_true', help='Append new unique tags, preserve those already present')
p.add_argument('--device.custom_fields', default=r'',
help='custom_fields to use for a host, eg: field1=v1,field2=v2')
p.add_argument('--device.blade_role', default=r'Blade',

View file

@ -444,8 +444,15 @@ class ServerBase():
server.name = self.get_hostname()
update += 1
if sorted(set([x.name for x in server.tags])) != sorted(set(self.tags)):
server.tags = [x.id for x in self.nb_tags]
server_tags = sorted(set([x.name for x in server.tags]))
tags = sorted(set(self.tags))
if server_tags != tags:
new_tags_ids = [x.id for x in self.nb_tags]
if not config.preserve_tags:
server.tags = new_tags_ids
else:
server_tags_ids = [x.id for x in server.tags]
server.tags = sorted(set(new_tags_ids + server_tags_ids))
update += 1
if server.custom_fields != self.custom_fields: