Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2020 GuopengLin, (@t-glin)
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_route_info
14 version_added: '2.0.0'
15 short_description: Get Route info
16 description:
17 - Get info of Route.
18 options:
19 resource_group:
20 description:
21 - The name of the resource group.
22 required: true
23 type: str
24 route_table_name:
25 description:
26 - The name of the route table.
27 required: true
28 type: str
29 name:
30 description:
31 - The name of the route.
32 type: str
33 tags:
34 description:
35 - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.
36 type: list
37 elements: str
38 extends_documentation_fragment:
39 - azure.azcollection.azure
40 author:
41 - GuopengLin (@t-glin)
42 - Fred-Sun (@Fred-Sun)
43 - Haiyuan Zhang (@haiyuazhang)
44
45 '''
46
47 EXAMPLES = '''
48 - name: Get route
49 azure_rm_route_info:
50 resource_group: myResourceGroup
51 name: routename
52 route_table_name: routetabename
53
54
55 - name: List routes
56 azure_rm_route_info:
57 resource_group: myResourceGroup
58 route_table_name: routetabename
59 '''
60
61 RETURN = '''
62 routes:
63 description:
64 - A list of dict results where the key is the name of the Route and the values are the facts for that Route.
65 returned: always
66 type: complex
67 contains:
68 id:
69 description:
70 - Resource ID.
71 returned: always
72 type: str
73 sample: /subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/routeTables/table01/routes/route01
74 name:
75 description:
76 - The name of the resource that is unique within a resource group.
77 - This name can be used to access the resource.
78 returned: always
79 type: str
80 sample: route01
81 etag:
82 description:
83 - A unique read-only string that changes whenever the resource is updated.
84 returned: always
85 type: str
86 sample: fda240c5-6f42-4eb0-8f05-b1599dc722fe
87 address_prefix:
88 description:
89 - The destination CIDR to which the route applies.
90 returned: always
91 type: str
92 sample: 10.0.0.0/25
93 next_hop_type:
94 description:
95 - The type of Azure hop the packet should be sent to.
96 returned: always
97 type: str
98 sample: VirtualAppliance
99 next_hop_ip_address:
100 description:
101 - The IP address packets should be forwarded to.
102 - Next hop values are only allowed in routes where the next hop type is VirtualAppliance.
103 returned: always
104 type: str
105 sample: 10.0.0.0
106 provisioning_state:
107 description:
108 - The provisioning state of the route resource.
109 returned: always
110 type: str
111 sample: Succeeded
112
113 '''
114
115 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBase
116 try:
117 from azure.core.exceptions import ResourceNotFoundError
118 except ImportError:
119 # This is handled in azure_rm_common
120 pass
121
122
123 class AzureRMRouteInfo(AzureRMModuleBase):
124 def __init__(self):
125 self.module_arg_spec = dict(
126 resource_group=dict(
127 type='str',
128 required=True
129 ),
130 route_table_name=dict(
131 type='str',
132 required=True
133 ),
134 name=dict(
135 type='str'
136 ),
137 tags=dict(
138 type='list',
139 elements='str'
140 )
141 )
142
143 self.resource_group = None
144 self.route_table_name = None
145 self.name = None
146
147 self.results = dict(changed=False)
148 self.mgmt_client = None
149 self.state = None
150 self.url = None
151 self.status_code = [200]
152 self.tags = None
153
154 self.mgmt_client = None
155 super(AzureRMRouteInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False, facts_module=True)
156
157 def exec_module(self, **kwargs):
158
159 for key in self.module_arg_spec:
160 setattr(self, key, kwargs[key])
161
162 if (self.resource_group is not None and self.route_table_name is not None and self.name is not None):
163 self.results['routes'] = self.format_item(self.get())
164 elif (self.resource_group is not None and
165 self.route_table_name is not None):
166 self.results['routes'] = self.format_item(self.list())
167 return self.results
168
169 def get(self):
170 response = None
171
172 try:
173 response = self.network_client.routes.get(resource_group_name=self.resource_group,
174 route_table_name=self.route_table_name,
175 route_name=self.name)
176 except ResourceNotFoundError as e:
177 self.log('Could not get info for @(Model.ModuleOperationNameUpper).')
178
179 return response
180
181 def list(self):
182 response = None
183
184 try:
185 response = self.network_client.routes.list(resource_group_name=self.resource_group,
186 route_table_name=self.route_table_name)
187 except ResourceNotFoundError as e:
188 self.log('Could not get info for @(Model.ModuleOperationNameUpper).')
189
190 return response
191
192 def format_item(self, item):
193 if hasattr(item, 'as_dict'):
194 return [item.as_dict()]
195 else:
196 result = []
197 items = list(item)
198 for tmp in items:
199 result.append(tmp.as_dict())
200 return result
201
202
203 def main():
204 AzureRMRouteInfo()
205
206
207 if __name__ == '__main__':
208 main()