| 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_info |
| 14 |
version_added: "1.13.0" |
| 15 |
short_description: Retrieve NAT Gateway instance facts |
| 16 |
description: |
| 17 |
- Get facts for a NAT Gateway instance. |
| 18 |
options: |
| 19 |
name: |
| 20 |
description: |
| 21 |
- Only show results for a specific NAT gateway. |
| 22 |
type: str |
| 23 |
resource_group: |
| 24 |
description: |
| 25 |
- Limit results by resource group. |
| 26 |
type: str |
| 27 |
|
| 28 |
extends_documentation_fragment: |
| 29 |
- azure.azcollection.azure |
| 30 |
|
| 31 |
author: |
| 32 |
- Andrea Decorte (@andreadecorte) |
| 33 |
''' |
| 34 |
|
| 35 |
EXAMPLES = ''' |
| 36 |
- name: Get facts for NAT gateway by name. |
| 37 |
azure_rm_natgateway_info: |
| 38 |
name: Mynatgw |
| 39 |
resource_group: MyResourceGroup |
| 40 |
|
| 41 |
- name: Get facts for all NAT gateways in resource group. |
| 42 |
azure_rm_natgateway_info: |
| 43 |
resource_group: MyResourceGroup |
| 44 |
|
| 45 |
- name: Get facts for all NAT gateways. |
| 46 |
azure_rm_natgateway_info: |
| 47 |
''' |
| 48 |
|
| 49 |
RETURN = ''' |
| 50 |
gateways: |
| 51 |
description: |
| 52 |
- A list of dictionaries containing facts for a NAT gateway. |
| 53 |
returned: always |
| 54 |
type: list |
| 55 |
elements: dict |
| 56 |
contains: |
| 57 |
id: |
| 58 |
description: |
| 59 |
- NAT gateway resource ID. |
| 60 |
returned: always |
| 61 |
type: str |
| 62 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/natGateways/mynatgw |
| 63 |
name: |
| 64 |
description: |
| 65 |
- Name of NAT gateway. |
| 66 |
returned: always |
| 67 |
type: str |
| 68 |
sample: mynatgw |
| 69 |
resource_group: |
| 70 |
description: |
| 71 |
- Name of resource group. |
| 72 |
returned: always |
| 73 |
type: str |
| 74 |
sample: myResourceGroup |
| 75 |
location: |
| 76 |
description: |
| 77 |
- Location of NAT gateway. |
| 78 |
returned: always |
| 79 |
type: str |
| 80 |
sample: centralus |
| 81 |
idle_timeout_in_minutes: |
| 82 |
description: |
| 83 |
- The idle timeout of the NAT gateway. |
| 84 |
returned: always |
| 85 |
type: int |
| 86 |
sample: 4 |
| 87 |
sku: |
| 88 |
description: |
| 89 |
- SKU of the NAT gateway. |
| 90 |
returned: always |
| 91 |
type: dict |
| 92 |
contains: |
| 93 |
name: |
| 94 |
description: |
| 95 |
- The name of the SKU. |
| 96 |
returned: always |
| 97 |
type: str |
| 98 |
sample: Standard |
| 99 |
zones: |
| 100 |
description: |
| 101 |
- Availability Zones of the NAT gateway. |
| 102 |
returned: always |
| 103 |
type: list |
| 104 |
elements: str |
| 105 |
public_ip_addresses: |
| 106 |
description: |
| 107 |
- List of ids of public IP addresses associated to the NAT Gateway. |
| 108 |
returned: always |
| 109 |
type: list |
| 110 |
elements: str |
| 111 |
''' |
| 112 |
|
| 113 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 114 |
|
| 115 |
try: |
| 116 |
from azure.core.exceptions import ResourceNotFoundError |
| 117 |
from azure.mgmt.core.tools import parse_resource_id |
| 118 |
except ImportError: |
| 119 |
|
| 120 |
pass |
| 121 |
|
| 122 |
|
| 123 |
class AzureRMNATGatewayInfo(AzureRMModuleBase): |
| 124 |
|
| 125 |
def __init__(self): |
| 126 |
self.module_arg_spec = dict( |
| 127 |
name=dict(type='str'), |
| 128 |
resource_group=dict(type='str'), |
| 129 |
) |
| 130 |
|
| 131 |
self.results = dict( |
| 132 |
changed=False, |
| 133 |
) |
| 134 |
|
| 135 |
self.name = None |
| 136 |
self.resource_group = None |
| 137 |
|
| 138 |
super(AzureRMNATGatewayInfo, self).__init__(self.module_arg_spec, |
| 139 |
supports_check_mode=True, |
| 140 |
supports_tags=False, |
| 141 |
facts_module=True) |
| 142 |
|
| 143 |
def exec_module(self, **kwargs): |
| 144 |
for key in self.module_arg_spec: |
| 145 |
setattr(self, key, kwargs[key]) |
| 146 |
|
| 147 |
if self.name is not None: |
| 148 |
self.results["gateways"] = self.get() |
| 149 |
elif self.resource_group is not None: |
| 150 |
self.results["gateways"] = self.list_by_rg() |
| 151 |
else: |
| 152 |
self.results["gateways"] = self.list_all() |
| 153 |
|
| 154 |
return self.results |
| 155 |
|
| 156 |
def get(self): |
| 157 |
response = None |
| 158 |
results = [] |
| 159 |
try: |
| 160 |
response = self.network_client.nat_gateways.get(resource_group_name=self.resource_group, nat_gateway_name=self.name) |
| 161 |
except ResourceNotFoundError: |
| 162 |
pass |
| 163 |
|
| 164 |
if response is not None: |
| 165 |
results.append(self.format_response(response)) |
| 166 |
|
| 167 |
return results |
| 168 |
|
| 169 |
def list_by_rg(self): |
| 170 |
response = None |
| 171 |
results = [] |
| 172 |
try: |
| 173 |
response = self.network_client.nat_gateways.list(resource_group_name=self.resource_group) |
| 174 |
except Exception as exc: |
| 175 |
request_id = exc.request_id if exc.request_id else '' |
| 176 |
self.fail("Error listing NAT gateways in resource groups {0}: {1} - {2}".format(self.resource_group, request_id, str(exc))) |
| 177 |
|
| 178 |
for item in response: |
| 179 |
results.append(self.format_response(item)) |
| 180 |
|
| 181 |
return results |
| 182 |
|
| 183 |
def list_all(self): |
| 184 |
response = None |
| 185 |
results = [] |
| 186 |
try: |
| 187 |
response = self.network_client.nat_gateways.list_all() |
| 188 |
except Exception as exc: |
| 189 |
request_id = exc.request_id if exc.request_id else '' |
| 190 |
self.fail("Error listing all NAT gateways: {0} - {1}".format(request_id, str(exc))) |
| 191 |
|
| 192 |
for item in response: |
| 193 |
results.append(self.format_response(item)) |
| 194 |
|
| 195 |
return results |
| 196 |
|
| 197 |
def format_response(self, natgw): |
| 198 |
d = natgw.as_dict() |
| 199 |
id = d.get("id") |
| 200 |
id_dict = parse_resource_id(id) |
| 201 |
d = { |
| 202 |
"id": id, |
| 203 |
"name": d.get("name"), |
| 204 |
"resource_group": id_dict.get("resource_group", self.resource_group), |
| 205 |
"location": d.get("location"), |
| 206 |
"sku": d.get("sku"), |
| 207 |
"zones": d.get("zones"), |
| 208 |
"idle_timeout_in_minutes": d.get("idle_timeout_in_minutes"), |
| 209 |
"public_ip_addresses": d.get("public_ip_addresses") |
| 210 |
} |
| 211 |
return d |
| 212 |
|
| 213 |
|
| 214 |
def main(): |
| 215 |
AzureRMNATGatewayInfo() |
| 216 |
|
| 217 |
|
| 218 |
if __name__ == "__main__": |
| 219 |
main() |