Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 '''
3 (c) 2018-2025, NetApp, Inc
4 GNU General Public License v3.0+
5 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6 '''
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11 DOCUMENTATION = '''
12
13 module: na_ontap_ldap
14
15 short_description: NetApp ONTAP LDAP
16 extends_documentation_fragment:
17 - netapp.ontap.netapp.na_ontap_zapi
18 version_added: 2.9.0
19 author: Milan Zink (@zeten30) <[email protected]>/<[email protected]>
20
21 description:
22 - Create, modify or delete LDAP on NetApp ONTAP SVM/vserver
23
24 options:
25
26 state:
27 description:
28 - Whether the LDAP is present or not.
29 choices: ['present', 'absent']
30 default: 'present'
31 type: str
32
33 vserver:
34 description:
35 - vserver/svm configured to use LDAP
36 required: true
37 type: str
38
39 name:
40 description:
41 - The name of LDAP client configuration
42 required: true
43 type: str
44
45 skip_config_validation:
46 description:
47 - Skip LDAP validation
48 choices: ['true', 'false']
49 type: str
50 '''
51
52 EXAMPLES = '''
53 - name: Enable LDAP on SVM
54 netapp.ontap.na_ontap_ldap:
55 state: present
56 name: example_ldap
57 vserver: vserver1
58 hostname: "{{ netapp_hostname }}"
59 username: "{{ netapp_username }}"
60 password: "{{ netapp_password }}"
61 '''
62
63 RETURN = '''
64 '''
65
66 import traceback
67 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
68 from ansible.module_utils.basic import AnsibleModule
69 from ansible.module_utils._text import to_native
70 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
71
72
73 class NetAppOntapLDAP:
74 '''
75 LDAP Client definition class
76 '''
77
78 def __init__(self):
79 self.argument_spec = netapp_utils.na_ontap_zapi_only_spec()
80 self.argument_spec.update(dict(
81 name=dict(required=True, type='str'),
82 skip_config_validation=dict(required=False, default=None, choices=['true', 'false']),
83 state=dict(required=False, choices=['present', 'absent'], default='present'),
84 vserver=dict(required=True, type='str')
85 ))
86
87 self.module = AnsibleModule(
88 argument_spec=self.argument_spec,
89 supports_check_mode=True
90 )
91 self.na_helper = NetAppModule()
92 self.parameters = self.na_helper.set_parameters(self.module.params)
93 msg = 'The module only supports ZAPI; refer to netapp.ontap.na_ontap_ldap_client module for RESTful equivalent.'
94 self.na_helper.fall_back_to_zapi(self.module, msg, self.parameters)
95
96 if not netapp_utils.has_netapp_lib():
97 self.module.fail_json(msg="the python NetApp-Lib module is required")
98 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver'])
99
100 def get_ldap(self, client_config_name=None):
101 '''
102 Checks if LDAP config exists.
103
104 :return:
105 ldap config object if found
106 None if not found
107 :rtype: object/None
108 '''
109 # Make query
110 config_info = netapp_utils.zapi.NaElement('ldap-config-get-iter')
111
112 if client_config_name is None:
113 client_config_name = self.parameters['name']
114
115 query_details = netapp_utils.zapi.NaElement.create_node_with_children('ldap-config', **{'client-config': client_config_name})
116
117 query = netapp_utils.zapi.NaElement('query')
118 query.add_child_elem(query_details)
119 config_info.add_child_elem(query)
120
121 result = self.server.invoke_successfully(config_info, enable_tunneling=True)
122
123 # Get LDAP configuration details
124 config_details = None
125 if (result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1):
126 attributes_list = result.get_child_by_name('attributes-list')
127 config_info = attributes_list.get_child_by_name('ldap-config')
128
129 # Define config details structure
130 config_details = {'client_config': config_info.get_child_content('client-config'),
131 'skip_config_validation': config_info.get_child_content('skip-config-validation'),
132 'vserver': config_info.get_child_content('vserver')}
133
134 return config_details
135
136 def create_ldap(self):
137 '''
138 Create LDAP configuration
139 '''
140 options = {
141 'client-config': self.parameters['name'],
142 'client-enabled': 'true'
143 }
144
145 if self.parameters.get('skip_config_validation') is not None:
146 options['skip-config-validation'] = self.parameters['skip_config_validation']
147
148 # Initialize NaElement
149 ldap_create = netapp_utils.zapi.NaElement.create_node_with_children('ldap-config-create', **options)
150
151 # Try to create LDAP configuration
152 try:
153 self.server.invoke_successfully(ldap_create, enable_tunneling=True)
154 except netapp_utils.zapi.NaApiError as errcatch:
155 self.module.fail_json(msg='Error creating LDAP configuration %s: %s' % (self.parameters['name'], to_native(errcatch)),
156 exception=traceback.format_exc())
157
158 def delete_ldap(self):
159 '''
160 Delete LDAP configuration
161 '''
162 ldap_client_delete = netapp_utils.zapi.NaElement.create_node_with_children('ldap-config-delete', **{})
163
164 try:
165 self.server.invoke_successfully(ldap_client_delete, enable_tunneling=True)
166 except netapp_utils.zapi.NaApiError as errcatch:
167 self.module.fail_json(msg='Error deleting LDAP configuration %s: %s' % (
168 self.parameters['name'], to_native(errcatch)), exception=traceback.format_exc())
169
170 def modify_ldap(self, modify):
171 '''
172 Modify LDAP
173 :param modify: list of modify attributes
174 '''
175 ldap_modify = netapp_utils.zapi.NaElement('ldap-config-modify')
176 ldap_modify.add_new_child('client-config', self.parameters['name'])
177
178 for attribute in modify:
179 if attribute == 'skip_config_validation':
180 ldap_modify.add_new_child('skip-config-validation', self.parameters[attribute])
181
182 # Try to modify LDAP
183 try:
184 self.server.invoke_successfully(ldap_modify, enable_tunneling=True)
185 except netapp_utils.zapi.NaApiError as errcatch:
186 self.module.fail_json(msg='Error modifying LDAP %s: %s' % (self.parameters['name'], to_native(errcatch)),
187 exception=traceback.format_exc())
188
189 def apply(self):
190 '''Call create/modify/delete operations.'''
191 current = self.get_ldap()
192 cd_action = self.na_helper.get_cd_action(current, self.parameters)
193 modify = self.na_helper.get_modified_attributes(current, self.parameters)
194
195 if self.na_helper.changed:
196 if self.module.check_mode:
197 pass
198 else:
199 if cd_action == 'create':
200 self.create_ldap()
201 elif cd_action == 'delete':
202 self.delete_ldap()
203 elif modify:
204 self.modify_ldap(modify)
205 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify)
206 self.module.exit_json(**result)
207
208
209 #
210 # MAIN
211 #
212 def main():
213 '''ONTAP LDAP client configuration'''
214 ldapclient = NetAppOntapLDAP()
215 ldapclient.apply()
216
217
218 if __name__ == '__main__':
219 main()