Debian Codegraph @

ansible_collections/purestorage/flasharray/plugins/modules/purefa_cbsexpand.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2023, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefa_cbsexpand
20 version_added: '1.0.0'
21 short_description: Modify the CBS array capacity
22 description:
23 - Expand the CBS array capacity. Capacity can only be updated to specific values.
24 author:
25 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
26 options:
27 state:
28 description:
29 - Action to be performed on the CBS array.
30 - I{list) will provide the options that I(capacity), in bytes, can be set to.
31 default: show
32 choices: [ show, expand ]
33 type: str
34 capacity:
35 description:
36 - Requested capacity of CBS array in bytes.
37 type: int
38 extends_documentation_fragment:
39 - purestorage.flasharray.purestorage.fa
40 """
41
42 EXAMPLES = r"""
43 - name: Show available expansion capacities
44 purestorage.flasharray.purefa_cbsexpand:
45 fa_url: 10.10.10.2
46 api_token: e31060a7-21fc-e277-6240-25983c6c4592
47
48 - name: Expand CBS to new capacity
49 purestorage.flasharray.purefa_cbsexpand:
50 state: expand
51 capacity: 10995116277760
52 fa_url: 10.10.10.2
53 api_token: e31060a7-21fc-e277-6240-25983c6c4592
54 """
55
56 RETURN = r"""
57 """
58
59 HAS_PURESTORAGE = True
60 try:
61 from pypureclient import flasharray
62 except ImportError:
63 HAS_PURESTORAGE = False
64
65 from ansible.module_utils.basic import AnsibleModule
66 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
67 LooseVersion,
68 )
69 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
70 get_array,
71 purefa_argument_spec,
72 )
73
74 EXPAND_API_VERSION = "2.29"
75
76
77 def _is_cbs(array):
78 """Is the selected array a Cloud Block Store"""
79 model = list(array.get_hardware(filter="type='controller'").items)[0].model
80 is_cbs = bool("CBS" in model)
81 return is_cbs
82
83
84 def list_capacity(module, array):
85 """Get available expansion points"""
86 steps = list(array.get_arrays_cloud_capacity_supported_steps().items)
87 available = []
88 for step in steps:
89 available.append(step.supported_capacity)
90 module.exit_json(changed=True, available=available)
91
92
93 def update_capacity(module, array):
94 """Expand CBS capacity"""
95 steps = list(array.get_arrays_cloud_capacity_supported_steps().items)
96 available = []
97 for step in steps:
98 available.append(step.supported_capacity)
99 if module.params["capacity"] not in available:
100 module.fail_json(
101 msg="Selected capacity is not available. "
102 "Run this module with `list` to get available capacity points."
103 )
104 expanded = array.patch_arrays_cloud_capacity(
105 capacity=flasharray.CloudCapacityStatus(
106 requested_capacity=module.params["capacity"]
107 )
108 )
109 if expanded.status_code != 200:
110 module.fail_json(
111 msg="Expansion request failed. Error: {0}".format(
112 expanded.errors[0].message
113 )
114 )
115
116
117 def main():
118 argument_spec = purefa_argument_spec()
119 argument_spec.update(
120 dict(
121 state=dict(type="str", default="show", choices=["show", "expand"]),
122 capacity=dict(type="int"),
123 )
124 )
125
126 required_if = [["state", "expand", ["capacity"]]]
127 module = AnsibleModule(
128 argument_spec, required_if=required_if, supports_check_mode=True
129 )
130
131 array = get_array(module)
132 if not HAS_PURESTORAGE:
133 module.fail_json(
134 msg="py-pure-client sdk is required to support 'count' parameter"
135 )
136 if not _is_cbs(array):
137 module.fail_json(msg="Module only valid on Cloud Block Store array")
138 api_version = array.get_rest_version()
139 if LooseVersion(EXPAND_API_VERSION) > LooseVersion(api_version):
140 module.fail_json(
141 msg="FlashArray REST version not supported. "
142 "Minimum version required: {0}".format(EXPAND_API_VERSION)
143 )
144 if module.params["state"] == "show":
145 list_capacity(module, array)
146 else:
147 update_capacity(module, array)
148 module.exit_json(changed=False)
149
150
151 if __name__ == "__main__":
152 main()