From c1c32b82272e304e4a1a177abdedd8fe22aa6ec3 Mon Sep 17 00:00:00 2001 From: Simon Dodsley Date: Wed, 18 Dec 2024 17:13:21 -0500 Subject: [PATCH] Add drives module --- README.md | 1 + plugins/modules/pure1_drives.py | 112 ++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 plugins/modules/pure1_drives.py diff --git a/README.md b/README.md index 49a00b7..db80d93 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ Release notes for this collection can be found [here](https://github.com/Pure-St - pure1_alerts - Get alerts from Pure1 - pure1_array_tags - Manage array tags for managed devices in Pure1 +- pure1_drives - Get array drives information from Pure1 - pure1_info - Get information on fleet configuration - pure1_nics - Get network interface information from Pure1 - pure1_pods - Get FlashArray pod information from Pure1 diff --git a/plugins/modules/pure1_drives.py b/plugins/modules/pure1_drives.py new file mode 100644 index 0000000..31c75b2 --- /dev/null +++ b/plugins/modules/pure1_drives.py @@ -0,0 +1,112 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# (c) 2024, Simon Dodsley (simon@purestorage.com) +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +ANSIBLE_METADATA = { + "metadata_version": "1.1", + "status": ["preview"], + "supported_by": "community", +} + +DOCUMENTATION = r""" +--- +module: pure1_drives +version_added: '1.5.0' +short_description: Collect array drives information from Pure1 +description: + - Collect array drives information from a Pure1 +options: + array: + description: + - Filter to provide only drives for a specifically named array or blade + type: str +author: + - Pure Storage Ansible Team (@sdodsley) +extends_documentation_fragment: + - purestorage.pure1.purestorage.p1 +""" + +EXAMPLES = r""" +- name: collect all drives information + purestorage.pure1.pure1_drives: + register: pure1_drives + +- name: collect only drives information for array X + purestorage.pure1.pure1_drives: + array: X + register: pure1_drives + +- name: show drives information + debug: + msg: "{{ pure1_info['pure1_drives']['drives'] }}" +""" + +RETURN = r""" +pure1_drives: + description: Returns array drives information collected from Pure1 + returned: always + type: dict +""" + + +from ansible.module_utils.basic import AnsibleModule +from ansible_collections.purestorage.pure1.plugins.module_utils.pure1 import ( + get_pure1, + pure1_argument_spec, +) + + +def generate_drives_dict(module, pure_1): + drives_info = {} + if module.params["array"]: + res = pure_1.get_drives(filter="arrays.name='" + module.params["array"] + "'") + if res.status_code == 200 and res.total_item_count != 0: + drives = list(res.items) + else: + module.warn( + "No drives information available for array {0}".format( + module.params["array"] + ) + ) + module.exit_json(changed=False) + else: + drives = list(pure_1.get_drives().items) + for drive in range(0, len(drives)): + array = drives[drive].arrays[0].name + drives_info[array] = [] + for drive in range(0, len(drives)): + array = drives[drive].arrays[0].name + drive_name = drives[drive].name + drive_details = { + drive_name: { + "capacity": getattr(drives[drive], "capacity", None), + "protocol": getattr(drives[drive], "protocol", None), + "status": getattr(drives[drive], "status", None), + "type": getattr(drives[drive], "type", None), + } + } + drives_info[array].append(drive_details) + return drives_info + + +def main(): + argument_spec = pure1_argument_spec() + argument_spec.update(dict(array=dict(type="str"))) + module = AnsibleModule(argument_spec, supports_check_mode=True) + pure_1 = get_pure1(module) + + drives = {} + + drives["drives"] = generate_drives_dict(module, pure_1) + + module.exit_json(changed=False, pure1_drives=drives) + + +if __name__ == "__main__": + main()