Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_routetable_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2018 Yuwei Zhou, <[email protected]>
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = '''
12 ---
13 module: azure_rm_routetable_info
14
15 version_added: "0.1.2"
16
17 short_description: Get route table facts
18
19 description:
20 - Get facts for a specific route table or all route table in a resource group or subscription.
21
22 options:
23 name:
24 description:
25 - Limit results to a specific route table.
26 type: str
27 resource_group:
28 description:
29 - Limit results in a specific resource group.
30 type: str
31 tags:
32 description:
33 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
34 type: list
35 elements: str
36
37 extends_documentation_fragment:
38 - azure.azcollection.azure
39
40 author:
41 - Yuwei Zhou (@yuwzho)
42
43 '''
44
45 EXAMPLES = '''
46 - name: Get facts for one route table
47 azure_rm_routetable_info:
48 name: Testing
49 resource_group: myResourceGroup
50
51 - name: Get facts for all route tables
52 azure_rm_routetable_info:
53 resource_group: myResourceGroup
54
55 - name: Get facts by tags
56 azure_rm_routetable_info:
57 tags:
58 - testing
59 - foo:bar
60 '''
61 RETURN = '''
62 id:
63 description:
64 - Resource ID.
65 returned: success
66 type: str
67 sample: "/subscriptions/xxxx...xxxx/resourceGroups/v-xisuRG/providers/Microsoft.Network/routeTables/tableb57dc95236"
68 name:
69 description:
70 - Name of the resource.
71 returned: success
72 type: str
73 sample: tableb57dc95236
74 resource_group:
75 description:
76 - Resource group of the route table.
77 returned: success
78 type: str
79 sample: v-xisuRG
80 disable_bgp_route_propagation:
81 description:
82 - Whether the routes learned by BGP on that route table disabled.
83 returned: success
84 type: bool
85 sample: false
86 tags:
87 description:
88 - Tags of the route table.
89 returned: success
90 type: dict
91 sample: { 'key1':'value1', 'key2':'value2'}
92 routes:
93 description:
94 - Current routes of the route table.
95 returned: success
96 type: list
97 sample: [
98 {
99 "id": "/subscriptions/xxx...xxx/resourceGroups/v-xisuRG/providers/Microsoft.Network/routeTables/tableb57dc95236/routes/routeb57dc95540",
100 "name": "routeb57dc95540",
101 "resource_group": "v-xisuRG",
102 "route_table_name": "tableb57dc95236",
103 "address_prefix": "10.1.0.0/24",
104 "next_hop_type": "virtual_network_gateway",
105 "next_hop_ip_address": null
106 }
107 ]
108 '''
109
110 try:
111 from azure.core.exceptions import ResourceNotFoundError
112 except Exception:
113 # This is handled in azure_rm_common
114 pass
115
116 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase, azure_id_to_dict
117 from ansible.module_utils.common.dict_transformations import _camel_to_snake
118
119
120 def route_to_dict(route):
121 id_dict = azure_id_to_dict(route.id)
122 return dict(
123 id=route.id,
124 name=route.name,
125 resource_group=id_dict.get('resourceGroups'),
126 route_table_name=id_dict.get('routeTables'),
127 address_prefix=route.address_prefix,
128 next_hop_type=_camel_to_snake(route.next_hop_type),
129 next_hop_ip_address=route.next_hop_ip_address
130 )
131
132
133 def instance_to_dict(table):
134 return dict(
135 id=table.id,
136 name=table.name,
137 resource_group=azure_id_to_dict(table.id).get('resourceGroups'),
138 location=table.location,
139 routes=[route_to_dict(i) for i in table.routes] if table.routes else [],
140 disable_bgp_route_propagation=table.disable_bgp_route_propagation,
141 tags=table.tags
142 )
143
144
145 class AzureRMRouteTableInfo(AzureRMModuleBase):
146
147 def __init__(self):
148
149 self.module_arg_spec = dict(
150 name=dict(type='str'),
151 resource_group=dict(type='str'),
152 tags=dict(type='list', elements='str')
153 )
154
155 self.results = dict(
156 changed=False,
157 route_tables=[]
158 )
159
160 self.name = None
161 self.resource_group = None
162 self.tags = None
163
164 super(AzureRMRouteTableInfo, self).__init__(self.module_arg_spec,
165 supports_check_mode=True,
166 supports_tags=False,
167 facts_module=True)
168
169 def exec_module(self, **kwargs):
170
171 for key in self.module_arg_spec:
172 setattr(self, key, kwargs[key])
173
174 response = []
175 if self.name:
176 response = self.get_item()
177 elif self.resource_group:
178 response = self.list_items()
179 else:
180 response = self.list_all_items()
181
182 self.results['route_tables'] = [instance_to_dict(x) for x in response if self.has_tags(x.tags, self.tags)]
183 return self.results
184
185 def get_item(self):
186 self.log('Get route table for {0}-{1}'.format(self.resource_group, self.name))
187 try:
188 item = self.network_client.route_tables.get(self.resource_group, self.name)
189 return [item]
190 except ResourceNotFoundError:
191 pass
192 return []
193
194 def list_items(self):
195 self.log('List all items in resource group')
196 try:
197 return self.network_client.route_tables.list(self.resource_group)
198 except ResourceNotFoundError as exc:
199 self.fail("Failed to list items - {0}".format(str(exc)))
200 return []
201
202 def list_all_items(self):
203 self.log("List all items in subscription")
204 try:
205 return self.network_client.route_tables.list_all()
206 except ResourceNotFoundError as exc:
207 self.fail("Failed to list all items - {0}".format(str(exc)))
208 return []
209
210
211 def main():
212 AzureRMRouteTableInfo()
213
214
215 if __name__ == '__main__':
216 main()