Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2021, 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_eradication
20 version_added: '1.9.0'
21 short_description: Configure Pure Storage FlashArray Eradication Timer
22 description:
23 - Configure the eradication timer for destroyed items on a FlashArray.
24 - Valid values are integer days from 1 to 30. Default is 1.
25 author:
26 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
27 options:
28 timer:
29 description:
30 - Set the eradication timer for the FlashArray
31 - Allowed values are integers from 1 to 30. Default is 1
32 - This parameter is not allowed to be specified with I(disabled_delay)
33 or I(enabled_delay)
34 type: int
35 disabled_delay:
36 description:
37 - Configures the eradication delay
38 for destroyed objects that I(are) protected by SafeMode (objects for which
39 eradication is disabled)
40 - Allowed values are integers from 1 to 30. Default is 1
41 default: 1
42 type: int
43 version_added: "1.22.0"
44 enabled_delay:
45 description:
46 - Configures the eradication delay
47 for destroyed objects that I(are not) protected by SafeMode (objects for which
48 eradication is disabled)
49 - Allowed values are integers from 1 to 30. Default is 1
50 default: 1
51 type: int
52 version_added: "1.22.0"
53 context:
54 description:
55 - Name of fleet member on which to perform the operation.
56 - This requires the array receiving the request is a member of a fleet
57 and the context name to be a member of the same fleet.
58 type: str
59 default: ""
60 version_added: '1.39.0'
61 extends_documentation_fragment:
62 - purestorage.flasharray.purestorage.fa
63 """
64
65 EXAMPLES = r"""
66 - name: Set eradication timer to 30 days
67 purestorage.flasharray.purefa_eradication:
68 timer: 30
69 fa_url: 10.10.10.2
70 api_token: e31060a7-21fc-e277-6240-25983c6c4592
71
72 - name: Set eradication timer to 1 day
73 purestorage.flasharray.purefa_eradication:
74 fa_url: 10.10.10.2
75 api_token: e31060a7-21fc-e277-6240-25983c6c4592
76 """
77
78 RETURN = r"""
79 """
80
81 HAS_PURESTORAGE = True
82 try:
83 from pypureclient.flasharray import Arrays, EradicationConfig
84 except ImportError:
85 HAS_PURESTORAGE = False
86
87
88 from ansible.module_utils.basic import AnsibleModule
89 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
90 get_array,
91 purefa_argument_spec,
92 )
93 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
94 LooseVersion,
95 )
96
97 SEC_PER_DAY = 86400000
98 ERADICATION_API_VERSION = "2.6"
99 DELAY_API_VERSION = "2.26"
100 CONTEXT_VERSION = "2.38"
101
102
103 def main():
104 argument_spec = purefa_argument_spec()
105 argument_spec.update(
106 dict(
107 timer=dict(type="int"),
108 disabled_delay=dict(type="int", default=1),
109 enabled_delay=dict(type="int", default=1),
110 context=dict(type="str", default=""),
111 )
112 )
113 mutually_exclusive = [["timer", "disabled_delay"], ["timer", "enabled_delay"]]
114 module = AnsibleModule(
115 argument_spec, supports_check_mode=True, mutually_exclusive=mutually_exclusive
116 )
117 if module.params["timer"] and not 30 >= module.params["timer"] >= 1:
118 module.fail_json(msg="Eradication Timer must be between 1 and 30 days.")
119 if not 30 >= module.params["disabled_delay"] >= 1:
120 module.fail_json(msg="disabled_delay must be between 1 and 30 days.")
121 if not 30 >= module.params["enabled_delay"] >= 1:
122 module.fail_json(msg="enabled_delay must be between 1 and 30 days.")
123 if not HAS_PURESTORAGE:
124 module.fail_json(msg="py-pure-client sdk is required for this module")
125 array = get_array(module)
126 api_version = array.get_rest_version()
127 changed = False
128 current_disabled = None
129 current_enabled = None
130 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
131 current_eradication_config = list(
132 array.get_arrays(context_names=[module.params["context"]]).items
133 )[0].eradication_config
134 else:
135 current_eradication_config = list(array.get_arrays().items)[
136 0
137 ].eradication_config
138 if LooseVersion(ERADICATION_API_VERSION) <= LooseVersion(api_version):
139 base_eradication_timer = getattr(
140 current_eradication_config, "eradication_delay", None
141 )
142 if base_eradication_timer:
143 current_eradication_timer = int(base_eradication_timer / SEC_PER_DAY)
144 if not module.params["timer"]:
145 module.params["timer"] = current_eradication_timer
146 if LooseVersion(DELAY_API_VERSION) <= LooseVersion(api_version):
147 current_disabled = int(
148 current_eradication_config.disabled_delay / SEC_PER_DAY
149 )
150 current_enabled = int(
151 current_eradication_config.enabled_delay / SEC_PER_DAY
152 )
153 if (
154 base_eradication_timer
155 and module.params["timer"] != current_eradication_timer
156 ):
157 if module.params["timer"] != current_eradication_timer:
158 target_timer = module.params["timer"]
159 else:
160 target_timer = current_eradication_timer
161 changed = True
162 if not module.check_mode:
163 new_timer = SEC_PER_DAY * target_timer
164 eradication_config = EradicationConfig(eradication_delay=new_timer)
165 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
166 res = array.patch_arrays(
167 context_names=[module.params["context"]],
168 array=Arrays(eradication_config=eradication_config),
169 )
170 else:
171 res = array.patch_arrays(
172 array=Arrays(eradication_config=eradication_config)
173 )
174 if res.status_code != 200:
175 module.fail_json(
176 msg="Failed to change Eradication Timer. Error: {0}".format(
177 res.errors[0].message
178 )
179 )
180 if current_disabled and (
181 module.params["enabled_delay"] != current_enabled
182 or module.params["disabled_delay"] != current_disabled
183 ):
184 changed = True
185 if not module.check_mode:
186 new_disabled = SEC_PER_DAY * module.params["disabled_delay"]
187 new_enabled = SEC_PER_DAY * module.params["enabled_delay"]
188 eradication_config = EradicationConfig(
189 enabled_delay=new_enabled, disabled_delay=new_disabled
190 )
191 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
192 res = array.patch_arrays(
193 context_names=[module.params["context"]],
194 array=Arrays(eradication_config=eradication_config),
195 )
196 else:
197 res = array.patch_arrays(
198 array=Arrays(eradication_config=eradication_config)
199 )
200 if res.status_code != 200:
201 module.fail_json(
202 msg="Failed to change Eradication Timers. Error: {0}".format(
203 res.errors[0].message
204 )
205 )
206 else:
207 module.fail_json(
208 msg="Purity version does not support changing Eradication Timer"
209 )
210 module.exit_json(changed=changed)
211
212
213 if __name__ == "__main__":
214 main()