def get_job_filter(data=None): """Helper function to return a the filterable list of OS's based on platform.slug and a specific custom value.""" if not data: data = {} query = {} for field in FIELDS: if data.get(field): query[f"{field}_id"] = data[field].values_list("pk", flat=True) # Handle case where object is from single device run all. if data.get("device") and isinstance(data["device"], Device): query.update({"id": [str(data["device"].pk)]}) elif data.get("device"): query.update({"id": data["device"].values_list("pk", flat=True)}) base_qs = models.GoldenConfigSetting.objects.first().get_queryset() if base_qs.count() == 0: raise NornirNautobotException( "The base queryset didn't find any devices. Please check the Golden Config Setting scope." ) devices_filtered = DeviceFilterSet(data=query, queryset=base_qs) if devices_filtered.qs.count() == 0: raise NornirNautobotException( "The provided job parameters didn't match any devices detected by the Golden Config scope. Please check the scope defined within Golden Config Settings or select the correct job parameters to correctly match devices." ) devices_no_platform = devices_filtered.qs.filter(platform__isnull=True) if devices_no_platform.count() > 0: raise NornirNautobotException( f"The following device(s) {', '.join([device.name for device in devices_no_platform])} have no platform defined. Platform is required." ) return devices_filtered.qs
def get_job_filter(data=None): """Helper function to return a the filterable list of OS's based on platform.slug and a specific custom value.""" if not data: data = {} query = {} for field in FIELDS: if data.get(field): query[f"{field}_id"] = data[field].values_list("pk", flat=True) # Handle case where object is from single device run all. if data.get("device") and isinstance(data["device"], Device): query.update({"id": [str(data["device"].pk)]}) elif data.get("device"): query.update({"id": data["device"].values_list("pk", flat=True)}) base_qs = models.GoldenConfigSetting.objects.first().get_queryset() return DeviceFilterSet(data=query, queryset=base_qs).qs
def get_allowed_os(data=None): """Helper function to return a the filterable list of OS's based on platform.slug and a specific custom value.""" if not data: data = {} query = {} for field in FIELDS: if data.get(field): query[f"{field}_id"] = data[field].values_list("pk", flat=True) # Handle case where object is from single device run all. if data.get("device") and isinstance(data["device"], Device): query.update({"id": [str(data["device"].pk)]}) elif data.get("device"): query.update({"id": data["device"].values_list("pk", flat=True)}) if "all" in ALLOWED_OS: _allowed_os = Platform.objects.values_list("slug", flat=True) else: _allowed_os = ALLOWED_OS return DeviceFilterSet( data=query, queryset=Device.objects.filter(platform__slug__in=_allowed_os)).qs
def test_device_mac_address_iregex_negation(self): params = {"mac_address__nire": ["^aa:"]} self.assertEqual( DeviceFilterSet(params, self.device_queryset).qs.count(), 2)
def test_device_mac_address_icontains_negation(self): params = {"mac_address__nic": ["aa:", "bb"]} self.assertEqual( DeviceFilterSet(params, self.device_queryset).qs.count(), 1)
def test_device_mac_address_regex(self): params = {"mac_address__re": ["^AA:"]} self.assertEqual( DeviceFilterSet(params, self.device_queryset).qs.count(), 1)
def test_device_mac_address_startswith(self): params = {"mac_address__isw": ["aa:"]} self.assertEqual( DeviceFilterSet(params, self.device_queryset).qs.count(), 1)
def test_device_mac_address_endswith_negation(self): params = {"mac_address__niew": [":02"]} self.assertEqual( DeviceFilterSet(params, self.device_queryset).qs.count(), 2)
def test_device_mac_address_negation(self): params = {"mac_address__n": ["00-00-00-00-00-01", "aa-00-00-00-00-01"]} self.assertEqual( DeviceFilterSet(params, self.device_queryset).qs.count(), 2)
def test_device_name_icontains_negation(self): params = {"name__nic": [" "]} self.assertEqual( DeviceFilterSet(params, self.device_queryset).qs.count(), 0)
def test_device_name_endswith_negation(self): params = {"name__niew": [" 1"]} self.assertEqual( DeviceFilterSet(params, self.device_queryset).qs.count(), 2)
def test_device_name_startswith(self): params = {"name__isw": ["Device"]} self.assertEqual( DeviceFilterSet(params, self.device_queryset).qs.count(), 3)
def test_device_name_eq(self): params = {"name": ["Device 1"]} self.assertEqual( DeviceFilterSet(params, self.device_queryset).qs.count(), 1)