Skip to content

Commit e6a9826

Browse files
author
Nivedithaa Mahendran
committed
update
1 parent 12f1fb6 commit e6a9826

2 files changed

Lines changed: 15 additions & 128 deletions

File tree

src/mas/devops/users.py

Lines changed: 7 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def get_user(self, user_id):
209209
Raises:
210210
Exception: If the API returns an unexpected status code.
211211
"""
212-
self.logger.info(f"Getting user {user_id}")
212+
self.logger.debug(f"Getting user {user_id}")
213213
resource_id = None
214214

215215
# For MAS version >= 9.1, use the Manage API masperuser endpoint
@@ -234,11 +234,8 @@ def get_user(self, user_id):
234234
cert=self.manage_internal_client_pem_file_path,
235235
verify=self.manage_internal_ca_pem_file_path
236236
)
237-
self.logger.info(f"GET {url} returned {response.status_code}")
238-
self.logger.info(f"Response: {response.text}")
239237

240238
user_info = response.json()
241-
self.logger.info(f"Response json: {user_info}")
242239

243240
# Parse resource_id from user_info
244241
if user_info and "member" in user_info and len(user_info["member"]) > 0:
@@ -248,7 +245,7 @@ def get_user(self, user_id):
248245
resource_id = href.split("/")[-1]
249246
self.logger.info(f"Extracted resource_id: {resource_id} from user_info")
250247

251-
# Second request: Get full user details using resource_id
248+
# Second request: Get full user details
252249
url = f"{self.manage_api_url_internal}/maximo/api/os/masperuser/"
253250
querystring = {
254251
"lean": 1,
@@ -266,8 +263,6 @@ def get_user(self, user_id):
266263
cert=self.manage_internal_client_pem_file_path,
267264
verify=self.manage_internal_ca_pem_file_path
268265
)
269-
self.logger.info(f"GET {url} returned {response.status_code}")
270-
self.logger.info(f"Response: {response.json()}")
271266
else:
272267
# For earlier versions, use the Core API v3/users endpoint
273268
url = f"{self.mas_api_url_internal}/v3/users/{user_id}"
@@ -352,7 +347,7 @@ def get_or_create_user(self, payload):
352347
"Content-Type": "application/json",
353348
"apikey": maxadmin_manage_api_key["apikey"]
354349
}
355-
self.logger.info(f"Creating new user {user_id} with Manage API with payload {payload}")
350+
self.logger.debug(f"Creating new user {user_id} with Manage API with payload {payload}")
356351
response = requests.post(
357352
url,
358353
json=payload,
@@ -361,8 +356,6 @@ def get_or_create_user(self, payload):
361356
cert=self.manage_internal_client_pem_file_path,
362357
verify=self.manage_internal_ca_pem_file_path
363358
)
364-
self.logger.info(f"Response status code: {response.status_code}")
365-
self.logger.info(f"Response text: {response.text}")
366359
if response.status_code == 201:
367360
# Manage API returns empty response body on success, fetch the user
368361
if response.text:
@@ -373,7 +366,7 @@ def get_or_create_user(self, payload):
373366
href = response_data["member"][0].get("href", "")
374367
if href and "/" in href:
375368
resource_id = href.split("/")[-1]
376-
self.logger.info(f"Extracted resource_id: {resource_id} from create response")
369+
self.logger.debug(f"Extracted resource_id: {resource_id} from create response")
377370
return resource_id, response_data
378371
else:
379372
# Fetch the newly created user
@@ -447,7 +440,7 @@ def set_user_group_reassignment_auth(self, user_id, resource_id, groupreassign,
447440
"grpreassignauth": groupreassign
448441
}
449442
}
450-
self.logger.info(f"Sending PATCH request to {url} with payload: {payload}")
443+
self.logger.debug(f"Sending PATCH request to {url} with payload: {payload}")
451444

452445
response = requests.post(
453446
url,
@@ -457,8 +450,6 @@ def set_user_group_reassignment_auth(self, user_id, resource_id, groupreassign,
457450
cert=self.manage_internal_client_pem_file_path,
458451
verify=self.manage_internal_ca_pem_file_path
459452
)
460-
self.logger.info(f"Response status code: {response.status_code}")
461-
self.logger.info(f"Response text: {response.text}")
462453

463454
if response.status_code in [200, 204]:
464455
self.logger.info(f"Successfully set group reassignment authorization for resource {resource_id}")
@@ -619,7 +610,7 @@ def link_user_to_local_idp(self, user_id, email_password=False, manage_api_key=N
619610
]
620611
}
621612
}
622-
self.logger.info(f"Sending PATCH request to {url} with payload: {payload}")
613+
self.logger.debug(f"Sending PATCH request to {url} with payload: {payload}")
623614

624615
response = requests.post(
625616
url,
@@ -629,8 +620,6 @@ def link_user_to_local_idp(self, user_id, email_password=False, manage_api_key=N
629620
cert=self.manage_internal_client_pem_file_path,
630621
verify=self.manage_internal_ca_pem_file_path
631622
)
632-
self.logger.info(f"Response status code: {response.status_code}")
633-
self.logger.info(f"Response text: {response.text}")
634623

635624
if response.status_code in [200, 204]:
636625
self.logger.info(f"Successfully linked user {user_id} to local IDP")
@@ -1005,7 +994,6 @@ def get_manage_api_key_for_user(self, user_id):
1005994
Raises:
1006995
Exception: If the API call fails.
1007996
"""
1008-
self.logger.info(f"Getting Manage API Key for user {user_id}")
1009997
url = f"{self.manage_api_url_internal}/maximo/api/os/mxapiapikey"
1010998
querystring = {
1011999
"ccm": 1,
@@ -1024,7 +1012,6 @@ def get_manage_api_key_for_user(self, user_id):
10241012
verify=self.manage_internal_ca_pem_file_path,
10251013
cert=self.manage_internal_client_pem_file_path
10261014
)
1027-
self.logger.info(f"Response: {response.status_code} {response.text}")
10281015

