| 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_appgateway_info |
| 14 |
version_added: "1.10.0" |
| 15 |
short_description: Retrieve Application Gateway instance facts |
| 16 |
description: |
| 17 |
- Get facts for a Application Gateway instance. |
| 18 |
options: |
| 19 |
name: |
| 20 |
description: |
| 21 |
- Only show results for a specific application 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 |
- Ross Bender (@l3ender) |
| 33 |
''' |
| 34 |
|
| 35 |
EXAMPLES = ''' |
| 36 |
- name: Get facts for application gateway by name. |
| 37 |
azure_rm_appgateway_info: |
| 38 |
name: MyAppgw |
| 39 |
resource_group: MyResourceGroup |
| 40 |
|
| 41 |
- name: Get facts for application gateways in resource group. |
| 42 |
azure_rm_appgateway_info: |
| 43 |
resource_group: MyResourceGroup |
| 44 |
|
| 45 |
- name: Get facts for all application gateways. |
| 46 |
azure_rm_appgateway_info: |
| 47 |
''' |
| 48 |
|
| 49 |
RETURN = ''' |
| 50 |
gateways: |
| 51 |
description: |
| 52 |
- A list of dictionaries containing facts for an application gateway. |
| 53 |
returned: always |
| 54 |
type: list |
| 55 |
elements: dict |
| 56 |
contains: |
| 57 |
id: |
| 58 |
description: |
| 59 |
- Application gateway resource ID. |
| 60 |
returned: always |
| 61 |
type: str |
| 62 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/applicationGateways/myAppGw |
| 63 |
name: |
| 64 |
description: |
| 65 |
- Name of application gateway. |
| 66 |
returned: always |
| 67 |
type: str |
| 68 |
sample: myAppGw |
| 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 application gateway. |
| 78 |
returned: always |
| 79 |
type: str |
| 80 |
sample: centralus |
| 81 |
operational_state: |
| 82 |
description: |
| 83 |
- Operating state of application gateway. |
| 84 |
returned: always |
| 85 |
type: str |
| 86 |
sample: Running |
| 87 |
provisioning_state: |
| 88 |
description: |
| 89 |
- Provisioning state of application gateway. |
| 90 |
returned: always |
| 91 |
type: str |
| 92 |
sample: Succeeded |
| 93 |
ssl_policy: |
| 94 |
description: |
| 95 |
- SSL policy of the application gateway. |
| 96 |
returned: always |
| 97 |
type: complex |
| 98 |
version_added: "1.11.0" |
| 99 |
contains: |
| 100 |
policy_type: |
| 101 |
description: |
| 102 |
- The type of SSL policy. |
| 103 |
returned: always |
| 104 |
type: str |
| 105 |
sample: predefined |
| 106 |
policy_name: |
| 107 |
description: |
| 108 |
- The name of the SSL policy. |
| 109 |
returned: always |
| 110 |
type: str |
| 111 |
sample: ssl_policy20170401_s |
| 112 |
''' |
| 113 |
|
| 114 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 115 |
from ansible.module_utils.common.dict_transformations import _camel_to_snake |
| 116 |
|
| 117 |
try: |
| 118 |
from azure.core.exceptions import ResourceNotFoundError |
| 119 |
from azure.mgmt.core.tools import parse_resource_id |
| 120 |
except ImportError: |
| 121 |
|
| 122 |
pass |
| 123 |
|
| 124 |
|
| 125 |
class AzureRMApplicationGatewayInfo(AzureRMModuleBase): |
| 126 |
|
| 127 |
def __init__(self): |
| 128 |
|
| 129 |
self.module_arg_spec = dict( |
| 130 |
name=dict(type='str'), |
| 131 |
resource_group=dict(type='str'), |
| 132 |
) |
| 133 |
|
| 134 |
self.results = dict( |
| 135 |
changed=False, |
| 136 |
) |
| 137 |
|
| 138 |
self.name = None |
| 139 |
self.resource_group = None |
| 140 |
|
| 141 |
super(AzureRMApplicationGatewayInfo, self).__init__(self.module_arg_spec, |
| 142 |
supports_check_mode=True, |
| 143 |
supports_tags=False, |
| 144 |
facts_module=True) |
| 145 |
|
| 146 |
def exec_module(self, **kwargs): |
| 147 |
for key in self.module_arg_spec: |
| 148 |
setattr(self, key, kwargs[key]) |
| 149 |
|
| 150 |
if self.name is not None: |
| 151 |
self.results["gateways"] = self.get() |
| 152 |
elif self.resource_group is not None: |
| 153 |
self.results["gateways"] = self.list_by_rg() |
| 154 |
else: |
| 155 |
self.results["gateways"] = self.list_all() |
| 156 |
|
| 157 |
return self.results |
| 158 |
|
| 159 |
def get(self): |
| 160 |
response = None |
| 161 |
results = [] |
| 162 |
try: |
| 163 |
response = self.network_client.application_gateways.get(resource_group_name=self.resource_group, application_gateway_name=self.name) |
| 164 |
except ResourceNotFoundError: |
| 165 |
pass |
| 166 |
|
| 167 |
if response is not None: |
| 168 |
results.append(self.format_response(response)) |
| 169 |
|
| 170 |
return results |
| 171 |
|
| 172 |
def list_by_rg(self): |
| 173 |
response = None |
| 174 |
results = [] |
| 175 |
try: |
| 176 |
response = self.network_client.application_gateways.list(resource_group_name=self.resource_group) |
| 177 |
except Exception as exc: |
| 178 |
request_id = exc.request_id if exc.request_id else '' |
| 179 |
self.fail("Error listing application gateways in resource groups {0}: {1} - {2}".format(self.resource_group, request_id, str(exc))) |
| 180 |
|
| 181 |
for item in response: |
| 182 |
results.append(self.format_response(item)) |
| 183 |
|
| 184 |
return results |
| 185 |
|
| 186 |
def list_all(self): |
| 187 |
response = None |
| 188 |
results = [] |
| 189 |
try: |
| 190 |
response = self.network_client.application_gateways.list_all() |
| 191 |
except Exception as exc: |
| 192 |
request_id = exc.request_id if exc.request_id else '' |
| 193 |
self.fail("Error listing all application gateways: {0} - {1}".format(request_id, str(exc))) |
| 194 |
|
| 195 |
for item in response: |
| 196 |
results.append(self.format_response(item)) |
| 197 |
|
| 198 |
return results |
| 199 |
|
| 200 |
def format_response(self, appgw): |
| 201 |
d = appgw.as_dict() |
| 202 |
id = d.get("id") |
| 203 |
id_dict = parse_resource_id(id) |
| 204 |
d = { |
| 205 |
"id": id, |
| 206 |
"name": d.get("name"), |
| 207 |
"resource_group": id_dict.get('resource_group', self.resource_group), |
| 208 |
"location": d.get("location"), |
| 209 |
"operational_state": d.get("operational_state"), |
| 210 |
"provisioning_state": d.get("provisioning_state"), |
| 211 |
"ssl_policy": None if d.get("ssl_policy") is None else { |
| 212 |
"policy_type": _camel_to_snake(d.get("ssl_policy").get("policy_type", None)), |
| 213 |
"policy_name": self.ssl_policy_name(d.get("ssl_policy").get("policy_name", None)), |
| 214 |
}, |
| 215 |
} |
| 216 |
return d |
| 217 |
|
| 218 |
def ssl_policy_name(self, policy_name): |
| 219 |
if policy_name == "AppGwSslPolicy20150501": |
| 220 |
return "ssl_policy20150501" |
| 221 |
elif policy_name == "AppGwSslPolicy20170401": |
| 222 |
return "ssl_policy20170401" |
| 223 |
elif policy_name == "AppGwSslPolicy20170401S": |
| 224 |
return "ssl_policy20170401_s" |
| 225 |
return None |
| 226 |
|
| 227 |
|
| 228 |
def main(): |
| 229 |
AzureRMApplicationGatewayInfo() |
| 230 |
|
| 231 |
|
| 232 |
if __name__ == '__main__': |
| 233 |
main() |