Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions cterasdk/core/directoryservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ def fetch(self, active_directory_accounts):
"""
Instruct the Portal to fetch the provided Active Directory Accounts

This imports the accounts into the Portal (``syncAD``). A read-only lookup is
:meth:`search_directory_services`.

:param list[cterasdk.core.types.PortalAccount] active_directory_accounts: List of Active Directory Accounts to fetch

:return: Response Code
Expand Down Expand Up @@ -233,23 +236,36 @@ def fetch(self, active_directory_accounts):
return response

def _search_users(self, domain, user):
return self._search_directory_services(SearchType.Users, domain, user)
return self.search_directory_services(SearchType.Users, domain, user)

def _search_groups(self, domain, group):
return self._search_directory_services(SearchType.Groups, domain, group)
return self.search_directory_services(SearchType.Groups, domain, group)

def search_directory_services(self, mode, domain, name):
"""
Search directory services for one user or group.

def _search_directory_services(self, search_type, domain, name):
Read-only. Calls ``searchAD`` and returns the matching principal. The principal
carries ``rid``, not ``sid``. This method does not import the account into the Portal.

:param str mode: Search mode, :attr:`cterasdk.core.enum.SearchType.Users` or
:attr:`cterasdk.core.enum.SearchType.Groups`
:param str domain: Directory domain
:param str name: Account name
:returns: The matching directory principal
:rtype: cterasdk.common.object.Object
"""
param = Object()
param.mode = search_type
param.mode = mode
param.name = name
param.domain = domain

logger.info('Searching %(search_type)s: %(info)s', dict(search_type=search_type, info={'domain': domain, 'name': name}))
logger.info('Searching %(mode)s: %(info)s', dict(mode=mode, info={'domain': domain, 'name': name}))

objects = self._core.api.execute('', 'searchAD', param)
if not objects:
logger.info('Could not find %s: %s@%s.', search_type, name, domain)
raise CTERAException(f'Could not find {search_type[:-1]}: {name}@{domain}')
logger.info('Could not find %s: %s@%s.', mode, name, domain)
raise CTERAException(f'Could not find {mode[:-1]}: {name}@{domain}')

for principal in objects:
if principal.name == name:
Expand Down
16 changes: 10 additions & 6 deletions cterasdk/core/shares.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ def _deduce_unique_identifier(share):
return share
raise ValueError('Could not determine share identifier')

def _get_configuration(self, share):
def get_configuration(self, share):
"""
Get Share
Get a share's configuration.

:param cterasdk.core.types.Share or str: Share object or unique idenifier.
:returns: Share metadata
Read-only. Returns the Portal configuration, including ``directory``, ``access_type``,
``device_shares`` and ``acl_rules``. This is the raw configuration, not
:class:`cterasdk.core.types.ShareInfo`.

:param cterasdk.core.types.Share or str share: Share object or unique identifier.
:returns: Share configuration
:rtype: cterasdk.common.object.Object
"""
uid = Shares._deduce_unique_identifier(share)
Expand All @@ -52,7 +56,7 @@ def get(self, share):
:returns: Share information
:rtype: cterasdk.core.types.ShareInfo
"""
response = self._get_configuration(share)
response = self.get_configuration(share)
return ShareInfo.from_server_object(response)

def all(self, devices=None, protocol=None, search=None):
Expand Down Expand Up @@ -154,7 +158,7 @@ def modify(self, share, name=None, directory=None, # pylint: disable=too-many-a
devices=None, acl=None, description=None, access=None, export_to_nfs=None,
nfs_krb=None, trusted_nfs_clients=None, krb_sec=None, block_files=None,
export_to_ftp=None, validate_acl=True):
param = self._get_configuration(share)
param = self.get_configuration(share)

if name:
param.name = name
Expand Down
10 changes: 10 additions & 0 deletions docs/source/UserGuides/Miscellaneous/Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Changelog
=========

Unreleased
----------

Improvements
^^^^^^^^^^^^

* Publish ``Shares.get_configuration`` and ``DirectoryService.search_directory_services``.
Both are read-only. Directory search calls ``searchAD`` and does not import accounts.
``DirectoryService.fetch`` still calls ``syncAD``.

2.20.50
-------

Expand Down
18 changes: 18 additions & 0 deletions docs/source/UserGuides/Portal/Administration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,15 @@ Fetch Users & Groups
Directory Services
==================

.. automethod:: cterasdk.core.directoryservice.DirectoryService.search_directory_services
:noindex:

.. code-block:: python

"""Look up a domain group without importing it into the Portal"""
group = admin.directoryservice.search_directory_services(core_enum.SearchType.Groups, 'demo.local', 'Domain Users')
print(group.name, group.rid)

.. automethod:: cterasdk.core.directoryservice.DirectoryService.connect
:noindex:

Expand Down Expand Up @@ -1611,6 +1620,15 @@ Shares
.. automethod:: cterasdk.core.shares.Shares.get
:noindex:

.. automethod:: cterasdk.core.shares.Shares.get_configuration
:noindex:

