From 523225d6e92c7ba0cd811af9ba65e1fc19e75f67 Mon Sep 17 00:00:00 2001 From: Ivan-Lukianov <86720733+Ivan-Lukianov@users.noreply.github.com> Date: Sat, 13 May 2023 21:08:51 +0400 Subject: [PATCH] Autoscaling: Add metrics for E2 target tracking scaling (#6314) --- moto/autoscaling/responses.py | 32 +++++++++++++ tests/test_autoscaling/test_autoscaling.py | 54 ++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/moto/autoscaling/responses.py b/moto/autoscaling/responses.py index e6faab3ba..a209cea55 100644 --- a/moto/autoscaling/responses.py +++ b/moto/autoscaling/responses.py @@ -1022,6 +1022,38 @@ DESCRIBE_SCALING_POLICIES_TEMPLATE = """{{ policy.target_tracking_config["CustomizedMetricSpecification"].get("Unit") }} {% endif %} + {% if policy.target_tracking_config["CustomizedMetricSpecification"].get("Metrics") %} + + {% for metric in policy.target_tracking_config["CustomizedMetricSpecification"].get("Metrics", []) %} + + {{ metric.get("Id") }} + {% if metric.get("MetricStat") is none %} + {{ metric.get("Expression") }} + {% endif %} + {% if metric.get("Expression") is none %} + + + {{ metric.get("MetricStat", {}).get("Metric", {}).get("Namespace") }} + {{ metric.get("MetricStat", {}).get("Metric", {}).get("MetricName") }} + + {% for dim in metric.get("MetricStat", {}).get("Metric", {}).get("Dimensions", []) %} + + {{ dim.get("Name") }} + {{ dim.get("Value") }} + + {% endfor %} + + + {{ metric.get("MetricStat", {}).get("Stat") }} + {{ metric.get("MetricStat", {}).get("Unit") }} + + {% endif %} + + {{ 'true' if metric.get("ReturnData") is none else metric.get("ReturnData") }} + + {% endfor %} + + {% endif %} {% endif %} {{ policy.target_tracking_config.get("TargetValue") }} diff --git a/tests/test_autoscaling/test_autoscaling.py b/tests/test_autoscaling/test_autoscaling.py index 293c66159..8e4fade10 100644 --- a/tests/test_autoscaling/test_autoscaling.py +++ b/tests/test_autoscaling/test_autoscaling.py @@ -771,6 +771,30 @@ def test_create_autoscaling_policy_with_policytype__targettrackingscaling(): "PredefinedMetricType": "ASGAverageNetworkIn", }, "TargetValue": 1000000.0, + "CustomizedMetricSpecification": { + "Metrics": [ + { + "Label": "Get ASGAverageCPUUtilization", + "Id": "cpu", + "MetricStat": { + "Metric": { + "MetricName": "CPUUtilization", + "Namespace": "AWS/EC2", + "Dimensions": [ + {"Name": "AutoScalingGroupName", "Value": asg_name} + ], + }, + "Stat": "Average", + }, + "ReturnData": False, + }, + { + "Label": "Calculate square cpu", + "Id": "load", + "Expression": "cpu^2", + }, + ], + }, }, ) @@ -786,6 +810,36 @@ def test_create_autoscaling_policy_with_policytype__targettrackingscaling(): "PredefinedMetricSpecification": { "PredefinedMetricType": "ASGAverageNetworkIn", }, + "CustomizedMetricSpecification": { + "MetricName": "None", + "Namespace": "None", + "Dimensions": [], + "Statistic": "None", + "Metrics": [ + { + "Label": "Get ASGAverageCPUUtilization", + "Id": "cpu", + "MetricStat": { + "Metric": { + "MetricName": "CPUUtilization", + "Namespace": "AWS/EC2", + "Dimensions": [ + {"Name": "AutoScalingGroupName", "Value": asg_name} + ], + }, + "Stat": "Average", + "Unit": "None", + }, + "ReturnData": False, + }, + { + "Label": "Calculate square cpu", + "Id": "load", + "Expression": "cpu^2", + "ReturnData": True, + }, + ], + }, "TargetValue": 1000000.0, } )