Skip to content

Commit e1c95d0

Browse files
committed
chore: adjust code pattern
1 parent 50f470f commit e1c95d0

8 files changed

+103
-27
lines changed

src/akamai/converter.py

+45-12
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def process_resource(azion_resources: AzionResource, resource: Dict[str, Any]):
3535
return []
3636

3737
# Extract edge_hostname and origin_hostname
38-
main_setting_name = global_settings.get("attributes", {}).get("main_setting_name", None)
38+
main_setting_name = global_settings.get("attributes", {}).get("main_setting_name")
3939
edge_hostname = global_settings.get("attributes", {}).get("edge_hostname", "")
4040
origin_hostname = global_settings.get("attributes", {}).get("origin_hostname", "")
4141

@@ -51,11 +51,13 @@ def process_resource(azion_resources: AzionResource, resource: Dict[str, Any]):
5151
for instance_name, instance_data in resource_data.items():
5252
logging.info(f"Processing Akamai instance: {instance_name}")
5353
try:
54-
convert_akamai_to_azion(azion_resources,
55-
instance_data,
56-
main_setting_name,
57-
edge_hostname,
58-
origin_hostname)
54+
convert_akamai_to_azion(
55+
azion_resources,
56+
instance_data,
57+
main_setting_name,
58+
edge_hostname,
59+
origin_hostname
60+
)
5961
except KeyError as e:
6062
logging.error(f"Missing expected key during processing of {instance_name}: {e}")
6163
except TypeError as e:
@@ -124,7 +126,14 @@ def process_rules(azion_resources: AzionResource, rules: Any, main_setting_name:
124126
logging.info("[Akamai Rules] Processing rules finished.")
125127

126128

127-
def process_rule_behaviors(azion_resources: AzionResource, rule: Dict[str, Any], main_setting_name: str, origin_hostname: str, index: int, normalized_name: str):
129+
def process_rule_behaviors(
130+
azion_resources: AzionResource,
131+
rule: Dict[str, Any],
132+
main_setting_name: str,
133+
origin_hostname: str,
134+
index: int,
135+
normalized_name: str
136+
):
128137
"""
129138
Processes the list of behaviors rules and converts them into Azion resources.
130139
@@ -134,7 +143,7 @@ def process_rule_behaviors(azion_resources: AzionResource, rule: Dict[str, Any],
134143
main_setting_name (str): The main setting name for Azion configuration.
135144
origin_hostname (str): The origin hostname for Azion configuration.
136145
"""
137-
behaviors = rule.get("behaviors", None)
146+
behaviors = rule.get("behaviors")
138147
if behaviors is None:
139148
logging.warning("[Akamai Rules] No behaviors found in rules. Skipping rule processing.")
140149
return
@@ -183,7 +192,14 @@ def process_rule_behaviors(azion_resources: AzionResource, rule: Dict[str, Any],
183192
logging.info(f"[Akamai Rules] Processing behaviors for rules '{normalized_name}'. Finished.")
184193

185194

186-
def process_rule_children(azion_resources: AzionResource, children: List[Dict[str, Any]], main_setting_name: str, origin_hostname: str, parent_rule_index: int, parent_rule_name: str):
195+
def process_rule_children(
196+
azion_resources: AzionResource,
197+
children: List[Dict[str, Any]],
198+
main_setting_name: str,
199+
origin_hostname: str,
200+
parent_rule_index: int,
201+
parent_rule_name: str
202+
):
187203
"""
188204
Processes the list of children rules and converts them into Azion resources.
189205
@@ -210,7 +226,12 @@ def process_rule_children(azion_resources: AzionResource, children: List[Dict[st
210226
context["rule_name"] = rule_name
211227
context["rule_index"] = child_index
212228

213-
logging.info(f"[Akamai Rules][Children] Rule name: '{rule_name}', parent rule: '{parent_rule_name}', parent_index: {parent_rule_index}, index: {child_index}")
229+
logging.info(
230+
f"[Akamai Rules][Children] Rule name: '{rule_name}', "
231+
f"parent rule: '{parent_rule_name}', "
232+
f"parent_index: {parent_rule_index}, "
233+
f"index: {child_index}"
234+
)
214235
try:
215236
behaviors = rule.get("behaviors", [])
216237
for behavior in behaviors:
@@ -270,7 +291,12 @@ def process_rule_children(azion_resources: AzionResource, children: List[Dict[st
270291
logging.info(f"[Akamai Rules][Children] Processing children rules from rule '{parent_rule_name}'. Finished.")
271292

272293

273-
def create_main_resources(azion_resources: AzionResource, attributes: Dict[str, Any], main_setting_name: str, origin_hostname: str):
294+
def create_main_resources(
295+
azion_resources: AzionResource,
296+
attributes: Dict[str, Any],
297+
main_setting_name: str,
298+
origin_hostname: str
299+
):
274300
"""
275301
Creates the main setting, origin, and domain resources.
276302
@@ -291,6 +317,7 @@ def create_main_resources(azion_resources: AzionResource, attributes: Dict[str,
291317
logging.error(f"Error creating main resources: {e}")
292318
raise
293319

320+
294321
def process_waf_behavior(azion_resources: AzionResource, attributes: Dict[str, Any]):
295322
"""
296323
Adds WAF rule to Azion resources if available.
@@ -308,7 +335,13 @@ def process_waf_behavior(azion_resources: AzionResource, attributes: Dict[str, A
308335
logging.error(f"Error processing WAF rule: {e}")
309336

310337

311-
def convert_akamai_to_azion(azion_resources: AzionResource, attributes: Dict[str, Any], main_setting_name: str, edge_hostname: str, origin_hostname: str):
338+
def convert_akamai_to_azion(
339+
azion_resources: AzionResource,
340+
attributes: Dict[str, Any],
341+
main_setting_name: str,
342+
edge_hostname: str,
343+
origin_hostname: str
344+
):
312345
"""
313346
Converts Akamai property to Azion resources, including handling rules of different formats.
314347

src/akamai/converter_cache_settings.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ def map_allow_behavior_to_azion(allow_behavior: str, ttl: int) -> Dict[str, Any]
4545
return cache_settings
4646

4747

48-
def create_cache_setting(azion_resources: AzionResource, rules: List[Dict[str, Any]], main_setting_name: str, cache_name: Optional[str] = None) -> Optional[Dict[str, Any]]:
48+
def create_cache_setting(
49+
azion_resources: AzionResource,
50+
rules: List[Dict[str, Any]],
51+
main_setting_name: str,
52+
cache_name: Optional[str] = None
53+
) -> Optional[Dict[str, Any]]:
4954
"""
5055
Creates a single Azion cache setting resource.
5156

src/akamai/converter_digital_certificate.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
from azion_resources import AzionResource
55

66

7-
def create_digital_certificate(azion_resources: AzionResource, custom_certificates: List[Dict[str, Any]]) -> Optional[Dict[str, Any]]:
7+
def create_digital_certificate(
8+
azion_resources: AzionResource,
9+
custom_certificates: List[Dict[str, Any]]
10+
) -> Optional[Dict[str, Any]]:
811
"""
912
Creates a Digital Certificate resource in Azion based on Akamai customCertificates.
1013

src/akamai/converter_domain.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
from azion_resources import AzionResource
55

6-
def create_domain(azion_resources: AzionResource, attributes: Dict[str, Any], main_setting_name: str) -> Optional[Dict[str, Any]]:
6+
def create_domain(
7+
azion_resources: AzionResource,
8+
attributes: Dict[str, Any],
9+
main_setting_name: str
10+
) -> Optional[Dict[str, Any]]:
711
"""
812
Creates the Azion domain resource from Akamai attributes.
913
@@ -18,7 +22,7 @@ def create_domain(azion_resources: AzionResource, attributes: Dict[str, Any], ma
1822
logging.info("Processing domains.")
1923

2024
# Extract and validate 'hostnames'
21-
hostnames = attributes.get("hostnames", None)
25+
hostnames = attributes.get("hostnames")
2226
if not hostnames:
2327
logging.warning("Hostname session not found in the configuration.")
2428
return None

src/akamai/converter_main_settings.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ def validate_and_apply_defaults(attributes: Dict[str, Any], defaults: Dict[str,
4848

4949
return validated
5050

51-
def create_main_setting(azion_resources: AzionResource, attributes: Dict[str, Any], main_setting_name: str) -> Optional[Dict[str, Any]]:
51+
def create_main_setting(
52+
azion_resources: AzionResource,
53+
attributes: Dict[str, Any],
54+
main_setting_name: str
55+
) -> Optional[Dict[str, Any]]:
5256
"""
5357
Creates the main application setting resource for Azion.
5458

src/akamai/mapping.py

+34-6
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@
7575
#"cloudletsOrigin": {"azion_condition": "$${upstream_addr}", "azion_operator": "matches"},
7676

7777
# Response Phase Variables
78-
"responseHeader": {"azion_condition": "$${sent_http_header}", "azion_operator": "matches", "phase": "response", "name": "filter_response_header"},
78+
"responseHeader": {
79+
"azion_condition": "$${sent_http_header}",
80+
"azion_operator": "matches",
81+
"phase": "response",
82+
"name": "filter_response_header"
83+
},
7984
"statusCode": {"azion_condition": "$${status}", "azion_operator": "matches", "phase": "response"},
8085
"upstreamAddress": {"azion_condition": "$${upstream_addr}", "azion_operator": "matches", "phase": "response"},
8186
"upstreamCookie": {"azion_condition": "$${upstream_cookie_name}", "azion_operator": "matches", "phase": "response"},
@@ -128,9 +133,22 @@
128133
#},
129134

130135
# Cookies
131-
"modifyOutgoingResponseCookie": {"azion_behavior": "set_cookie", "target": {"name": "cookie_name", "value": "cookie_value"}, "phase": "response", "akamai_behavior": "modifyOutgoingResponseCookie"},
132-
"modifyIncomingRequestCookie": {"azion_behavior": "add_request_cookie", "target": {"name": "cookie_name", "value": "cookie_value"}},
133-
"removeResponseCookie": {"azion_behavior": "filter_response_cookie", "target": "cookie_name", "phase": "response", "akamai_behavior": "removeResponseCookie"},
136+
"modifyOutgoingResponseCookie": {
137+
"azion_behavior": "set_cookie",
138+
"target": {"name": "cookie_name", "value": "cookie_value"},
139+
"phase": "response",
140+
"akamai_behavior": "modifyOutgoingResponseCookie"
141+
},
142+
"modifyIncomingRequestCookie": {
143+
"azion_behavior": "add_request_cookie",
144+
"target": {"name": "cookie_name", "value": "cookie_value"}
145+
},
146+
"removeResponseCookie": {
147+
"azion_behavior": "filter_response_cookie",
148+
"target": "cookie_name",
149+
"phase": "response",
150+
"akamai_behavior": "removeResponseCookie"
151+
},
134152
"removeRequestCookie": {"azion_behavior": "filter_request_cookie", "target": "cookie_name"},
135153
"forwardCookies": {"azion_behavior": "forward_cookies"},
136154
"cookies": {
@@ -146,15 +164,25 @@
146164
"phase": "response",
147165
"akamai_behavior": "modifyOutgoingResponseHeader"
148166
},
149-
"removeOutgoingResponseHeader": {"azion_behavior": "filter_response_header", "target": "header_name", "phase": "response", "akamai_behavior": "removeOutgoingResponseHeader"},
167+
"removeOutgoingResponseHeader": {
168+
"azion_behavior": "filter_response_header",
169+
"target": "header_name",
170+
"phase": "response",
171+
"akamai_behavior": "removeOutgoingResponseHeader"
172+
},
150173
"allowTransferEncoding": {
151174
"azion_behavior": lambda options: None if options.get("enabled", True) else "filter_request_header",
152175
"target": {
153176
"name": "Transfer-Encoding",
154177
"target": "Transfer-Encoding"
155178
}
156179
},
157-
"removeVary": {"azion_behavior": "filter_response_header","target": {"target": "Vary"}, "phase": "response", "akamai_behavior": "removeVary"},
180+
"removeVary": {
181+
"azion_behavior": "filter_response_header",
182+
"target": {"target": "Vary"},
183+
"phase": "response",
184+
"akamai_behavior": "removeVary"
185+
},
158186

159187
# Redirects
160188
"redirect": {

src/akamai/utils.py

-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ def get_input_hostname(values: List[str]) -> str:
332332
patterns.append(pattern)
333333
return r"^(%s)$" % "|".join(patterns)
334334

335-
336335
def get_redirect_target(options):
337336
"""
338337
Generate redirect target based on Akamai redirect behavior options.

src/writer.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ def write_main_setting_block(f, resource):
117117
resource (dict): Resource containing the main setting.
118118
"""
119119
try:
120-
attributes = resource.get("attributes", None)
120+
attributes = resource.get("attributes")
121121
edge_application = attributes.get("edge_application")
122122

123-
name = edge_application.get("name", None)
123+
name = edge_application.get("name")
124124
if not name:
125125
name = "Unnamed Edge Application"
126126
normalized_name = sanitize_name(name)
@@ -471,7 +471,7 @@ def write_azion_edge_application_edge_functions_instance_block(f, attributes: di
471471
results = attributes.get("results", {})
472472
name = results.get("name")
473473
edge_function_id = results.get("edge_function_id")
474-
args = results.get("args", None)
474+
args = results.get("args")
475475

476476
# Validate edge_functions in main_setting
477477
if not attributes.get("edge_functions", False):

0 commit comments

Comments
 (0)