Debian Codegraph @

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

raw
1 #!/usr/bin/python
2
3 # (c) 2018-2025, NetApp Inc.
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 '''
7 na_ontap_vscan
8 '''
9
10 from __future__ import absolute_import, division, print_function
11
12 __metaclass__ = type
13
14 ANSIBLE_METADATA = {'metadata_version': '1.1',
15 'status': ['preview'],
16 'supported_by': 'certified'}
17
18 DOCUMENTATION = '''
19 module: na_ontap_vscan
20 short_description: NetApp ONTAP Vscan enable/disable.
21 extends_documentation_fragment:
22 - netapp.ontap.netapp.na_ontap
23 version_added: 2.9.0
24 author: NetApp Ansible Team (@carchi8py) <[email protected]>
25 notes:
26 - on demand task, on_access_policy and scanner_pools must be set up before running this module
27 description:
28 - Enable and Disable Vscan
29 options:
30 enable:
31 description:
32 - Whether to enable to disable a Vscan
33 type: bool
34 default: True
35
36 vserver:
37 description:
38 - the name of the data vserver to use.
39 required: true
40 type: str
41 '''
42
43 EXAMPLES = """
44 - name: Enable Vscan
45 netapp.ontap.na_ontap_vscan:
46 enable: true
47 username: '{{ netapp_username }}'
48 password: '{{ netapp_password }}'
49 hostname: '{{ netapp_hostname }}'
50 vserver: ansibleSVM
51
52 - name: Disable Vscan
53 netapp.ontap.na_ontap_vscan:
54 enable: false
55 username: '{{ netapp_username }}'
56 password: '{{ netapp_password }}'
57 hostname: '{{ netapp_hostname }}'
58 vserver: ansibleSVM
59 """
60
61 RETURN = """
62
63 """
64
65 import traceback
66
67 from ansible.module_utils.basic import AnsibleModule
68 from ansible.module_utils._text import to_native
69 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
70 from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI
71 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
72
73 HAS_NETAPP_LIB = netapp_utils.has_netapp_lib()
74
75
76 class NetAppOntapVscan(object):
77 ''' enable/disable vscan '''
78 def __init__(self):
79 self.use_rest = False
80 self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
81 self.argument_spec.update(dict(
82 enable=dict(type='bool', default=True),
83 vserver=dict(required=True, type='str'),
84 ))
85 self.module = AnsibleModule(
86 argument_spec=self.argument_spec,
87 supports_check_mode=True
88 )
89 self.na_helper = NetAppModule()
90 self.parameters = self.na_helper.set_parameters(self.module.params)
91
92 # API should be used for ONTAP 9.6 or higher, Zapi for lower version
93 self.rest_api = OntapRestAPI(self.module)
94 if self.rest_api.is_rest():
95 self.use_rest = True
96 else:
97 if HAS_NETAPP_LIB is False:
98 self.module.fail_json(msg="the python NetApp-Lib module is required")
99 else:
100 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver'])
101
102 def get_vscan(self):
103 if self.use_rest:
104 params = {'fields': 'svm,enabled',
105 "svm.name": self.parameters['vserver']}
106 api = "protocols/vscan"
107 message, error = self.rest_api.get(api, params)
108 if error:
109 self.module.fail_json(msg=error)
110 return message['records'][0]
111 else:
112 vscan_status_iter = netapp_utils.zapi.NaElement('vscan-status-get-iter')
113 vscan_status_info = netapp_utils.zapi.NaElement('vscan-status-info')
114 vscan_status_info.add_new_child('vserver', self.parameters['vserver'])
115 query = netapp_utils.zapi.NaElement('query')
116 query.add_child_elem(vscan_status_info)
117 vscan_status_iter.add_child_elem(query)
118 try:
119 result = self.server.invoke_successfully(vscan_status_iter, True)
120 except netapp_utils.zapi.NaApiError as error:
121 self.module.fail_json(msg='Error getting Vscan info for Vserver %s: %s' %
122 (self.parameters['vserver'], to_native(error)),
123 exception=traceback.format_exc())
124 if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1:
125 return result.get_child_by_name('attributes-list').get_child_by_name('vscan-status-info')
126
127 def enable_vscan(self, uuid=None):
128 if self.use_rest:
129 params = {"svm.name": self.parameters['vserver']}
130 data = {"enabled": self.parameters['enable']}
131 api = "protocols/vscan/" + uuid
132 dummy, error = self.rest_api.patch(api, data, params)
133 if error is not None:
134 self.module.fail_json(msg=error)
135 else:
136 vscan_status_obj = netapp_utils.zapi.NaElement("vscan-status-modify")
137 vscan_status_obj.add_new_child('is-vscan-enabled', str(self.parameters['enable']))
138 try:
139 self.server.invoke_successfully(vscan_status_obj, True)
140 except netapp_utils.zapi.NaApiError as error:
141 self.module.fail_json(msg="Error Enable/Disabling Vscan: %s" % to_native(error), exception=traceback.format_exc())
142
143 def apply(self):
144 changed = False
145 current = self.get_vscan()
146 if self.use_rest:
147 if current['enabled'] != self.parameters['enable']:
148 if not self.module.check_mode:
149 self.enable_vscan(current['svm']['uuid'])
150 changed = True
151 else:
152 if current.get_child_content('is-vscan-enabled') != str(self.parameters['enable']).lower():
153 if not self.module.check_mode:
154 self.enable_vscan()
155 changed = True
156 self.module.exit_json(changed=changed)
157
158
159 def main():
160 """
161 Execute action from playbook
162 """
163 command = NetAppOntapVscan()
164 command.apply()
165
166
167 if __name__ == '__main__':
168 main()