| 1 |
#!/usr/bin/python |
| 2 |
''' |
| 3 |
(c) 2018-2026, 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 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
|
| 14 |
module: na_ontap_ldap_client |
| 15 |
|
| 16 |
short_description: NetApp ONTAP LDAP client |
| 17 |
extends_documentation_fragment: |
| 18 |
- netapp.ontap.netapp.na_ontap |
| 19 |
version_added: 2.9.0 |
| 20 |
author: Milan Zink (@zeten30) <[email protected]>/<[email protected]> |
| 21 |
|
| 22 |
description: |
| 23 |
- Create, modify or delete LDAP client on NetApp ONTAP. |
| 24 |
|
| 25 |
options: |
| 26 |
|
| 27 |
state: |
| 28 |
description: |
| 29 |
- Whether the specified LDAP client configuration exist or not. |
| 30 |
choices: ['present', 'absent'] |
| 31 |
default: 'present' |
| 32 |
type: str |
| 33 |
|
| 34 |
vserver: |
| 35 |
description: |
| 36 |
- vserver/svm that holds LDAP client configuration. |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
|
| 40 |
name: |
| 41 |
description: |
| 42 |
- The name of LDAP client configuration. |
| 43 |
- Supported only in ZAPI. |
| 44 |
- Required with ZAPI. |
| 45 |
type: str |
| 46 |
|
| 47 |
servers: |
| 48 |
description: |
| 49 |
- Comma separated list of LDAP servers. FQDN's or IP addreses. |
| 50 |
- servers or ad_domain is required if I(state=present). |
| 51 |
- Mutually exclusive with preferred_ad_servers and ad_domain. |
| 52 |
type: list |
| 53 |
elements: str |
| 54 |
aliases: ['ldap_servers'] |
| 55 |
|
| 56 |
schema: |
| 57 |
description: |
| 58 |
- LDAP schema. |
| 59 |
- Required if I(state=present). |
| 60 |
- default schemas - 'AD-IDMU', 'AD-SFU', 'MS-AD-BIS', 'RFC-2307'. |
| 61 |
- custom schemas are allowed as well. |
| 62 |
type: str |
| 63 |
|
| 64 |
ad_domain: |
| 65 |
description: |
| 66 |
- Active Directory Domain Name. |
| 67 |
- servers or ad_domain is required if I(state=present). |
| 68 |
- Mutually exclusive with servers. |
| 69 |
type: str |
| 70 |
|
| 71 |
base_dn: |
| 72 |
description: |
| 73 |
- LDAP base DN. |
| 74 |
type: str |
| 75 |
|
| 76 |
base_scope: |
| 77 |
description: |
| 78 |
- LDAP search scope. |
| 79 |
choices: ['subtree', 'onelevel', 'base'] |
| 80 |
type: str |
| 81 |
|
| 82 |
bind_as_cifs_server: |
| 83 |
description: |
| 84 |
- The cluster uses the CIFS server's credentials to bind to the LDAP server. |
| 85 |
type: bool |
| 86 |
|
| 87 |
preferred_ad_servers: |
| 88 |
description: |
| 89 |
- Preferred Active Directory (AD) Domain Controllers. |
| 90 |
- Mutually exclusive with servers. |
| 91 |
type: list |
| 92 |
elements: str |
| 93 |
|
| 94 |
port: |
| 95 |
description: |
| 96 |
- LDAP server TCP port. |
| 97 |
type: int |
| 98 |
aliases: ['tcp_port'] |
| 99 |
version_added: 21.3.0 |
| 100 |
|
| 101 |
query_timeout: |
| 102 |
description: |
| 103 |
- LDAP server query timeout. |
| 104 |
type: int |
| 105 |
|
| 106 |
min_bind_level: |
| 107 |
description: |
| 108 |
- Minimal LDAP server bind level. |
| 109 |
choices: ['anonymous', 'simple', 'sasl'] |
| 110 |
type: str |
| 111 |
|
| 112 |
bind_dn: |
| 113 |
description: |
| 114 |
- LDAP bind user DN. |
| 115 |
type: str |
| 116 |
|
| 117 |
bind_password: |
| 118 |
description: |
| 119 |
- LDAP bind user password. |
| 120 |
type: str |
| 121 |
|
| 122 |
use_start_tls: |
| 123 |
description: |
| 124 |
- Start TLS on LDAP connection. |
| 125 |
type: bool |
| 126 |
|
| 127 |
referral_enabled: |
| 128 |
description: |
| 129 |
- LDAP Referral Chasing. |
| 130 |
type: bool |
| 131 |
|
| 132 |
session_security: |
| 133 |
description: |
| 134 |
- Client Session Security. |
| 135 |
choices: ['none', 'sign', 'seal'] |
| 136 |
type: str |
| 137 |
|
| 138 |
ldaps_enabled: |
| 139 |
description: |
| 140 |
- Specifies whether or not LDAPS is enabled. |
| 141 |
type: bool |
| 142 |
version_added: 21.22.0 |
| 143 |
|
| 144 |
skip_config_validation: |
| 145 |
description: |
| 146 |
- Indicates whether or not the validation for the specified LDAP configuration is disabled. |
| 147 |
- By default, errors are reported with REST when server names cannot be resolved for instance. |
| 148 |
- Requires ONTAP 9.9 or later. |
| 149 |
- This is ignored with ZAPI. |
| 150 |
type: bool |
| 151 |
version_added: 22.0.0 |
| 152 |
|
| 153 |
group_dn: |
| 154 |
description: |
| 155 |
- Specifies the group Distinguished Name (DN) |
| 156 |
that is used as the starting point in the LDAP directory tree for group lookups. |
| 157 |
type: str |
| 158 |
version_added: 23.4.0 |
| 159 |
|
| 160 |
user_dn: |
| 161 |
description: |
| 162 |
- Specifies the user Distinguished Name (DN) |
| 163 |
that is used as the starting point in the LDAP directory tree for user lookups. |
| 164 |
type: str |
| 165 |
version_added: 23.4.0 |
| 166 |
|
| 167 |
lambda_config: |
| 168 |
description: |
| 169 |
- Configuration parameters for AWS Lambda proxy functionality. |
| 170 |
- These option and suboptions are only supported with REST. |
| 171 |
type: dict |
| 172 |
version_added: 23.5.0 |
| 173 |
suboptions: |
| 174 |
function_name: |
| 175 |
description: |
| 176 |
- The name of the AWS Lambda function to invoke. |
| 177 |
type: str |
| 178 |
required: true |
| 179 |
aws_region: |
| 180 |
description: |
| 181 |
- The name of the AWS region. |
| 182 |
type: str |
| 183 |
required: true |
| 184 |
aws_profile: |
| 185 |
description: |
| 186 |
- The name of the AWS profile to use for authentication. |
| 187 |
type: str |
| 188 |
|
| 189 |
notes: |
| 190 |
- LDAP client created using ZAPI should be deleted using ZAPI. |
| 191 |
- LDAP client created using REST should be deleted using REST. |
| 192 |
- REST only supports create, modify and delete data svm ldap client configuration. |
| 193 |
- Supports AWS Lambda proxy functionality when using REST. See the README file for examples. |
| 194 |
|
| 195 |
''' |
| 196 |
|
| 197 |
EXAMPLES = ''' |
| 198 |
- name: Create LDAP client |
| 199 |
# assuming credentials are set using module_defaults |
| 200 |
netapp.ontap.na_ontap_ldap_client: |
| 201 |
state: present |
| 202 |
vserver: 'vserver1' |
| 203 |
servers: 'ldap1.example.company.com,ldap2.example.company.com' |
| 204 |
base_dn: 'dc=example,dc=company,dc=com' |
| 205 |
group_dn: 'ou=group,dc=ds,dc=eng,dc=netapp,dc=com' |
| 206 |
user_dn: 'ou=people,dc=ds,dc=eng,dc=netapp,dc=com' |
| 207 |
|
| 208 |
- name: Modify LDAP client |
| 209 |
# assuming credentials are set using module_defaults |
| 210 |
netapp.ontap.na_ontap_ldap_client: |
| 211 |
state: present |
| 212 |
vserver: 'vserver1' |
| 213 |
servers: 'ldap1.example.company.com' |
| 214 |
base_dn: 'dc=example,dc=company,dc=com' |
| 215 |
skip_config_validation: true |
| 216 |
|
| 217 |
- name: Delete LDAP client |
| 218 |
# assuming credentials are set using module_defaults |
| 219 |
netapp.ontap.na_ontap_ldap_client: |
| 220 |
state: absent |
| 221 |
vserver: 'vserver1' |
| 222 |
''' |
| 223 |
|
| 224 |
RETURN = ''' |
| 225 |
''' |
| 226 |
import traceback |
| 227 |
|
| 228 |
from ansible.module_utils.basic import AnsibleModule |
| 229 |
from ansible.module_utils._text import to_native |
| 230 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 231 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 232 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp import OntapRestAPI |
| 233 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic, rest_vserver |
| 234 |
|
| 235 |
|
| 236 |
class NetAppOntapLDAPClient: |
| 237 |
''' |
| 238 |
LDAP Client definition class |
| 239 |
''' |
| 240 |
|
| 241 |
def __init__(self): |
| 242 |
self.argument_spec = netapp_utils.na_ontap_host_argument_spec() |
| 243 |
self.argument_spec.update(dict( |
| 244 |
ad_domain=dict(required=False, default=None, type='str'), |
| 245 |
base_dn=dict(required=False, type='str'), |
| 246 |
base_scope=dict(required=False, default=None, choices=['subtree', 'onelevel', 'base']), |
| 247 |
bind_as_cifs_server=dict(required=False, type='bool'), |
| 248 |
bind_dn=dict(required=False, default=None, type='str'), |
| 249 |
bind_password=dict(type='str', required=False, default=None, no_log=True), |
| 250 |
name=dict(required=False, type='str'), |
| 251 |
servers=dict(required=False, type='list', elements='str', aliases=['ldap_servers']), |
| 252 |
min_bind_level=dict(required=False, default=None, choices=['anonymous', 'simple', 'sasl']), |
| 253 |
preferred_ad_servers=dict(required=False, type='list', elements='str'), |
| 254 |
port=dict(required=False, type='int', aliases=['tcp_port']), |
| 255 |
query_timeout=dict(required=False, default=None, type='int'), |
| 256 |
referral_enabled=dict(required=False, type='bool'), |
| 257 |
schema=dict(required=False, type='str'), |
| 258 |
session_security=dict(required=False, default=None, choices=['none', 'sign', 'seal']), |
| 259 |
state=dict(required=False, choices=['present', 'absent'], default='present'), |
| 260 |
use_start_tls=dict(required=False, type='bool'), |
| 261 |
vserver=dict(required=True, type='str'), |
| 262 |
ldaps_enabled=dict(required=False, type='bool'), |
| 263 |
skip_config_validation=dict(required=False, type='bool'), |
| 264 |
group_dn=dict(required=False, type='str'), |
| 265 |
user_dn=dict(required=False, type='str'), |
| 266 |
)) |
| 267 |
|
| 268 |
self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec()) |
| 269 |
|
| 270 |
self.module = AnsibleModule( |
| 271 |
argument_spec=self.argument_spec, |
| 272 |
supports_check_mode=True, |
| 273 |
required_if=[ |
| 274 |
('state', 'present', ['schema']), |
| 275 |
('use_lambda', True, ['lambda_config']), |
| 276 |
], |
| 277 |
mutually_exclusive=[ |
| 278 |
['servers', 'ad_domain'], |
| 279 |
['servers', 'preferred_ad_servers'], |
| 280 |
['use_start_tls', 'ldaps_enabled'] |
| 281 |
], |
| 282 |
) |
| 283 |
self.na_helper = NetAppModule() |
| 284 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 285 |
# Set up Rest API |
| 286 |
self.rest_api = OntapRestAPI(self.module) |
| 287 |
unsupported_rest_properties = ['name'] |
| 288 |
partially_supported_rest_properties = [['bind_as_cifs_server', (9, 9, 0)], ['query_timeout', (9, 9, 0)], ['referral_enabled', (9, 9, 0)], |
| 289 |
['ldaps_enabled', (9, 9, 0)], ['skip_config_validation', (9, 9, 0)], |
| 290 |
['group_dn', (9, 9, 0)], ['user_dn', (9, 9, 0)]] |
| 291 |
self.use_rest = self.rest_api.is_rest_supported_properties(self.parameters, unsupported_rest_properties, partially_supported_rest_properties) |
| 292 |
if not self.use_rest: |
| 293 |
if self.parameters.get('use_lambda'): |
| 294 |
self.module.fail_json(msg="Error: AWS Lambda proxy for ONTAP APIs is only supported with REST.") |
| 295 |
if not netapp_utils.has_netapp_lib(): |
| 296 |
self.module.fail_json(msg=netapp_utils.netapp_lib_is_required()) |
| 297 |
self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver']) |
| 298 |
if not self.parameters.get('name'): |
| 299 |
self.module.fail_json(msg="Error: name is a required field with ZAPI.") |
| 300 |
|
| 301 |
self.simple_attributes = [ |
| 302 |
'ad_domain', |
| 303 |
'base_dn', |
| 304 |
'base_scope', |
| 305 |
'bind_as_cifs_server', |
| 306 |
'bind_dn', |
| 307 |
'bind_password', |
| 308 |
'min_bind_level', |
| 309 |
'tcp_port', |
| 310 |
'query_timeout', |
| 311 |
'referral_enabled', |
| 312 |
'session_security', |
| 313 |
'use_start_tls', |
| 314 |
'ldaps_enabled' |
| 315 |
] |
| 316 |
|
| 317 |
def get_ldap_client(self, client_config_name=None, vserver_name=None): |
| 318 |
''' |
| 319 |
Checks if LDAP client config exists. |
| 320 |
|
| 321 |
:return: |
| 322 |
ldap client config object if found |
| 323 |
None if not found |
| 324 |
:rtype: object/None |
| 325 |
''' |
| 326 |
# Make query |
| 327 |
client_config_info = netapp_utils.zapi.NaElement('ldap-client-get-iter') |
| 328 |
|
| 329 |
if client_config_name is None: |
| 330 |
client_config_name = self.parameters['name'] |
| 331 |
|
| 332 |
if vserver_name is None: |
| 333 |
vserver_name = '*' |
| 334 |
|
| 335 |
query_details = netapp_utils.zapi.NaElement.create_node_with_children('ldap-client', |
| 336 |
**{ |
| 337 |
'ldap-client-config': client_config_name, |
| 338 |
'vserver': vserver_name}) |
| 339 |
|
| 340 |
query = netapp_utils.zapi.NaElement('query') |
| 341 |
query.add_child_elem(query_details) |
| 342 |
client_config_info.add_child_elem(query) |
| 343 |
|
| 344 |
result = self.server.invoke_successfully(client_config_info, enable_tunneling=False) |
| 345 |
|
| 346 |
# Get LDAP client configuration details |
| 347 |
client_config_details = None |
| 348 |
if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1: |
| 349 |
attributes_list = result.get_child_by_name('attributes-list') |
| 350 |
client_config_info = attributes_list.get_child_by_name('ldap-client') |
| 351 |
ldap_server_list = self.get_list_from_children(client_config_info, 'ldap-servers') |
| 352 |
preferred_ad_servers_list = self.get_list_from_children(client_config_info, 'preferred-ad-servers') |
| 353 |
|
| 354 |
# Define config details structure |
| 355 |
client_config_details = { |
| 356 |
'name': client_config_info.get_child_content('ldap-client-config'), |
| 357 |
'servers': ldap_server_list, |
| 358 |
'ad_domain': client_config_info.get_child_content('ad-domain'), |
| 359 |
'base_dn': client_config_info.get_child_content('base-dn'), |
| 360 |
'base_scope': client_config_info.get_child_content('base-scope'), |
| 361 |
'bind_as_cifs_server': self.na_helper.get_value_for_bool(from_zapi=True, |
| 362 |
value=client_config_info.get_child_content('bind-as-cifs-server')), |
| 363 |
'bind_dn': client_config_info.get_child_content('bind-dn'), |
| 364 |
'bind_password': client_config_info.get_child_content('bind-password'), |
| 365 |
'min_bind_level': client_config_info.get_child_content('min-bind-level'), |
| 366 |
'tcp_port': self.na_helper.get_value_for_int(from_zapi=True, value=client_config_info.get_child_content('tcp-port')), |
| 367 |
'preferred_ad_servers': preferred_ad_servers_list, |
| 368 |
'query_timeout': self.na_helper.get_value_for_int(from_zapi=True, |
| 369 |
value=client_config_info.get_child_content('query-timeout')), |
| 370 |
'referral_enabled': self.na_helper.get_value_for_bool(from_zapi=True, |
| 371 |
value=client_config_info.get_child_content('referral-enabled')), |
| 372 |
'schema': client_config_info.get_child_content('schema'), |
| 373 |
'session_security': client_config_info.get_child_content('session-security'), |
| 374 |
'use_start_tls': self.na_helper.get_value_for_bool(from_zapi=True, |
| 375 |
value=client_config_info.get_child_content('use-start-tls')), |
| 376 |
'ldaps_enabled': self.na_helper.get_value_for_bool(from_zapi=True, |
| 377 |
value=client_config_info.get_child_content('ldaps-enabled')), |
| 378 |
} |
| 379 |
return client_config_details |
| 380 |
|
| 381 |
def get_list_from_children(self, client_config_info, element_name): |
| 382 |
# Get list for element chidren |
| 383 |
# returns empty list if element does not exist |
| 384 |
get_list = client_config_info.get_child_by_name(element_name) |
| 385 |
return [x.get_content() for x in get_list.get_children()] if get_list is not None else [] |
| 386 |
|
| 387 |
def create_ldap_client(self): |
| 388 |
''' |
| 389 |
Create LDAP client configuration |
| 390 |
''' |
| 391 |
options = { |
| 392 |
'ldap-client-config': self.parameters['name'], |
| 393 |
'schema': self.parameters['schema'], |
| 394 |
} |
| 395 |
|
| 396 |
# Other options/attributes |
| 397 |
for attribute in self.simple_attributes: |
| 398 |
if self.parameters.get(attribute) is not None: |
| 399 |
options[str(attribute).replace('_', '-')] = str(self.parameters[attribute]) |
| 400 |
|
| 401 |
# Initialize NaElement |
| 402 |
ldap_client_create = netapp_utils.zapi.NaElement.create_node_with_children('ldap-client-create', **options) |
| 403 |
|
| 404 |
# LDAP servers NaElement |
| 405 |
if self.parameters.get('servers') is not None: |
| 406 |
self.add_element_with_children('ldap-servers', 'servers', 'string', ldap_client_create) |
| 407 |
|
| 408 |
# preferred_ad_servers |
| 409 |
if self.parameters.get('preferred_ad_servers') is not None: |
| 410 |
self.add_element_with_children('preferred-ad-servers', 'preferred_ad_servers', 'ip-address', ldap_client_create) |
| 411 |
|
| 412 |
# Try to create LDAP configuration |
| 413 |
try: |
| 414 |
self.server.invoke_successfully(ldap_client_create, enable_tunneling=True) |
| 415 |
except netapp_utils.zapi.NaApiError as errcatch: |
| 416 |
self.module.fail_json( |
| 417 |
msg='Error creating LDAP client %s: %s' % (self.parameters['name'], to_native(errcatch)), |
| 418 |
exception=traceback.format_exc()) |
| 419 |
|
| 420 |
def add_element_with_children(self, element_name, param_name, child_name, ldap_client_create): |
| 421 |
ldap_servers_element = netapp_utils.zapi.NaElement(element_name) |
| 422 |
for ldap_server_name in self.parameters[param_name]: |
| 423 |
ldap_servers_element.add_new_child(child_name, ldap_server_name) |
| 424 |
ldap_client_create.add_child_elem(ldap_servers_element) |
| 425 |
|
| 426 |
def delete_ldap_client(self): |
| 427 |
''' |
| 428 |
Delete LDAP client configuration |
| 429 |
''' |
| 430 |
ldap_client_delete = netapp_utils.zapi.NaElement.create_node_with_children( |
| 431 |
'ldap-client-delete', **{'ldap-client-config': self.parameters['name']}) |
| 432 |
|
| 433 |
try: |
| 434 |
self.server.invoke_successfully(ldap_client_delete, enable_tunneling=True) |
| 435 |
except netapp_utils.zapi.NaApiError as errcatch: |
| 436 |
self.module.fail_json(msg='Error deleting LDAP client configuration %s: %s' % ( |
| 437 |
self.parameters['name'], to_native(errcatch)), exception=traceback.format_exc()) |
| 438 |
|
| 439 |
def modify_ldap_client(self, modify): |
| 440 |
''' |
| 441 |
Modify LDAP client |
| 442 |
:param modify: list of modify attributes |
| 443 |
''' |
| 444 |
ldap_client_modify = netapp_utils.zapi.NaElement('ldap-client-modify') |
| 445 |
ldap_client_modify.add_new_child('ldap-client-config', self.parameters['name']) |
| 446 |
|
| 447 |
for attribute in modify: |
| 448 |
# LDAP_servers |
| 449 |
if attribute == 'servers': |
| 450 |
self.add_element_with_children('ldap-servers', attribute, 'string', ldap_client_modify) |
| 451 |
# preferred_ad_servers |
| 452 |
if attribute == 'preferred_ad_servers': |
| 453 |
self.add_element_with_children('preferred-ad-servers', attribute, 'ip-address', ldap_client_modify) |
| 454 |
# Simple attributes |
| 455 |
if attribute in self.simple_attributes: |
| 456 |
ldap_client_modify.add_new_child(str(attribute).replace('_', '-'), str(self.parameters[attribute])) |
| 457 |
|
| 458 |
# Try to modify LDAP client |
| 459 |
try: |
| 460 |
self.server.invoke_successfully(ldap_client_modify, enable_tunneling=True) |
| 461 |
except netapp_utils.zapi.NaApiError as errcatch: |
| 462 |
self.module.fail_json( |
| 463 |
msg='Error modifying LDAP client %s: %s' % (self.parameters['name'], to_native(errcatch)), |
| 464 |
exception=traceback.format_exc()) |
| 465 |
|
| 466 |
def get_ldap_client_rest(self): |
| 467 |
""" |
| 468 |
Retrives ldap client config with rest API. |
| 469 |
""" |
| 470 |
if not self.use_rest: |
| 471 |
return self.get_ldap_client() |
| 472 |
query = {'svm.name': self.parameters.get('vserver'), |
| 473 |
'fields': 'svm.uuid,' |
| 474 |
'ad_domain,' |
| 475 |
'servers,' |
| 476 |
'preferred_ad_servers,' |
| 477 |
'bind_dn,' |
| 478 |
'schema,' |
| 479 |
'port,' |
| 480 |
'base_dn,' |
| 481 |
'base_scope,' |
| 482 |
'min_bind_level,' |
| 483 |
'session_security,' |
| 484 |
'use_start_tls,'} |
| 485 |
if self.rest_api.meets_rest_minimum_version(self.use_rest, 9, 9, 0): |
| 486 |
query['fields'] += 'bind_as_cifs_server,query_timeout,referral_enabled,ldaps_enabled,group_dn,user_dn' |
| 487 |
record, error = rest_generic.get_one_record(self.rest_api, 'name-services/ldap', query) |
| 488 |
if error: |
| 489 |
self.module.fail_json(msg="Error on getting idap client info: %s" % error) |
| 490 |
if record: |
| 491 |
return { |
| 492 |
'svm': {'uuid': self.na_helper.safe_get(record, ['svm', 'uuid'])}, |
| 493 |
'ad_domain': self.na_helper.safe_get(record, ['ad_domain']), |
| 494 |
'preferred_ad_servers': self.na_helper.safe_get(record, ['preferred_ad_servers']), |
| 495 |
'servers': self.na_helper.safe_get(record, ['servers']), |
| 496 |
'schema': self.na_helper.safe_get(record, ['schema']), |
| 497 |
'port': self.na_helper.safe_get(record, ['port']), |
| 498 |
'ldaps_enabled': self.na_helper.safe_get(record, ['ldaps_enabled']), |
| 499 |
'min_bind_level': self.na_helper.safe_get(record, ['min_bind_level']), |
| 500 |
'bind_dn': self.na_helper.safe_get(record, ['bind_dn']), |
| 501 |
'base_dn': self.na_helper.safe_get(record, ['base_dn']), |
| 502 |
'base_scope': self.na_helper.safe_get(record, ['base_scope']), |
| 503 |
'use_start_tls': self.na_helper.safe_get(record, ['use_start_tls']), |
| 504 |
'session_security': self.na_helper.safe_get(record, ['session_security']), |
| 505 |
'referral_enabled': self.na_helper.safe_get(record, ['referral_enabled']), |
| 506 |
'bind_as_cifs_server': self.na_helper.safe_get(record, ['bind_as_cifs_server']), |
| 507 |
'query_timeout': self.na_helper.safe_get(record, ['query_timeout']), |
| 508 |
'group_dn': self.na_helper.safe_get(record, ['group_dn']), |
| 509 |
'user_dn': self.na_helper.safe_get(record, ['user_dn']) |
| 510 |
} |
| 511 |
return None |
| 512 |
|
| 513 |
def create_ldap_client_body_rest(self, modify=None): |
| 514 |
""" |
| 515 |
ldap client config body for create and modify with rest API. |
| 516 |
""" |
| 517 |
config_options = ['ad_domain', 'servers', 'preferred_ad_servers', 'bind_dn', 'schema', 'port', 'base_dn', 'referral_enabled', 'ldaps_enabled', |
| 518 |
'base_scope', 'bind_as_cifs_server', 'bind_password', 'min_bind_level', 'query_timeout', 'session_security', 'use_start_tls', |
| 519 |
'group_dn', 'user_dn'] |
| 520 |
processing_options = ['skip_config_validation'] |
| 521 |
body = {} |
| 522 |
for key in config_options: |
| 523 |
if not modify and key in self.parameters: |
| 524 |
body[key] = self.parameters[key] |
| 525 |
elif modify and key in modify: |
| 526 |
body[key] = modify[key] |
| 527 |
for key in processing_options: |
| 528 |
if body and key in self.parameters: |
| 529 |
body[key] = self.parameters[key] |
| 530 |
return body |
| 531 |
|
| 532 |
def create_ldap_client_rest(self): |
| 533 |
""" |
| 534 |
create ldap client config with rest API. |
| 535 |
""" |
| 536 |
if not self.use_rest: |
| 537 |
return self.create_ldap_client() |
| 538 |
body = self.create_ldap_client_body_rest() |
| 539 |
body['svm.name'] = self.parameters['vserver'] |
| 540 |
api = 'name-services/ldap' |
| 541 |
dummy, error = rest_generic.post_async(self.rest_api, api, body) |
| 542 |
if error is not None: |
| 543 |
self.module.fail_json(msg="Error on creating ldap client: %s" % error) |
| 544 |
|
| 545 |
def delete_ldap_client_rest(self, current): |
| 546 |
""" |
| 547 |
delete ldap client config with rest API. |
| 548 |
""" |
| 549 |
if not self.use_rest: |
| 550 |
return self.delete_ldap_client() |
| 551 |
api = 'name-services/ldap' |
| 552 |
dummy, error = rest_generic.delete_async(self.rest_api, api, current['svm']['uuid'], body=None) |
| 553 |
if error is not None: |
| 554 |
self.module.fail_json(msg="Error on deleting ldap client rest: %s" % error) |
| 555 |
|
| 556 |
def modify_ldap_client_rest(self, current, modify): |
| 557 |
""" |
| 558 |
modif ldap client config with rest API. |
| 559 |
""" |
| 560 |
if not self.use_rest: |
| 561 |
return self.modify_ldap_client(modify) |
| 562 |
body = self.create_ldap_client_body_rest(modify) |
| 563 |
if body: |
| 564 |
api = 'name-services/ldap' |
| 565 |
dummy, error = rest_generic.patch_async(self.rest_api, api, current['svm']['uuid'], body) |
| 566 |
if error is not None: |
| 567 |
self.module.fail_json(msg="Error on modifying ldap client config: %s" % error) |
| 568 |
|
| 569 |
def apply(self): |
| 570 |
'''Call create/modify/delete operations.''' |
| 571 |
current = self.get_ldap_client_rest() |
| 572 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 573 |
# state is present, either servers or ad_domain is required |
| 574 |
if self.parameters['state'] == 'present' and not self.parameters.get('servers') \ |
| 575 |
and self.parameters.get('ad_domain') is None: |
| 576 |
self.module.fail_json(msg='Required one of servers or ad_domain') |
| 577 |
# REST retrives only data svm ldap configuration, error if try to use non data svm. |
| 578 |
if cd_action == "create" and self.use_rest: |
| 579 |
rest_vserver.get_vserver_uuid(self.rest_api, self.parameters['vserver'], self.module, True) |
| 580 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) if cd_action is None else None |
| 581 |
if self.na_helper.changed and not self.module.check_mode: |
| 582 |
if cd_action == 'create': |
| 583 |
self.create_ldap_client_rest() |
| 584 |
elif cd_action == 'delete': |
| 585 |
self.delete_ldap_client_rest(current) |
| 586 |
elif modify: |
| 587 |
self.modify_ldap_client_rest(current, modify) |
| 588 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify) |
| 589 |
self.module.exit_json(**result) |
| 590 |
|
| 591 |
|
| 592 |
def main(): |
| 593 |
'''ONTAP LDAP client configuration''' |
| 594 |
ldapclient = NetAppOntapLDAPClient() |
| 595 |
ldapclient.apply() |
| 596 |
|
| 597 |
|
| 598 |
if __name__ == '__main__': |
| 599 |
main() |