Debian Codegraph @

ansible_collections/netapp/ontap/plugins/modules/na_ontap_cli_timeout.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: NetApp, Inc
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 __metaclass__ = type
9
10 DOCUMENTATION = """
11 module: na_ontap_cli_timeout
12 short_description: NetApp ONTAP module to set the CLI inactivity timeout value.
13 extends_documentation_fragment:
14 - netapp.ontap.netapp.na_ontap_rest
15 version_added: '22.9.0'
16 author: NetApp Ansible Team (@carchi8py) <[email protected]>
17 description:
18 - Modify the timeout value for CLI sessions.
19 options:
20 state:
21 description:
22 - Modify timeout value, only present is supported.
23 choices: ['present']
24 type: str
25 default: present
26 timeout:
27 description:
28 - Specifies the timeout value, in minutes.
29 - To prevent CLI sessions from timing out, specify a value of 0 (zero).
30 type: int
31 required: true
32
33 notes:
34 - Only supported with REST and requires ONTAP 9.6 or later.
35 """
36
37 EXAMPLES = """
38 - name: Modify the timeout value for CLI sessions to be 15 minutes
39 netapp.ontap.na_ontap_cli_timeout:
40 state: present
41 timeout: 15
42 hostname: "{{ netapp_hostname }}"
43 username: "{{ netapp_username }}"
44 password: "{{ netapp_password }}"
45 https: true
46 validate_certs: "{{ validate_certs }}"
47
48 - name: Prevent CLI sessions from timing out
49 netapp.ontap.na_ontap_cli_timeout:
50 state: present
51 timeout: 0
52 hostname: "{{ netapp_hostname }}"
53 username: "{{ netapp_username }}"
54 password: "{{ netapp_password }}"
55 https: true
56 validate_certs: "{{ validate_certs }}"
57 """
58
59 RETURN = """
60 """
61
62 import traceback
63 from ansible.module_utils.basic import AnsibleModule
64 from ansible.module_utils._text import to_native
65 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
66 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
67 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
68
69
70 class NetAppOntapCliTimeout:
71 def __init__(self):
72 self.argument_spec = netapp_utils.na_ontap_rest_only_spec()
73 self.argument_spec.update(dict(
74 state=dict(required=False, type='str', choices=['present'], default='present'),
75 timeout=dict(required=True, type='int')
76 ))
77 self.module = AnsibleModule(
78 argument_spec=self.argument_spec,
79 supports_check_mode=True
80 )
81 self.na_helper = NetAppModule(self.module)
82 self.parameters = self.na_helper.check_and_set_parameters(self.module)
83 self.rest_api = netapp_utils.OntapRestAPI(self.module)
84 self.rest_api.fail_if_not_rest_minimum_version('na_ontap_cli_timeout:', 9, 6)
85
86 def get_timeout_value_rest(self):
87 """ Get CLI inactivity timeout value """
88 fields = 'timeout'
89 api = 'private/cli/system/timeout'
90 record, error = rest_generic.get_one_record(self.rest_api, api, query=None, fields=fields)
91 if error:
92 self.module.fail_json(msg="Error fetching CLI sessions timeout value: %s" % to_native(error),
93 exception=traceback.format_exc())
94 if record:
95 return {
96 'timeout': record.get('timeout')
97 }
98 return None
99
100 def modify_timeout_value_rest(self, modify):
101 """ Modify CLI inactivity timeout value """
102 api = 'private/cli/system/timeout'
103 dummy, error = rest_generic.patch_async(self.rest_api, api, uuid_or_name=None, body=modify)
104 if error:
105 self.module.fail_json(msg='Error modifying CLI sessions timeout value: %s.' % to_native(error),
106 exception=traceback.format_exc())
107
108 def apply(self):
109 current = self.get_timeout_value_rest()
110 modify = self.na_helper.get_modified_attributes(current, self.parameters)
111 if self.na_helper.changed and not self.module.check_mode:
112 self.modify_timeout_value_rest(modify)
113 result = netapp_utils.generate_result(self.na_helper.changed, modify=modify)
114 self.module.exit_json(**result)
115
116
117 def main():
118 cli_timeout = NetAppOntapCliTimeout()
119 cli_timeout.apply()
120
121
122 if __name__ == '__main__':
123 main()