From bced7682a7a6eaff07e0799ccc0537c10741fbaa Mon Sep 17 00:00:00 2001 From: jmisset-cb Date: Sun, 12 Nov 2023 16:13:27 +0100 Subject: [PATCH 1/4] cloudwatch_metric_alarm: fix idempotency for alarm without dimensions --- plugins/modules/cloudwatch_metric_alarm.py | 4 ++ .../cloudwatch_metric_alarm/tasks/main.yml | 38 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/plugins/modules/cloudwatch_metric_alarm.py b/plugins/modules/cloudwatch_metric_alarm.py index f412e94e548..00c47a319f5 100644 --- a/plugins/modules/cloudwatch_metric_alarm.py +++ b/plugins/modules/cloudwatch_metric_alarm.py @@ -467,6 +467,10 @@ def create_metric_alarm(connection, module, params): if "TreatMissingData" not in alarm.keys(): alarm["TreatMissingData"] = "missing" + # Prevent alarm without dimensions to always return changed + if not alarm["Dimensions"]: + alarm.pop("Dimensions", None) + # Exclude certain props from change detection for key in [ "ActionsEnabled", diff --git a/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml b/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml index 439a1c000bd..6b58a9b2240 100644 --- a/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml +++ b/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml @@ -509,6 +509,44 @@ - ec2_instance_metric_mutually_exclusive.failed - '"parameters are mutually exclusive" in ec2_instance_metric_mutually_exclusive.msg' + - name: create alarm without dimensions + cloudwatch_metric_alarm: + state: present + name: '{{ alarm_full_name }}' + metric: CPUUtilization + namespace: AWS/EC2 + treat_missing_data: missing + statistic: Average + comparison: LessThanOrEqualToThreshold + threshold: 5.0 + period: 300 + evaluation_periods: 3 + unit: Percent + description: This will alarm when an instance's cpu usage average is lower than + 5% for 15 minutes + + - name: create alarm without dimensions (idempotent) + cloudwatch_metric_alarm: + state: present + name: '{{ alarm_full_name }}' + metric: CPUUtilization + namespace: AWS/EC2 + treat_missing_data: missing + statistic: Average + comparison: LessThanOrEqualToThreshold + threshold: 5.0 + period: 300 + evaluation_periods: 3 + unit: Percent + description: This will alarm when an instance's cpu usage average is lower than + 5% for 15 minutes + register: ec2_instance_metric_alarm_no_dimensions_idempotent + + - name: "Verify alarm without dimensions does not register as changed after update" + assert: + that: + - not ec2_instance_metric_alarm_no_dimensions_idempotent.changed + always: - name: try to delete the alarm amazon.aws.cloudwatch_metric_alarm: From 7208e74fbb688b9364624b8bab7435817d0c33e9 Mon Sep 17 00:00:00 2001 From: jmisset-cb Date: Tue, 19 Dec 2023 11:04:14 +0100 Subject: [PATCH 2/4] cloudwatch_metric_alarm: fix idempotency for alarm without dimensions (syntax fix) --- .../cloudwatch_metric_alarm/tasks/main.yml | 74 +++++++++---------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml b/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml index 6b58a9b2240..2f0e73ecba1 100644 --- a/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml +++ b/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml @@ -509,43 +509,43 @@ - ec2_instance_metric_mutually_exclusive.failed - '"parameters are mutually exclusive" in ec2_instance_metric_mutually_exclusive.msg' - - name: create alarm without dimensions - cloudwatch_metric_alarm: - state: present - name: '{{ alarm_full_name }}' - metric: CPUUtilization - namespace: AWS/EC2 - treat_missing_data: missing - statistic: Average - comparison: LessThanOrEqualToThreshold - threshold: 5.0 - period: 300 - evaluation_periods: 3 - unit: Percent - description: This will alarm when an instance's cpu usage average is lower than - 5% for 15 minutes - - - name: create alarm without dimensions (idempotent) - cloudwatch_metric_alarm: - state: present - name: '{{ alarm_full_name }}' - metric: CPUUtilization - namespace: AWS/EC2 - treat_missing_data: missing - statistic: Average - comparison: LessThanOrEqualToThreshold - threshold: 5.0 - period: 300 - evaluation_periods: 3 - unit: Percent - description: This will alarm when an instance's cpu usage average is lower than - 5% for 15 minutes - register: ec2_instance_metric_alarm_no_dimensions_idempotent - - - name: "Verify alarm without dimensions does not register as changed after update" - assert: - that: - - not ec2_instance_metric_alarm_no_dimensions_idempotent.changed + - name: create alarm without dimensions + cloudwatch_metric_alarm: + state: present + name: '{{ alarm_full_name }}' + metric: CPUUtilization + namespace: AWS/EC2 + treat_missing_data: missing + statistic: Average + comparison: LessThanOrEqualToThreshold + threshold: 5.0 + period: 300 + evaluation_periods: 3 + unit: Percent + description: This will alarm when an instance's cpu usage average is lower than + 5% for 15 minutes + + - name: create alarm without dimensions (idempotent) + cloudwatch_metric_alarm: + state: present + name: '{{ alarm_full_name }}' + metric: CPUUtilization + namespace: AWS/EC2 + treat_missing_data: missing + statistic: Average + comparison: LessThanOrEqualToThreshold + threshold: 5.0 + period: 300 + evaluation_periods: 3 + unit: Percent + description: This will alarm when an instance's cpu usage average is lower than + 5% for 15 minutes + register: ec2_instance_metric_alarm_no_dimensions_idempotent + + - name: "Verify alarm without dimensions does not register as changed after update" + assert: + that: + - not ec2_instance_metric_alarm_no_dimensions_idempotent.changed always: - name: try to delete the alarm From f82f4e898c47d027dd6e97a9029a6735b7a11d54 Mon Sep 17 00:00:00 2001 From: jmisset-cb Date: Wed, 14 Feb 2024 12:10:08 +0100 Subject: [PATCH 3/4] cloudwatch_metric_alarm: add extra assertion step in integration tests --- .../targets/cloudwatch_metric_alarm/tasks/main.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml b/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml index 2f0e73ecba1..5cf9dda6324 100644 --- a/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml +++ b/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml @@ -524,6 +524,19 @@ unit: Percent description: This will alarm when an instance's cpu usage average is lower than 5% for 15 minutes + register: ec2_instance_metric_alarm_no_dimensions + + - name: get info on alarm without dimensions + amazon.aws.cloudwatch_metric_alarm_info: + alarm_names: + - "{{ alarm_full_name }}" + register: alarm_info_metrics_alarm_no_dimensions + + - name: verify that an alarm was created without dimensions + ansible.builtin.assert: + that: + - ec2_instance_metric_alarm_no_dimensions.changed + - alarm_info_metrics_alarm_no_dimensions.metric_alarms[0].dimensions | length == 0 - name: create alarm without dimensions (idempotent) cloudwatch_metric_alarm: From db059adf80edffa6f59c53f11a80aaeb943c3f49 Mon Sep 17 00:00:00 2001 From: jmisset-cb Date: Fri, 16 Feb 2024 10:53:04 +0100 Subject: [PATCH 4/4] cloudwatch_metric_alarm: add changelog entry for idempotency fix #1865 --- .../fragments/20240216-cloudwatch-metric-alarm-idempotency.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelogs/fragments/20240216-cloudwatch-metric-alarm-idempotency.yml diff --git a/changelogs/fragments/20240216-cloudwatch-metric-alarm-idempotency.yml b/changelogs/fragments/20240216-cloudwatch-metric-alarm-idempotency.yml new file mode 100644 index 00000000000..bd31947942e --- /dev/null +++ b/changelogs/fragments/20240216-cloudwatch-metric-alarm-idempotency.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - cloudwatch_metric_alarm - Fix idempotency when creating cloudwatch metric alarm without dimensions (https://github.com/ansible-collections/amazon.aws/pull/1865). \ No newline at end of file