Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upstreaming patches: filter_by_networks #212

Closed
darix opened this issue Feb 19, 2020 · 1 comment
Closed

Upstreaming patches: filter_by_networks #212

darix opened this issue Feb 19, 2020 · 1 comment

Comments

@darix
Copy link
Member

darix commented Feb 19, 2020

Index: salt-2019.2.2-suse/salt/utils/network.py
===================================================================
--- salt-2019.2.2-suse.orig/salt/utils/network.py
+++ salt-2019.2.2-suse/salt/utils/network.py
@@ -2032,3 +2032,42 @@ def is_fqdn(hostname):
 
     compliant = re.compile(r"(?!-)[A-Z\d\-\_]{1,63}(?<!-)$", re.IGNORECASE)
     return "." in hostname and len(hostname) < 0xff and all(compliant.match(x) for x in hostname.rstrip(".").split("."))
+
+
+def _filter_by_networks(values, networks):
+    filtered_addresses = []
+
+    for value in values:
+        address = ipaddress.ip_address(value)
+        for network in networks:
+            if network.__contains__(address):
+                filtered_addresses.append(value)
+
+    return filtered_addresses
+
+
+@jinja_filter('filter_by_networks')
+def filter_by_networks(values, filters=None):
+    '''
+    Returns a the list of IPs filtered by the optional network list.
+    If no filter is specified all all items are returned.
+    {% set networks = ['192.168.0.0/24', 'fe80::/64'] %}
+    {{ grains['ip_interfaces'] | filter_by_networks(networks) }}
+    {{ grains['ipv6'] | filter_by_networks(networks) }}
+    {{ grains['ipv4'] | filter_by_networks(networks) }}
+    '''
+    if filters:
+        networks = [ipaddress.ip_network(network) for network in filters]
+        new_values = None
+        if isinstance(values, dict):
+            new_values = {}
+            for interface, addresses in values.items():
+                new_values[interface] = _filter_by_networks(addresses,
+                                                            networks)
+        elif isinstance(values, list):
+            new_values = _filter_by_networks(values, networks)
+        else:
+            raise ValueError('Do not know how to filter a {}'.format(type(values)))
+        return new_values
+    else:
+        return values
darix added a commit to darix/salt that referenced this issue Feb 20, 2020
If no filter is specified all all items are returned.

```
{% set networks = ['192.168.0.0/24', 'fe80::/64'] %}
{{ grains['ip_interfaces'] | filter_by_networks(networks) }}
{{ grains['ipv6'] | filter_by_networks(networks) }}
{{ grains['ipv4'] | filter_by_networks(networks) }}
```

Fixes openSUSE#212
darix added a commit to darix/salt that referenced this issue Feb 24, 2020
If no filter is specified all all items are returned.

```
{% set networks = ['192.168.0.0/24', 'fe80::/64'] %}
{{ grains['ip_interfaces'] | filter_by_networks(networks) }}
{{ grains['ipv6'] | filter_by_networks(networks) }}
{{ grains['ipv4'] | filter_by_networks(networks) }}
```

Fixes openSUSE#212
agraul pushed a commit that referenced this issue Mar 17, 2020
If no filter is specified all all items are returned.

```
{% set networks = ['192.168.0.0/24', 'fe80::/64'] %}
{{ grains['ip_interfaces'] | filter_by_networks(networks) }}
{{ grains['ipv6'] | filter_by_networks(networks) }}
{{ grains['ipv4'] | filter_by_networks(networks) }}
```

Fixes #212
agraul added a commit to darix/salt that referenced this issue Apr 21, 2020
IPs are filtered out if they don't belong to any of the given networks.
If `None` is passed as the network, all IPs are returned. An empty list
rejects all IPs.

Example:

	{% set networks = ['192.168.0.0/24', 'fe80::/64'] %}
	{{ grains['ip_interfaces'] | filter_by_networks(networks) }}
	{{ grains['ipv6'] | filter_by_networks(networks) }}
	{{ grains['ipv4'] | filter_by_networks(networks) }}

Fixes openSUSE#212
Co-authored-by: Alexander Graul <agraul@suse.com>
agraul added a commit to agraul/salt-1 that referenced this issue Apr 27, 2020
IPs are filtered out if they don't belong to any of the given networks.
If `None` is passed as the network, all IPs are returned. An empty list
rejects all IPs.

Example:

	{% set networks = ['192.168.0.0/24', 'fe80::/64'] %}
	{{ grains['ip_interfaces'] | filter_by_networks(networks) }}
	{{ grains['ipv6'] | filter_by_networks(networks) }}
	{{ grains['ipv4'] | filter_by_networks(networks) }}

Fixes openSUSE#212
Co-authored-by: Alexander Graul <agraul@suse.com>
agraul added a commit to agraul/salt-1 that referenced this issue Apr 27, 2020
IPs are filtered out if they don't belong to any of the given networks.
If `None` is passed as the network, all IPs are returned. An empty list
rejects all IPs.

Example:

	{% set networks = ['192.168.0.0/24', 'fe80::/64'] %}
	{{ grains['ip_interfaces'] | filter_by_networks(networks) }}
	{{ grains['ipv6'] | filter_by_networks(networks) }}
	{{ grains['ipv4'] | filter_by_networks(networks) }}

Fixes openSUSE#212
Co-authored-by: Alexander Graul <agraul@suse.com>
agraul added a commit to agraul/salt-1 that referenced this issue Apr 27, 2020
IPs are filtered out if they don't belong to any of the given networks.
If `None` is passed as the network, all IPs are returned. An empty list
rejects all IPs.

Example:

	{% set networks = ['192.168.0.0/24', 'fe80::/64'] %}
	{{ grains['ip_interfaces'] | filter_by_networks(networks) }}
	{{ grains['ipv6'] | filter_by_networks(networks) }}
	{{ grains['ipv4'] | filter_by_networks(networks) }}

Fixes openSUSE#212
Co-authored-by: Alexander Graul <agraul@suse.com>
meaksh pushed a commit that referenced this issue May 7, 2020
IPs are filtered out if they don't belong to any of the given networks.
If `None` is passed as the network, all IPs are returned. An empty list
rejects all IPs.

Example:

	{% set networks = ['192.168.0.0/24', 'fe80::/64'] %}
	{{ grains['ip_interfaces'] | filter_by_networks(networks) }}
	{{ grains['ipv6'] | filter_by_networks(networks) }}
	{{ grains['ipv4'] | filter_by_networks(networks) }}

Fixes #212
Co-authored-by: Alexander Graul <agraul@suse.com>

Add unit tests for filter_by_networks
@brejoc
Copy link
Member

brejoc commented Aug 11, 2020

I think this issue can be closed. Please re-open if you think we still need it.

@brejoc brejoc closed this as completed Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants