| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import (absolute_import, division, print_function) |
| 5 |
__metaclass__ = type |
| 6 |
|
| 7 |
DOCUMENTATION = """ |
| 8 |
name: santricity_volume |
| 9 |
author: |
| 10 |
- Nathan Swartz (@swartzn) |
| 11 |
- Vu Tran (@VuTran007) |
| 12 |
short_description: NetApp E-Series manage storage volumes |
| 13 |
description: |
| 14 |
- Collect volumes from NetApp E/EF-series storage array for defined host. |
| 15 |
""" |
| 16 |
|
| 17 |
EXAMPLES = r""" |
| 18 |
- name: Collect volume information for defined host |
| 19 |
ansible.builtin.set_fact: |
| 20 |
volumes: "{{ lookup('netapp_eseries.santricity.santricity_volume', hostvars[inventory_hostname])) }}" |
| 21 |
""" |
| 22 |
|
| 23 |
import re |
| 24 |
from ansible.plugins.lookup import LookupBase |
| 25 |
from ansible.errors import AnsibleError |
| 26 |
from itertools import product |
| 27 |
|
| 28 |
|
| 29 |
class LookupModule(LookupBase): |
| 30 |
|
| 31 |
def run(self, inventory, **kwargs): |
| 32 |
if isinstance(inventory, list): |
| 33 |
inventory = inventory[0] |
| 34 |
|
| 35 |
if ("eseries_storage_pool_configuration" not in inventory.keys() or not isinstance(inventory["eseries_storage_pool_configuration"], list) or |
| 36 |
len(inventory["eseries_storage_pool_configuration"]) == 0): |
| 37 |
return list() |
| 38 |
|
| 39 |
vol_list = list() |
| 40 |
for sp_info in inventory["eseries_storage_pool_configuration"]: |
| 41 |
if "name" not in sp_info.keys(): |
| 42 |
continue |
| 43 |
if "volumes" in sp_info.keys() and ("criteria_volume_count" in sp_info.keys() or "criteria_reserve_free_capacity_pct" in sp_info.keys()): |
| 44 |
raise AnsibleError("Incompatible parameters: You cannot specify both volumes with either criteria_volume_count or " |
| 45 |
"criteria_reserve_free_capacity for any given eseries_storage_pool_configuration entry.") |
| 46 |
if ("common_volume_configuration" in sp_info.keys() and isinstance(sp_info["common_volume_configuration"], dict) and |
| 47 |
"size" in sp_info["common_volume_configuration"].keys() and "criteria_reserve_free_capacity_pct" in sp_info.keys()): |
| 48 |
raise AnsibleError("Incompatible parameters: You cannot specify both size in common_volume_configuration with " |
| 49 |
"criteria_reserve_free_capacity for any given eseries_storage_pool_configuration entry.") |
| 50 |
|
| 51 |
if "volumes" not in sp_info.keys(): |
| 52 |
if "criteria_volume_count" in sp_info.keys(): |
| 53 |
if "common_volume_configuration" not in sp_info: |
| 54 |
sp_info.update({"common_volume_configuration": {}}) |
| 55 |
|
| 56 |
reserve_free_capacity_pct = sp_info["criteria_reserve_free_capacity_pct"] if "criteria_reserve_free_capacity_pct" in sp_info.keys() else 0.0 |
| 57 |
volume_size = (100.0 - reserve_free_capacity_pct) / sp_info["criteria_volume_count"] |
| 58 |
count_digits = len(str(sp_info["criteria_volume_count"])) |
| 59 |
|
| 60 |
if "size" not in sp_info["common_volume_configuration"].keys(): |
| 61 |
sp_info["common_volume_configuration"].update({"size": volume_size, "size_unit": "pct"}) |
| 62 |
if "host" not in sp_info["common_volume_configuration"].keys() and "common_volume_host" in sp_info.keys(): |
| 63 |
sp_info["common_volume_configuration"].update({"host": sp_info["common_volume_host"]}) |
| 64 |
|
| 65 |
if (("eseries_remove_all_configuration_state" in inventory and inventory["eseries_remove_all_configuration_state"] == "absent") or |
| 66 |
("state" in sp_info and sp_info["state"] == "absent") or |
| 67 |
("state" not in sp_info and "eseries_volume_state" in inventory and inventory["eseries_volume_state"] == "absent")): |
| 68 |
sp_info["common_volume_configuration"].update({"state": "absent"}) |
| 69 |
else: |
| 70 |
sp_info["common_volume_configuration"].update({"state": "present"}) |
| 71 |
|
| 72 |
for count in range(sp_info["criteria_volume_count"]): |
| 73 |
if "volumes" not in sp_info.keys(): |
| 74 |
sp_info.update({"volumes": []}) |
| 75 |
sp_info["volumes"].append({"name": "[pool]_%0*d" % (count_digits, count)}) |
| 76 |
else: |
| 77 |
continue |
| 78 |
|
| 79 |
elif not isinstance(sp_info["volumes"], list): |
| 80 |
raise AnsibleError("Volumes must be a list") |
| 81 |
|
| 82 |
for sp in patternize(sp_info["name"], inventory): |
| 83 |
for vol_info in sp_info["volumes"]: |
| 84 |
|
| 85 |
if not isinstance(vol_info, dict): |
| 86 |
raise AnsibleError("Volume in the storage pool, %s, must be a dictionary." % sp_info["name"]) |
| 87 |
|
| 88 |
for vol in patternize(vol_info["name"], inventory, storage_pool=sp): |
| 89 |
vol_options = dict() |
| 90 |
|
| 91 |
|
| 92 |
combined_volume_metadata = {} |
| 93 |
if "common_volume_configuration" in sp_info: |
| 94 |
for option, value in sp_info["common_volume_configuration"].items(): |
| 95 |
vol_options.update({option: value}) |
| 96 |
if "volume_metadata" in sp_info["common_volume_configuration"].keys(): |
| 97 |
combined_volume_metadata.update(sp_info["common_volume_configuration"]["volume_metadata"]) |
| 98 |
|
| 99 |
|
| 100 |
for option, value in vol_info.items(): |
| 101 |
vol_options.update({option: value}) |
| 102 |
if "volume_metadata" in vol_info.keys(): |
| 103 |
combined_volume_metadata.update(vol_info["volume_metadata"]) |
| 104 |
vol_options.update({"volume_metadata": combined_volume_metadata}) |
| 105 |
|
| 106 |
if (("eseries_remove_all_configuration_state" in inventory and inventory["eseries_remove_all_configuration_state"] == "absent") or |
| 107 |
("state" in sp_info and sp_info["state"] == "absent") or |
| 108 |
("state" not in sp_info and "eseries_volume_state" in inventory and inventory["eseries_volume_state"] == "absent")): |
| 109 |
vol_options.update({"state": "absent"}) |
| 110 |
else: |
| 111 |
vol_options.update({"state": "present"}) |
| 112 |
|
| 113 |
vol_options.update({"name": vol, "storage_pool_name": sp}) |
| 114 |
vol_list.append(vol_options) |
| 115 |
return vol_list |
| 116 |
|
| 117 |
|
| 118 |
def patternize(pattern, inventory, storage_pool=None): |
| 119 |
"""Generate list of strings determined by a pattern""" |
| 120 |
if storage_pool: |
| 121 |
pattern = pattern.replace("[pool]", storage_pool) |
| 122 |
|
| 123 |
if inventory: |
| 124 |
inventory_tokens = re.findall(r"\[[a-zA-Z0-9_]*\]", pattern) |
| 125 |
for token in inventory_tokens: |
| 126 |
pattern = pattern.replace(token, str(inventory[token[1:-1]])) |
| 127 |
|
| 128 |
tokens = re.findall(r"\[[0-9]-[0-9]\]|\[[a-z]-[a-z]\]|\[[A-Z]-[A-Z]\]", pattern) |
| 129 |
segments = "%s".join(re.split(r"\[[0-9]-[0-9]\]|\[[a-z]-[a-z]\]|\[[A-Z]-[A-Z]\]", pattern)) |
| 130 |
|
| 131 |
if len(tokens) == 0: |
| 132 |
return [pattern] |
| 133 |
|
| 134 |
combinations = [] |
| 135 |
for token in tokens: |
| 136 |
start, stop = token[1:-1].split("-") |
| 137 |
|
| 138 |
try: |
| 139 |
start = int(start) |
| 140 |
stop = int(stop) |
| 141 |
combinations.append([str(number) for number in range(start, stop + 1)]) |
| 142 |
except ValueError: |
| 143 |
combinations.append([chr(number) for number in range(ord(start), ord(stop) + 1)]) |
| 144 |
|
| 145 |
return [segments % subset for subset in list(product(*combinations))] |