| 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_virtualnetworkgatewaynatrule_info |
| 14 |
|
| 15 |
version_added: "2.4.0" |
| 16 |
|
| 17 |
short_description: Gets or list nat rules for a particular virtual network gateway |
| 18 |
|
| 19 |
description: |
| 20 |
- Gets or list nat rules for a particular virtual network gateway. |
| 21 |
|
| 22 |
options: |
| 23 |
resource_group: |
| 24 |
description: |
| 25 |
- The local network gateway's resource group. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
virtual_network_gateway_name: |
| 29 |
description: |
| 30 |
- The name of the local network gateway. |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
name: |
| 34 |
description: |
| 35 |
- The name of the nat rule. |
| 36 |
type: str |
| 37 |
|
| 38 |
extends_documentation_fragment: |
| 39 |
- azure.azcollection.azure |
| 40 |
|
| 41 |
author: |
| 42 |
- xuzhang3 (@xuzhang3) |
| 43 |
- Fred Sun (@Fred-sun) |
| 44 |
''' |
| 45 |
|
| 46 |
EXAMPLES = ''' |
| 47 |
- name: Gets the nat rule by the name |
| 48 |
azure_rm_virtualnetworkgatewaynatrule_info: |
| 49 |
resource_group: "{{ resource_group }}" |
| 50 |
virtual_network_gateway_name: "{{ local_networkgateway_name }}" |
| 51 |
name: "{{ name }}" |
| 52 |
|
| 53 |
- name: List all nat rules for a particular virtual network gateway |
| 54 |
azure_rm_virtualnetworkgatewaynatrule_info: |
| 55 |
resource_group: "{{ resource_group }}" |
| 56 |
virtual_network_gateway_name: "{{ local_networkgateway_name }}" |
| 57 |
''' |
| 58 |
|
| 59 |
RETURN = ''' |
| 60 |
state: |
| 61 |
description: |
| 62 |
- Gets the nat rules for a particular virtual network gateway |
| 63 |
returned: always |
| 64 |
type: complex |
| 65 |
contains: |
| 66 |
id: |
| 67 |
description: |
| 68 |
- The resource ID. |
| 69 |
type: str |
| 70 |
returned: always |
| 71 |
sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Network/virtualNetworkGateways/vng01/natRules/natrule" |
| 72 |
internal_mappings: |
| 73 |
description: |
| 74 |
- The private IP address internal mapping for NAT. |
| 75 |
type: list |
| 76 |
returned: always |
| 77 |
sample: ["10.1.0.0/24"] |
| 78 |
external_mappings: |
| 79 |
description: |
| 80 |
- The private IP address external mapping for NAT. |
| 81 |
type: list |
| 82 |
returned: always |
| 83 |
sample: ["192.168.1.0/24"] |
| 84 |
ip_configuration_id: |
| 85 |
description: |
| 86 |
- he IP Configuration ID this NAT rule applies to. |
| 87 |
type: str |
| 88 |
returned: always |
| 89 |
sample: "/subscriptions/subid/resourceGroups/rg1/providers/Microsoft.Network/virtualNetworkGateways/gateway1/ipConfigurations/default" |
| 90 |
type_properties_type: |
| 91 |
description: |
| 92 |
- The type of NAT rule for VPN NAT. |
| 93 |
type: str |
| 94 |
returned: always |
| 95 |
sample: Static |
| 96 |
mode: |
| 97 |
description: |
| 98 |
- The Source NAT direction of a VPN NAT. |
| 99 |
type: str |
| 100 |
returned: always |
| 101 |
sample: EgressSnat |
| 102 |
name: |
| 103 |
description: |
| 104 |
- The resource name. |
| 105 |
type: str |
| 106 |
returned: always |
| 107 |
sample: natrule_name |
| 108 |
resource_group: |
| 109 |
description: |
| 110 |
- The resource group name. |
| 111 |
type: str |
| 112 |
returned: always |
| 113 |
sample: testRG |
| 114 |
etag: |
| 115 |
description: |
| 116 |
- A unique read-only string that changes whenever the resource is updated. |
| 117 |
type: str |
| 118 |
returned: always |
| 119 |
sample: b5a32693-2e75-49e0-9137-ded19db658d6 |
| 120 |
provisioning_state: |
| 121 |
description: |
| 122 |
- The provisioning state of the nat rule resource. |
| 123 |
type: str |
| 124 |
returned: always |
| 125 |
sample: Succeeded |
| 126 |
type: |
| 127 |
description: |
| 128 |
- The resource type. |
| 129 |
type: str |
| 130 |
returned: always |
| 131 |
sample: Microsoft.Network/virtualNetworkGateways/natRules |
| 132 |
''' |
| 133 |
|
| 134 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 135 |
|
| 136 |
try: |
| 137 |
from azure.core.exceptions import HttpResponseError |
| 138 |
except Exception: |
| 139 |
|
| 140 |
pass |
| 141 |
|
| 142 |
|
| 143 |
class AzureRMVirtualNetworkGatewayNatRuleInfo(AzureRMModuleBase): |
| 144 |
"""Utility class to get Azure Kubernetes Service Credentials facts""" |
| 145 |
|
| 146 |
def __init__(self): |
| 147 |
|
| 148 |
self.module_args = dict( |
| 149 |
name=dict(type='str'), |
| 150 |
resource_group=dict(type='str', required=True), |
| 151 |
virtual_network_gateway_name=dict(type='str', required=True), |
| 152 |
) |
| 153 |
|
| 154 |
self.name = None |
| 155 |
|
| 156 |
self.results = dict( |
| 157 |
changed=False, |
| 158 |
state=[], |
| 159 |
) |
| 160 |
|
| 161 |
super(AzureRMVirtualNetworkGatewayNatRuleInfo, self).__init__(derived_arg_spec=self.module_args, |
| 162 |
supports_check_mode=True, |
| 163 |
supports_tags=False, |
| 164 |
facts_module=True) |
| 165 |
|
| 166 |
def exec_module(self, **kwargs): |
| 167 |
|
| 168 |
for key in self.module_args: |
| 169 |
setattr(self, key, kwargs[key]) |
| 170 |
|
| 171 |
if self.name is not None: |
| 172 |
self.results['state'] = self.get_by_name() |
| 173 |
else: |
| 174 |
self.results['state'] = self.list_by_virtual_network_gateway() |
| 175 |
|
| 176 |
return self.results |
| 177 |
|
| 178 |
def get_by_name(self): |
| 179 |
"""Gets the nat rule by name""" |
| 180 |
response = None |
| 181 |
|
| 182 |
try: |
| 183 |
response = self.network_client.virtual_network_gateway_nat_rules.get(self.resource_group, self.virtual_network_gateway_name, self.name) |
| 184 |
except HttpResponseError as ec: |
| 185 |
self.log("Gets the nat rule by name got a Exception, Exception as {0}".format(ec)) |
| 186 |
if response: |
| 187 |
return [self.format_response(response)] |
| 188 |
else: |
| 189 |
return [] |
| 190 |
|
| 191 |
def list_by_virtual_network_gateway(self): |
| 192 |
"""Gets all the nat rule in the local network gateway""" |
| 193 |
response = None |
| 194 |
try: |
| 195 |
response = self.network_client.virtual_network_gateway_nat_rules.list_by_virtual_network_gateway(self.resource_group, |
| 196 |
self.virtual_network_gateway_name) |
| 197 |
except HttpResponseError as ec: |
| 198 |
self.log("Gets all nat rule by the local network gateway got Exception, Exception as {0}".format(ec)) |
| 199 |
|
| 200 |
if response: |
| 201 |
return [self.format_response(item) for item in response] |
| 202 |
else: |
| 203 |
return [] |
| 204 |
|
| 205 |
def format_response(self, item): |
| 206 |
result = dict( |
| 207 |
resource_group=self.resource_group, |
| 208 |
id=item.id, |
| 209 |
name=item.name, |
| 210 |
type=item.type, |
| 211 |
etag=item.etag, |
| 212 |
provisioning_state=item.provisioning_state, |
| 213 |
type_properties_type=item.type_properties_type, |
| 214 |
mode=item.mode, |
| 215 |
internal_mappings=list(), |
| 216 |
external_mappings=list(), |
| 217 |
ip_configuration_id=item.ip_configuration_id |
| 218 |
) |
| 219 |
|
| 220 |
if item.internal_mappings is not None: |
| 221 |
for value in item.internal_mappings: |
| 222 |
result['internal_mappings'].append(value.address_space) |
| 223 |
if item.external_mappings is not None: |
| 224 |
for value in item.external_mappings: |
| 225 |
result['external_mappings'].append(value.address_space) |
| 226 |
return result |
| 227 |
|
| 228 |
|
| 229 |
def main(): |
| 230 |
"""Main module execution code path""" |
| 231 |
|
| 232 |
AzureRMVirtualNetworkGatewayNatRuleInfo() |
| 233 |
|
| 234 |
|
| 235 |
if __name__ == '__main__': |
| 236 |
main() |