From 62cd2d9c18a008bb7540e32bda4417dd9af06530 Mon Sep 17 00:00:00 2001
From: whiter <rob.white@truelocal.com.au>
Date: Tue, 13 Oct 2015 17:54:16 +1100
Subject: [PATCH] Use filters attribute rather than eni id so facts can be
 filtered on much more.  Matches the new ec2_vpc_route_table_facts and
 ec2_vpc_subnet_facts modules

---
 cloud/amazon/ec2_eni_facts.py | 29 +++++++++++------------------
 1 file changed, 11 insertions(+), 18 deletions(-)

diff --git a/cloud/amazon/ec2_eni_facts.py b/cloud/amazon/ec2_eni_facts.py
index 2014c3e8eb5..c25535f51eb 100644
--- a/cloud/amazon/ec2_eni_facts.py
+++ b/cloud/amazon/ec2_eni_facts.py
@@ -22,11 +22,12 @@ description:
 version_added: "2.0"
 author: "Rob White (@wimnat)"
 options:
-  eni_id:
+  filters:
     description:
-      - The ID of the ENI. Pass this option to gather facts about a particular ENI, otherwise, all ENIs are returned.
+      - A dict of filters to apply. Each dict item consists of a filter key and a filter value. See U(http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeNetworkInterfaces.html) for possible filters.
     required: false
     default: null
+
 extends_documentation_fragment:
     - aws
     - ec2
@@ -40,12 +41,11 @@ EXAMPLES = '''
 
 # Gather facts about a particular ENI
 - ec2_eni_facts:
-    eni_id: eni-xxxxxxx
+    filters:
+      network-interface-id: eni-xxxxxxx
 
 '''
 
-import xml.etree.ElementTree as ET
-
 try:
     import boto.ec2
     from boto.exception import BotoServerError
@@ -53,14 +53,6 @@ try:
 except ImportError:
     HAS_BOTO = False
 
-
-def get_error_message(xml_string):
-
-    root = ET.fromstring(xml_string)
-    for message in root.findall('.//Message'):
-        return message.text
-
-
 def get_eni_info(interface):
 
     interface_info = {'id': interface.id,
@@ -89,13 +81,13 @@ def get_eni_info(interface):
 
 def list_eni(connection, module):
 
-    eni_id = module.params.get("eni_id")
+    filters = module.params.get("filters")
     interface_dict_array = []
 
     try:
-        all_eni = connection.get_all_network_interfaces(eni_id)
+        all_eni = connection.get_all_network_interfaces(filters=filters)
     except BotoServerError as e:
-        module.fail_json(msg=get_error_message(e.args[2]))
+        module.fail_json(msg=e.message)
 
     for interface in all_eni:
         interface_dict_array.append(get_eni_info(interface))
@@ -107,7 +99,7 @@ def main():
     argument_spec = ec2_argument_spec()
     argument_spec.update(
         dict(
-            eni_id = dict(default=None)
+            filters = dict(default=None, type='dict')
         )
     )
 
@@ -131,4 +123,5 @@ def main():
 from ansible.module_utils.basic import *
 from ansible.module_utils.ec2 import *
 
-main()
+if __name__ == '__main__':
+    main()