Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_routetable.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
14 version_added: "0.1.2"
15 short_description: Manage Azure route table resource
16 description:
17 - Create, update or delete a route table.
18 options:
19 resource_group:
20 description:
21 - Name of resource group.
22 required: true
23 type: str
24 name:
25 description:
26 - Name of the route table.
27 required: true
28 type: str
29 state:
30 description:
31 - Assert the state of the route table. Use C(present) to create or update and C(absent) to delete.
32 default: present
33 type: str
34 choices:
35 - absent
36 - present
37 disable_bgp_route_propagation:
38 description:
39 - Specified whether to disable the routes learned by BGP on that route table.
40 type: bool
41 default: False
42 location:
43 description:
44 - Region of the resource.
45 - Derived from I(resource_group) if not specified.
46 type: str
47
48 extends_documentation_fragment:
49 - azure.azcollection.azure
50 - azure.azcollection.azure_tags
51
52 author:
53 - Yuwei Zhou (@yuwzho)
54
55 '''
56
57 EXAMPLES = '''
58 - name: Create a route table
59 azure_rm_routetable:
60 resource_group: myResourceGroup
61 name: myRouteTable
62 disable_bgp_route_propagation: false
63 tags:
64 purpose: testing
65
66 - name: Delete a route table
67 azure_rm_routetable:
68 resource_group: myResourceGroup
69 name: myRouteTable
70 state: absent
71 '''
72 RETURN = '''
73 changed:
74 description:
75 - Whether the resource is changed.
76 returned: always
77 type: bool
78 sample: true
79 id:
80 description:
81 - Resource ID.
82 returned: success
83 type: str
84 sample: "/subscriptions/xxx...xxx/resourceGroups/v-xisuRG/providers/Microsoft.Network/routeTables/tableb57dc95642/routes/routeb57dc95986"
85 '''
86
87 try:
88 from azure.core.exceptions import ResourceNotFoundError
89 except ImportError:
90 # This is handled in azure_rm_common
91 pass
92
93 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase, normalize_location_name
94
95
96 class AzureRMRouteTable(AzureRMModuleBase):
97
98 def __init__(self):
99
100 self.module_arg_spec = dict(
101 resource_group=dict(type='str', required=True),
102 name=dict(type='str', required=True),
103 state=dict(type='str', default='present', choices=['present', 'absent']),
104 location=dict(type='str'),
105 disable_bgp_route_propagation=dict(type='bool', default=False)
106 )
107
108 self.resource_group = None
109 self.name = None
110 self.state = None
111 self.location = None
112 self.tags = None
113 self.disable_bgp_route_propagation = None
114
115 self.results = dict(
116 changed=False
117 )
118
119 super(AzureRMRouteTable, self).__init__(self.module_arg_spec,
120 supports_check_mode=True)
121
122 def exec_module(self, **kwargs):
123 for key in list(self.module_arg_spec.keys()) + ['tags']:
124 setattr(self, key, kwargs[key])
125
126 resource_group = self.get_resource_group(self.resource_group)
127 if not self.location:
128 # Set default location
129 self.location = resource_group.location
130 self.location = normalize_location_name(self.location)
131
132 result = dict()
133 changed = False
134
135 result = self.get_table()
136 if self.state == 'absent' and result:
137 changed = True
138 if not self.check_mode:
139 self.delete_table()
140 elif self.state == 'present':
141 routes = []
142 subnets = None
143 if not result:
144 changed = True # create new route table
145 else: # check update
146 routes = result.routes
147 subnets = result.subnets
148 update_tags, self.tags = self.update_tags(result.tags)
149 if update_tags:
150 changed = True
151 if self.disable_bgp_route_propagation != result.disable_bgp_route_propagation:
152 changed = True
153
154 if changed:
155 result = self.network_models.RouteTable(location=self.location,
156 tags=self.tags,
157 routes=routes,
158 subnets=subnets,
159 disable_bgp_route_propagation=self.disable_bgp_route_propagation)
160 if not self.check_mode:
161 result = self.create_or_update_table(result)
162
163 self.results['id'] = result.id if result else None
164 self.results['changed'] = changed
165 return self.results
166
167 def create_or_update_table(self, param):
168 try:
169 poller = self.network_client.route_tables.begin_create_or_update(self.resource_group, self.name, param)
170 return self.get_poller_result(poller)
171 except Exception as exc:
172 self.fail("Error creating or updating route table {0} - {1}".format(self.name, str(exc)))
173
174 def delete_table(self):
175 try:
176 poller = self.network_client.route_tables.begin_delete(self.resource_group, self.name)
177 result = self.get_poller_result(poller)
178 return result
179 except Exception as exc:
180 self.fail("Error deleting virtual network {0} - {1}".format(self.name, str(exc)))
181
182 def get_table(self):
183 try:
184 return self.network_client.route_tables.get(self.resource_group, self.name)
185 except ResourceNotFoundError as cloud_err:
186 # Return None iff the resource is not found
187 if cloud_err.status_code == 404:
188 self.log('{0}'.format(str(cloud_err)))
189 return None
190 self.fail('Error: failed to get resource {0} - {1}'.format(self.name, str(cloud_err)))
191 except Exception as exc:
192 self.fail('Error: failed to get resource {0} - {1}'.format(self.name, str(exc)))
193
194
195 def main():
196 AzureRMRouteTable()
197
198
199 if __name__ == '__main__':
200 main()