diff options
| author | Jason Koelker <jason@koelker.net> | 2011-09-14 14:02:54 -0500 |
|---|---|---|
| committer | Jason Koelker <jason@koelker.net> | 2011-09-14 14:02:54 -0500 |
| commit | 8f9453aeb8882509d825c9715fde4e6827b0bbf7 (patch) | |
| tree | b4fff3511558324c6c9d1bee22ac4574caf53885 | |
| parent | ee11a4661bb855c354ae567fd18b8b3274d05df8 (diff) | |
move ip filtering over to the network side
| -rw-r--r-- | nova/compute/api.py | 7 | ||||
| -rw-r--r-- | nova/network/api.py | 4 | ||||
| -rw-r--r-- | nova/network/manager.py | 57 |
3 files changed, 42 insertions, 26 deletions
diff --git a/nova/compute/api.py b/nova/compute/api.py index 3b217a85e..f9f87fd28 100644 --- a/nova/compute/api.py +++ b/nova/compute/api.py @@ -934,10 +934,13 @@ class API(base.Base): if 'ip6' in filters or 'ip' in filters: res = self.network_api.get_instance_ids_by_ip_filter(context, filters) + # NOTE(jkoelker) It is possible that we will get the same + # instance id twice (one for ipv4 and ipv4) + ids = set([r['instance_id'] for r in res]) + # NOTE(jkoelker) When this flips to using UUIDS the name # needs to be updated accordingingly - ip_instances = [self.db.instance_get(r['instance_id']) \ - for r in res] + ip_instances = [self.db.instance_get(id) for id in ids] return self.db.instance_get_all_by_filters(context, filters, ip_instances) diff --git a/nova/network/api.py b/nova/network/api.py index 014583a63..abc4e5a41 100644 --- a/nova/network/api.py +++ b/nova/network/api.py @@ -212,11 +212,11 @@ class API(base.Base): {'method': 'validate_networks', 'args': args}) - def get_instance_ids_by_ip_filter(self, context, ip_filter): + def get_instance_ids_by_ip_filter(self, context, filters): """Returns a list of dicts in the form of {'instance_id': id, 'ip': ip} that matched the ip_filter """ - args = {'ip_filter': ip_filter} + args = {'filters': filters} return rpc.call(context, FLAGS.network_topic, {'method': 'get_instance_ids_by_ip_filter', 'args': args}) diff --git a/nova/network/manager.py b/nova/network/manager.py index 3ba6766c0..9efe9153d 100644 --- a/nova/network/manager.py +++ b/nova/network/manager.py @@ -48,6 +48,7 @@ import datetime import itertools import math import netaddr +import re import socket from eventlet import greenpool @@ -401,28 +402,40 @@ class NetworkManager(manager.SchedulerDependentManager): instance_id) return vifs - def get_instance_ids_by_ip_filter(self, context, ip_filter): -# def _regexp_filter_by_ipv6(instance, filter_re): -# for interface in instance['virtual_interfaces']: -# fixed_ipv6 = interface.get('fixed_ipv6') -# if fixed_ipv6 and filter_re.match(fixed_ipv6): -# return True -# return False - -# def _regexp_filter_by_ip(instance, filter_re): -# for interface in instance['virtual_interfaces']: -# for fixed_ip in interface['fixed_ips']: -# if not fixed_ip or not fixed_ip['address']: -# continue -# if filter_re.match(fixed_ip['address']): -# return True -# for floating_ip in fixed_ip.get('floating_ips', []): -# if not floating_ip or not floating_ip['address']: -# continue -# if filter_re.match(floating_ip['address']): -# return True -# return False - return [] + def get_instance_ids_by_ip_filter(self, context, filters): + ip_filter = re.compile(str(filters.get('ip'))) + ipv6_filter = re.compile(str(filters.get('ip6'))) + + # NOTE(jkoelker) Should probably figur out a better way to do + # this. But for now it "works", this could suck on + # large installs. + + vifs = self.db.virtual_interface_get_all(context) + results = [] + + for vif in vifs: + fixed_ipv6 = vif.get('fixed_ipv6') + if fixed_ipv6 and ipv6_filter.match(fixed_ipv6): + # NOTE(jkoelker) Will need to update for the UUID flip + results.append({'instance_id': vif['instance_id'], + 'ip': fixed_ipv6}) + + for fixed_ip in vif['fixed_ips']: + if not fixed_ip or not fixed_ip['address']: + continue + if ip_filter.match(fixed_ip['address']): + results.append({'instance_id': vif['instance_id'], + 'ip': fixed_ip['address']}) + break + for floating_ip in fixed_ip.get('floating_ips', []): + if not floating_ip or not floating_ip['address']: + continue + if ip_filter.match(floating_ip['address']): + results.append({'instance_id': vif['instance_id'], + 'ip': fixed_ip['address']}) + break + return results + def _get_networks_for_instance(self, context, instance_id, project_id, requested_networks=None): |
