Пример #1
0
def delete_ds(module, blade):
    """Delete Directory Service"""
    changed = True
    if not module.check_mode:
        dirserv = blade.directory_services.list_directory_services(
            names=[module.params["dstype"]]
        )
        try:
            if module.params["dstype"] == "management":
                if dirserv.items[0].uris:
                    dir_service = DirectoryService(
                        uris=[""],
                        base_dn="",
                        bind_user="",
                        bind_password="",
                        enabled=False,
                    )
                else:
                    changed = False
            elif module.params["dstype"] == "smb":
                if dirserv.items[0].uris:
                    smb_attrs = {"join_ou": ""}
                    dir_service = DirectoryService(
                        uris=[""],
                        base_dn="",
                        bind_user="",
                        bind_password="",
                        smb=smb_attrs,
                        enabled=False,
                    )
                else:
                    changed = False
            elif module.params["dstype"] == "nfs":
                if dirserv.items[0].uris:
                    dir_service = DirectoryService(
                        uris=[""],
                        base_dn="",
                        bind_user="",
                        bind_password="",
                        enabled=False,
                    )
                elif dirserv.items[0].nfs.nis_domains:
                    nfs_attrs = {"nis_domains": [], "nis_servers": []}
                    dir_service = DirectoryService(nfs=nfs_attrs, enabled=False)
                else:
                    changed = False
            if changed:
                blade.directory_services.update_directory_services(
                    names=[module.params["dstype"]], directory_service=dir_service
                )
        except Exception:
            module.fail_json(
                msg="Delete {0} Directory Service failed".format(
                    module.params["dstype"]
                )
            )
    module.exit_json(changed=changed)
Пример #2
0
def create_ds(module, blade):
    """Create Directory Service"""
    changed = True
    if not module.check_mode:
        try:
            if module.params['dstype'] == 'management':
                if module.params['uri']:
                    dir_service = DirectoryService(
                        uris=module.params['uri'][0:30],
                        base_dn=module.params['base_dn'],
                        bind_user=module.params['bind_user'],
                        bind_password=module.params['bind_password'],
                        enabled=module.params['enable'])
                else:
                    module.fail_json(
                        msg=
                        "URI and associated params must be specified to create dstype {0}"
                        .format(module.params['dstype']))
            elif module.params['dstype'] == 'smb':
                if module.params['uri']:
                    smb_attrs = {'join_ou': module.params['join_ou']}
                    dir_service = DirectoryService(
                        uris=module.params['uri'][0:30],
                        base_dn=module.params['base_dn'],
                        bind_user=module.params['bind_user'],
                        bind_password=module.params['bind_password'],
                        smb=smb_attrs,
                        enabled=module.params['enable'])
                else:
                    module.fail_json(
                        msg=
                        "URI and associated params must be specified to create dstype {0}"
                        .format(module.params['dstype']))
            elif module.params['dstype'] == 'nfs':
                if module.params['nis_domain']:
                    nfs_attrs = {
                        'nis_domains': [module.params['nis_domain']],
                        'nis_servers': module.params['nis_servers'][0:30]
                    }
                    dir_service = DirectoryService(
                        nfs=nfs_attrs, enabled=module.params['enable'])
                else:
                    dir_service = DirectoryService(
                        uris=module.params['uri'][0:30],
                        base_dn=module.params['base_dn'],
                        bind_user=module.params['bind_user'],
                        bind_password=module.params['bind_password'],
                        enabled=module.params['enable'])
            blade.directory_services.update_directory_services(
                names=[module.params['dstype']], directory_service=dir_service)
        except Exception:
            module.fail_json(
                msg='Create {0} Directory Service failed: Check configuration'.
                format(module.params['dstype']))
    module.exit_json(changed=changed)
def delete_ds(module, blade):
    """Delete Directory Service"""
    changed = True
    if not module.check_mode:
        dirserv = blade.directory_services.list_directory_services(
            names=[module.params['dstype']])
        try:
            if module.params['dstype'] == 'management':
                if dirserv.items[0].uris:
                    dir_service = DirectoryService(uris=[''],
                                                   base_dn="",
                                                   bind_user="",
                                                   bind_password="",
                                                   enabled=False)
                else:
                    changed = False
            elif module.params['dstype'] == 'smb':
                if dirserv.items[0].uris:
                    smb_attrs = {'join_ou': ''}
                    dir_service = DirectoryService(uris=[''],
                                                   base_dn='',
                                                   bind_user='',
                                                   bind_password='',
                                                   smb=smb_attrs,
                                                   enabled=False)
                else:
                    changed = False
            elif module.params['dstype'] == 'nfs':
                if dirserv.items[0].uris:
                    dir_service = DirectoryService(uris=[''],
                                                   base_dn='',
                                                   bind_user='',
                                                   bind_password='',
                                                   enabled=False)
                elif dirserv.items[0].nfs.nis_domains:
                    nfs_attrs = {'nis_domains': [], 'nis_servers': []}
                    dir_service = DirectoryService(nfs=nfs_attrs,
                                                   enabled=False)
                else:
                    changed = False
            if changed:
                blade.directory_services.update_directory_services(
                    names=[module.params['dstype']],
                    directory_service=dir_service)
        except Exception:
            module.fail_json(msg='Delete {0} Directory Service failed'.format(
                module.params['dstype']))
    module.exit_json(changed=changed)
