Skip to content

Commit

Permalink
[minor_changes] Added functionality to resolve same name in remote an…
Browse files Browse the repository at this point in the history
…d local user.
  • Loading branch information
anvitha-jain committed Oct 4, 2023
1 parent c368798 commit 22cb1fb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
29 changes: 27 additions & 2 deletions plugins/module_utils/mso.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from ansible.module_utils._text import to_native, to_text
from ansible.module_utils.connection import Connection
from ansible_collections.cisco.mso.plugins.module_utils.constants import NDO_API_VERSION_PATH_FORMAT
from ansible_collections.cisco.nd.plugins.module_utils.nd import NDModule

try:
from requests_toolbelt.multipart.encoder import MultipartEncoder
Expand Down Expand Up @@ -238,6 +239,12 @@ def mso_site_anp_epg_bulk_staticport_spec():
mode=dict(type="str", choices=["native", "regular", "untagged"]),
)

def ndo_remote_user_spec():
return dict(
name=dict(type="str"),
login_domain=dict(type="str"), # This parameter is not required for querying all objects
)


# Copied from ansible's module uri.py (url): https://github.com/ansible/ansible/blob/cdf62edc65f564fff6b7e575e084026fa7faa409/lib/ansible/modules/uri.py
def write_file(module, url, dest, content, resp, tmpsrc=None):
Expand Down Expand Up @@ -916,9 +923,21 @@ def lookup_users(self, users, ignore_not_found_error=False):
users.append("admin")

ids = []
if self.platform == "nd":
nd = NDModule(self.module)
remote_users = nd.request("/nexus/infra/api/aaa/v4/remoteusers", method="GET")
local_users = nd.request("/nexus/infra/api/aaa/v4/localusers", method="GET")

for user in users:
u = dict()
if self.platform == "nd":
u = self.get_obj("users", loginID=user, api_version="v2")
local_user = self.get_user_from_list_of_users(user, local_users)
u = local_user
if local_user is None:
remote_user = self.get_user_from_list_of_users(user, remote_users)
u = remote_user

# u = self.get_obj("users", loginID=user, api_version="v2")
else:
u = self.get_obj("users", username=user)
if not u and not ignore_not_found_error:
Expand All @@ -935,8 +954,14 @@ def lookup_users(self, users, ignore_not_found_error=False):
if id in ids:
self.fail_json(msg="User '{0}' is duplicate.".format(user))
ids.append(id)

return ids

def get_user_from_list_of_users(self, user_name, list_of_users):
"""Get user from list of users"""
for user in list_of_users.get("items"):
if user.get("spec").get("loginID") == user_name:
return user.get("spec")
return None

def create_label(self, label, label_type):
"""Create a new label"""
Expand Down
6 changes: 5 additions & 1 deletion plugins/modules/mso_tenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"""

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, ndo_remote_user_spec
from ansible_collections.cisco.mso.plugins.module_utils.constants import YES_OR_NO_TO_BOOL_STRING_MAP


Expand All @@ -130,6 +130,7 @@ def main():
display_name=dict(type="str"),
tenant=dict(type="str", aliases=["name"]),
users=dict(type="list", elements="str"),
remote_users=dict(type="list", elements="dict", options=ndo_remote_user_spec()),
sites=dict(type="list", elements="str"),
orchestrator_only=dict(type="str", default="yes", choices=["yes", "no"]),
state=dict(type="str", default="present", choices=["absent", "present", "query"]),
Expand All @@ -156,6 +157,9 @@ def main():
sites = mso.lookup_sites(module.params.get("sites"))
users = mso.lookup_users(module.params.get("users"))


# remote_users = mso.lookup_users(module.params.get("remote_users"))

tenant_id = None
path = "tenants"

Expand Down

0 comments on commit 22cb1fb

Please sign in to comment.