コード例 #1
0
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
コード例 #2
0
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
コード例 #3
0
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
コード例 #4
0
 def test_device_mac_address_iregex_negation(self):
     params = {"mac_address__nire": ["^aa:"]}
     self.assertEqual(
         DeviceFilterSet(params, self.device_queryset).qs.count(), 2)
コード例 #5
0
 def test_device_mac_address_icontains_negation(self):
     params = {"mac_address__nic": ["aa:", "bb"]}
     self.assertEqual(
         DeviceFilterSet(params, self.device_queryset).qs.count(), 1)
コード例 #6
0
 def test_device_mac_address_regex(self):
     params = {"mac_address__re": ["^AA:"]}
     self.assertEqual(
         DeviceFilterSet(params, self.device_queryset).qs.count(), 1)
コード例 #7
0
 def test_device_mac_address_startswith(self):
     params = {"mac_address__isw": ["aa:"]}
     self.assertEqual(
         DeviceFilterSet(params, self.device_queryset).qs.count(), 1)
コード例 #8
0
 def test_device_mac_address_endswith_negation(self):
     params = {"mac_address__niew": [":02"]}
     self.assertEqual(
         DeviceFilterSet(params, self.device_queryset).qs.count(), 2)
コード例 #9
0
 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)
コード例 #10
0
 def test_device_name_icontains_negation(self):
     params = {"name__nic": [" "]}
     self.assertEqual(
         DeviceFilterSet(params, self.device_queryset).qs.count(), 0)
コード例 #11
0
 def test_device_name_endswith_negation(self):
     params = {"name__niew": [" 1"]}
     self.assertEqual(
         DeviceFilterSet(params, self.device_queryset).qs.count(), 2)
コード例 #12
0
 def test_device_name_startswith(self):
     params = {"name__isw": ["Device"]}
     self.assertEqual(
         DeviceFilterSet(params, self.device_queryset).qs.count(), 3)
コード例 #13
0
 def test_device_name_eq(self):
     params = {"name": ["Device 1"]}
     self.assertEqual(
         DeviceFilterSet(params, self.device_queryset).qs.count(), 1)