Skip to content

Commit

Permalink
chore: code adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
helio-neto committed Feb 28, 2025
1 parent 029cf1d commit 060c6d3
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 98 deletions.
16 changes: 15 additions & 1 deletion src/akamai/akamai.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
def generate_azion_config(akamai_config: dict) -> dict:
"""
Converts Akamai configuration to Azion-compatible configuration.
Args:
akamai_config (dict): The Akamai configuration to convert.
Returns:
dict: The Azion-compatible configuration.
"""
azion_resources = AzionResource("azion_resources")
try:
Expand Down Expand Up @@ -51,6 +57,14 @@ def generate_azion_config(akamai_config: dict) -> dict:
return {"resources": azion_resources.get_azion_resources()}

def akamai_converter(config: dict) -> dict:
"""Process Akamai configuration and return Azion-compatible configuration."""
"""
Process Akamai configuration and return Azion-compatible configuration.
Args:
config (dict): The Akamai configuration to convert.
Returns:
dict: The Azion-compatible configuration.
"""
logging.info("Converting Akamai configuration.")
return generate_azion_config(config)
42 changes: 24 additions & 18 deletions src/akamai/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


# Main processing and conversion logic
def process_resource(azion_resources: AzionResource, resource: Dict[str, Any]):
def process_resource(azion_resources: AzionResource, resource: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
Processes a single Akamai resource and converts it to Azion.
Expand Down Expand Up @@ -71,7 +71,12 @@ def process_resource(azion_resources: AzionResource, resource: Dict[str, Any]):
logging.info(f"Finished processing resources. Total Azion resources generated: {len(azion_resources.get_azion_resources())}")


def process_rules(azion_resources: AzionResource, rules: Any, main_setting_name: str, origin_hostname: str):
def process_rules(
azion_resources: AzionResource,
rules: Any,
main_setting_name: str,
origin_hostname: str
) -> List[Dict[str, Any]]:
"""
Processes Akamai rules (children or list format) and generates the corresponding Azion resources.
Expand Down Expand Up @@ -133,7 +138,7 @@ def process_rule_behaviors(
origin_hostname: str,
index: int,
normalized_name: str
):
) -> List[Dict[str, Any]]:
"""
Processes the list of behaviors rules and converts them into Azion resources.
Expand All @@ -160,34 +165,35 @@ def process_rule_behaviors(


for behavior in behaviors:
if behavior.get("name") == "caching": # Cache Settings
behavior_name = behavior.get("name")
if behavior_name == "caching": # Cache Settings
cache_setting.append(behavior)
cache_setting = create_cache_setting(azion_resources, cache_setting, main_setting_name, rule.get("name"))
cache_setting = create_cache_setting(azion_resources, cache_setting, main_setting_name, context["rule_name"])
if cache_setting:
azion_resources.append(cache_setting)
context["cache_setting"] = cache_setting

idx, main_settings = azion_resources.query_azion_resource_by_type('azion_edge_application_main_setting')
index_main_settings, main_settings = azion_resources.query_azion_resource_by_type('azion_edge_application_main_setting')
if main_settings:
main_settings["attributes"]["edge_application"]["caching"] = True
resources = azion_resources.get_azion_resources()
resources[idx] = main_settings
resources[index_main_settings] = main_settings

elif behavior.get("name") == "webApplicationFirewall":
elif behavior_name == "webApplicationFirewall":
process_waf_behavior(azion_resources, behavior) # WAF Settings
elif behavior.get("name") == "allowPost": # Application Acceleration
idx, main_settings = azion_resources.query_azion_resource_by_type('azion_edge_application_main_setting')
elif behavior_name == "allowPost": # Application Acceleration
index_main_settings, main_settings = azion_resources.query_azion_resource_by_type('azion_edge_application_main_setting')
if main_settings:
resources = azion_resources.get_azion_resources()
main_settings["attributes"]["edge_application"]["application_acceleration"] = True
resources[idx] = main_settings
elif behavior.get("name") == "origin": # Origin
origin = create_origin(azion_resources, behavior, main_setting_name, origin_hostname, rule.get("name"))
resources[index_main_settings] = main_settings
elif behavior_name == "origin": # Origin
origin = create_origin(azion_resources, behavior, main_setting_name, origin_hostname, context["rule_name"])
if origin:
azion_resources.append(origin)
context["origin"] = origin

azion_resources.extend(create_rule_engine(azion_resources, rule, context, rule.get("name")))
azion_resources.extend(create_rule_engine(azion_resources, rule, context, context["rule_name"]))

logging.info(f"[Akamai Rules] Processing behaviors for rules '{normalized_name}'. Finished.")

Expand All @@ -199,7 +205,7 @@ def process_rule_children(
origin_hostname: str,
parent_rule_index: int,
parent_rule_name: str
):
) -> List[Dict[str, Any]]:
"""
Processes the list of children rules and converts them into Azion resources.
Expand Down Expand Up @@ -296,7 +302,7 @@ def create_main_resources(
attributes: Dict[str, Any],
main_setting_name: str,
origin_hostname: str
):
) -> List[Dict[str, Any]]:
"""
Creates the main setting, origin, and domain resources.
Expand All @@ -318,7 +324,7 @@ def create_main_resources(
raise


def process_waf_behavior(azion_resources: AzionResource, attributes: Dict[str, Any]):
def process_waf_behavior(azion_resources: AzionResource, attributes: Dict[str, Any]) -> List[Dict[str, Any]]:
"""
Adds WAF rule to Azion resources if available.
Expand All @@ -341,7 +347,7 @@ def convert_akamai_to_azion(
main_setting_name: str,
edge_hostname: str,
origin_hostname: str
):
) -> List[Dict[str, Any]]:
"""
Converts Akamai property to Azion resources, including handling rules of different formats.
Expand Down
3 changes: 2 additions & 1 deletion src/akamai/converter_cache_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ def create_cache_setting(
Creates a single Azion cache setting resource.
Parameters:
azion_resources (AzionResource): The Azion resource container.
rules (List[Dict[str, Any]]): List of rules extracted from Akamai configuration.
main_setting_name (str): Name of the main Azion edge application resource.
cache_name (Optional[str]): Name of the cache setting resource.
Returns:
Dict[str, Any]: Azion-compatible cache setting resource.
Optional[Dict[str, Any]]: Azion-compatible cache setting resource.
"""
# Extract and validate caching behavior
caching_behavior = next((rule['options'] for rule in rules if rule.get("name") == "caching"), None)
Expand Down
7 changes: 4 additions & 3 deletions src/akamai/converter_digital_certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ def create_digital_certificate(
Creates a Digital Certificate resource in Azion based on Akamai customCertificates.
Parameters:
custom_certificates (list): List of Akamai custom certificates.
azion_resources (AzionResource): The Azion resource container.
custom_certificates (List[Dict[str, Any]]): List of Akamai custom certificates.
Returns:
str: The reference ID of the created Azion Digital Certificate resource.
Optional[Dict[str, Any]]: The reference ID of the created Azion Digital Certificate resource.
"""
if not custom_certificates:
logging.warning("No custom certificates provided. Skipping certificate creation.")
Expand Down Expand Up @@ -43,4 +44,4 @@ def create_digital_certificate(

except ValueError as e:
logging.error(f"Error creating digital certificate: {str(e)}")
return None
return None
7 changes: 4 additions & 3 deletions src/akamai/converter_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ def create_domain(
Creates the Azion domain resource from Akamai attributes.
Parameters:
attributes (dict): Attributes from Akamai configuration.
azion_resources (AzionResource): The Azion resource container.
attributes (Dict[str, Any]): Attributes from Akamai configuration.
main_setting_name (str): The main setting name for Azion edge application.
Returns:
dict: Azion-compatible domain resource.
Optional[Dict[str, Any]]: Azion-compatible domain resource.
"""
try:
logging.info("Processing domains.")
Expand Down Expand Up @@ -80,4 +81,4 @@ def create_domain(

except Exception as e:
logging.error(f"Error creating domain resource: {e}")
raise
raise
8 changes: 6 additions & 2 deletions src/akamai/converter_edge_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

from azion_resources import AzionResource

def create_edge_function(azion_resources: AzionResource, rule_name: str, logic: str) -> Optional[Dict[str, Any]]:
def create_edge_function(
azion_resources: AzionResource,
rule_name: str, logic: str
) -> Optional[Dict[str, Any]]:
"""
Creates an Azion Edge Function resource for complex logic.
Parameters:
azion_resources (AzionResource): The Azion resource container.
rule_name (str): Name of the rule requiring Edge Function.
logic (str): Custom logic for the Edge Function.
Returns:
dict: Azion-compatible Edge Function resource.
Optional[Dict[str, Any]]: Azion-compatible Edge Function resource.
"""
function_code = """
TBD
Expand Down
5 changes: 3 additions & 2 deletions src/akamai/converter_main_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ def create_main_setting(
Creates the main application setting resource for Azion.
Parameters:
attributes (dict): Attributes from Akamai configuration.
azion_resources (AzionResource): The Azion resource container.
attributes (Dict[str, Any]): Attributes from Akamai configuration.
main_setting_name (str): Name of the main Azion edge application resource.
Returns:
dict: Azion-compatible main_setting resource.
Optional[Dict[str, Any]]: Azion-compatible main_setting resource.
"""
defaults = {
"name": "Default Edge Application",
Expand Down
15 changes: 11 additions & 4 deletions src/akamai/converter_origin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@
from akamai.utils import map_origin_protocol_policy, map_origin_type, map_forward_host_header


def create_origin(azion_resources: AzionResource, origin_attributes: Dict[str, Any], main_setting_name: str, edge_hostname: Optional[str], name: Optional[str] = None) -> Optional[Dict[str, Any]]:
def create_origin(
azion_resources: AzionResource,
origin_attributes: Dict[str, Any],
main_setting_name: str,
edge_hostname: Optional[str],
name: Optional[str] = None
) -> Optional[Dict[str, Any]]:
"""
Creates the origin resource for Azion, dynamically mapping Akamai addresses.
Parameters:
attributes (dict): Attributes from Akamai configuration.
azion_resources (AzionResource): The Azion resource container.
origin_attributes (Dict[str, Any]): Attributes from Akamai configuration.
main_setting_name (str): Name of the main Azion edge application resource.
edge_hostname (Optional[str]): The edge hostname extracted from Akamai configuration.
name (Optional[str]): Name of the origin resource.
Returns:
dict: Azion-compatible origin resource.
Optional[Dict[str, Any]]: Azion-compatible origin resource.
"""
try:
logging.info("Creating Azion origin resource.")
Expand Down Expand Up @@ -89,4 +96,4 @@ def create_origin(azion_resources: AzionResource, origin_attributes: Dict[str, A

except Exception as e:
logging.error(f"Error creating origin resource: {e}")
raise
raise
Loading

0 comments on commit 060c6d3

Please sign in to comment.