1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253 | def get_circuits(
self,
devices: list,
cid: list = None,
instance: str = None,
dry_run: bool = False,
cache: Union[bool, str] = True,
) -> dict:
"""
Task to retrieve device's circuits data from Netbox.
:param devices: list of devices to retrieve interface for
:param instance: Netbox instance name
:param dry_run: only return query content, do not run it
:param cid: list of circuit identifiers to retrieve data for
:param cache: if `True` use data stored in cache if it is up to date
refresh it otherwise, `False` do not use cache do not update cache,
`refresh` ignore data in cache and replace it with data fetched
from Netbox, `force` use data in cache without checking if it is up
to date
:return: dictionary keyed by device names with circuits data values
"""
log.info(
f"{self.name}:get_circuits - {instance or self.default_instance} Netbox, "
f"devices {', '.join(devices)}, cid {cid}"
)
# form final result object
ret = Result(task=f"{self.name}:get_circuits", result={d: {} for d in devices})
cache = self.cache_use if cache is None else cache
cid = cid or []
circuit_fields = [
"cid",
"tags {name}",
"provider {name}",
"commit_rate",
"description",
"status",
"type {name}",
"provider_account {name}",
"tenant {name}",
"termination_a {id last_updated}",
"termination_z {id last_updated}",
"custom_fields",
"comments",
"last_updated",
]
# form initial circuits filters based on devices' sites and cid list
circuits_filters_dict = {}
device_data = self.get_devices(
devices=copy.deepcopy(devices), instance=instance, cache=cache
)
sites = list(set([i["site"]["slug"] for i in device_data.result.values()]))
if self.nb_version[0] == 4:
circuits_filters_dict = {"site": sites}
if cid:
cid_list = '["{cl}"]'.format(cl='", "'.join(cid))
circuits_filters_dict["cid"] = f"{{in_list: {cid_list}}}"
elif self.nb_version[0] == 3:
circuits_filters_dict = {"site": sites}
if cid:
cid_list = '["{cl}"]'.format(cl='", "'.join(cid))
circuits_filters_dict["cid"] = cid_list
log.info(
f"{self.name}:get_circuits - constructed circuits filters: {circuits_filters_dict}"
)
if cache == True or cache == "force":
cid_list = [] # new cid list for follow up query
# retrieve last updated data from Netbox for circuits and their terminations
last_updated = self.graphql(
obj="circuit_list",
filters=circuits_filters_dict,
fields=[
"cid",
"last_updated",
"termination_a {id last_updated}",
"termination_z {id last_updated}",
],
dry_run=dry_run,
instance=instance,
)
last_updated.raise_for_status(f"{self.name} - get circuits query failed")
# return dry run result
if dry_run:
ret.result["get_circuits_dry_run"] = last_updated.result
return ret
# retrieve circuits data from cache
self.cache.expire() # remove expired items from cache
for device in devices:
for circuit in last_updated.result:
circuit_cache_key = f"get_circuits::{device}::{circuit['cid']}"
# check if cache is up to date and use it if so
if circuit_cache_key in self.cache:
cache_ckt = self.cache[circuit_cache_key]
# use cache forcefully
if cache == "force":
ret.result[device][circuit["cid"]] = cache_ckt
# check circuit cache is up to date
if cache_ckt["last_updated"] != circuit["last_updated"]:
continue
if (
cache_ckt["termination_a"]
and circuit["termination_a"]
and cache_ckt["termination_a"]["last_updated"]
!= circuit["termination_a"]["last_updated"]
):
continue
if (
cache_ckt["termination_z"]
and circuit["termination_z"]
and cache_ckt["termination_z"]["last_updated"]
!= circuit["termination_z"]["last_updated"]
):
continue
ret.result[device][circuit["cid"]] = cache_ckt
elif circuit["cid"] not in cid_list:
cid_list.append(circuit["cid"])
# form new filters dictionary to fetch remaining circuits data
circuits_filters_dict = {}
if cid_list:
cid_list = '["{cl}"]'.format(cl='", "'.join(cid_list))
if self.nb_version[0] == 4:
circuits_filters_dict["cid"] = f"{{in_list: {cid_list}}}"
elif self.nb_version[0] == 3:
circuits_filters_dict["cid"] = cid_list
# ignore cache data, fetch circuits from netbox
elif cache == False or cache == "refresh":
pass
if circuits_filters_dict:
query_result = self.graphql(
obj="circuit_list",
filters=circuits_filters_dict,
fields=circuit_fields,
dry_run=dry_run,
instance=instance,
)
query_result.raise_for_status(f"{self.name} - get circuits query failed")
# return dry run result
if dry_run is True:
return query_result
all_circuits = query_result.result
# iterate over circuits and map them to devices
log.info(
f"{self.name}:get_circuits - mapping device endpoints for {len(all_circuits)} circuits"
)
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
results = [
executor.submit(
self._map_circuit, circuit, ret, instance, devices, cache
)
for circuit in all_circuits
]
for _ in concurrent.futures.as_completed(results):
continue
return ret
|