.. code-block:: python

"""Read a share's configuration, including its directory, access type and ACL rules"""
configuration = admin.shares.get_configuration(share)
print(configuration.directory, configuration.access_type)

.. automethod:: cterasdk.core.shares.Shares.add
:noindex:

Expand Down
30 changes: 29 additions & 1 deletion tests/ut/core/admin/test_directory_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from cterasdk.core import directoryservice
from cterasdk.core.types import UserAccount, GroupAccount, DomainControllers, AccessControlEntry
from cterasdk.core.enum import Role, DirectoryServiceType, DirectoryServiceFetchMode, DirectorySearchEntityType
from cterasdk.core.enum import Role, DirectoryServiceType, DirectoryServiceFetchMode, DirectorySearchEntityType, SearchType
from cterasdk.common.types import ADDomainIDMapping
from cterasdk.common.object import Object
from cterasdk import exceptions
Expand Down Expand Up @@ -234,6 +234,34 @@ def test_user_search_found(self):
self._global_admin.api.execute.assert_called_once_with('', 'searchAD', mock.ANY)
self.assertEqual(ret.name, self._account_user_name)

def test_search_directory_services_returns_rid(self):
principal = munch.Munch({'name': self._account_group_name, 'rid': 513})
self._init_global_admin(execute_response=[principal])
ret = directoryservice.DirectoryService(self._global_admin).search_directory_services(
SearchType.Groups, self._domain, self._account_group_name)
self._global_admin.api.execute.assert_called_once_with('', 'searchAD', mock.ANY)
actual_param = self._global_admin.api.execute.call_args[0][2]
self.assertEqual(actual_param.mode, SearchType.Groups)
self.assertEqual(actual_param.domain, self._domain)
self.assertEqual(actual_param.name, self._account_group_name)
self.assertEqual(ret.rid, 513)
self.assertNotIn('syncAD', [call.args[1] for call in self._global_admin.api.execute.call_args_list])

def test_search_directory_services_not_found(self):
self._init_global_admin(execute_response=None)
with self.assertRaises(exceptions.CTERAException) as error:
directoryservice.DirectoryService(self._global_admin).search_directory_services(
SearchType.Users, self._domain, self._account_user_name)
self._global_admin.api.execute.assert_called_once_with('', 'searchAD', mock.ANY)
self.assertEqual(f'Could not find user: {self._account_user_name}@{self._domain}', str(error.exception))

def test_search_directory_services_no_match(self):
self._init_global_admin(execute_response=[munch.Munch({'name': 'random'})])
with self.assertRaises(exceptions.CTERAException) as error:
directoryservice.DirectoryService(self._global_admin).search_directory_services(
SearchType.Users, self._domain, self._account_user_name)
self.assertEqual('Search returned multiple results, but none matched the specified criteria.', str(error.exception))

def _create_access_control_entry(self, ace_type, ace_name, ace_role):
ace = Object()
ace.group = Object()
Expand Down
47 changes: 47 additions & 0 deletions tests/ut/core/admin/test_shares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from unittest import mock

import munch

from cterasdk import exceptions
from cterasdk.core import shares
from cterasdk.core.types import Share
from tests.ut.core.admin import base_admin


class TestCoreShares(base_admin.BaseCoreTest):

def setUp(self):
super().setUp()
self._share_id = '05db1713-842e-4f7b-ac27-43787f10c695'
self._configuration = munch.Munch({
'id': self._share_id,
'directory': '/cloud/users/Admin user/Projects',
'access_type': 'winAclMode',
'device_shares': [munch.Munch({'device_name': 'edge1'})],
'acl_rules': [],
})

def test_get_configuration(self):
self._mock_configuration(self._configuration)
ret = shares.Shares(self._global_admin).get_configuration(self._share_id)
self._global_admin.clients.v2.get.assert_called_once_with(f'shareManagement/configurations/{self._share_id}')
self.assertEqual(ret.directory, '/cloud/users/Admin user/Projects')
self.assertEqual(ret.access_type, 'winAclMode')
self.assertEqual(ret.device_shares[0].device_name, 'edge1')

def test_get_configuration_from_share(self):
self._mock_configuration(self._configuration)
share = Share(self._share_id, 'Projects', 'Admin user/Projects', ['SMB'], 'winAclMode', ['edge1'])
ret = shares.Shares(self._global_admin).get_configuration(share)
self._global_admin.clients.v2.get.assert_called_once_with(f'shareManagement/configurations/{self._share_id}')
self.assertEqual(ret, self._configuration)

def test_get_configuration_not_found(self):
self._mock_configuration(None)
with self.assertRaises(exceptions.ObjectNotFoundException) as error:
shares.Shares(self._global_admin).get_configuration(self._share_id)
self.assertEqual(f'Object not found: {self._share_id}', str(error.exception))

def _mock_configuration(self, configuration):
response = None if configuration is None else munch.Munch({'data': configuration})
self._global_admin.clients.v2.get = mock.MagicMock(return_value=response)
Loading