Пример #4
0
def update_ds(module, blade):
    """Update Directory Service"""
    changed = True
    if not module.check_mode:
        changed = False
        mod_ds = False
        attr = {}
        try:
            ds_now = blade.directory_services.list_directory_services(
                names=[module.params['dstype']]).items[0]
            if module.params['dstype'] == 'nfs' and module.params[
                    'nis_servers']:
                if sorted(module.params['nis_servers']) != sorted(ds_now.nfs.nis_servers) or \
                        module.params['nis_domain'] != ''.join(map(str, ds_now.nfs.nis_domains)):
                    attr['nfs'] = {
                        'nis_domains': [module.params['nis_domain']],
                        'nis_servers': module.params['nis_servers'][0:30]
                    }
                    mod_ds = True
            else:
                if module.params['uri']:
                    if sorted(module.params['uri'][0:30]) != sorted(
                            ds_now.uris):
                        attr['uris'] = module.params['uri'][0:30]
                        mod_ds = True
                if module.params['base_dn']:
                    if module.params['base_dn'] != ds_now.base_dn:
                        attr['base_dn'] = module.params['base_dn']
                        mod_ds = True
                if module.params['bind_user']:
                    if module.params['bind_user'] != ds_now.bind_user:
                        attr['bind_user'] = module.params['bind_user']
                        mod_ds = True
                if module.params['enable']:
                    if module.params['enable'] != ds_now.enabled:
                        attr['enabled'] = module.params['enable']
                        mod_ds = True
                if module.params['bind_password']:
                    attr['bind_password'] = module.params['bind_password']
                    mod_ds = True
                if module.params['dstype'] == 'smb':
                    if module.params['join_ou'] != ds_now.smb.join_ou:
                        attr['smb'] = {'join_ou': module.params['join_ou']}
                        mod_ds = True
            if mod_ds:
                n_attr = DirectoryService(**attr)
                try:
                    blade.directory_services.update_directory_services(
                        names=[module.params['dstype']],
                        directory_service=n_attr)
                except Exception:
                    module.fail_json(
                        msg='Failed to change {0} directory service.'.format(
                            module.params['dstype']))
        except Exception:
            module.fail_json(
                msg='Failed to get current {0} directory service.'.format(
                    module.params['dstype']))
        changed = True
    module.exit_json(changed=changed)
Пример #5
0
def disable_ds(module, blade):
    """Disable Directory Service"""
    changed = True
    if not module.check_mode:
        try:
            blade.directory_services.update_directory_services(names=[module.params['dstype']],
                                                               directory_service=DirectoryService(enabled=False))
        except Exception:
            module.fail_json(msg='Disable {0} Directory Service failed'.format(module.params['dstype']))
    module.exit_json(changed=changed)
Пример #6
0
def enable_ds(module, blade):
    """Enable Directory Service"""
    changed = False
    try:
        blade.directory_services.update_directory_services(names=[module.params['dstype']],
                                                           directory_service=DirectoryService(enabled=True))
        changed = True
    except Exception:
        module.fail_json(msg='Enable {0} Directory Service failed: Check Configuration'.format(module.params['dstype']))
    module.exit_json(changed=changed)
Пример #7
0
def create_ds(module, blade):
    """Create Directory Service"""
    changed = False
    try:
        dir_service = DirectoryService(uris=module.params['uri'],
                                       base_dn=module.params['base_dn'],
                                       bind_user=module.params['bind_user'],
                                       bind_password=module.params['bind_password'],
                                       enabled=module.params['enable'])
        blade.directory_services.update_directory_services(names=[module.params['dstype']],
                                                           directory_service=dir_service)
        changed = True
    except Exception:
        module.fail_json(msg='Create {0} Directory Service failed: Check configuration'.format(module.params['dstype']))
    module.exit_json(changed=changed)
Пример #8
0
def delete_ds(module, blade):
    """Delete Directory Service"""
    changed = False
    try:
        dir_service = DirectoryService(uris=[''],
                                       base_dn="",
                                       bind_user="",
                                       bind_password="",
                                       enabled=False)
        blade.directory_services.update_directory_services(names=[module.params['dstype']],
                                                           directory_service=dir_service)
        changed = True
    except Exception:
        module.fail_json(msg='Delete {0} Directory Service failed'.format(module.params['dstype']))
    module.exit_json(changed=changed)
