Skip to content

Commit

Permalink
Merge pull request #293 from sinavir/fix_vm_tags
Browse files Browse the repository at this point in the history
Fix not working tag with VM
  • Loading branch information
ribetm authored Oct 22, 2024
2 parents 13be03c + c9a57de commit ada94b1
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions netbox_agent/virtualmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def __init__(self, dmi=None):
self.device_platform = get_device_platform(config.device.platform)

self.tags = list(set(config.device.tags.split(','))) if config.device.tags else []
if self.tags and len(self.tags):
create_netbox_tags(self.tags)
self.nb_tags = create_netbox_tags(self.tags)

def get_memory(self):
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') # e.g. 4015976448
Expand Down Expand Up @@ -107,7 +106,7 @@ def netbox_create_or_update(self, config):
vcpus=vcpus,
memory=memory,
tenant=tenant.id if tenant else None,
tags=self.tags,
tags=[{'name': x} for x in self.tags],
)
created = True

Expand All @@ -121,9 +120,18 @@ def netbox_create_or_update(self, config):
if vm.memory != memory:
vm.memory = memory
updated += 1
if sorted(set(vm.tags)) != sorted(set(self.tags)):
vm.tags = self.tags

vm_tags = sorted(set([x.name for x in vm.tags]))
tags = sorted(set(self.tags))
if vm_tags != tags:
new_tags_ids = [x.id for x in self.nb_tags]
if not config.preserve_tags:
vm.tags = new_tags_ids
else:
vm_tags_ids = [x.id for x in vm.tags]
vm.tags = sorted(set(new_tags_ids + vm_tags_ids))
updated += 1

if vm.platform != self.device_platform:
vm.platform = self.device_platform
updated += 1
Expand Down

0 comments on commit ada94b1

Please # to comment.