Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backup_plan_info: bugfix to enable getting info of all backup plans #2083

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if this is a bugfix. This feature was not working because it was not implemented and the module does not say it supports listing all backup plans (that's why backup_plan_names was a required parameter). To me it is probably minor_changes, a new functionality. You should also document that when O(backup_plan_names) is omitted the module gathers information about all backup plans.

Copy link
Contributor Author

@mandar242 mandar242 May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added it as a bugfix because the existing module code documentation had an example to do this
but example was not working
https://github.com/ansible-collections/amazon.aws/blob/main/plugins/modules/backup_plan_info.py#L33-L35
@alinabuzachis

- backup_plan_info - Bugfix to enable getting info of all backup plans (https://github.com/ansible-collections/amazon.aws/pull/2083).
23 changes: 17 additions & 6 deletions plugins/modules/backup_plan_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
backup_plan_names:
type: list
elements: str
required: true
description:
- Specifies a list of plan names.
extends_documentation_fragment:
Expand All @@ -31,10 +30,11 @@

EXAMPLES = r"""
# Note: These examples do not set authentication details, see the AWS Guide for details.
# Gather information about all backup plans
- amazon.aws.backup_plan_info
# Gather information about a particular backup plan
- amazon.aws.backup_plan_info:
- name: Gather information about all backup plans
amazon.aws.backup_plan_info:

- name: Gather information about a particular backup plan
amazon.aws.backup_plan_info:
backup plan_names:
- elastic
"""
Expand Down Expand Up @@ -110,10 +110,21 @@
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry


def get_all_backup_plans_info(client):
paginator = client.get_paginator("list_backup_plans")
return paginator.paginate().build_full_result()


def get_backup_plan_detail(client, module):
backup_plan_list = []
backup_plan_names = module.params.get("backup_plan_names")

if backup_plan_names is None:
backup_plan_names = []
backup_plan_list_info = get_all_backup_plans_info(client)["BackupPlansList"]
for backup_plan in backup_plan_list_info:
backup_plan_names.append(backup_plan["BackupPlanName"])

for name in backup_plan_names:
backup_plan_list.extend(get_plan_details(module, client, name))

Expand All @@ -122,7 +133,7 @@ def get_backup_plan_detail(client, module):

def main():
argument_spec = dict(
backup_plan_names=dict(type="list", elements="str", required=True),
backup_plan_names=dict(type="list", elements="str"),
)

module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)
Expand Down
31 changes: 30 additions & 1 deletion tests/integration/targets/backup_plan/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,41 @@
- backup_plan_create_result.exists is true
- backup_plan_create_result.changed is false

- name: Create another backup plan
amazon.aws.backup_plan:
backup_plan_name: "{{ backup_plan_name }}-1"
rules:
- rule_name: daily
target_backup_vault_name: "{{ backup_vault_name }}"
tags:
Environment: Test
register: backup_plan_create_result_1

- name: Verify backup plan create result
ansible.builtin.assert:
that:
- backup_plan_create_result_1.exists is true
- backup_plan_create_result_1.changed is true

- name: Get info of all install plans
amazon.aws.backup_plan_info:
register: backup_plan_info_result

- name: Assert that info of all backup plans is fetched
ansible.builtin.assert:
that:
- backup_plan_info_result is not failed
- backup_plan_info_result.backup_plans | length > 1

always:
- name: Delete AWS Backup plan created during this test
amazon.aws.backup_plan:
backup_plan_name: "{{ backup_plan_name }}"
backup_plan_name: "{{ item }}"
state: absent
ignore_errors: true
with_items:
- "{{ backup_plan_name }}"
- "{{ backup_plan_name }}-1"

- name: Delete AWS Backup vault created during this test
amazon.aws.backup_vault:
Expand Down
Loading