Пример #9
0
def enable_ds(module, blade):
    """Enable Directory Service"""
    changed = True
    if not module.check_mode:
        try:
            blade.directory_services.update_directory_services(
                names=[module.params["dstype"]],
                directory_service=DirectoryService(enabled=True),
            )
            changed = True
        except Exception:
            module.fail_json(
                msg="Enable {0} Directory Service failed".format(
                    module.params["dstype"]
                )
            )
    module.exit_json(changed=changed)
Пример #10
0
def create_ds(module, blade):
    """Create Directory Service"""
    changed = True
    if not module.check_mode:
        try:
            if module.params["dstype"] == "management":
                if module.params["uri"]:
                    dir_service = DirectoryService(
                        uris=module.params["uri"][0:30],
                        base_dn=module.params["base_dn"],
                        bind_user=module.params["bind_user"],
                        bind_password=module.params["bind_password"],
                        enabled=module.params["enable"],
                    )
                else:
                    module.fail_json(
                        msg="Incorrect parameters provided for dstype {0}".format(
                            module.params["dstype"]
                        )
                    )
            elif module.params["dstype"] == "smb":
                if module.params["uri"]:
                    smb_attrs = {"join_ou": module.params["join_ou"]}
                    dir_service = DirectoryService(
                        uris=module.params["uri"][0:30],
                        base_dn=module.params["base_dn"],
                        bind_user=module.params["bind_user"],
                        bind_password=module.params["bind_password"],
                        smb=smb_attrs,
                        enabled=module.params["enable"],
                    )
                else:
                    module.fail_json(
                        msg="Incorrect parameters provided for dstype {0}".format(
                            module.params["dstype"]
                        )
                    )
            elif module.params["dstype"] == "nfs":
                if module.params["nis_domain"]:
                    nfs_attrs = {
                        "nis_domains": [module.params["nis_domain"]],
                        "nis_servers": module.params["nis_servers"][0:30],
                    }
                    dir_service = DirectoryService(
                        nfs=nfs_attrs, enabled=module.params["enable"]
                    )
                else:
                    dir_service = DirectoryService(
                        uris=module.params["uri"][0:30],
                        base_dn=module.params["base_dn"],
                        bind_user=module.params["bind_user"],
                        bind_password=module.params["bind_password"],
                        enabled=module.params["enable"],
                    )
            blade.directory_services.update_directory_services(
                names=[module.params["dstype"]], directory_service=dir_service
            )
        except Exception:
            module.fail_json(
                msg="Create {0} Directory Service failed".format(
                    module.params["dstype"]
                )
            )
    module.exit_json(changed=changed)
Пример #11
0
def update_ds(module, blade):
    """Update Directory Service"""
    mod_ds = False
    attr = {}
    try:
        ds_now = blade.directory_services.list_directory_services(
            names=[module.params["dstype"]]
        ).items[0]
        if module.params["dstype"] == "nfs" and module.params["nis_servers"]:
            if sorted(module.params["nis_servers"]) != sorted(
                ds_now.nfs.nis_servers
            ) or module.params["nis_domain"] != "".join(
                map(str, ds_now.nfs.nis_domains)
            ):
                attr["nfs"] = {
                    "nis_domains": [module.params["nis_domain"]],
                    "nis_servers": module.params["nis_servers"][0:30],
                }
                mod_ds = True
        else:
            if module.params["uri"]:
                if sorted(module.params["uri"][0:30]) != sorted(ds_now.uris):
                    attr["uris"] = module.params["uri"][0:30]
                    mod_ds = True
            if module.params["base_dn"]:
                if module.params["base_dn"] != ds_now.base_dn:
                    attr["base_dn"] = module.params["base_dn"]
                    mod_ds = True
            if module.params["bind_user"]:
                if module.params["bind_user"] != ds_now.bind_user:
                    attr["bind_user"] = module.params["bind_user"]
                    mod_ds = True
            if module.params["enable"]:
                if module.params["enable"] != ds_now.enabled:
                    attr["enabled"] = module.params["enable"]
                    mod_ds = True
            if module.params["bind_password"]:
                attr["bind_password"] = module.params["bind_password"]
                mod_ds = True
            if module.params["dstype"] == "smb":
                if module.params["join_ou"] != ds_now.smb.join_ou:
                    attr["smb"] = {"join_ou": module.params["join_ou"]}
                    mod_ds = True
        if mod_ds:
            changed = True
            if not module.check_mode:
                n_attr = DirectoryService(**attr)
                try:
                    blade.directory_services.update_directory_services(
                        names=[module.params["dstype"]], directory_service=n_attr
                    )
                except Exception:
                    module.fail_json(
                        msg="Failed to change {0} directory service.".format(
                            module.params["dstype"]
                        )
                    )
    except Exception:
        module.fail_json(
            msg="Failed to get current {0} directory service.".format(
                module.params["dstype"]
            )
        )
    module.exit_json(changed=changed)