Skip to content

Commit

Permalink
backup_plan_info: bugfix to enable getting info of all backup plans (a…
Browse files Browse the repository at this point in the history
…nsible-collections#2083)

backup_plan_info: bugfix to enable getting info of all backup plans

SUMMARY

Fix being unable to fetch info of all backup plans.
With backup_plan_names being a required parameter, the functionality to get all plans info was not working
# Gather information about all backup plans
- name: Get info of all backup plans 
  amazon.aws.backup_plan_info:
  register: plan_info_result

gave
**********
fatal: [localhost]: FAILED! => {"changed": false, "msg": "missing required arguments: backup_plan_names"}



ISSUE TYPE


Bugfix Pull Request

COMPONENT NAME

backup_plan_info
ADDITIONAL INFORMATION

Reviewed-by: GomathiselviS
Reviewed-by: Alina Buzachis
Reviewed-by: Mandar Kulkarni <[email protected]>
Reviewed-by: Mark Chappell
  • Loading branch information
mandar242 authored Jun 4, 2024
1 parent 3d62e43 commit 9c53b6c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- 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

0 comments on commit 9c53b6c

Please sign in to comment.