Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2018, 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_timeout
20 version_added: '1.0.0'
21 short_description: Configure Pure Storage FlashArray GUI idle timeout
22 description:
23 - Configure GUI idle timeout for Pure Storage FlashArrays.
24 - This does not affect existing GUI sessions.
25 author:
26 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
27 options:
28 state:
29 description:
30 - Set or disable the GUI idle timeout
31 default: present
32 type: str
33 choices: [ present, absent ]
34 timeout:
35 description:
36 - Minutes for idle timeout.
37 type: int
38 default: 30
39 context:
40 description:
41 - Name of fleet member on which to perform the operation.
42 - This requires the array receiving the request is a member of a fleet
43 and the context name to be a member of the same fleet.
44 type: str
45 default: ""
46 version_added: '1.39.0'
47 extends_documentation_fragment:
48 - purestorage.flasharray.purestorage.fa
49 """
50
51 EXAMPLES = r"""
52 - name: Set GUI idle timeout to 25 minutes
53 purestorage.flasharray.purefa_timeout:
54 timeout: 25
55 state: present
56 fa_url: 10.10.10.2
57 api_token: e31060a7-21fc-e277-6240-25983c6c4592
58
59 - name: Disable idle timeout
60 purestorage.flasharray.purefa_timeout:
61 state: absent
62 fa_url: 10.10.10.2
63 api_token: e31060a7-21fc-e277-6240-25983c6c4592
64 """
65
66 RETURN = r"""
67 """
68
69 HAS_PURESTORAGE = True
70 try:
71 from pypureclient.flasharray import Arrays
72 except ImportError:
73 HAS_PURESTORAGE = False
74
75 from ansible.module_utils.basic import AnsibleModule
76 from ansible_collections.purestorage.flasharray.plugins.module_utils.purefa import (
77 get_array,
78 purefa_argument_spec,
79 )
80 from ansible_collections.purestorage.flasharray.plugins.module_utils.version import (
81 LooseVersion,
82 )
83
84 CONTEXT_VERSION = "2.38"
85
86
87 def set_timeout(module, array):
88 """Set GUI idle timeout"""
89 changed = True
90 api_version = array.get_rest_version()
91 if not module.check_mode:
92 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
93 res = array.patch_arrays(
94 array=Arrays(idle_timeout=module.params["timeout"]),
95 context_names=[module.params["context"]],
96 )
97 else:
98 res = array.patch_arrays(
99 array=Arrays(idle_timeout=module.params["timeout"])
100 )
101 if res.status_code != 200:
102 module.fail_json(
103 msg="Failed to set GUI idle timeout. Error: {0}".format(
104 res.errors[0].message
105 )
106 )
107
108 module.exit_json(changed=changed)
109
110
111 def disable_timeout(module, array):
112 """Disable idle timeout"""
113 changed = True
114 api_version = array.get_rest_version()
115 if not module.check_mode:
116 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
117 res = array.patch_arrays(
118 array=Arrays(idle_timeout=0), context_names=[module.params["context"]]
119 )
120 else:
121 res = array.patch_arrays(array=Arrays(idle_timeout=0))
122 if res.status_code != 200:
123 module.fail_json(msg="Failed to disable GUI idle timeout")
124 module.exit_json(changed=changed)
125
126
127 def main():
128 argument_spec = purefa_argument_spec()
129 argument_spec.update(
130 dict(
131 timeout=dict(type="int", default=30),
132 state=dict(type="str", default="present", choices=["present", "absent"]),
133 context=dict(type="str", default=""),
134 )
135 )
136
137 module = AnsibleModule(argument_spec, supports_check_mode=True)
138
139 state = module.params["state"]
140 if 5 < module.params["timeout"] > 180 and module.params["timeout"] != 0:
141 module.fail_json(msg="Timeout value must be between 5 and 180 minutes")
142 module.params["timeout"] = module.params["timeout"] * 60000
143 if not HAS_PURESTORAGE:
144 module.fail_json(msg="py-pure-client sdk is required for this module")
145 array = get_array(module)
146 api_version = array.get_rest_version()
147 if LooseVersion(CONTEXT_VERSION) <= LooseVersion(api_version):
148 current_timeout = list(
149 array.get_arrays(context_names=[module.params["context"]]).items
150 )[0].idle_timeout
151 else:
152 current_timeout = list(array.get_arrays().items)[0].idle_timeout
153 if state == "present" and current_timeout != module.params["timeout"]:
154 set_timeout(module, array)
155 elif state == "absent" and current_timeout != 0:
156 disable_timeout(module, array)
157
158 module.exit_json(changed=False)
159
160
161 if __name__ == "__main__":
162 main()