| 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_natgateway |
| 14 |
short_description: Manage Azure NAT Gateway instance |
| 15 |
description: |
| 16 |
- Create, update and delete instances of Azure NAT Gateway. |
| 17 |
|
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- The name of the resource group. |
| 22 |
required: True |
| 23 |
type: str |
| 24 |
name: |
| 25 |
description: |
| 26 |
- The name of the NAT Gateway. |
| 27 |
required: True |
| 28 |
type: str |
| 29 |
location: |
| 30 |
description: |
| 31 |
- Resource location. If not set, location from the resource group will be used as default. |
| 32 |
type: str |
| 33 |
zones: |
| 34 |
description: |
| 35 |
- List of Availability Zones in which this NAT Gateway should be located. |
| 36 |
type: list |
| 37 |
elements: int |
| 38 |
choices: |
| 39 |
- 1 |
| 40 |
- 2 |
| 41 |
- 3 |
| 42 |
sku: |
| 43 |
description: |
| 44 |
- SKU of the NAT gateway resource. |
| 45 |
type: dict |
| 46 |
suboptions: |
| 47 |
name: |
| 48 |
description: |
| 49 |
- Name of the NAT gateway SKU. Defaults to C(standard). |
| 50 |
choices: |
| 51 |
- 'standard' |
| 52 |
default: 'standard' |
| 53 |
type: str |
| 54 |
idle_timeout_in_minutes: |
| 55 |
description: |
| 56 |
- The idle timeout in minutes which should be used. Defaults to 4. |
| 57 |
default: 4 |
| 58 |
type: int |
| 59 |
public_ip_addresses: |
| 60 |
description: |
| 61 |
- A list of Public IP Addresses which should be associated with the NAT Gateway resource. |
| 62 |
- Each element can be the name or resource id, or a dict contains C(name), C(resource_group) information of the IP address. |
| 63 |
type: list |
| 64 |
elements: str |
| 65 |
state: |
| 66 |
description: |
| 67 |
- Assert the state of the NAT gateway. Use C(present) to create or update and C(absent) to delete. |
| 68 |
default: present |
| 69 |
type: str |
| 70 |
choices: |
| 71 |
- absent |
| 72 |
- present |
| 73 |
|
| 74 |
extends_documentation_fragment: |
| 75 |
- azure.azcollection.azure |
| 76 |
- azure.azcollection.azure_tags |
| 77 |
|
| 78 |
author: |
| 79 |
- Andrea Decorte (@andreadecorte) |
| 80 |
|
| 81 |
""" |
| 82 |
|
| 83 |
EXAMPLES = """ |
| 84 |
- name: Create instance of NAT Gateway |
| 85 |
azure_rm_natgateway: |
| 86 |
resource_group: myResourceGroup |
| 87 |
name: myNATGateway |
| 88 |
public_ip_addresses: |
| 89 |
- pip_name |
| 90 |
|
| 91 |
- name: Create instance of NAT Gateway |
| 92 |
azure_rm_natgateway: |
| 93 |
resource_group: myResourceGroup |
| 94 |
name: myNATGateway |
| 95 |
idle_timeout_in_minutes: 10 |
| 96 |
location: eastus |
| 97 |
zones: [1] |
| 98 |
sku: |
| 99 |
name: standard |
| 100 |
""" |
| 101 |
|
| 102 |
RETURN = """ |
| 103 |
id: |
| 104 |
description: |
| 105 |
- NAT gateway resource ID. |
| 106 |
returned: always |
| 107 |
type: str |
| 108 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/natGateways/myNATGw |
| 109 |
name: |
| 110 |
description: |
| 111 |
- Name of NAT gateway. |
| 112 |
returned: always |
| 113 |
type: str |
| 114 |
sample: myNATGw |
| 115 |
resource_group: |
| 116 |
description: |
| 117 |
- Name of resource group. |
| 118 |
returned: always |
| 119 |
type: str |
| 120 |
sample: myResourceGroup |
| 121 |
location: |
| 122 |
description: |
| 123 |
- Location of NAT gateway. |
| 124 |
returned: always |
| 125 |
type: str |
| 126 |
sample: centralus |
| 127 |
""" |
| 128 |
|
| 129 |
import time |
| 130 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase, format_resource_id |
| 131 |
|
| 132 |
try: |
| 133 |
from azure.core.exceptions import ResourceNotFoundError |
| 134 |
from azure.core.polling import LROPoller |
| 135 |
from azure.mgmt.core.tools import parse_resource_id, is_valid_resource_id |
| 136 |
except ImportError: |
| 137 |
|
| 138 |
pass |
| 139 |
|
| 140 |
|
| 141 |
class Actions: |
| 142 |
NoAction, Create, Update, Delete = range(4) |
| 143 |
|
| 144 |
|
| 145 |
sku_spec = dict( |
| 146 |
name=dict(type='str', choices=['standard'], default='standard') |
| 147 |
) |
| 148 |
|
| 149 |
|
| 150 |
class AzureRMNATGateways(AzureRMModuleBase): |
| 151 |
"""Configuration class for an Azure RM NAT Gateway resource""" |
| 152 |
|
| 153 |
def __init__(self): |
| 154 |
self.module_arg_spec = dict( |
| 155 |
resource_group=dict( |
| 156 |
type='str', |
| 157 |
required=True |
| 158 |
), |
| 159 |
name=dict( |
| 160 |
type='str', |
| 161 |
required=True |
| 162 |
), |
| 163 |
location=dict( |
| 164 |
type='str' |
| 165 |
), |
| 166 |
sku=dict( |
| 167 |
type='dict', |
| 168 |
options=sku_spec |
| 169 |
), |
| 170 |
idle_timeout_in_minutes=dict( |
| 171 |
type='int', |
| 172 |
default=4 |
| 173 |
), |
| 174 |
zones=dict( |
| 175 |
type='list', |
| 176 |
elements='int', |
| 177 |
choices=[1, 2, 3] |
| 178 |
), |
| 179 |
public_ip_addresses=dict( |
| 180 |
type='list', |
| 181 |
elements='str' |
| 182 |
), |
| 183 |
state=dict( |
| 184 |
type='str', |
| 185 |
default='present', |
| 186 |
choices=['present', 'absent'] |
| 187 |
) |
| 188 |
) |
| 189 |
|
| 190 |
self.resource_group = None |
| 191 |
self.name = None |
| 192 |
self.parameters = dict() |
| 193 |
|
| 194 |
self.results = dict(changed=False) |
| 195 |
self.state = None |
| 196 |
self.to_do = Actions.NoAction |
| 197 |
|
| 198 |
super(AzureRMNATGateways, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 199 |
supports_check_mode=True, supports_tags=True) |
| 200 |
|
| 201 |
def exec_module(self, **kwargs): |
| 202 |
"""Main module execution method""" |
| 203 |
|
| 204 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 205 |
if hasattr(self, key): |
| 206 |
setattr(self, key, kwargs[key]) |
| 207 |
elif kwargs[key] is not None: |
| 208 |
if key == "id": |
| 209 |
self.parameters["id"] = kwargs[key] |
| 210 |
elif key == "location": |
| 211 |
self.parameters["location"] = kwargs[key] |
| 212 |
elif key == "idle_timeout_in_minutes": |
| 213 |
self.parameters["idle_timeout_in_minutes"] = kwargs[key] |
| 214 |
elif key == "zones": |
| 215 |
self.parameters["zones"] = kwargs[key] |
| 216 |
elif key == "public_ip_addresses": |
| 217 |
if "public_ip_addresses" not in self.parameters: |
| 218 |
self.parameters["public_ip_addresses"] = [] |
| 219 |
for resource in kwargs[key]: |
| 220 |
self.parameters["public_ip_addresses"].append({"id": self.return_resource_id(resource)}) |
| 221 |
elif key == "sku": |
| 222 |
ev = kwargs[key] |
| 223 |
if "name" in ev: |
| 224 |
if ev["name"] == "standard": |
| 225 |
ev["name"] = "Standard" |
| 226 |
self.parameters["sku"] = ev |
| 227 |
|
| 228 |
old_response = None |
| 229 |
response = None |
| 230 |
|
| 231 |
resource_group = self.get_resource_group(self.resource_group) |
| 232 |
|
| 233 |
if "location" not in self.parameters: |
| 234 |
self.parameters["location"] = resource_group.location |
| 235 |
|
| 236 |
|
| 237 |
if "sku" not in self.parameters: |
| 238 |
self.parameters["sku"] = {"name": "Standard"} |
| 239 |
|
| 240 |
old_response = self.get_natgateway() |
| 241 |
|
| 242 |
if not old_response: |
| 243 |
self.log("NAT Gateway instance doesn't exist") |
| 244 |
if self.state == "absent": |
| 245 |
self.log("Old instance didn't exist") |
| 246 |
else: |
| 247 |
self.to_do = Actions.Create |
| 248 |
else: |
| 249 |
self.log("NAT Gateway instance already exists") |
| 250 |
if self.state == "absent": |
| 251 |
self.to_do = Actions.Delete |
| 252 |
elif self.state == "present": |
| 253 |
self.log("Need to check if NAT Gateway instance has to be deleted or may be updated") |
| 254 |
self.to_do = Actions.Update |
| 255 |
|
| 256 |
if (self.to_do == Actions.Update): |
| 257 |
if (self.parameters["location"] != old_response["location"] or |
| 258 |
self.check_if_changed("zones", old_response) or |
| 259 |
self.check_if_changed("idle_timeout_in_minutes", old_response) or |
| 260 |
self.check_if_changed("public_ip_addresses", old_response) or |
| 261 |
self.parameters["sku"]["name"] != old_response["sku"]["name"]): |
| 262 |
self.to_do = Actions.Update |
| 263 |
else: |
| 264 |
self.to_do = Actions.NoAction |
| 265 |
|
| 266 |
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update): |
| 267 |
self.log("Need to Create / Update the NAT Gateway instance") |
| 268 |
|
| 269 |
if self.check_mode: |
| 270 |
self.results["changed"] = True |
| 271 |
self.results["parameters"] = self.parameters |
| 272 |
return self.results |
| 273 |
|
| 274 |
response = self.create_update_natgateway() |
| 275 |
|
| 276 |
if not old_response: |
| 277 |
self.results["changed"] = True |
| 278 |
else: |
| 279 |
self.results["changed"] = (old_response != response) |
| 280 |
self.log("Creation / Update done") |
| 281 |
|
| 282 |
elif self.to_do == Actions.Delete: |
| 283 |
self.log("NAT Gateway instance deleted") |
| 284 |
self.results["changed"] = True |
| 285 |
|
| 286 |
if self.check_mode: |
| 287 |
return self.results |
| 288 |
|
| 289 |
self.delete_natgateway() |
| 290 |
|
| 291 |
|
| 292 |
while self.get_natgateway(): |
| 293 |
time.sleep(20) |
| 294 |
else: |
| 295 |
self.log("NAT Gateway instance unchanged") |
| 296 |
self.results["changed"] = False |
| 297 |
response = old_response |
| 298 |
|
| 299 |
if response: |
| 300 |
self.results.update(self.format_response(response)) |
| 301 |
|
| 302 |
return self.results |
| 303 |
|
| 304 |
def create_update_natgateway(self): |
| 305 |
""" |
| 306 |
Creates or updates NAT Gateway with the specified configuration. |
| 307 |
|
| 308 |
:return: deserialized NAT Gateway instance state dictionary |
| 309 |
""" |
| 310 |
self.log("Creating / Updating the NAT Gateway instance {0}".format(self.name)) |
| 311 |
|
| 312 |
try: |
| 313 |
response = self.network_client.nat_gateways.begin_create_or_update(resource_group_name=self.resource_group, |
| 314 |
nat_gateway_name=self.name, |
| 315 |
parameters=self.parameters) |
| 316 |
if isinstance(response, LROPoller): |
| 317 |
response = self.get_poller_result(response) |
| 318 |
|
| 319 |
except Exception as exc: |
| 320 |
self.log("Error attempting to create the NAT Gateway instance.") |
| 321 |
self.fail("Error creating the NAT Gateway instance: {0}".format(str(exc))) |
| 322 |
return response.as_dict() |
| 323 |
|
| 324 |
def delete_natgateway(self): |
| 325 |
""" |
| 326 |
Deletes specified NAT Gateway instance in the specified subscription and resource group. |
| 327 |
|
| 328 |
:return: True |
| 329 |
""" |
| 330 |
self.log("Deleting the NAT Gateway instance {0}".format(self.name)) |
| 331 |
try: |
| 332 |
response = self.network_client.nat_gateways.begin_delete(resource_group_name=self.resource_group, |
| 333 |
nat_gateway_name=self.name) |
| 334 |
except Exception as e: |
| 335 |
self.log("Error attempting to delete the NAT Gateway instance.") |
| 336 |
self.fail("Error deleting the NAT Gateway instance: {0}".format(str(e))) |
| 337 |
|
| 338 |
return True |
| 339 |
|
| 340 |
def get_natgateway(self): |
| 341 |
""" |
| 342 |
Gets the properties of the specified NAT Gateway. |
| 343 |
|
| 344 |
:return: deserialized NAT Gateway instance state dictionary |
| 345 |
""" |
| 346 |
self.log("Checking if the NAT Gateway instance {0} is present".format(self.name)) |
| 347 |
found = False |
| 348 |
try: |
| 349 |
response = self.network_client.nat_gateways.get(resource_group_name=self.resource_group, |
| 350 |
nat_gateway_name=self.name) |
| 351 |
found = True |
| 352 |
self.log("Response : {0}".format(response)) |
| 353 |
self.log("NAT Gateway instance : {0} found".format(response.name)) |
| 354 |
except ResourceNotFoundError as e: |
| 355 |
self.log("Did not find the NAT Gateway instance.") |
| 356 |
if found is True: |
| 357 |
return response.as_dict() |
| 358 |
|
| 359 |
return False |
| 360 |
|
| 361 |
def check_if_changed(self, parameter_name, old_response): |
| 362 |
"""" |
| 363 |
Compute if there is an update to the resource or not |
| 364 |
|
| 365 |
:return: True if resource is changed compared to the current one |
| 366 |
""" |
| 367 |
if parameter_name in self.parameters and (parameter_name not in old_response or self.parameters[parameter_name] != old_response[parameter_name]): |
| 368 |
|
| 369 |
return True |
| 370 |
elif parameter_name not in self.parameters and parameter_name in old_response: |
| 371 |
|
| 372 |
return True |
| 373 |
else: |
| 374 |
return False |
| 375 |
|
| 376 |
def format_response(self, natgw_dict): |
| 377 |
""" |
| 378 |
Build format of the response |
| 379 |
|
| 380 |
:return dictionary filled with resource data |
| 381 |
""" |
| 382 |
id = natgw_dict.get("id") |
| 383 |
id_dict = parse_resource_id(id) |
| 384 |
d = { |
| 385 |
"id": id, |
| 386 |
"name": natgw_dict.get("name"), |
| 387 |
"resource_group": id_dict.get("resource_group", self.resource_group), |
| 388 |
"location": natgw_dict.get("location") |
| 389 |
} |
| 390 |
return d |
| 391 |
|
| 392 |
def return_resource_id(self, resource): |
| 393 |
""" |
| 394 |
Build an IP Address resource id from different inputs |
| 395 |
|
| 396 |
:return string containing the Azure id of the resource |
| 397 |
""" |
| 398 |
if is_valid_resource_id(resource): |
| 399 |
return resource |
| 400 |
resource_dict = self.parse_resource_to_dict(resource) |
| 401 |
return format_resource_id(val=resource_dict["name"], |
| 402 |
subscription_id=resource_dict.get("subscription_id"), |
| 403 |
namespace="Microsoft.Network", |
| 404 |
types="publicIPAddresses", |
| 405 |
resource_group=resource_dict.get("resource_group")) |
| 406 |
|
| 407 |
|
| 408 |
def main(): |
| 409 |
"""Main execution""" |
| 410 |
AzureRMNATGateways() |
| 411 |
|
| 412 |
|
| 413 |
if __name__ == "__main__": |
| 414 |
main() |