1#!/usr/bin/python
2# Copyright: Ansible Project
3# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5from __future__ import absolute_import, division, print_function
6__metaclass__ = type
7
8
9ANSIBLE_METADATA = {'metadata_version': '1.1',
10                    'status': ['preview'],
11                    'supported_by': 'community'}
12
13
14DOCUMENTATION = '''
15---
16module: ec2_vpc_route_table_info
17short_description: Gather information about ec2 VPC route tables in AWS
18description:
19    - Gather information about ec2 VPC route tables in AWS
20    - This module was called C(ec2_vpc_route_table_facts) before Ansible 2.9. The usage did not change.
21version_added: "2.0"
22author: "Rob White (@wimnat)"
23options:
24  filters:
25    description:
26      - A dict of filters to apply. Each dict item consists of a filter key and a filter value.
27        See U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeRouteTables.html) for possible filters.
28extends_documentation_fragment:
29    - aws
30    - ec2
31'''
32
33EXAMPLES = '''
34# Note: These examples do not set authentication details, see the AWS Guide for details.
35
36# Gather information about all VPC route tables
37- ec2_vpc_route_table_info:
38
39# Gather information about a particular VPC route table using route table ID
40- ec2_vpc_route_table_info:
41    filters:
42      route-table-id: rtb-00112233
43
44# Gather information about any VPC route table with a tag key Name and value Example
45- ec2_vpc_route_table_info:
46    filters:
47      "tag:Name": Example
48
49# Gather information about any VPC route table within VPC with ID vpc-abcdef00
50- ec2_vpc_route_table_info:
51    filters:
52      vpc-id: vpc-abcdef00
53
54'''
55
56try:
57    import boto.vpc
58    from boto.exception import BotoServerError
59    HAS_BOTO = True
60except ImportError:
61    HAS_BOTO = False
62
63from ansible.module_utils.basic import AnsibleModule
64from ansible.module_utils.ec2 import AnsibleAWSError, connect_to_aws, ec2_argument_spec, get_aws_connection_info
65
66
67def get_route_table_info(route_table):
68
69    # Add any routes to array
70    routes = []
71    associations = []
72    for route in route_table.routes:
73        routes.append(route.__dict__)
74    for association in route_table.associations:
75        associations.append(association.__dict__)
76
77    route_table_info = {'id': route_table.id,
78                        'routes': routes,
79                        'associations': associations,
80                        'tags': route_table.tags,
81                        'vpc_id': route_table.vpc_id
82                        }
83
84    return route_table_info
85
86
87def list_ec2_vpc_route_tables(connection, module):
88
89    filters = module.params.get("filters")
90    route_table_dict_array = []
91
92    try:
93        all_route_tables = connection.get_all_route_tables(filters=filters)
94    except BotoServerError as e:
95        module.fail_json(msg=e.message)
96
97    for route_table in all_route_tables:
98        route_table_dict_array.append(get_route_table_info(route_table))
99
100    module.exit_json(route_tables=route_table_dict_array)
101
102
103def main():
104    argument_spec = ec2_argument_spec()
105    argument_spec.update(
106        dict(
107            filters=dict(default=None, type='dict')
108        )
109    )
110
111    module = AnsibleModule(argument_spec=argument_spec,
112                           supports_check_mode=True)
113    if module._name == 'ec2_vpc_route_table_facts':
114        module.deprecate("The 'ec2_vpc_route_table_facts' module has been renamed to 'ec2_vpc_route_table_info'", version='2.13')
115
116    if not HAS_BOTO:
117        module.fail_json(msg='boto required for this module')
118
119    region, ec2_url, aws_connect_params = get_aws_connection_info(module)
120
121    if region:
122        try:
123            connection = connect_to_aws(boto.vpc, region, **aws_connect_params)
124        except (boto.exception.NoAuthHandlerFound, AnsibleAWSError) as e:
125            module.fail_json(msg=str(e))
126    else:
127        module.fail_json(msg="region must be specified")
128
129    list_ec2_vpc_route_tables(connection, module)
130
131
132if __name__ == '__main__':
133    main()
134