| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright: Contributors to the Ansible project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import typing |
| 9 |
|
| 10 |
if typing.TYPE_CHECKING: |
| 11 |
from typing import Dict |
| 12 |
from typing import List |
| 13 |
|
| 14 |
from ..transformation import AnsibleAWSResource |
| 15 |
from ..transformation import AnsibleAWSResourceList |
| 16 |
from ..transformation import BotoResource |
| 17 |
from ..transformation import BotoResourceList |
| 18 |
|
| 19 |
from ..tagging import boto3_tag_list_to_ansible_dict |
| 20 |
from ..transformation import boto3_resource_list_to_ansible_dict |
| 21 |
from ..transformation import boto3_resource_to_ansible_dict |
| 22 |
|
| 23 |
|
| 24 |
def _normalize_attributes(attributes: BotoResourceList) -> Dict[str, str]: |
| 25 |
""" |
| 26 |
Normalize ELB load balancer attributes from boto3 format to Ansible format. |
| 27 |
|
| 28 |
Converts attributes from the list of Key/Value dicts returned by |
| 29 |
describe_load_balancer_attributes to a flat dict with underscored keys. |
| 30 |
|
| 31 |
Parameters: |
| 32 |
attributes (list): List of attribute dicts from boto3, format: |
| 33 |
[{"Key": "access_logs.s3.enabled", "Value": "true"}, ...] |
| 34 |
|
| 35 |
Returns: |
| 36 |
Dict with underscored keys and string values: |
| 37 |
{"access_logs_s3_enabled": "true", ...} |
| 38 |
""" |
| 39 |
if not attributes: |
| 40 |
return {} |
| 41 |
|
| 42 |
# Convert from boto3 format [{"Key": "...", "Value": "..."}] to dict |
| 43 |
attrs_dict = boto3_tag_list_to_ansible_dict(attributes) |
| 44 |
|
| 45 |
# Replace '.' with '_' in attribute keys to make it more Ansibley |
| 46 |
return {k.replace(".", "_"): v for k, v in attrs_dict.items()} |
| 47 |
|
| 48 |
|
| 49 |
def _sort_actions(actions: BotoResourceList) -> BotoResourceList: |
| 50 |
""" |
| 51 |
Sort ELB listener actions by Order and Type. |
| 52 |
|
| 53 |
Sorts actions by Order field (defaulting to 0), then by Type for stability. |
| 54 |
This ensures consistent ordering and avoids comparing dict/None which causes TypeError. |
| 55 |
|
| 56 |
Parameters: |
| 57 |
actions (list): List of action dicts from boto3 |
| 58 |
|
| 59 |
Returns: |
| 60 |
List of actions sorted by Order and Type |
| 61 |
""" |
| 62 |
if not actions: |
| 63 |
return actions |
| 64 |
|
| 65 |
return sorted( |
| 66 |
actions, |
| 67 |
key=lambda x: ( |
| 68 |
x.get("Order", 0), |
| 69 |
x.get("Type", ""), |
| 70 |
), |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
def _normalize_listener_actions(actions: BotoResourceList) -> AnsibleAWSResourceList: |
| 75 |
""" |
| 76 |
Normalize and sort ELB listener actions by Order and Type. |
| 77 |
|
| 78 |
Converts actions from the CamelCase boto3 format to the snake_case Ansible format |
| 79 |
and sorts them by Order (defaulting to 0) then Type for stability. |
| 80 |
|
| 81 |
Parameters: |
| 82 |
actions (list): List of listener actions from boto3 |
| 83 |
|
| 84 |
Returns: |
| 85 |
List of actions in Ansible format, sorted by Order and Type |
| 86 |
""" |
| 87 |
if not actions: |
| 88 |
return actions |
| 89 |
|
| 90 |
# Sort actions before conversion |
| 91 |
sorted_actions = _sort_actions(actions) |
| 92 |
|
| 93 |
# Convert to Ansible dict format (force_tags=False to prevent automatic tags field) |
| 94 |
return boto3_resource_list_to_ansible_dict(sorted_actions, force_tags=False) |
| 95 |
|
| 96 |
|
| 97 |
def _normalize_listener_rules(rules: BotoResourceList) -> AnsibleAWSResourceList: |
| 98 |
""" |
| 99 |
Normalize and sort ELB listener rules by priority. |
| 100 |
|
| 101 |
Converts listener rules from the CamelCase boto3 format to the snake_case Ansible format |
| 102 |
and sorts them numerically by priority, with the "default" rule appearing last. |
| 103 |
Actions within each rule are also sorted by Order and Type. |
| 104 |
|
| 105 |
Parameters: |
| 106 |
rules (list): List of listener rules from boto3 |
| 107 |
|
| 108 |
Returns: |
| 109 |
List of rules in Ansible format, sorted by priority with sorted actions |
| 110 |
""" |
| 111 |
if not rules: |
| 112 |
return rules |
| 113 |
|
| 114 |
# First sort the rules by Priority (while still in CamelCase) |
| 115 |
def sort_key(rule): |
| 116 |
priority = rule.get("Priority", "default") |
| 117 |
if priority == "default": |
| 118 |
# Put default rule last by using a very large number |
| 119 |
return float("inf") |
| 120 |
try: |
| 121 |
return int(priority) |
| 122 |
except (ValueError, TypeError): |
| 123 |
# Fallback for unexpected priority values |
| 124 |
return float("inf") |
| 125 |
|
| 126 |
sorted_rules = sorted(rules, key=sort_key) |
| 127 |
|
| 128 |
# Convert to Ansible dict format, also sorting Actions within each rule |
| 129 |
# (force_tags=False to prevent automatic tags field) |
| 130 |
transforms = {"Actions": _normalize_listener_actions} |
| 131 |
return [ |
| 132 |
boto3_resource_to_ansible_dict(rule, nested_transforms=transforms, force_tags=False) for rule in sorted_rules |
| 133 |
] |
| 134 |
|
| 135 |
|
| 136 |
def _normalize_listeners(listeners: BotoResourceList) -> AnsibleAWSResourceList: |
| 137 |
""" |
| 138 |
Normalize ELB listeners. |
| 139 |
|
| 140 |
Converts listeners from boto3 format to Ansible format, applying action and rule sorting. |
| 141 |
DefaultActions are sorted by Order and Type for consistent output. |
| 142 |
|
| 143 |
Parameters: |
| 144 |
listeners (list): List of listeners from boto3 |
| 145 |
|
| 146 |
Returns: |
| 147 |
List of listeners in Ansible format with sorted default actions and sorted rules |
| 148 |
""" |
| 149 |
if not listeners: |
| 150 |
return listeners |
| 151 |
|
| 152 |
# Transform each listener, applying nested transforms to DefaultActions and Rules |
| 153 |
# (force_tags=False to prevent automatic tags field) |
| 154 |
transforms = { |
| 155 |
"DefaultActions": _normalize_listener_actions, |
| 156 |
"Rules": _normalize_listener_rules, |
| 157 |
} |
| 158 |
return [ |
| 159 |
boto3_resource_to_ansible_dict(listener, nested_transforms=transforms, force_tags=False) |
| 160 |
for listener in listeners |
| 161 |
] |
| 162 |
|
| 163 |
|
| 164 |
def normalize_application_load_balancer(alb: BotoResource) -> AnsibleAWSResource: |
| 165 |
""" |
| 166 |
Normalize an Application Load Balancer from boto3 format to Ansible format. |
| 167 |
|
| 168 |
Handles conversion of the ALB and its nested listeners and rules. Listener rules |
| 169 |
are sorted by priority with the "default" rule appearing last. |
| 170 |
|
| 171 |
If the ALB dict contains an "Attributes" key (from describe_load_balancer_attributes), |
| 172 |
the attributes will be converted and merged as flat fields into the result. |
| 173 |
|
| 174 |
Parameters: |
| 175 |
alb (dict): Application Load Balancer dict in boto3 format |
| 176 |
|
| 177 |
Returns: |
| 178 |
Normalized ALB dict in Ansible format |
| 179 |
""" |
| 180 |
if not alb: |
| 181 |
return alb |
| 182 |
|
| 183 |
# Make a copy to avoid modifying the original |
| 184 |
alb_copy = alb.copy() |
| 185 |
|
| 186 |
# Extract and normalize attributes separately (they get flattened into the result) |
| 187 |
attributes = None |
| 188 |
if "Attributes" in alb_copy: |
| 189 |
attributes = _normalize_attributes(alb_copy.pop("Attributes")) |
| 190 |
|
| 191 |
# Transform the ALB, applying nested transforms to Listeners |
| 192 |
# force_tags=False because tags require a separate API call and should be added by caller |
| 193 |
transforms = {"Listeners": _normalize_listeners} |
| 194 |
result = boto3_resource_to_ansible_dict(alb_copy, nested_transforms=transforms, force_tags=False) |
| 195 |
|
| 196 |
# Merge normalized attributes as flat fields |
| 197 |
if attributes: |
| 198 |
result.update(attributes) |
| 199 |
|
| 200 |
return result |