Skip to content

Commit

Permalink
fixes null value in targets(when interf. has no ip) (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhipfel authored Oct 17, 2023
1 parent ef15897 commit ccdbaa6
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions netbox_atlas_plugin/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,26 @@ def get_targets(vm, target):
if getattr(vm, "primary_ip4", None) is not None:
return [str(IPNetwork(vm.primary_ip4.address).ip)]
elif target == "mgmt_only":
interfaces = []
targets = []
if hasattr(vm, "interfaces") and vm.interfaces is not None:
result = vm.interfaces.filter(mgmt_only=True)
interfaces = map(get_address, result)
return interfaces
targets = get_interface_addresses(result)
return targets
elif target == "loopback10":
interfaces = []
targets = []
if hasattr(vm, "interfaces") and vm.interfaces is not None:
result = vm.interfaces.filter(name='Loopback10')
interfaces = map(get_address, result)
return interfaces
targets = get_interface_addresses(result)
return targets
else:
if hasattr(vm, "primary_ip") and vm.primary_ip is not None:
return [str(IPNetwork(vm.primary_ip.address).ip)]


def get_address(interface):
def get_interface_addresses(interfaces):
interfaces = [i for i in map(map_ip_address, interfaces) if i is not None]
return interfaces

def map_ip_address(interface):
if len(interface.ip_addresses.all()) > 0:
return str(IPNetwork(interface.ip_addresses.first().address).ip)

0 comments on commit ccdbaa6

Please sign in to comment.