From cfa7ecaec45c0f6a168717f6d5c7a7080e76b958 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 30 Aug 2022 12:34:17 -0700 Subject: [PATCH 01/14] New module: cloudwatch_metric_alarm_info --- meta/runtime.yml | 1 + .../modules/cloudwatch_metric_alarm_info.py | 100 ++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 plugins/modules/cloudwatch_metric_alarm_info.py diff --git a/meta/runtime.yml b/meta/runtime.yml index d5b1c573ca0..ce1935ddca0 100644 --- a/meta/runtime.yml +++ b/meta/runtime.yml @@ -16,6 +16,7 @@ action_groups: - cloudwatchlogs_log_group - cloudwatchlogs_log_group_info - cloudwatchlogs_log_group_metric_filter + - cloudwatch_metric_alarm_info - ec2_ami - ec2_ami_info - ec2_eip diff --git a/plugins/modules/cloudwatch_metric_alarm_info.py b/plugins/modules/cloudwatch_metric_alarm_info.py new file mode 100644 index 00000000000..6c19726928b --- /dev/null +++ b/plugins/modules/cloudwatch_metric_alarm_info.py @@ -0,0 +1,100 @@ +#!/usr/bin/python +# This file is part of Ansible +# GNU General Public License v3.0+ (see COPYING or https://wwww.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +DOCUMENTATION = ''' +--- +module: cloudwatch_metric_alarm_info +version_added: 5.0.0 +short_description: Gather information about the alarms for the specified metric +description: + - Retrieves the alarms for the specified metric. +author: + - Mandar Vijay Kulkarni (@mandar242) +options: + alarm_names: + description: + - The name of the metric. + required: false + type: list + elements: str + +extends_documentation_fragment: +- amazon.aws.aws +- amazon.aws.ec2 +''' + +EXAMPLES = ''' +# Note: These examples do not set authentication details, see the AWS Guide for details. + +- name: describe the metric alarm based on metric name and namespace + amazon.aws.cloudwatch_metric_alarm_info: + cloudwatch_metric_alarm_info: + alarm_names: + - my-test-alarm-1 + - my-test-alarm-2 + +''' + +# RETURN BLOCK IS WIP +RETURN = ''' +''' + + +try: + import botocore +except ImportError: + pass # Handled by AnsibleAWSModule +from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule +from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry +from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict + + +def _describe_alarms(connection, **params): + paginator = connection.get_paginator('describe_alarms') + return paginator.paginate(**params).build_full_result() + + +def describe_metric_alarms_info(connection, module): + + params = {} + params['AlarmNames'] = module.params.get('alarm_names') + + try: + describe_metric_alarms_info_response = _describe_alarms(connection, **params)['MetricAlarms'] + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: + module.fail_json_aws(e, msg='Failed to describe cloudwatch metric alarm') + + metric_alarms = [] + for response_list_item in describe_metric_alarms_info_response: + metric_alarms.append(camel_dict_to_snake_dict(response_list_item)) + + if len(metric_alarms) == 0: + module.exit_json(msg='No metric alarms found for specified options') + + module.exit_json(metric_alarms=metric_alarms) + + +def main(): + + argument_spec = dict( + alarm_names=dict(type='list', elements='str', required=False), + ) + module = AnsibleAWSModule( + argument_spec=argument_spec, + supports_check_mode=True + ) + try: + connection = module.client('cloudwatch', retry_decorator=AWSRetry.jittered_backoff()) + except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: + module.fail_json_aws(e, msg='Failed to connect to AWS') + + describe_metric_alarms_info(connection, module) + + +if __name__ == '__main__': + main() From b3706372339d12b24975a09a3f7ed872baaee03c Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Wed, 31 Aug 2022 11:07:51 -0700 Subject: [PATCH 02/14] Add RETURN block --- .../modules/cloudwatch_metric_alarm_info.py | 105 +++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/plugins/modules/cloudwatch_metric_alarm_info.py b/plugins/modules/cloudwatch_metric_alarm_info.py index 6c19726928b..4968a0c3c99 100644 --- a/plugins/modules/cloudwatch_metric_alarm_info.py +++ b/plugins/modules/cloudwatch_metric_alarm_info.py @@ -40,8 +40,111 @@ ''' -# RETURN BLOCK IS WIP RETURN = ''' +metric_alarms: + description: The gathered information about specified metric alarms. + returned: when success + type: list + elements: dict + contains: + actions_enabled: + description: Indicates whether actions should be executed during any changes to the alarm state. + returned: always + type: str + alarm_arn: + description: The Amazon Resource Name (ARN) of the alarm. + returned: always + type: str + alarm_configuration_updated_timestamp: + description: The time stamp of the last update to the alarm configuration. + returned: always + type: str + alarm_description: + description: The description of the alarm. + returned: always + type: str + alarm_name: + description: Unique name for the alarm. + returned: always + type: str + comparison_operator: + description: The arithmetic operation to use when comparing the specified statistic and threshold. + returned: always + type: str + dimensions: + description: The dimensions for the metric. + returned: always + type: list + elements: dict + contains: + name: + description: The name of the dimension. + returned: always + type: str + value: + description: The value of the dimension. + returned: always + type: str + evaluation_period: + description: The number of periods over which data is compared to the specified threshold. + returned: always + type: str + insufficient_data_actions: + description: The actions to execute when this alarm transitions to the INSUFFICIENT_DATA state from any other state. + returned: always + type: str + metric_name: + description: Name of the monitored metric (e.g. C(CPUUtilization)). + returned: always + type: str + namespace: + description: + - Name of the appropriate namespace (C(AWS/EC2), C(System/Linux), etc.). + - Determines the category it will appear under in CloudWatch. + returned: always + type: str + ok_actions: + description: The actions to execute when this alarm transitions to an OK state from any other state. + returned: always + type: str + period: + description: + - The length, in seconds, used each time the metric specified in MetricName is evaluated. + - Valid values are 10, 30, and any multiple of 60. + returned: always + type: int + state_reason: + description: An explanation for the alarm state, in text format. + returned: always + type: str + state_reason_data: + description: An explanation for the alarm state, in JSON format. + returned: always + type: str + state_updated_timestamp: + description: The time stamp of the last update to the alarm state. + returned: always + type: str + state_value: + description: The state value for the alarm. + returned: always + type: str + statistic: + description: The statistic for the metric associated with the alarm, other than percentile. + returned: always + type: str + threshold: + description: The value to compare with the specified statistic. + returned: always + type: str + treat_missing_data: + description: Sets how alarm is to handle missing data points. + returned: always + type: str + unit: + description: Unit used when storing the metric + returned: always + type: str ''' From f7fb47a6bc4f765c64fe1a710b4656a8f8b429a8 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 6 Sep 2022 17:31:42 -0700 Subject: [PATCH 03/14] Return empty list instead of a msg on non-existent alarm --- plugins/modules/cloudwatch_metric_alarm_info.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/modules/cloudwatch_metric_alarm_info.py b/plugins/modules/cloudwatch_metric_alarm_info.py index 4968a0c3c99..521fa496f15 100644 --- a/plugins/modules/cloudwatch_metric_alarm_info.py +++ b/plugins/modules/cloudwatch_metric_alarm_info.py @@ -157,6 +157,7 @@ from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict +@AWSRetry.jittered_backoff(retries=10) def _describe_alarms(connection, **params): paginator = connection.get_paginator('describe_alarms') return paginator.paginate(**params).build_full_result() @@ -177,7 +178,7 @@ def describe_metric_alarms_info(connection, module): metric_alarms.append(camel_dict_to_snake_dict(response_list_item)) if len(metric_alarms) == 0: - module.exit_json(msg='No metric alarms found for specified options') + module.exit_json(metric_alarms=[]) module.exit_json(metric_alarms=metric_alarms) From cb868d3a93ae8beca8bc7ef8a3b0d4fb869dfb2e Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 6 Sep 2022 18:08:35 -0700 Subject: [PATCH 04/14] Add support for other params for describe_alarms() --- .../modules/cloudwatch_metric_alarm_info.py | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/plugins/modules/cloudwatch_metric_alarm_info.py b/plugins/modules/cloudwatch_metric_alarm_info.py index 521fa496f15..05a84e8071f 100644 --- a/plugins/modules/cloudwatch_metric_alarm_info.py +++ b/plugins/modules/cloudwatch_metric_alarm_info.py @@ -165,11 +165,13 @@ def _describe_alarms(connection, **params): def describe_metric_alarms_info(connection, module): - params = {} - params['AlarmNames'] = module.params.get('alarm_names') + params = build_params(module) + + alarm_type_mapping = {'composite_alarm': 'CompositeAlarm', 'metric_alarm': 'MetricAlarms'} try: - describe_metric_alarms_info_response = _describe_alarms(connection, **params)['MetricAlarms'] + describe_metric_alarms_info_response = _describe_alarms(connection, **params) + describe_metric_alarms_info_response = describe_metric_alarms_info_response[alarm_type_mapping[module.params.get('alarm_type')]] except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg='Failed to describe cloudwatch metric alarm') @@ -183,11 +185,43 @@ def describe_metric_alarms_info(connection, module): module.exit_json(metric_alarms=metric_alarms) +def build_params(module): + + params = {} + + params['AlarmNames'] = module.params.get('alarm_names') + + if module.params.get('alarm_name_prefix'): + params['AlarmNamePrefix'] = module.params.get('alarm_name_prefix') + + if module.params.get('children_of_alarm_name'): + params['ChildrenOfAlarmName'] = module.params.get('children_of_alarm_name') + + if module.params.get('parents_of_alarm_name'): + params['ParentsOfAlarmName'] = module.params.get('parents_of_alarm_name') + + if module.params.get('state_value'): + state_value_mapping = {'ok': 'OK', 'alarm': 'ALARM', 'insufficient_data': 'INSUFFICIENT_DATA'} + params['StateValue'] = state_value_mapping[module.params.get('state_value')] + + if module.params.get('action_prefix'): + params['ActionPrefix'] = module.params.get('action_prefix') + + return params + + def main(): argument_spec = dict( alarm_names=dict(type='list', elements='str', required=False), + alarm_name_prefix=dict(type='str', required=False), + alarm_type=dict(type='str', choices=['composite_alarm', 'metric_alarm'], default='metric_alarm', required=False), + children_of_alarm_name=dict(type='str', required=False), + parents_of_alarm_name=dict(type='str', required=False), + state_value=dict(type='str', choices=['ok', 'alarm', 'insufficient_data'], required=False), + action_prefix=dict(type='str', required=False), ) + module = AnsibleAWSModule( argument_spec=argument_spec, supports_check_mode=True From 1a19cf84661b88a59746fbbf3adca40b67d224e7 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Wed, 7 Sep 2022 10:59:49 -0700 Subject: [PATCH 05/14] Add DOCUMENTATION for remaining parameters --- .../modules/cloudwatch_metric_alarm_info.py | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/plugins/modules/cloudwatch_metric_alarm_info.py b/plugins/modules/cloudwatch_metric_alarm_info.py index 05a84e8071f..d0a99567cdd 100644 --- a/plugins/modules/cloudwatch_metric_alarm_info.py +++ b/plugins/modules/cloudwatch_metric_alarm_info.py @@ -22,6 +22,41 @@ required: false type: list elements: str + alarm_name_prefix: + description: + - An alarm name prefix to retrieve information about alarms that have names that start with this prefix. + - Can not be used with alarm_names. + required: false + type: str + alarm_type: + description: + - Specify this to return metric alarms or composite alarms. + - Module is defaulted to return metric alarms but can return composite alarms if I(alarm_type=composite_alarm). + required: false + type: str + default: metric_alarm + choices: [composite_alarm, metric_alarm] + children_of_alarm_name: + description: + - If specified returns information about the "children" alarms of the alarm name specified. + required: false + type: str + parents_of_alarm_name: + description: + - If specified returns information about the "parent" alarms of the alarm name specified. + required: false + type: str + state_value: + description: + - If specified returns information only about alarms that are currently in the particular state. + required: false + type: str + choices: ['ok', 'alarm', 'insufficient_data'] + action_prefix: + description: + - This parameter can be used to filter the results of the operation to only those alarms that use a certain alarm action. + required: false + type: str extends_documentation_fragment: - amazon.aws.aws @@ -152,6 +187,7 @@ import botocore except ImportError: pass # Handled by AnsibleAWSModule + from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict @@ -224,8 +260,10 @@ def main(): module = AnsibleAWSModule( argument_spec=argument_spec, + mutually_exclusive=[['alarm_names', 'alarm_name_prefix']], supports_check_mode=True ) + try: connection = module.client('cloudwatch', retry_decorator=AWSRetry.jittered_backoff()) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: From 195806765e712af50472088a728373d080b55b58 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Wed, 7 Sep 2022 12:34:52 -0700 Subject: [PATCH 06/14] Add more examples --- .../modules/cloudwatch_metric_alarm_info.py | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/plugins/modules/cloudwatch_metric_alarm_info.py b/plugins/modules/cloudwatch_metric_alarm_info.py index d0a99567cdd..b1a2f8e66f0 100644 --- a/plugins/modules/cloudwatch_metric_alarm_info.py +++ b/plugins/modules/cloudwatch_metric_alarm_info.py @@ -66,12 +66,22 @@ EXAMPLES = ''' # Note: These examples do not set authentication details, see the AWS Guide for details. -- name: describe the metric alarm based on metric name and namespace +- name: describe the metric alarm based on alarm names amazon.aws.cloudwatch_metric_alarm_info: - cloudwatch_metric_alarm_info: - alarm_names: - - my-test-alarm-1 - - my-test-alarm-2 + alarm_names: + - my-test-alarm-1 + - my-test-alarm-2 + +- name: describe the metric alarm based alarm names and state value + amazon.aws.cloudwatch_metric_alarm_info: + alarm_names: + - my-test-alarm-1 + - my-test-alarm-2 + state_value: ok + +- name: describe the metric alarm based alarm names prefix + amazon.aws.cloudwatch_metric_alarm_info: + alarm_name_prefix: my-test- ''' @@ -225,7 +235,8 @@ def build_params(module): params = {} - params['AlarmNames'] = module.params.get('alarm_names') + if module.params.get('alarm_names'): + params['AlarmNames'] = module.params.get('alarm_names') if module.params.get('alarm_name_prefix'): params['AlarmNamePrefix'] = module.params.get('alarm_name_prefix') From e29e87d064f8576b8f53e8b26ef98d8d1fc0374b Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Wed, 7 Sep 2022 16:26:46 -0700 Subject: [PATCH 07/14] Add RETURN block data for remaining response params --- .../modules/cloudwatch_metric_alarm_info.py | 126 +++++++++++------- 1 file changed, 79 insertions(+), 47 deletions(-) diff --git a/plugins/modules/cloudwatch_metric_alarm_info.py b/plugins/modules/cloudwatch_metric_alarm_info.py index b1a2f8e66f0..e73050e0949 100644 --- a/plugins/modules/cloudwatch_metric_alarm_info.py +++ b/plugins/modules/cloudwatch_metric_alarm_info.py @@ -92,50 +92,55 @@ type: list elements: dict contains: - actions_enabled: - description: Indicates whether actions should be executed during any changes to the alarm state. + alarm_name: + description: Unique name for the alarm. returned: always type: str alarm_arn: description: The Amazon Resource Name (ARN) of the alarm. returned: always type: str - alarm_configuration_updated_timestamp: - description: The time stamp of the last update to the alarm configuration. - returned: always - type: str alarm_description: description: The description of the alarm. returned: always type: str - alarm_name: - description: Unique name for the alarm. + alarm_configuration_updated_timestamp: + description: The time stamp of the last update to the alarm configuration. returned: always type: str - comparison_operator: - description: The arithmetic operation to use when comparing the specified statistic and threshold. + actions_enabled: + description: Indicates whether actions should be executed during any changes to the alarm state. returned: always - type: str - dimensions: - description: The dimensions for the metric. + type: bool + ok_actions: + description: The actions to execute when this alarm transitions to an OK state from any other state. returned: always type: list - elements: dict - contains: - name: - description: The name of the dimension. - returned: always - type: str - value: - description: The value of the dimension. - returned: always - type: str - evaluation_period: - description: The number of periods over which data is compared to the specified threshold. + elements: str + alarm_actions: + description: The actions to execute when this alarm transitions to an ALARM state from any other state. returned: always - type: str + type: list + elements: str insufficient_data_actions: - description: The actions to execute when this alarm transitions to the INSUFFICIENT_DATA state from any other state. + description: The actions to execute when this alarm transitions to an INSUFFICIENT_DATA state from any other state. + returned: always + type: list + elements: str + state_value: + description: The state value for the alarm. + returned: always + type: str + state_reason: + description: An explanation for the alarm state, in text format. + returned: always + type: str + state_reason_data: + description: An explanation for the alarm state, in JSON format. + returned: always + type: str + state_updated_timestamp: + description: The time stamp of the last update to the alarm state. returned: always type: str metric_name: @@ -148,48 +153,75 @@ - Determines the category it will appear under in CloudWatch. returned: always type: str - ok_actions: - description: The actions to execute when this alarm transitions to an OK state from any other state. + statistic: + description: The statistic for the metric associated with the alarm, other than percentile. + returned: always + type: str + extended_statistic: + description: The percentile statistic for the metric associated with the alarm. returned: always type: str + dimensions: + description: The dimensions for the metric. + returned: always + type: list + elements: dict + contains: + name: + description: The name of the dimension. + returned: always + type: str + value: + description: The value of the dimension. + returned: always + type: str period: description: - The length, in seconds, used each time the metric specified in MetricName is evaluated. - Valid values are 10, 30, and any multiple of 60. returned: always type: int - state_reason: - description: An explanation for the alarm state, in text format. - returned: always - type: str - state_reason_data: - description: An explanation for the alarm state, in JSON format. - returned: always - type: str - state_updated_timestamp: - description: The time stamp of the last update to the alarm state. + unit: + description: Unit used when storing the metric returned: always type: str - state_value: - description: The state value for the alarm. + evaluation_period: + description: The number of periods over which data is compared to the specified threshold. returned: always - type: str - statistic: - description: The statistic for the metric associated with the alarm, other than percentile. + type: int + datapoints_to_alarm: + description: The number of data points that must be breaching to trigger the alarm. returned: always - type: str + type: int threshold: description: The value to compare with the specified statistic. returned: always + type: float + comparison_operator: + description: The arithmetic operation to use when comparing the specified statistic and threshold. + returned: always type: str treat_missing_data: description: Sets how alarm is to handle missing data points. returned: always type: str - unit: - description: Unit used when storing the metric + evaluate_low_sample_count_percentile: + description: + - Used only for alarms based on percentiles. + - If I(ignore), the alarm state does not change during periods with too few data points to be statistically significant. + - If I(evaluate) or this parameter is not used, the alarm is always evaluated and possibly changes state. returned: always type: str + metrics: + description: An array of MetricDataQuery structures, used in an alarm based on a metric math expression. + returned: always + type: list + elements: dict + threshold_metric_id: + description: This is the ID of the ANOMALY_DETECTION_BAND function used as the threshold for the alarm. + returned: always + type: str + ''' From 9f74bae165fddec65a4f0b41564b96e570a1a2a6 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Wed, 7 Sep 2022 16:49:45 -0700 Subject: [PATCH 08/14] Minor fix: handle return value keyword based on alarm type --- plugins/modules/cloudwatch_metric_alarm_info.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/plugins/modules/cloudwatch_metric_alarm_info.py b/plugins/modules/cloudwatch_metric_alarm_info.py index e73050e0949..845e67933d6 100644 --- a/plugins/modules/cloudwatch_metric_alarm_info.py +++ b/plugins/modules/cloudwatch_metric_alarm_info.py @@ -246,21 +246,22 @@ def describe_metric_alarms_info(connection, module): params = build_params(module) alarm_type_mapping = {'composite_alarm': 'CompositeAlarm', 'metric_alarm': 'MetricAlarms'} + alarm_type_to_return = alarm_type_mapping[module.params.get('alarm_type')] try: describe_metric_alarms_info_response = _describe_alarms(connection, **params) - describe_metric_alarms_info_response = describe_metric_alarms_info_response[alarm_type_mapping[module.params.get('alarm_type')]] + describe_metric_alarms_info_response = describe_metric_alarms_info_response[alarm_type_to_return] except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg='Failed to describe cloudwatch metric alarm') - metric_alarms = [] + result = [] for response_list_item in describe_metric_alarms_info_response: - metric_alarms.append(camel_dict_to_snake_dict(response_list_item)) + result.append(camel_dict_to_snake_dict(response_list_item)) - if len(metric_alarms) == 0: - module.exit_json(metric_alarms=[]) + if alarm_type_to_return == 'composite_alarm': + module.exit_json(composite_alarms=result) - module.exit_json(metric_alarms=metric_alarms) + module.exit_json(metric_alarms=result) def build_params(module): From 8b2320053a40c931c8b6a4a860df30b48bbcf418 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 13 Sep 2022 09:19:48 -0700 Subject: [PATCH 09/14] Modified based on feedback --- plugins/modules/cloudwatch_metric_alarm_info.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/plugins/modules/cloudwatch_metric_alarm_info.py b/plugins/modules/cloudwatch_metric_alarm_info.py index 845e67933d6..9132a7148f3 100644 --- a/plugins/modules/cloudwatch_metric_alarm_info.py +++ b/plugins/modules/cloudwatch_metric_alarm_info.py @@ -25,7 +25,7 @@ alarm_name_prefix: description: - An alarm name prefix to retrieve information about alarms that have names that start with this prefix. - - Can not be used with alarm_names. + - Can not be used with I(alarm_names). required: false type: str alarm_type: @@ -51,7 +51,7 @@ - If specified returns information only about alarms that are currently in the particular state. required: false type: str - choices: ['ok', 'alarm', 'insufficient_data'] + choices: ['OK', 'ALARM', 'INSUFFICIENT_DATA'] action_prefix: description: - This parameter can be used to filter the results of the operation to only those alarms that use a certain alarm action. @@ -77,7 +77,7 @@ alarm_names: - my-test-alarm-1 - my-test-alarm-2 - state_value: ok + state_value: OK - name: describe the metric alarm based alarm names prefix amazon.aws.cloudwatch_metric_alarm_info: @@ -281,8 +281,7 @@ def build_params(module): params['ParentsOfAlarmName'] = module.params.get('parents_of_alarm_name') if module.params.get('state_value'): - state_value_mapping = {'ok': 'OK', 'alarm': 'ALARM', 'insufficient_data': 'INSUFFICIENT_DATA'} - params['StateValue'] = state_value_mapping[module.params.get('state_value')] + params['StateValue'] = module.params.get('state_value') if module.params.get('action_prefix'): params['ActionPrefix'] = module.params.get('action_prefix') @@ -298,7 +297,7 @@ def main(): alarm_type=dict(type='str', choices=['composite_alarm', 'metric_alarm'], default='metric_alarm', required=False), children_of_alarm_name=dict(type='str', required=False), parents_of_alarm_name=dict(type='str', required=False), - state_value=dict(type='str', choices=['ok', 'alarm', 'insufficient_data'], required=False), + state_value=dict(type='str', choices=['OK', 'ALARM', 'INSUFFICIENT_DATA'], required=False), action_prefix=dict(type='str', required=False), ) From 2c5769331c4ede491f6ba5723e5e1e0f76b61efc Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Wed, 21 Sep 2022 20:37:49 -0700 Subject: [PATCH 10/14] replace aws cli commands with info module --- .../cloudwatch_metric_alarm/tasks/main.yml | 306 +++++++----------- 1 file changed, 122 insertions(+), 184 deletions(-) diff --git a/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml b/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml index f713f973251..13476e84f97 100644 --- a/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml +++ b/tests/integration/targets/cloudwatch_metric_alarm/tasks/main.yml @@ -13,12 +13,9 @@ include_tasks: env_setup.yml - name: get info on alarms - command: aws cloudwatch describe-alarms --alarm-names {{ alarm_full_name }} - environment: - AWS_ACCESS_KEY_ID: '{{ aws_access_key }}' - AWS_SECRET_ACCESS_KEY: '{{ aws_secret_key }}' - AWS_SESSION_TOKEN: "{{ security_token | default('') }}" - AWS_DEFAULT_REGION: '{{ aws_region }}' + amazon.aws.cloudwatch_metric_alarm_info: + alarm_names: + - "{{ alarm_full_name }}" register: alarm_info_query - name: Make instance in a default subnet of the VPC @@ -59,24 +56,17 @@ register: ec2_instance_metric_alarm_check - name: get info on alarms - command: aws cloudwatch describe-alarms --alarm-names {{ alarm_full_name }} - environment: - AWS_ACCESS_KEY_ID: '{{ aws_access_key }}' - AWS_SECRET_ACCESS_KEY: '{{ aws_secret_key }}' - AWS_SESSION_TOKEN: "{{ security_token | default('') }}" - AWS_DEFAULT_REGION: '{{ aws_region }}' - register: alarm_info_query_check - - - name: convert it to an object - set_fact: - alarm_info_check: '{{ alarm_info_query_check.stdout | from_json }}' + amazon.aws.cloudwatch_metric_alarm_info: + alarm_names: + - "{{ alarm_full_name }}" + register: alarm_info_check - - name: verify that an alarm was not created in check mode + - name: "verify that an alarm was not created in check mode" assert: that: - - ec2_instance_metric_alarm_check.changed - - not ec2_instance_metric_alarm_check.alarm_arn - - alarm_info_check["MetricAlarms"] | length == 0 + - 'ec2_instance_metric_alarm_check.changed' + - 'not ec2_instance_metric_alarm_check.alarm_arn' + - 'alarm_info_check.metric_alarms | length == 0' - name: create ec2 metric alarm on ec2 instance ec2_metric_alarm: @@ -98,34 +88,27 @@ register: ec2_instance_metric_alarm - name: get info on alarms - command: aws cloudwatch describe-alarms --alarm-names {{ alarm_full_name }} - environment: - AWS_ACCESS_KEY_ID: '{{ aws_access_key }}' - AWS_SECRET_ACCESS_KEY: '{{ aws_secret_key }}' - AWS_SESSION_TOKEN: "{{ security_token | default('') }}" - AWS_DEFAULT_REGION: '{{ aws_region }}' - register: alarm_info_query - - - name: convert it to an object - set_fact: - alarm_info: '{{ alarm_info_query.stdout | from_json }}' + amazon.aws.cloudwatch_metric_alarm_info: + alarm_names: + - "{{ alarm_full_name }}" + register: alarm_info - - name: verify that an alarm was created + - name: "verify that an alarm was created" assert: that: - - ec2_instance_metric_alarm.changed - - ec2_instance_metric_alarm.alarm_arn - - ec2_instance_metric_alarm.statistic == alarm_info["MetricAlarms"][0].Statistic - - ec2_instance_metric_alarm.name == alarm_info["MetricAlarms"][0].AlarmName - - ec2_instance_metric_alarm.metric == alarm_info["MetricAlarms"][0].MetricName - - ec2_instance_metric_alarm.namespace == alarm_info["MetricAlarms"][0].Namespace - - ec2_instance_metric_alarm.comparison == alarm_info["MetricAlarms"][0].ComparisonOperator - - ec2_instance_metric_alarm.threshold == alarm_info["MetricAlarms"][0].Threshold - - ec2_instance_metric_alarm.period == alarm_info["MetricAlarms"][0].Period - - ec2_instance_metric_alarm.unit == alarm_info["MetricAlarms"][0].Unit - - ec2_instance_metric_alarm.evaluation_periods == alarm_info["MetricAlarms"][0].EvaluationPeriods - - ec2_instance_metric_alarm.description == alarm_info["MetricAlarms"][0].AlarmDescription - - ec2_instance_metric_alarm.treat_missing_data == alarm_info["MetricAlarms"][0].TreatMissingData + - 'ec2_instance_metric_alarm.changed' + - 'ec2_instance_metric_alarm.alarm_arn' + - 'ec2_instance_metric_alarm.statistic == alarm_info.metric_alarms[0].statistic' + - 'ec2_instance_metric_alarm.name == alarm_info.metric_alarms[0].alarm_name' + - 'ec2_instance_metric_alarm.metric == alarm_info.metric_alarms[0].metric_name' + - 'ec2_instance_metric_alarm.namespace == alarm_info.metric_alarms[0].namespace' + - 'ec2_instance_metric_alarm.comparison == alarm_info.metric_alarms[0].comparison_operator' + - 'ec2_instance_metric_alarm.threshold == alarm_info.metric_alarms[0].threshold' + - 'ec2_instance_metric_alarm.period == alarm_info.metric_alarms[0].period' + - 'ec2_instance_metric_alarm.unit == alarm_info.metric_alarms[0].unit' + - 'ec2_instance_metric_alarm.evaluation_periods == alarm_info.metric_alarms[0].evaluation_periods' + - 'ec2_instance_metric_alarm.description == alarm_info.metric_alarms[0].alarm_description' + - 'ec2_instance_metric_alarm.treat_missing_data == alarm_info.metric_alarms[0].treat_missing_data' - name: create ec2 metric alarm on ec2 instance (idempotent) (check mode) ec2_metric_alarm: @@ -148,42 +131,33 @@ register: ec2_instance_metric_alarm_idempotent_check - name: get info on alarms - command: aws cloudwatch describe-alarms --alarm-names {{ alarm_full_name }} - environment: - AWS_ACCESS_KEY_ID: '{{ aws_access_key }}' - AWS_SECRET_ACCESS_KEY: '{{ aws_secret_key }}' - AWS_SESSION_TOKEN: "{{ security_token | default('') }}" - AWS_DEFAULT_REGION: '{{ aws_region }}' - register: alarm_info_query_idempotent_check - - - name: convert it to an object - set_fact: - alarm_info_idempotent_check: '{{ alarm_info_query_idempotent_check.stdout | - from_json }}' - - - name: Verify alarm does not register as changed after update in check mode + amazon.aws.cloudwatch_metric_alarm_info: + alarm_names: + - "{{ alarm_full_name }}" + register: alarm_info_idempotent_check + + - name: "Verify alarm does not register as changed after update in check mode" assert: that: - - not ec2_instance_metric_alarm_idempotent_check.changed + - not ec2_instance_metric_alarm_idempotent_check.changed - - name: Verify alarm did not change after updating in check mode + - name: "Verify alarm did not change after updating in check mode" assert: that: - - alarm_info['MetricAlarms'][0]['{{item}}'] == alarm_info_idempotent_check['MetricAlarms'][0]['{{ - item }}'] + - "alarm_info.metric_alarms[0]['{{item}}'] == alarm_info_idempotent_check.metric_alarms[0]['{{ item }}']" with_items: - - AlarmArn - - Statistic - - AlarmName - - MetricName - - Namespace - - ComparisonOperator - - Threshold - - Period - - Unit - - EvaluationPeriods - - AlarmDescription - - TreatMissingData + - alarm_arn + - statistic + - alarm_name + - metric_name + - namespace + - comparison_operator + - threshold + - period + - unit + - evaluation_periods + - alarm_description + - treat_missing_data - name: create ec2 metric alarm on ec2 instance (idempotent) ec2_metric_alarm: @@ -205,41 +179,33 @@ register: ec2_instance_metric_alarm_idempotent - name: get info on alarms - command: aws cloudwatch describe-alarms --alarm-names {{ alarm_full_name }} - environment: - AWS_ACCESS_KEY_ID: '{{ aws_access_key }}' - AWS_SECRET_ACCESS_KEY: '{{ aws_secret_key }}' - AWS_SESSION_TOKEN: "{{ security_token | default('') }}" - AWS_DEFAULT_REGION: '{{ aws_region }}' - register: alarm_info_query_idempotent - - - name: convert it to an object - set_fact: - alarm_info_idempotent: '{{ alarm_info_query_idempotent.stdout | from_json }}' - - - name: Verify alarm does not register as changed after update + amazon.aws.cloudwatch_metric_alarm_info: + alarm_names: + - "{{ alarm_full_name }}" + register: alarm_info_idempotent_check + + - name: "Verify alarm does not register as changed after update in check mode" assert: that: - - not ec2_instance_metric_alarm_idempotent.changed + - not ec2_instance_metric_alarm_idempotent_check.changed - - name: Verify alarm did not change after updating + - name: "Verify alarm did not change after updating in check mode" assert: that: - - alarm_info['MetricAlarms'][0]['{{item}}'] == alarm_info_idempotent['MetricAlarms'][0]['{{ - item }}'] + - "alarm_info.metric_alarms[0]['{{item}}'] == alarm_info_idempotent_check.metric_alarms[0]['{{ item }}']" with_items: - - AlarmArn - - Statistic - - AlarmName - - MetricName - - Namespace - - ComparisonOperator - - Threshold - - Period - - Unit - - EvaluationPeriods - - AlarmDescription - - TreatMissingData + - alarm_arn + - statistic + - alarm_name + - metric_name + - namespace + - comparison_operator + - threshold + - period + - unit + - evaluation_periods + - alarm_description + - treat_missing_data - name: update alarm (check mode) ec2_metric_alarm: @@ -269,18 +235,18 @@ assert: that: - ec2_instance_metric_alarm_update_check.changed - - ec2_instance_metric_alarm_update_check.period == alarm_info["MetricAlarms"][0].Period # Period of actual alarm should not change - - ec2_instance_metric_alarm_update_check.alarm_arn == ec2_instance_metric_alarm.alarm_arn - - ec2_instance_metric_alarm_update_check.statistic == alarm_info["MetricAlarms"][0].Statistic - - ec2_instance_metric_alarm_update_check.name == alarm_info["MetricAlarms"][0].AlarmName - - ec2_instance_metric_alarm_update_check.metric == alarm_info["MetricAlarms"][0].MetricName - - ec2_instance_metric_alarm_update_check.namespace == alarm_info["MetricAlarms"][0].Namespace - - ec2_instance_metric_alarm_update_check.statistic == alarm_info["MetricAlarms"][0].Statistic - - ec2_instance_metric_alarm_update_check.comparison == alarm_info["MetricAlarms"][0].ComparisonOperator - - ec2_instance_metric_alarm_update_check.threshold == alarm_info["MetricAlarms"][0].Threshold - - ec2_instance_metric_alarm_update_check.unit == alarm_info["MetricAlarms"][0].Unit - - ec2_instance_metric_alarm_update_check.evaluation_periods == alarm_info["MetricAlarms"][0].EvaluationPeriods - - ec2_instance_metric_alarm_update_check.treat_missing_data == alarm_info["MetricAlarms"][0].TreatMissingData + - 'ec2_instance_metric_alarm_update_check.period == alarm_info.metric_alarms[0].period' # Period of actual alarm should not change + - 'ec2_instance_metric_alarm_update_check.alarm_arn == ec2_instance_metric_alarm.alarm_arn' + - 'ec2_instance_metric_alarm_update_check.statistic == alarm_info.metric_alarms[0].statistic' + - 'ec2_instance_metric_alarm_update_check.name == alarm_info.metric_alarms[0].alarm_name' + - 'ec2_instance_metric_alarm_update_check.metric == alarm_info.metric_alarms[0].metric_name' + - 'ec2_instance_metric_alarm_update_check.namespace == alarm_info.metric_alarms[0].namespace' + - 'ec2_instance_metric_alarm_update_check.statistic == alarm_info.metric_alarms[0].statistic' + - 'ec2_instance_metric_alarm_update_check.comparison == alarm_info.metric_alarms[0].comparison_operator' + - 'ec2_instance_metric_alarm_update_check.threshold == alarm_info.metric_alarms[0].threshold' + - 'ec2_instance_metric_alarm_update_check.unit == alarm_info.metric_alarms[0].unit' + - 'ec2_instance_metric_alarm_update_check.evaluation_periods == alarm_info.metric_alarms[0].evaluation_periods' + - 'ec2_instance_metric_alarm_update_check.treat_missing_data == alarm_info.metric_alarms[0].treat_missing_data' - name: update alarm ec2_metric_alarm: @@ -311,16 +277,16 @@ - ec2_instance_metric_alarm_update.changed - ec2_instance_metric_alarm_update.period == 60 # Period should be 60, not matching old value - ec2_instance_metric_alarm_update.alarm_arn == ec2_instance_metric_alarm.alarm_arn - - ec2_instance_metric_alarm_update.statistic == alarm_info["MetricAlarms"][0].Statistic - - ec2_instance_metric_alarm_update.name == alarm_info["MetricAlarms"][0].AlarmName - - ec2_instance_metric_alarm_update.metric == alarm_info["MetricAlarms"][0].MetricName - - ec2_instance_metric_alarm_update.namespace == alarm_info["MetricAlarms"][0].Namespace - - ec2_instance_metric_alarm_update.statistic == alarm_info["MetricAlarms"][0].Statistic - - ec2_instance_metric_alarm_update.comparison == alarm_info["MetricAlarms"][0].ComparisonOperator - - ec2_instance_metric_alarm_update.threshold == alarm_info["MetricAlarms"][0].Threshold - - ec2_instance_metric_alarm_update.unit == alarm_info["MetricAlarms"][0].Unit - - ec2_instance_metric_alarm_update.evaluation_periods == alarm_info["MetricAlarms"][0].EvaluationPeriods - - ec2_instance_metric_alarm_update.treat_missing_data == alarm_info["MetricAlarms"][0].TreatMissingData + - 'ec2_instance_metric_alarm_update.statistic == alarm_info.metric_alarms[0].statistic' + - 'ec2_instance_metric_alarm_update.name == alarm_info.metric_alarms[0].alarm_name' + - 'ec2_instance_metric_alarm_update.metric == alarm_info.metric_alarms[0].metric_name' + - 'ec2_instance_metric_alarm_update.namespace == alarm_info.metric_alarms[0].namespace' + - 'ec2_instance_metric_alarm_update.statistic == alarm_info.metric_alarms[0].statistic' + - 'ec2_instance_metric_alarm_update.comparison == alarm_info.metric_alarms[0].comparison_operator' + - 'ec2_instance_metric_alarm_update.threshold == alarm_info.metric_alarms[0].threshold' + - 'ec2_instance_metric_alarm_update.unit == alarm_info.metric_alarms[0].unit' + - 'ec2_instance_metric_alarm_update.evaluation_periods == alarm_info.metric_alarms[0].evaluation_periods' + - 'ec2_instance_metric_alarm_update.treat_missing_data == alarm_info.metric_alarms[0].treat_missing_data' - name: try to remove the alarm (check mode) ec2_metric_alarm: @@ -335,22 +301,15 @@ - ec2_instance_metric_alarm_deletion_check.changed - name: get info on alarms - command: aws cloudwatch describe-alarms --alarm-names {{ alarm_full_name }} - environment: - AWS_ACCESS_KEY_ID: '{{ aws_access_key }}' - AWS_SECRET_ACCESS_KEY: '{{ aws_secret_key }}' - AWS_SESSION_TOKEN: "{{ security_token | default('') }}" - AWS_DEFAULT_REGION: '{{ aws_region }}' + amazon.aws.cloudwatch_metric_alarm_info: + alarm_names: + - "{{ alarm_full_name }}" register: alarm_info_query_check - - name: convert it to an object - set_fact: - alarm_info: '{{ alarm_info_query_check.stdout | from_json }}' - - name: Verify that the alarm was not deleted in check mode using cli assert: that: - - alarm_info["MetricAlarms"] | length > 0 + - 'alarm_info.metric_alarms | length > 0' - name: try to remove the alarm ec2_metric_alarm: @@ -364,22 +323,15 @@ - ec2_instance_metric_alarm_deletion.changed - name: get info on alarms - command: aws cloudwatch describe-alarms --alarm-names {{ alarm_full_name }} - environment: - AWS_ACCESS_KEY_ID: '{{ aws_access_key }}' - AWS_SECRET_ACCESS_KEY: '{{ aws_secret_key }}' - AWS_SESSION_TOKEN: "{{ security_token | default('') }}" - AWS_DEFAULT_REGION: '{{ aws_region }}' - register: alarm_info_query - - - name: convert it to an object - set_fact: - alarm_info: '{{ alarm_info_query.stdout | from_json }}' + amazon.aws.cloudwatch_metric_alarm_info: + alarm_names: + - "{{ alarm_full_name }}" + register: alarm_info - name: Verify that the alarm was deleted using cli assert: that: - - alarm_info["MetricAlarms"] | length == 0 + - 'alarm_info.metric_alarms | length == 0' - name: create ec2 metric alarm with no unit on ec2 instance ec2_metric_alarm: @@ -400,34 +352,27 @@ register: ec2_instance_metric_alarm_no_unit - name: get info on alarms - command: aws cloudwatch describe-alarms --alarm-names {{ alarm_full_name }} - environment: - AWS_ACCESS_KEY_ID: '{{ aws_access_key }}' - AWS_SECRET_ACCESS_KEY: '{{ aws_secret_key }}' - AWS_SESSION_TOKEN: "{{ security_token | default('') }}" - AWS_DEFAULT_REGION: '{{ aws_region }}' - register: alarm_info_query_no_unit - - - name: convert it to an object - set_fact: - alarm_info_no_unit: '{{ alarm_info_query_no_unit.stdout | from_json }}' + amazon.aws.cloudwatch_metric_alarm_info: + alarm_names: + - "{{ alarm_full_name }}" + register: alarm_info_no_unit - name: verify that an alarm was created assert: that: - ec2_instance_metric_alarm_no_unit.changed - ec2_instance_metric_alarm_no_unit.alarm_arn - - ec2_instance_metric_alarm_no_unit.statistic == alarm_info_no_unit["MetricAlarms"][0].Statistic - - ec2_instance_metric_alarm_no_unit.name == alarm_info_no_unit["MetricAlarms"][0].AlarmName - - ec2_instance_metric_alarm_no_unit.metric == alarm_info_no_unit["MetricAlarms"][0].MetricName - - ec2_instance_metric_alarm_no_unit.namespace == alarm_info_no_unit["MetricAlarms"][0].Namespace - - ec2_instance_metric_alarm_no_unit.comparison == alarm_info_no_unit["MetricAlarms"][0].ComparisonOperator - - ec2_instance_metric_alarm_no_unit.threshold == alarm_info_no_unit["MetricAlarms"][0].Threshold - - ec2_instance_metric_alarm_no_unit.period == alarm_info_no_unit["MetricAlarms"][0].Period - - alarm_info_no_unit["MetricAlarms"][0].Unit is not defined - - ec2_instance_metric_alarm_no_unit.evaluation_periods == alarm_info_no_unit["MetricAlarms"][0].EvaluationPeriods - - ec2_instance_metric_alarm_no_unit.description == alarm_info_no_unit["MetricAlarms"][0].AlarmDescription - - ec2_instance_metric_alarm_no_unit.treat_missing_data == alarm_info_no_unit["MetricAlarms"][0].TreatMissingData + - 'ec2_instance_metric_alarm_no_unit.statistic == alarm_info_no_unit.metric_alarms[0].statistic' + - 'ec2_instance_metric_alarm_no_unit.name == alarm_info_no_unit.metric_alarms[0].alarm_name' + - 'ec2_instance_metric_alarm_no_unit.metric == alarm_info_no_unit.metric_alarms[0].metric_name' + - 'ec2_instance_metric_alarm_no_unit.namespace == alarm_info_no_unit.metric_alarms[0].namespace' + - 'ec2_instance_metric_alarm_no_unit.comparison == alarm_info_no_unit.metric_alarms[0].comparison_operator' + - 'ec2_instance_metric_alarm_no_unit.threshold == alarm_info_no_unit.metric_alarms[0].threshold' + - 'ec2_instance_metric_alarm_no_unit.period == alarm_info_no_unit.metric_alarms[0].period' + - 'alarm_info_no_unit.metric_alarms[0].Unit is not defined' + - 'ec2_instance_metric_alarm_no_unit.evaluation_periods == alarm_info_no_unit.metric_alarms[0].evaluation_periods' + - 'ec2_instance_metric_alarm_no_unit.description == alarm_info_no_unit.metric_alarms[0].alarm_description' + - 'ec2_instance_metric_alarm_no_unit.treat_missing_data == alarm_info_no_unit.metric_alarms[0].treat_missing_data' - name: try to remove the alarm ec2_metric_alarm: @@ -441,22 +386,15 @@ - ec2_instance_metric_alarm_deletion_no_unit.changed - name: get info on alarms - command: aws cloudwatch describe-alarms --alarm-names {{ alarm_full_name }} - environment: - AWS_ACCESS_KEY_ID: '{{ aws_access_key }}' - AWS_SECRET_ACCESS_KEY: '{{ aws_secret_key }}' - AWS_SESSION_TOKEN: "{{ security_token | default('') }}" - AWS_DEFAULT_REGION: '{{ aws_region }}' - register: alarm_info_query_no_unit - - - name: convert it to an object - set_fact: - alarm_info_no_unit: '{{ alarm_info_query_no_unit.stdout | from_json }}' + amazon.aws.cloudwatch_metric_alarm_info: + alarm_names: + - "{{ alarm_full_name }}" + register: alarm_info_no_unit - name: Verify that the alarm was deleted using cli assert: that: - - alarm_info_no_unit["MetricAlarms"] | length == 0 + - 'alarm_info_no_unit.metric_alarms | length == 0' always: - name: try to delete the alarm From a812e0284a82981a80e16f1a5df3ce65ffcebc1c Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Mon, 26 Sep 2022 13:56:53 -0700 Subject: [PATCH 11/14] Fix alarm type case --- plugins/modules/cloudwatch_metric_alarm_info.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/plugins/modules/cloudwatch_metric_alarm_info.py b/plugins/modules/cloudwatch_metric_alarm_info.py index 9132a7148f3..bff4dcc442d 100644 --- a/plugins/modules/cloudwatch_metric_alarm_info.py +++ b/plugins/modules/cloudwatch_metric_alarm_info.py @@ -31,11 +31,11 @@ alarm_type: description: - Specify this to return metric alarms or composite alarms. - - Module is defaulted to return metric alarms but can return composite alarms if I(alarm_type=composite_alarm). + - Module is defaulted to return metric alarms but can return composite alarms if I(alarm_type=CompositeAlarm). required: false type: str - default: metric_alarm - choices: [composite_alarm, metric_alarm] + default: MetricAlarm + choices: ['CompositeAlarm', 'MetricAlarm'] children_of_alarm_name: description: - If specified returns information about the "children" alarms of the alarm name specified. @@ -245,8 +245,7 @@ def describe_metric_alarms_info(connection, module): params = build_params(module) - alarm_type_mapping = {'composite_alarm': 'CompositeAlarm', 'metric_alarm': 'MetricAlarms'} - alarm_type_to_return = alarm_type_mapping[module.params.get('alarm_type')] + alarm_type_to_return = module.params.get('alarm_type') try: describe_metric_alarms_info_response = _describe_alarms(connection, **params) @@ -258,7 +257,7 @@ def describe_metric_alarms_info(connection, module): for response_list_item in describe_metric_alarms_info_response: result.append(camel_dict_to_snake_dict(response_list_item)) - if alarm_type_to_return == 'composite_alarm': + if alarm_type_to_return == 'CompositeAlarm': module.exit_json(composite_alarms=result) module.exit_json(metric_alarms=result) @@ -294,7 +293,7 @@ def main(): argument_spec = dict( alarm_names=dict(type='list', elements='str', required=False), alarm_name_prefix=dict(type='str', required=False), - alarm_type=dict(type='str', choices=['composite_alarm', 'metric_alarm'], default='metric_alarm', required=False), + alarm_type=dict(type='str', choices=['CompositeAlarm', 'MetricAlarm'], default='MetricAlarm', required=False), children_of_alarm_name=dict(type='str', required=False), parents_of_alarm_name=dict(type='str', required=False), state_value=dict(type='str', choices=['OK', 'ALARM', 'INSUFFICIENT_DATA'], required=False), From 977ca374a69093e3b03b1aaa434fb3493b72dcad Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 27 Sep 2022 09:43:31 -0700 Subject: [PATCH 12/14] minor bugfix in return info --- plugins/modules/cloudwatch_metric_alarm_info.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/plugins/modules/cloudwatch_metric_alarm_info.py b/plugins/modules/cloudwatch_metric_alarm_info.py index bff4dcc442d..d78e8302e49 100644 --- a/plugins/modules/cloudwatch_metric_alarm_info.py +++ b/plugins/modules/cloudwatch_metric_alarm_info.py @@ -249,17 +249,20 @@ def describe_metric_alarms_info(connection, module): try: describe_metric_alarms_info_response = _describe_alarms(connection, **params) - describe_metric_alarms_info_response = describe_metric_alarms_info_response[alarm_type_to_return] + # describe_metric_alarms_info_response = describe_metric_alarms_info_response[alarm_type_to_return] except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg='Failed to describe cloudwatch metric alarm') result = [] - for response_list_item in describe_metric_alarms_info_response: - result.append(camel_dict_to_snake_dict(response_list_item)) if alarm_type_to_return == 'CompositeAlarm': + for response_list_item in describe_metric_alarms_info_response['CompositeAlarms']: + result.append(camel_dict_to_snake_dict(response_list_item)) module.exit_json(composite_alarms=result) + for response_list_item in describe_metric_alarms_info_response['MetricAlarms']: + result.append(camel_dict_to_snake_dict(response_list_item)) + module.exit_json(metric_alarms=result) From 6865196b55766e5b35308518dcdb7033e622c7ac Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 27 Sep 2022 10:46:22 -0700 Subject: [PATCH 13/14] Added changelogs fragment --- .../988-cloudwatch_metric_alarm_info-add-new-module.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/988-cloudwatch_metric_alarm_info-add-new-module.yml diff --git a/changelogs/fragments/988-cloudwatch_metric_alarm_info-add-new-module.yml b/changelogs/fragments/988-cloudwatch_metric_alarm_info-add-new-module.yml new file mode 100644 index 00000000000..701766b78e4 --- /dev/null +++ b/changelogs/fragments/988-cloudwatch_metric_alarm_info-add-new-module.yml @@ -0,0 +1,2 @@ +minor_changes: +- cloudwatch_metric_alarm_info - Added a new module that describes the cloudwatch metric alarms (https://github.com/ansible-collections/amazon.aws/pull/988). \ No newline at end of file From a39e10a0252323efc9ea775a98dc573e7a0319d8 Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Tue, 27 Sep 2022 10:46:47 -0700 Subject: [PATCH 14/14] Added changelogs fragment --- .../988-cloudwatch_metric_alarm_info-add-new-module.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/988-cloudwatch_metric_alarm_info-add-new-module.yml b/changelogs/fragments/988-cloudwatch_metric_alarm_info-add-new-module.yml index 701766b78e4..7bd35fb878a 100644 --- a/changelogs/fragments/988-cloudwatch_metric_alarm_info-add-new-module.yml +++ b/changelogs/fragments/988-cloudwatch_metric_alarm_info-add-new-module.yml @@ -1,2 +1,2 @@ minor_changes: -- cloudwatch_metric_alarm_info - Added a new module that describes the cloudwatch metric alarms (https://github.com/ansible-collections/amazon.aws/pull/988). \ No newline at end of file +- cloudwatch_metric_alarm_info - Added a new module that describes the cloudwatch metric alarms (https://github.com/ansible-collections/amazon.aws/pull/988).