| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: azure_rm_afdrules_info |
| 15 |
version_added: "3.4.0" |
| 16 |
short_description: Get Azure Front Door Rule facts to be used with Standard or Premium Frontdoor Service |
| 17 |
description: |
| 18 |
- Get facts for a specific Azure Front Door (AFD) Rule or all AFD Rules in a Ruleset. |
| 19 |
- This differs from the Front Door classic service and only is intended to be used by the Standard or Premium service offering. |
| 20 |
|
| 21 |
options: |
| 22 |
name: |
| 23 |
description: |
| 24 |
- Name of the delivery rule which is unique within the endpoint. |
| 25 |
type: str |
| 26 |
profile_name: |
| 27 |
description: |
| 28 |
- Name of the Azure Front Door Standard or Azure Front Door Premium profile which is unique within the resource group |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
resource_group: |
| 32 |
description: |
| 33 |
- Name of the Resource group within the Azure subscription. |
| 34 |
required: true |
| 35 |
type: str |
| 36 |
rule_set_name: |
| 37 |
description: |
| 38 |
- Name of the rule set under the profile. |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
|
| 42 |
extends_documentation_fragment: |
| 43 |
- azure.azcollection.azure |
| 44 |
|
| 45 |
author: |
| 46 |
- Jarret Tooley (@jartoo) |
| 47 |
''' |
| 48 |
|
| 49 |
EXAMPLES = ''' |
| 50 |
- name: Get facts for all Rules in the AFD Profile |
| 51 |
azure_rm_afdrule_info: |
| 52 |
rule_set_name: myRuleset |
| 53 |
profile_name: myProfile |
| 54 |
resource_group: myResourceGroup |
| 55 |
|
| 56 |
- name: Get facts of specific AFD Rule |
| 57 |
azure_rm_afdrule_info: |
| 58 |
name: myRule1 |
| 59 |
rule_set_name: myRuleset |
| 60 |
profile_name: myProfile |
| 61 |
resource_group: myResourceGroup |
| 62 |
''' |
| 63 |
|
| 64 |
RETURN = ''' |
| 65 |
afdrules: |
| 66 |
description: List of AFD Rules. |
| 67 |
returned: always |
| 68 |
type: complex |
| 69 |
contains: |
| 70 |
actions: |
| 71 |
description: |
| 72 |
- A list of actions that are executed when all the conditions of a rule are satisfied. |
| 73 |
type: list |
| 74 |
elements: dict |
| 75 |
contains: |
| 76 |
cache_behavior: |
| 77 |
description: |
| 78 |
- Caching behavior for the requests. |
| 79 |
type: str |
| 80 |
cache_duration: |
| 81 |
description: |
| 82 |
- The duration for which the content needs to be cached. Allowed format is [d.]hh:mm:ss. |
| 83 |
type: str |
| 84 |
custom_fragment: |
| 85 |
description: |
| 86 |
- Fragment to add to the redirect URL. Fragment is the part of the URL that comes after #. Do not include the # |
| 87 |
type: str |
| 88 |
custom_hostname: |
| 89 |
description: |
| 90 |
- Host to redirect. Leave empty to use the incoming host as the destination host. |
| 91 |
type: str |
| 92 |
custom_path: |
| 93 |
description: |
| 94 |
- The full path to redirect. Path cannot be empty and must start with /. Leave empty to use the incoming path as destination path. |
| 95 |
type: str |
| 96 |
custom_query_string: |
| 97 |
description: |
| 98 |
- The set of query strings to be placed in the redirect URL. |
| 99 |
- Setting this value would replace any existing query string; leave empty to preserve the incoming query string. |
| 100 |
- Query string must be in <key>=:code:<value> format. ? and & will be added automatically so do not include them. |
| 101 |
type: str |
| 102 |
destination: |
| 103 |
description: |
| 104 |
- Define the relative URL to which the above requests will be rewritten by. |
| 105 |
type: str |
| 106 |
destination_protocol: |
| 107 |
description: |
| 108 |
- Protocol to use for the redirect. |
| 109 |
type: str |
| 110 |
forwarding_protocol: |
| 111 |
description: |
| 112 |
- Protocol this rule will use when forwarding traffic to backends. |
| 113 |
type: str |
| 114 |
header_action: |
| 115 |
description: |
| 116 |
- Action to perform. |
| 117 |
type: str |
| 118 |
header_name: |
| 119 |
description: |
| 120 |
- Name of the header to modify. |
| 121 |
type: str |
| 122 |
is_compression_enabled: |
| 123 |
description: |
| 124 |
- The caching configuration for this route. Indicates whether content compression is enabled on AzureFrontDoor. |
| 125 |
- If compression is enabled, content will be served as compressed if user requests for a compressed version. |
| 126 |
- Content won't be compressed on AzureFrontDoor when requested content is smaller than 1 byte or larger than 1 MB. |
| 127 |
type: str |
| 128 |
name: |
| 129 |
description: |
| 130 |
- The name of the action for the delivery rule. |
| 131 |
type: str |
| 132 |
origin_group: |
| 133 |
description: |
| 134 |
- defines the OriginGroup that would override the DefaultOriginGroup. |
| 135 |
type: str |
| 136 |
preserve_unmatched_path: |
| 137 |
description: |
| 138 |
- Whether to preserve unmatched path. |
| 139 |
type: bool |
| 140 |
query_parameters: |
| 141 |
description: |
| 142 |
- query parameters to include or exclude (comma separated). |
| 143 |
type: str |
| 144 |
query_string_caching_behavior: |
| 145 |
description: |
| 146 |
- The caching configuration for this route. Defines how Frontdoor caches requests that include query strings. |
| 147 |
- You can ignore any query strings when caching, ignore specific query strings, cache every request with a unique URL, |
| 148 |
- or cache specific query strings. |
| 149 |
type: str |
| 150 |
redirect_type: |
| 151 |
description: |
| 152 |
- The redirect type the rule will use when redirecting traffic. |
| 153 |
type: str |
| 154 |
source_pattern: |
| 155 |
description: |
| 156 |
- Define a request URI pattern that identifies the type of requests that may be rewritten. |
| 157 |
- If value is blank, all strings are matched. |
| 158 |
type: str |
| 159 |
value: |
| 160 |
description: |
| 161 |
- Value for the specified action. |
| 162 |
type: str |
| 163 |
conditions: |
| 164 |
description: |
| 165 |
- A list of conditions that must be matched for the actions to be executed. |
| 166 |
type: list |
| 167 |
elements: dict |
| 168 |
contains: |
| 169 |
name: |
| 170 |
description: |
| 171 |
- The name of the condition for the delivery rule. |
| 172 |
type: str |
| 173 |
operator: |
| 174 |
description: |
| 175 |
- Describes operator to be matched. |
| 176 |
type: str |
| 177 |
match_values: |
| 178 |
description: |
| 179 |
- The match value for the condition of the delivery rule. |
| 180 |
type: list |
| 181 |
elements: str |
| 182 |
negate_condition: |
| 183 |
description: |
| 184 |
- Describes if this is a negate condition or not. |
| 185 |
type: bool |
| 186 |
selector: |
| 187 |
description: |
| 188 |
- Name of item to be matched. |
| 189 |
type: str |
| 190 |
transforms: |
| 191 |
description: |
| 192 |
- List of transforms. |
| 193 |
type: list |
| 194 |
elements: str |
| 195 |
match_processing_behavior: |
| 196 |
description: |
| 197 |
- If this rule is a match should the rules engine continue running the remaining rules or stop. |
| 198 |
type: str |
| 199 |
name: |
| 200 |
description: |
| 201 |
- Name of the delivery rule which is unique within the endpoint. |
| 202 |
type: str |
| 203 |
order: |
| 204 |
description: |
| 205 |
- The order in which the rules are applied for the endpoint. |
| 206 |
- A rule with a lesser order will be applied before a rule with a greater order. Rule with order 0 is a special rule. |
| 207 |
- It does not require any condition and actions listed in it will always be applied. |
| 208 |
type: int |
| 209 |
profile_name: |
| 210 |
description: |
| 211 |
- Name of the Azure Front Door Standard or Azure Front Door Premium profile which is unique within the resource group. |
| 212 |
type: str |
| 213 |
resource_group: |
| 214 |
description: |
| 215 |
- Name of the Resource group within the Azure subscription. |
| 216 |
type: str |
| 217 |
rule_set_name: |
| 218 |
description: |
| 219 |
- Name of the rule set under the profile. |
| 220 |
type: str |
| 221 |
state: |
| 222 |
description: |
| 223 |
- Assert the state of the CDN profile. Use C(present) to create or update a CDN profile and C(absent) to delete it. |
| 224 |
type: str |
| 225 |
type: |
| 226 |
description: |
| 227 |
- Resource type. |
| 228 |
type: str |
| 229 |
''' |
| 230 |
|
| 231 |
|
| 232 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 233 |
|
| 234 |
|
| 235 |
AZURE_OBJECT_CLASS = 'AFDRules' |
| 236 |
|
| 237 |
|
| 238 |
class AzureRMAFDRuleInfo(AzureRMModuleBase): |
| 239 |
"""Utility class to get Azure AFD Rule facts""" |
| 240 |
|
| 241 |
def __init__(self): |
| 242 |
|
| 243 |
self.module_args = dict( |
| 244 |
name=dict(type='str'), |
| 245 |
profile_name=dict(type='str', required=True), |
| 246 |
resource_group=dict(type='str', required=True), |
| 247 |
rule_set_name=dict(type='str', required=True) |
| 248 |
) |
| 249 |
|
| 250 |
self.results = dict( |
| 251 |
changed=False, |
| 252 |
afdrules=[] |
| 253 |
) |
| 254 |
|
| 255 |
self.name = None |
| 256 |
self.profile_name = None |
| 257 |
self.resource_group = None |
| 258 |
self.rule_set_name = None |
| 259 |
|
| 260 |
super(AzureRMAFDRuleInfo, self).__init__( |
| 261 |
supports_check_mode=True, |
| 262 |
derived_arg_spec=self.module_args, |
| 263 |
supports_tags=False, |
| 264 |
facts_module=True |
| 265 |
) |
| 266 |
|
| 267 |
def exec_module(self, **kwargs): |
| 268 |
|
| 269 |
for key in self.module_args: |
| 270 |
setattr(self, key, kwargs[key]) |
| 271 |
|
| 272 |
if self.name: |
| 273 |
self.results['afdrules'] = self.get_item() |
| 274 |
else: |
| 275 |
self.results['afdrules'] = self.list_by_endpoint() |
| 276 |
|
| 277 |
return self.results |
| 278 |
|
| 279 |
def get_item(self): |
| 280 |
"""Get a single Azure AFD Rule""" |
| 281 |
|
| 282 |
self.log('Get properties for {0}'.format(self.name)) |
| 283 |
|
| 284 |
item = None |
| 285 |
result = [] |
| 286 |
|
| 287 |
try: |
| 288 |
item = self.cdn_client.rules.get(resource_group_name=self.resource_group, |
| 289 |
profile_name=self.profile_name, |
| 290 |
rule_set_name=self.rule_set_name, |
| 291 |
rule_name=self.name) |
| 292 |
except Exception as exc: |
| 293 |
self.log("Failed to find an existing resource. {0}".format(str(exc))) |
| 294 |
|
| 295 |
if item: |
| 296 |
result = [self.serialize_afdrule(item)] |
| 297 |
|
| 298 |
return result |
| 299 |
|
| 300 |
def list_by_endpoint(self): |
| 301 |
"""Get all Azure AFD Rules within an AFD profile""" |
| 302 |
|
| 303 |
self.log('List all AFD Rules within an AFD profile') |
| 304 |
|
| 305 |
try: |
| 306 |
response = self.cdn_client.rules.list_by_rule_set(resource_group_name=self.resource_group, |
| 307 |
profile_name=self.profile_name, |
| 308 |
rule_set_name=self.rule_set_name) |
| 309 |
except Exception as exc: |
| 310 |
self.fail('Failed to list all items - {0}'.format(str(exc))) |
| 311 |
|
| 312 |
results = [] |
| 313 |
for item in response: |
| 314 |
results.append(self.serialize_afdrule(item)) |
| 315 |
|
| 316 |
return results |
| 317 |
|
| 318 |
def serialize_afdrule(self, rules): |
| 319 |
''' |
| 320 |
Convert a AFD Rule object to dict. |
| 321 |
:param afdrule: AFD Rule object |
| 322 |
:return: dict |
| 323 |
''' |
| 324 |
return dict( |
| 325 |
actions=parse_action_condition(rules.actions), |
| 326 |
conditions=parse_action_condition(rules.conditions), |
| 327 |
deployment_status=rules.deployment_status, |
| 328 |
id=rules.id, |
| 329 |
match_processing_behavior=rules.match_processing_behavior, |
| 330 |
name=rules.name, |
| 331 |
order=rules.order, |
| 332 |
provisioning_state=rules.provisioning_state, |
| 333 |
rule_set_name=rules.rule_set_name, |
| 334 |
type=rules.type |
| 335 |
) |
| 336 |
|
| 337 |
|
| 338 |
def parse_action_condition(items): |
| 339 |
''' Convert Actions and Conditions objects to list ''' |
| 340 |
|
| 341 |
parsed = [] |
| 342 |
for item in items: |
| 343 |
new_item = {} |
| 344 |
vitem = vars(item) |
| 345 |
for field in vitem: |
| 346 |
if field == 'parameters': |
| 347 |
subvitem = vars(vitem.get('parameters')) |
| 348 |
for subfield in subvitem: |
| 349 |
if subfield == 'origin_group_override': |
| 350 |
ogo = subvitem["origin_group_override"] |
| 351 |
new_item["forwarding_protocol"] = None |
| 352 |
new_item['origin_group_id'] = None |
| 353 |
if ogo: |
| 354 |
new_item["forwarding_protocol"] = ogo.forwarding_protocol |
| 355 |
og = ogo.origin_group |
| 356 |
if og: |
| 357 |
new_item['origin_group_id'] = og.id |
| 358 |
elif subfield == 'cache_configuration': |
| 359 |
cc = subvitem["cache_configuration"] |
| 360 |
new_item["query_string_caching_behavior"] = None |
| 361 |
new_item['query_parameters'] = None |
| 362 |
new_item['is_compression_enabled'] = None |
| 363 |
new_item['cache_behavior'] = None |
| 364 |
new_item['cache_duration'] = None |
| 365 |
if cc: |
| 366 |
new_item["query_string_caching_behavior"] = ogo.query_string_caching_behavior |
| 367 |
new_item["query_parameters"] = ogo.query_parameters |
| 368 |
new_item["is_compression_enabled"] = ogo.is_compression_enabled |
| 369 |
new_item["cache_behavior"] = ogo.cache_behavior |
| 370 |
new_item["cache_duration"] = ogo.cache_duration |
| 371 |
elif subfield != 'additional_properties': |
| 372 |
new_item[subfield] = subvitem[subfield] |
| 373 |
elif field != 'additional_properties': |
| 374 |
new_item[field] = vitem[field] |
| 375 |
parsed.append(new_item) |
| 376 |
return parsed |
| 377 |
|
| 378 |
|
| 379 |
def main(): |
| 380 |
"""Main module execution code path""" |
| 381 |
AzureRMAFDRuleInfo() |
| 382 |
|
| 383 |
|
| 384 |
if __name__ == '__main__': |
| 385 |
main() |