| 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_localnetworkgateway |
| 14 |
|
| 15 |
version_added: "2.4.0" |
| 16 |
|
| 17 |
short_description: Manage Azure Local Network Gateway in a resource group |
| 18 |
|
| 19 |
description: |
| 20 |
- Create, update or delete Azure Local Network Gateway in a resource group |
| 21 |
|
| 22 |
options: |
| 23 |
resource_group: |
| 24 |
description: |
| 25 |
- The local network gateway's resource group. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
name: |
| 29 |
description: |
| 30 |
- The name of the local network gateway. |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
location: |
| 34 |
description: |
| 35 |
- The location of the local network gateway. |
| 36 |
type: str |
| 37 |
local_network_address_space: |
| 38 |
description: |
| 39 |
- Local network site address space. |
| 40 |
type: dict |
| 41 |
suboptions: |
| 42 |
address_prefixes: |
| 43 |
description: |
| 44 |
- A list of address blocks reserved for this virtual network in CIDR notation. |
| 45 |
type: list |
| 46 |
elements: str |
| 47 |
gateway_ip_address: |
| 48 |
description: |
| 49 |
- IP address of local network gateway. |
| 50 |
type: str |
| 51 |
fqdn: |
| 52 |
description: |
| 53 |
- FQDN of local network gateway. |
| 54 |
type: str |
| 55 |
bgp_settings: |
| 56 |
description: |
| 57 |
- Local network gateway's BGP speaker settings. |
| 58 |
type: dict |
| 59 |
suboptions: |
| 60 |
asn: |
| 61 |
description: |
| 62 |
- The BGP speaker's ASN. |
| 63 |
type: int |
| 64 |
bgp_peering_address: |
| 65 |
description: |
| 66 |
- The BGP peering address and BGP identifier of this BGP speaker. |
| 67 |
type: str |
| 68 |
peer_weight: |
| 69 |
description: |
| 70 |
- The weight added to routes learned from this BGP speaker. |
| 71 |
type: int |
| 72 |
state: |
| 73 |
description: |
| 74 |
- Use C(present) to create or update a local network gateway. |
| 75 |
- Use C(absent) to delete the local network gateway. |
| 76 |
type: str |
| 77 |
default: present |
| 78 |
choices: |
| 79 |
- absent |
| 80 |
- present |
| 81 |
extends_documentation_fragment: |
| 82 |
- azure.azcollection.azure |
| 83 |
- azure.azcollection.azure_tags |
| 84 |
|
| 85 |
author: |
| 86 |
- xuzhang3 (@xuzhang3) |
| 87 |
- Fred Sun (@Fred-sun) |
| 88 |
''' |
| 89 |
|
| 90 |
EXAMPLES = ''' |
| 91 |
- name: Create a new local network gateway |
| 92 |
azure_rm_localnetworkgateway: |
| 93 |
resource_group: "{{ resource_group }}" |
| 94 |
name: "localgateway-name" |
| 95 |
local_network_address_space: |
| 96 |
address_prefixes: |
| 97 |
- 10.0.0.0/24 |
| 98 |
- 20.0.0.0/24 |
| 99 |
fqdn: fredtest.com |
| 100 |
tags: |
| 101 |
key: value |
| 102 |
bgp_settings: |
| 103 |
asn: 8 |
| 104 |
bgp_peering_address: 10.3.0.1 |
| 105 |
peer_weight: 3 |
| 106 |
|
| 107 |
- name: Delete local network gateway |
| 108 |
azure_rm_localnetworkgateway: |
| 109 |
resource_group: "{{ resource_group }}" |
| 110 |
name: "localgateway-name" |
| 111 |
state: absent |
| 112 |
''' |
| 113 |
|
| 114 |
RETURN = ''' |
| 115 |
state: |
| 116 |
description: |
| 117 |
- Current state of the Azure Local Network Gateway resource. |
| 118 |
returned: always |
| 119 |
type: complex |
| 120 |
contains: |
| 121 |
id: |
| 122 |
description: |
| 123 |
- The resource ID. |
| 124 |
type: str |
| 125 |
returned: always |
| 126 |
sample: "/subscriptions/xxxx-xxxx/resourceGroups/testRG/providers/Microsoft.Network/localNetworkGateways/testgateway" |
| 127 |
bgp_settings: |
| 128 |
description: |
| 129 |
- Local network gateway's BGP speaker settings. |
| 130 |
type: complex |
| 131 |
contains: |
| 132 |
asn: |
| 133 |
description: |
| 134 |
- The BGP speaker's ASN. |
| 135 |
type: int |
| 136 |
returned: always |
| 137 |
sample: 10 |
| 138 |
bgp_peering_address: |
| 139 |
description: |
| 140 |
- The BGP peering address and BGP identifier of this BGP speaker. |
| 141 |
type: str |
| 142 |
returned: always |
| 143 |
sample: 10.0.0.3 |
| 144 |
peer_weight: |
| 145 |
description: |
| 146 |
- The weight added to routes learned from this BGP speaker. |
| 147 |
type: int |
| 148 |
returned: always |
| 149 |
sample: 0 |
| 150 |
fqdn: |
| 151 |
description: |
| 152 |
- FQDN of local network gateway. |
| 153 |
type: str |
| 154 |
returned: always |
| 155 |
sample: testfqdn.com |
| 156 |
gateway_ip_address: |
| 157 |
description: |
| 158 |
- IP address of local network gateway. |
| 159 |
type: str |
| 160 |
returned: always |
| 161 |
sample: 10.1.1.1 |
| 162 |
etag: |
| 163 |
description: |
| 164 |
- A unique read-only string that changes whenever the resource is updated. |
| 165 |
type: str |
| 166 |
returned: always |
| 167 |
sample: b5a32693-2e75-49e0-9137-ded19db658d6 |
| 168 |
local_network_address_space: |
| 169 |
description: |
| 170 |
- Local network site address space. |
| 171 |
type: complex |
| 172 |
contains: |
| 173 |
address_prefixes: |
| 174 |
description: |
| 175 |
- A list of address blocks reserved for this virtual network in CIDR notation. |
| 176 |
type: list |
| 177 |
returned: always |
| 178 |
sample: ["10.0.0.0/24", "20.0.0.0/24"] |
| 179 |
location: |
| 180 |
description: |
| 181 |
- The resource location. |
| 182 |
type: str |
| 183 |
returned: always |
| 184 |
sample: eastus |
| 185 |
name: |
| 186 |
description: |
| 187 |
- The resource name. |
| 188 |
type: str |
| 189 |
returned: always |
| 190 |
sample: testgateway |
| 191 |
provisioning_state: |
| 192 |
description: |
| 193 |
- The provisioning state of the local network gateway resource. |
| 194 |
type: str |
| 195 |
returned: always |
| 196 |
sample: Succeeded |
| 197 |
tags: |
| 198 |
description: |
| 199 |
- The resource tags. |
| 200 |
type: str |
| 201 |
returned: always |
| 202 |
sample: {'key1': 'value1', 'key2': 'value2'} |
| 203 |
type: |
| 204 |
description: |
| 205 |
- The resource type. |
| 206 |
type: str |
| 207 |
returned: always |
| 208 |
sample: Microsoft.Network/localNetworkGateways |
| 209 |
''' |
| 210 |
|
| 211 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 212 |
|
| 213 |
try: |
| 214 |
from azure.core.exceptions import HttpResponseError |
| 215 |
from azure.core.polling import LROPoller |
| 216 |
except Exception: |
| 217 |
|
| 218 |
pass |
| 219 |
|
| 220 |
|
| 221 |
bgp_settings_spec = dict( |
| 222 |
asn=dict(type='int'), |
| 223 |
bgp_peering_address=dict(type='str'), |
| 224 |
peer_weight=dict(type='int'), |
| 225 |
) |
| 226 |
|
| 227 |
|
| 228 |
local_network_address_space_spec = dict( |
| 229 |
address_prefixes=dict(type='list', elements='str') |
| 230 |
) |
| 231 |
|
| 232 |
|
| 233 |
class AzureRMNetworkGateWay(AzureRMModuleBase): |
| 234 |
"""Utility class to get Azure Kubernetes Service Credentials facts""" |
| 235 |
|
| 236 |
def __init__(self): |
| 237 |
|
| 238 |
self.module_arg_spec = dict( |
| 239 |
name=dict(type='str', required=True), |
| 240 |
resource_group=dict(type='str', required=True), |
| 241 |
location=dict(type='str'), |
| 242 |
local_network_address_space=dict(type='dict', options=local_network_address_space_spec), |
| 243 |
gateway_ip_address=dict(type='str'), |
| 244 |
fqdn=dict(type='str'), |
| 245 |
bgp_settings=dict(type='dict', options=bgp_settings_spec), |
| 246 |
state=dict(type='str', default='present', choices=['present', 'absent']) |
| 247 |
) |
| 248 |
|
| 249 |
self.name = None |
| 250 |
self.location = None |
| 251 |
self.local_network_address_space = None |
| 252 |
self.gateway_ip_address = None |
| 253 |
self.fqdn = None |
| 254 |
self.tags = None |
| 255 |
self.bgp_settings = None |
| 256 |
|
| 257 |
self.results = dict( |
| 258 |
changed=False, |
| 259 |
state=[], |
| 260 |
) |
| 261 |
mutually_exclusive = [['gateway_ip_address', 'fqdn']] |
| 262 |
|
| 263 |
super(AzureRMNetworkGateWay, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 264 |
supports_check_mode=True, |
| 265 |
mutually_exclusive=mutually_exclusive, |
| 266 |
supports_tags=True, |
| 267 |
facts_module=False) |
| 268 |
|
| 269 |
def exec_module(self, **kwargs): |
| 270 |
|
| 271 |
for key in list(self.module_arg_spec) + ['tags']: |
| 272 |
setattr(self, key, kwargs[key]) |
| 273 |
|
| 274 |
if not self.location: |
| 275 |
|
| 276 |
resource_group = self.get_resource_group(self.resource_group) |
| 277 |
self.location = resource_group.location |
| 278 |
|
| 279 |
old_response = self.get_local_network_gateway() |
| 280 |
changed = False |
| 281 |
update_tags = False |
| 282 |
|
| 283 |
response = None |
| 284 |
if self.state == 'present': |
| 285 |
if old_response is not None: |
| 286 |
if self.fqdn is not None and self.fqdn != old_response['fqdn']: |
| 287 |
changed = True |
| 288 |
else: |
| 289 |
self.fqdn = old_response['fqdn'] |
| 290 |
if self.gateway_ip_address is not None and self.gateway_ip_address != old_response['gateway_ip_address']: |
| 291 |
changed = True |
| 292 |
else: |
| 293 |
self.gateway_ip_address = old_response['gateway_ip_address'] |
| 294 |
if self.bgp_settings is not None and\ |
| 295 |
not all(self.bgp_settings.get(key) == old_response['bgp_settings'].get(key) for key in self.bgp_settings.keys()): |
| 296 |
changed = True |
| 297 |
if self.local_network_address_space is not None: |
| 298 |
if old_response['local_network_address_space'].get('address_prefixes') is not None: |
| 299 |
new_address = list(set(self.local_network_address_space['address_prefixes'] + |
| 300 |
old_response['local_network_address_space']['address_prefixes'])) |
| 301 |
if len(new_address) > len(old_response['local_network_address_space'].get('address_prefixes')): |
| 302 |
changed = True |
| 303 |
self.local_network_address_space['address_prefixes'] = new_address |
| 304 |
else: |
| 305 |
changed = True |
| 306 |
else: |
| 307 |
self.local_network_address_space['address_prefixes'] = old_response['local_network_address_space'].get('address_prefixes') |
| 308 |
|
| 309 |
update_tags, new_tags = self.update_tags(old_response.get('tags')) |
| 310 |
if update_tags: |
| 311 |
|
| 312 |
self.fail("Can't update the local network gateway tags, Exception code as AllPropertiesAreReadOnly") |
| 313 |
changed = True |
| 314 |
else: |
| 315 |
changed = True |
| 316 |
|
| 317 |
local_network_address_space = None |
| 318 |
if self.local_network_address_space is not None: |
| 319 |
local_network_address_space = self.network_models.AddressSpace(address_prefixes=self.local_network_address_space['address_prefixes']) |
| 320 |
bgp_settings = None |
| 321 |
if self.bgp_settings is not None: |
| 322 |
bgp_settings = self.network_models.BgpSettings(asn=self.bgp_settings.get('asn'), |
| 323 |
bgp_peering_address=self.bgp_settings.get('bgp_peering_address'), |
| 324 |
peer_weight=self.bgp_settings.get('peer_weight')) |
| 325 |
|
| 326 |
gateway_resource = self.network_models.LocalNetworkGateway(location=self.location, |
| 327 |
tags=self.tags, |
| 328 |
gateway_ip_address=self.gateway_ip_address, |
| 329 |
fqdn=self.fqdn, |
| 330 |
local_network_address_space=local_network_address_space, |
| 331 |
bgp_settings=bgp_settings) |
| 332 |
if changed: |
| 333 |
if not self.check_mode: |
| 334 |
response = self.create_or_update_local_network_gateway(gateway_resource) |
| 335 |
|
| 336 |
if old_response is not None: |
| 337 |
update_tags, new_tags = self.update_tags(old_response.get('tags')) |
| 338 |
if update_tags: |
| 339 |
if not self.check_mode: |
| 340 |
response = self.update_local_network_gateway_tags(new_tags) |
| 341 |
changed = True |
| 342 |
else: |
| 343 |
if not self.check_mode: |
| 344 |
if old_response is not None: |
| 345 |
self.delete_local_network_gateway() |
| 346 |
changed = True |
| 347 |
response = None |
| 348 |
else: |
| 349 |
changed = True |
| 350 |
|
| 351 |
if response is None: |
| 352 |
response = old_response |
| 353 |
self.results['state'] = response |
| 354 |
self.results['changed'] = changed |
| 355 |
return self.results |
| 356 |
|
| 357 |
def get_local_network_gateway(self): |
| 358 |
"""Gets the specified local network gateway in a resource group""" |
| 359 |
response = None |
| 360 |
try: |
| 361 |
response = self.network_client.local_network_gateways.get(self.resource_group, self.name) |
| 362 |
except HttpResponseError as ec: |
| 363 |
self.log("Gets the specified local network gateway in a resource group Failed, Exception as {0}".format(ec)) |
| 364 |
return None |
| 365 |
return self.format_response(response) |
| 366 |
|
| 367 |
def create_or_update_local_network_gateway(self, body): |
| 368 |
"""Create or Update local network gateway""" |
| 369 |
response = None |
| 370 |
try: |
| 371 |
response = self.network_client.local_network_gateways.begin_create_or_update(self.resource_group, self.name, body) |
| 372 |
if isinstance(response, LROPoller): |
| 373 |
response = self.get_poller_result(response) |
| 374 |
except HttpResponseError as ec: |
| 375 |
self.fail("Create or Updated a local network gateway in a resource group Failed, Exception as {0}".format(ec)) |
| 376 |
|
| 377 |
return self.format_response(response) |
| 378 |
|
| 379 |
def update_local_network_gateway_tags(self, tags): |
| 380 |
"""Updates a local network gateway tags""" |
| 381 |
response = None |
| 382 |
try: |
| 383 |
response = self.network_client.local_network_gateways.update_tags(self.resource_group, self.name, tags) |
| 384 |
except HttpResponseError as ec: |
| 385 |
self.fail("Update a local network gateway tags Failed, Exception as {0}".format(ec)) |
| 386 |
return self.format_response(response) |
| 387 |
|
| 388 |
def delete_local_network_gateway(self): |
| 389 |
"""Deletes the specified local network gateway""" |
| 390 |
try: |
| 391 |
self.network_client.local_network_gateways.begin_delete(self.resource_group, self.name) |
| 392 |
except HttpResponseError as ec: |
| 393 |
self.fail("Deletes the specified local network gateway Failed, Exception as {0}".format(ec)) |
| 394 |
return None |
| 395 |
|
| 396 |
def format_response(self, item): |
| 397 |
result = dict( |
| 398 |
id=item.id, |
| 399 |
name=item.name, |
| 400 |
location=item.location, |
| 401 |
type=item.type, |
| 402 |
tags=item.tags, |
| 403 |
etag=item.etag, |
| 404 |
local_network_address_space=dict(), |
| 405 |
gateway_ip_address=item.gateway_ip_address, |
| 406 |
fqdn=item.fqdn, |
| 407 |
provisioning_state=item.provisioning_state, |
| 408 |
bgp_settings=dict(), |
| 409 |
) |
| 410 |
|
| 411 |
if item.local_network_address_space is not None: |
| 412 |
result['local_network_address_space']['address_prefixes'] = item.local_network_address_space.address_prefixes |
| 413 |
if item.bgp_settings is not None: |
| 414 |
result['bgp_settings']['asn'] = item.bgp_settings.asn |
| 415 |
result['bgp_settings']['bgp_peering_address'] = item.bgp_settings.bgp_peering_address |
| 416 |
result['bgp_settings']['peer_weight'] = item.bgp_settings.peer_weight |
| 417 |
return result |
| 418 |
|
| 419 |
|
| 420 |
def main(): |
| 421 |
"""Main module execution code path""" |
| 422 |
|
| 423 |
AzureRMNetworkGateWay() |
| 424 |
|
| 425 |
|
| 426 |
if __name__ == '__main__': |
| 427 |
main() |