| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_route |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure route resource |
| 16 |
description: |
| 17 |
- Create, update or delete a route. |
| 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. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
state: |
| 30 |
description: |
| 31 |
- Assert the state of the route. 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 |
address_prefix: |
| 38 |
description: |
| 39 |
- The destination CIDR to which the route applies. |
| 40 |
type: str |
| 41 |
next_hop_type: |
| 42 |
description: |
| 43 |
- The type of Azure hop the packet should be sent to. |
| 44 |
type: str |
| 45 |
choices: |
| 46 |
- virtual_network_gateway |
| 47 |
- vnet_local |
| 48 |
- internet |
| 49 |
- virtual_appliance |
| 50 |
- none |
| 51 |
default: 'none' |
| 52 |
next_hop_ip_address: |
| 53 |
description: |
| 54 |
- The IP address packets should be forwarded to. |
| 55 |
- Next hop values are only allowed in routes where the next hop type is VirtualAppliance. |
| 56 |
type: str |
| 57 |
route_table_name: |
| 58 |
description: |
| 59 |
- The name of the route table. |
| 60 |
required: true |
| 61 |
type: str |
| 62 |
|
| 63 |
|
| 64 |
extends_documentation_fragment: |
| 65 |
- azure.azcollection.azure |
| 66 |
- azure.azcollection.azure_tags |
| 67 |
|
| 68 |
author: |
| 69 |
- Yuwei Zhou (@yuwzho) |
| 70 |
|
| 71 |
''' |
| 72 |
|
| 73 |
EXAMPLES = ''' |
| 74 |
- name: Create a route |
| 75 |
azure_rm_route: |
| 76 |
resource_group: myResourceGroup |
| 77 |
name: myRoute |
| 78 |
address_prefix: 10.1.0.0/16 |
| 79 |
next_hop_type: virtual_network_gateway |
| 80 |
route_table_name: table |
| 81 |
|
| 82 |
- name: Delete a route |
| 83 |
azure_rm_route: |
| 84 |
resource_group: myResourceGroup |
| 85 |
name: myRoute |
| 86 |
route_table_name: table |
| 87 |
state: absent |
| 88 |
''' |
| 89 |
RETURN = ''' |
| 90 |
id: |
| 91 |
description: |
| 92 |
- Current state of the route. |
| 93 |
returned: success |
| 94 |
type: str |
| 95 |
sample: "/subscriptions/xxxx...xxxx/resourceGroups/v-xisuRG/providers/Microsoft.Network/routeTables/tableb57/routes/routeb57" |
| 96 |
''' |
| 97 |
|
| 98 |
try: |
| 99 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 100 |
from azure.core.exceptions import ResourceNotFoundError |
| 101 |
except ImportError: |
| 102 |
|
| 103 |
pass |
| 104 |
|
| 105 |
from ansible.module_utils.common.dict_transformations import _snake_to_camel |
| 106 |
|
| 107 |
|
| 108 |
class AzureRMRoute(AzureRMModuleBase): |
| 109 |
|
| 110 |
def __init__(self): |
| 111 |
|
| 112 |
self.module_arg_spec = dict( |
| 113 |
resource_group=dict(type='str', required=True), |
| 114 |
name=dict(type='str', required=True), |
| 115 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 116 |
address_prefix=dict(type='str'), |
| 117 |
next_hop_type=dict(type='str', |
| 118 |
choices=['virtual_network_gateway', |
| 119 |
'vnet_local', |
| 120 |
'internet', |
| 121 |
'virtual_appliance', |
| 122 |
'none'], |
| 123 |
default='none'), |
| 124 |
next_hop_ip_address=dict(type='str'), |
| 125 |
route_table_name=dict(type='str', required=True) |
| 126 |
) |
| 127 |
|
| 128 |
required_if = [ |
| 129 |
('state', 'present', ['next_hop_type']) |
| 130 |
] |
| 131 |
|
| 132 |
self.resource_group = None |
| 133 |
self.name = None |
| 134 |
self.state = None |
| 135 |
self.address_prefix = None |
| 136 |
self.next_hop_type = None |
| 137 |
self.next_hop_ip_address = None |
| 138 |
self.route_table_name = None |
| 139 |
|
| 140 |
self.results = dict( |
| 141 |
changed=False, |
| 142 |
id=None |
| 143 |
) |
| 144 |
|
| 145 |
super(AzureRMRoute, self).__init__(self.module_arg_spec, |
| 146 |
required_if=required_if, |
| 147 |
supports_check_mode=True) |
| 148 |
|
| 149 |
def exec_module(self, **kwargs): |
| 150 |
|
| 151 |
for key in list(self.module_arg_spec.keys()): |
| 152 |
setattr(self, key, kwargs[key]) |
| 153 |
|
| 154 |
result = dict() |
| 155 |
changed = False |
| 156 |
|
| 157 |
self.next_hop_type = _snake_to_camel(self.next_hop_type, capitalize_first=True) |
| 158 |
|
| 159 |
result = self.get_route() |
| 160 |
if self.state == 'absent' and result: |
| 161 |
changed = True |
| 162 |
if not self.check_mode: |
| 163 |
self.delete_route() |
| 164 |
elif self.state == 'present': |
| 165 |
if not result: |
| 166 |
changed = True |
| 167 |
else: |
| 168 |
if result.next_hop_type != self.next_hop_type: |
| 169 |
self.log('Update: {0} next_hop_type from {1} to {2}'.format(self.name, result.next_hop_type, self.next_hop_type)) |
| 170 |
changed = True |
| 171 |
if result.next_hop_ip_address != self.next_hop_ip_address: |
| 172 |
self.log('Update: {0} next_hop_ip_address from {1} to {2}'.format(self.name, result.next_hop_ip_address, self.next_hop_ip_address)) |
| 173 |
changed = True |
| 174 |
if result.address_prefix != self.address_prefix: |
| 175 |
self.log('Update: {0} address_prefix from {1} to {2}'.format(self.name, result.address_prefix, self.address_prefix)) |
| 176 |
changed = True |
| 177 |
if changed: |
| 178 |
result = self.network_models.Route(name=self.name, |
| 179 |
address_prefix=self.address_prefix, |
| 180 |
next_hop_type=self.next_hop_type, |
| 181 |
next_hop_ip_address=self.next_hop_ip_address) |
| 182 |
if not self.check_mode: |
| 183 |
result = self.create_or_update_route(result) |
| 184 |
|
| 185 |
self.results['id'] = result.id if result else None |
| 186 |
self.results['changed'] = changed |
| 187 |
return self.results |
| 188 |
|
| 189 |
def create_or_update_route(self, param): |
| 190 |
try: |
| 191 |
poller = self.network_client.routes.begin_create_or_update(self.resource_group, self.route_table_name, self.name, param) |
| 192 |
return self.get_poller_result(poller) |
| 193 |
except Exception as exc: |
| 194 |
self.fail("Error creating or updating route {0} - {1}".format(self.name, str(exc))) |
| 195 |
|
| 196 |
def delete_route(self): |
| 197 |
try: |
| 198 |
poller = self.network_client.routes.begin_delete(self.resource_group, self.route_table_name, self.name) |
| 199 |
result = self.get_poller_result(poller) |
| 200 |
return result |
| 201 |
except Exception as exc: |
| 202 |
self.fail("Error deleting route {0} - {1}".format(self.name, str(exc))) |
| 203 |
|
| 204 |
def get_route(self): |
| 205 |
try: |
| 206 |
return self.network_client.routes.get(self.resource_group, self.route_table_name, self.name) |
| 207 |
except ResourceNotFoundError as cloud_err: |
| 208 |
|
| 209 |
if cloud_err.status_code == 404: |
| 210 |
self.log('{0}'.format(str(cloud_err))) |
| 211 |
return None |
| 212 |
self.fail('Error: failed to get resource {0} - {1}'.format(self.name, str(cloud_err))) |
| 213 |
except Exception as exc: |
| 214 |
self.fail('Error: failed to get resource {0} - {1}'.format(self.name, str(exc))) |
| 215 |
|
| 216 |
|
| 217 |
def main(): |
| 218 |
AzureRMRoute() |
| 219 |
|
| 220 |
|
| 221 |
if __name__ == '__main__': |
| 222 |
main() |