10291016
if response.status_code == 200:
10301017
json = response.json()
@@ -1269,92 +1256,6 @@ def get_all_manage_groups(self):
12691256
self.logger.info(f"Found {len(groups)} security groups in Manage")
12701257
return groups
12711258

1272-
# def grant_group_reassignment_auth(self, user_id, group_name, manage_api_key):
1273-
# """
1274-
# Grant a user authorization to reassign users to/from a specific security group.
1275-
1276-
# This adds an entry to the grpreassignauth collection for the user, allowing them
1277-
# to manage membership in the specified security group.
1278-
1279-
# Args:
1280-
# user_id (str): The unique identifier of the user.
1281-
# group_name (str): The name of the security group to grant authorization for.
1282-
# manage_api_key (dict): API key record with 'apikey' field for authentication.
1283-
1284-
# Returns:
1285-
# None: Returns None on success.
1286-
1287-
# Raises:
1288-
# Exception: If the operation fails.
1289-
# """
1290-
# self.logger.info(f"Granting user {user_id} authorization to reassign group {group_name}")
1291-
1292-
# url = f"{self.manage_api_url_internal}/maximo/oslc/os/masperuser"
1293-
# querystring = {
1294-
# "lean": 1,
1295-
# "oslc.where": f"personid=\"{user_id}\"",
1296-
# }
1297-
# headers = {
1298-
# "Content-Type": "application/json",
1299-
# "Accept": "application/json",
1300-
# "x-method-override": "PATCH",
1301-
# "patchtype": "MERGE",
1302-
# "apikey": manage_api_key["apikey"],
1303-
# }
1304-
# payload = {
1305-
# "maxuser": [
1306-
# {
1307-
# "userid": user_id,
1308-
# "grpreassignauth": [
1309-
# {
1310-
# "groupname": group_name
1311-
# }
1312-
# ]
1313-
# }
1314-
# ]
1315-
# }
1316-
# response = requests.post(
1317-
# url,
1318-
# headers=headers,
1319-
# params=querystring,
1320-
# json=payload,
1321-
# verify=self.manage_internal_ca_pem_file_path,
1322-
# )
1323-
# if response.status_code != 204:
1324-
# raise Exception(f"{response.status_code} {response.text}")
1325-
1326-
# return None
1327-
1328-
# def grant_all_group_reassignment_auth(self, user_id, manage_api_key):
1329-
# """
1330-
# Grant a user authorization to reassign users to/from ALL security groups.
1331-
1332-
# This method fetches all security groups and grants reassignment authorization
1333-
# for each one, allowing the user to fully manage security group memberships.
1334-
1335-
# Args:
1336-
# user_id (str): The unique identifier of the user.
1337-
# manage_api_key (dict): API key record with 'apikey' field for authentication.
1338-
1339-
# Returns:
1340-
# None: Returns None on success.
1341-
1342-
# Raises:
1343-
# Exception: If the operation fails.
1344-
# """
1345-
# self.logger.info(f"Granting user {user_id} authorization to reassign ALL security groups")
1346-
1347-
# groups = self.get_all_manage_groups(manage_api_key)
1348-
1349-
# for group_name in groups:
1350-
# try:
1351-
# self.grant_group_reassignment_auth(user_id, group_name, manage_api_key)
1352-
# except Exception as e:
1353-
# self.logger.warning(f"Failed to grant reassignment auth for group {group_name}: {str(e)}")
1354-
# # Continue with other groups even if one fails
1355-
1356-
# self.logger.info(f"Completed granting group reassignment authorization for {len(groups)} groups")
1357-
13581259
def get_mas_applications_in_workspace(self):
13591260
"""
13601261
Retrieve all MAS applications configured in the workspace.
@@ -1547,7 +1448,6 @@ def create_initial_users_for_saas(self, initial_users):
15471448

15481449
all_security_groups = self.get_all_manage_groups()
15491450
groupreassign = [{"groupname": group} for group in all_security_groups]
1550-
self.logger.info(f"Group reassign: {groupreassign}")
15511451

15521452
for primary_user in primary_users:
15531453
self.logger.info("")
@@ -1750,30 +1650,18 @@ def create_initial_user_for_saas(self, user, user_type, groupreassign=None):
17501650
"personid": user_id,
17511651
"primaryemailtype": "Work",
17521652
"primaryemail": user_email,
1753-
# "username": username,
17541653
"primaryphone": "",
17551654
"addressline1": "",
17561655
"displayName": display_name,
17571656
"maxuser": maxuser_def,
1758-
# "issuer": "local",
1759-
# "permissions": permissions,
1760-
# "entitlement": entitlement,
1761-
# "givenName": user_given_name,
1762-
# "familyName": user_family_name
17631657
}
17641658

1765-
self.logger.info(f"User def - {user_def}")
1766-
resource_id, user_info = self.get_or_create_user(user_def)
1767-
self.logger.info(f"Resource ID - {resource_id}")
1768-
self.logger.info(f"User info - {user_info}")
1659+
resource_id, _ = self.get_or_create_user(user_def)
17691660

17701661
# For version >= 9.1, we always need a Manage API key and resource_id to link user to local IDP
17711662
# For version < 9.1, we may need it later for manage_security_groups
17721663
if Version(self.mas_version) >= Version('9.1') or (len(manage_security_groups) > 0 and "manage" in self.mas_workspace_application_ids):
17731664
maxadmin_manage_api_key = self.create_or_get_manage_api_key_for_user(MASUserUtils.MAXADMIN, temporary=True)
1774-
self.logger.info(f"Maxadmin manage api key - {maxadmin_manage_api_key}")
1775-
1776-
if Version(self.mas_version) >= Version('9.1'):
17771665
self.link_user_to_local_idp(user_id, email_password=True, manage_api_key=maxadmin_manage_api_key, resource_id=resource_id)
17781666
else:
17791667
self.link_user_to_local_idp(user_id, email_password=True)

test/src/test_users.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ def mock_get_user(requests_mock, user_id, json, status_code, mock_manage_api_key
223223
)
224224

225225
# Second request: Mock the query-based request with personid
226-
# This matches the actual implementation at line 258-275 in users.py
227226
# Always mock this for version >= 9.1, regardless of status_code
228227
manage_personid_mock = requests_mock.get(
229228
f"{MANAGE_API_URL}/maximo/api/os/masperuser/?lean=1&oslc.where=personid%3D%22{user_id}%22&oslc.select=personid%2Cdisplayname",
@@ -2152,7 +2151,7 @@ def test_create_initial_user_for_saas(
21522151
# Mock get_or_create_user to return appropriate response based on version
21532152
# Note: user_id might be None at this point, it gets set to user_email later
21542153
actual_user_id = user_id if user_id is not None else user_email
2155-
if mas_version == '9.1':
2154+
if Version(mas_version) >= Version('9.1'):
21562155
# For 9.1, return tuple (resource_id, user_data) with member array containing href
21572156
resource_id = f"_{actual_user_id.replace('@', '_').replace('.', '_')}_resource_id"
21582157
user_utils.get_or_create_user = MagicMock(return_value=(
@@ -2195,7 +2194,7 @@ def test_create_initial_user_for_saas(
21952194
username = user_id
21962195

21972196
# For version 9.1 PRIMARY users, pass groupreassign parameter
2198-
if mas_version == '9.1' and user_type == "PRIMARY":
2197+
if Version(mas_version) >= Version('9.1') and user_type == "PRIMARY":
21992198
groupreassign = [{"groupname": "USERMANAGEMENT"}]
22002199
user_utils.create_initial_user_for_saas(initial_users, user_type, groupreassign)
22012200
else:
@@ -2224,7 +2223,7 @@ def test_create_initial_user_for_saas(
22242223
"givenName": user_given_name,
22252224
"familyName": user_family_name
22262225
}
2227-
else: # 9.1
2226+
else: # >=9.1
22282227
if user_type == "PRIMARY":
22292228
maxuser_def = {
22302229
"userid": user_id,
@@ -2268,7 +2267,7 @@ def test_create_initial_user_for_saas(
22682267
user_utils.get_or_create_user.assert_called_once_with(expected_user_def)
22692268

22702269
# Check link_user_to_local_idp call based on version
2271-
if mas_version == '9.1':
2270+
if Version(mas_version) >= Version('9.1'):
22722271
resource_id = f"_{actual_user_id.replace('@', '_').replace('.', '_')}_resource_id"
22732272
user_utils.link_user_to_local_idp.assert_called_once_with(user_id, email_password=True, manage_api_key=manage_api_key, resource_id=resource_id)
22742273
else:
@@ -2285,7 +2284,7 @@ def test_create_initial_user_for_saas(
22852284
call(user_id, "iot", application_role),
22862285
call(user_id, "facilities", facilities_role),
22872286
])
2288-
else: # 9.1
2287+
else: # >=9.1
22892288
user_utils.await_mas_application_availability.assert_not_called()
22902289
user_utils.set_user_application_permission.assert_not_called()
22912290

@@ -2302,7 +2301,7 @@ def test_create_initial_user_for_saas(
23022301

23032302
# For version >= 9.1, API key is always created (needed for link_user_to_local_idp)
23042303
# For version < 9.1, API key is only created if there are manage_security_groups
2305-
if mas_version == '9.1' or len(manage_security_groups) > 0:
2304+
if Version(mas_version) >= Version('9.1') or len(manage_security_groups) > 0:
23062305
user_utils.create_or_get_manage_api_key_for_user.assert_called_once_with("MAXADMIN", temporary=True)
23072306
else:
23082307
user_utils.create_or_get_manage_api_key_for_user.assert_not_called()
@@ -2315,10 +2314,10 @@ def test_create_initial_user_for_saas(
23152314
list(map(lambda sg: call(user_id, sg, manage_api_key), manage_security_groups))
23162315
)
23172316
user_utils.set_user_group_reassignment_auth.assert_not_called()
2318-
else: # 9.1
2317+
else: # >=9.1
23192318
user_utils.add_user_to_manage_group.assert_not_called()
23202319
if user_type == "PRIMARY":
2321-
# For 9.1, both user_id and resource_id are passed
2320+
# For versions >= 9.1, both user_id and resource_id are passed
23222321
actual_user_id = user_id if user_id is not None else user_email
23232322
resource_id = f"_{actual_user_id.replace('@', '_').replace('.', '_')}_resource_id"
23242323
user_utils.set_user_group_reassignment_auth.assert_called_once_with(actual_user_id, resource_id, [{"groupname": "USERMANAGEMENT"}], manage_api_key)

0 commit comments

Comments
 (0)