813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987 | def get_connections(
self,
devices: list,
instance: str = None,
dry_run: bool = False,
cables: bool = False,
) -> dict:
"""
Function to retrieve device connections data from Netbox using GraphQL API.
:param instance: Netbox instance name
:param devices: list of devices to retrieve interface for
:param dry_run: only return query content, do not run it
:param cables: if True includes interfaces' directly attached cables details
:return: dictionary keyed by device name with connections data
"""
# form final result dictionary
ret = Result(
task=f"{self.name}:get_connections", result={d: {} for d in devices}
)
# form lists of fields to request from netbox
cable_fields = """
cable {
type
status
tenant {name}
label
tags {name}
length
length_unit
custom_fields
}
"""
if self.nb_version[0] == 4:
interfaces_fields = [
"name",
"device {name}",
"""connected_endpoints {
__typename
... on InterfaceType {name device {name}}
... on ProviderNetworkType {name}
}""",
]
elif self.nb_version[0] == 3:
interfaces_fields = [
"name",
"device {name}",
"""connected_endpoints {
__typename
... on InterfaceType {name device {name}}
}""",
]
interfaces_fields.append(
"""
link_peers {
__typename
... on InterfaceType {name device {name}}
... on FrontPortType {name device {name}}
... on RearPortType {name device {name}}
}
"""
)
console_ports_fields = [
"name",
"device {name}",
"""connected_endpoints {
__typename
... on ConsoleServerPortType {name device {name}}
}""",
"""link_peers {
__typename
... on ConsoleServerPortType {name device {name}}
... on FrontPortType {name device {name}}
... on RearPortType {name device {name}}
}""",
]
console_server_ports_fields = [
"name",
"device {name}",
"""connected_endpoints {
__typename
... on ConsolePortType {name device {name}}
}""",
"""link_peers {
__typename
... on ConsolePortType {name device {name}}
... on FrontPortType {name device {name}}
... on RearPortType {name device {name}}
}""",
]
# check if need to include cables info
if cables is True:
interfaces_fields.append(cable_fields)
console_ports_fields.append(cable_fields)
console_server_ports_fields.append(cable_fields)
# form query dictionary with aliases to get data from Netbox
queries = {
"interface": {
"obj": "interface_list",
"filters": {"device": devices},
"fields": interfaces_fields,
},
"consoleport": {
"obj": "console_port_list",
"filters": {"device": devices},
"fields": console_ports_fields,
},
"consoleserverport": {
"obj": "console_server_port_list",
"filters": {"device": devices},
"fields": console_server_ports_fields,
},
}
# retrieve full list of devices interface with all cables
query_result = self.graphql(queries=queries, instance=instance, dry_run=dry_run)
# return dry run result
if dry_run:
return query_result
all_ports = query_result.result
# extract interfaces
for port_type, ports in all_ports.items():
for port in ports:
endpoints = port["connected_endpoints"]
# skip ports that have no remote device connected
if not endpoints or not all(i for i in endpoints):
continue
# extract required parameters
cable = port.get("cable", {})
device_name = port["device"]["name"]
port_name = port["name"]
link_peers = port["link_peers"]
remote_termination_type = endpoints[0]["__typename"].lower()
remote_termination_type = remote_termination_type.replace("type", "")
# form initial connection dictionary
connection = {
"breakout": len(endpoints) > 1,
"remote_termination_type": remote_termination_type,
"termination_type": port_type,
}
# add remote connection details
if remote_termination_type == "providernetwork":
connection["remote_device"] = None
connection["remote_interface"] = None
connection["provider"] = endpoints[0]["name"]
else:
remote_interface = endpoints[0]["name"]
if len(endpoints) > 1:
remote_interface = [i["name"] for i in endpoints]
connection["remote_interface"] = remote_interface
connection["remote_device"] = endpoints[0]["device"]["name"]
# add cable and its peer details
if cables:
peer_termination_type = link_peers[0]["__typename"].lower()
peer_termination_type = peer_termination_type.replace("type", "")
cable["peer_termination_type"] = peer_termination_type
cable["peer_device"] = link_peers[0].get("device", {}).get("name")
cable["peer_interface"] = link_peers[0].get("name")
if len(link_peers) > 1: # handle breakout cable
cable["peer_interface"] = [i["name"] for i in link_peers]
connection["cable"] = cable
ret.result[device_name][port_name] = connection
return ret
|