Skip to content

Commit 1023456

Browse files
author
Nivedithaa Mahendran
committed
update
1 parent 4082dfb commit 1023456

2 files changed

Lines changed: 116 additions & 54 deletions

File tree

src/mas/devops/users.py

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,15 @@ def get_user(self, user_id):
202202
user_id (str): The unique identifier of the user to retrieve.
203203
204204
Returns:
205-
dict: User details dictionary if found, None if user doesn't exist (404).
205+
tuple: (resource_id, user_data) where:
206+
- resource_id (str|None): Resource ID extracted from href for version >= 9.1, None for version < 9.1
207+
- user_data (dict|None): User details dictionary if found, None if user doesn't exist (404)
206208
207209
Raises:
208210
Exception: If the API returns an unexpected status code.
209211
"""
210212
self.logger.debug(f"Getting user {user_id}")
213+
resource_id = None
211214

212215
# For MAS version >= 9.1, use the Manage API masperuser endpoint
213216
if Version(self.mas_version) >= Version('9.1'):
@@ -230,9 +233,36 @@ def get_user(self, user_id):
230233
cert=self.manage_internal_client_pem_file_path,
231234
verify=self.manage_internal_ca_pem_file_path
232235
)
236+
user_info = response.json()
233237
self.logger.info(f"GET {url} returned {response.status_code}")
234238
self.logger.info(f"Response: {response.text}")
235239
self.logger.info(f"Response json: {response.json}")
240+
241+
# Parse resource_id from user_info for version >= 9.1
242+
if Version(self.mas_version) >= Version('9.1') and user_info:
243+
# Check if user_info has member array with href
244+
if "member" in user_info and len(user_info["member"]) > 0:
245+
href = user_info["member"][0].get("href", "")
246+
# Extract resource_id from href (e.g., "api/os/masperuser/<resource_id>")
247+
if href and "/" in href:
248+
resource_id = href.split("/")[-1]
249+
self.logger.info(f"Extracted resource_id: {resource_id} from user_info")
250+
251+
if resource_id is not None:
252+
url = f"{self.manage_api_url_internal}/maximo/api/os/masperuser/{resource_id}"
253+
headers = {
254+
"Accept": "application/json",
255+
"apikey": maxadmin_manage_api_key["apikey"]
256+
}
257+
response = requests.get(
258+
url,
259+
headers=headers,
260+
cert=self.manage_internal_client_pem_file_path,
261+
verify=self.manage_internal_ca_pem_file_path
262+
)
263+
self.logger.info(f"GET {url} returned {response.status_code}")
264+
self.logger.info(f"Response: {response.text}")
265+
self.logger.info(f"Response json: {response.json}")
236266
else:
237267
# For earlier versions, use the Core API v3/users endpoint
238268
url = f"{self.mas_api_url_internal}/v3/users/{user_id}"
@@ -247,10 +277,10 @@ def get_user(self, user_id):
247277
)
248278

249279
if response.status_code == 404:
250-
return None
280+
return resource_id, None
251281

252282
if response.status_code == 200:
253-
return response.json()
283+
return resource_id, response.json()
254284

255285
raise Exception(f"{response.status_code} {response.text}")
256286

@@ -272,7 +302,9 @@ def get_or_create_user(self, payload):
272302
(version >= 9.1) as the unique identifier.
273303
274304
Returns:
275-
dict: The user record (either existing or newly created).
305+
tuple: (resource_id, user_data) where:
306+
- resource_id (str|None): Resource ID extracted from href for version >= 9.1, None for version < 9.1
307+
- user_data (dict): The user record (either existing or newly created)
276308
277309
Raises:
278310
Exception: If user creation fails with an unexpected status code.
@@ -281,13 +313,13 @@ def get_or_create_user(self, payload):
281313
user_id_field = "personid" if Version(self.mas_version) >= Version('9.1') else "id"
282314
user_id = payload[user_id_field]
283315

284-
existing_user = self.get_user(user_id)
316+
resource_id, existing_user = self.get_user(user_id)
285317

286318
if existing_user is not None:
287319
# Log using the appropriate field based on version
288320
user_identifier = existing_user.get('personid') or existing_user.get('id')
289321
self.logger.info(f"Existing user {user_identifier} found")
290-
return existing_user
322+
return resource_id, existing_user
291323

292324
self.logger.info(f"Creating new user {user_id}")
293325

@@ -318,9 +350,17 @@ def get_or_create_user(self, payload):
318350
if response.status_code == 201:
319351
# Manage API returns empty response body on success, fetch the user
320352
if response.text:
321-
return response.json()
353+
response_data = response.json()
354+
# Parse resource_id from response if available
355+
resource_id = None
356+
if "member" in response_data and len(response_data["member"]) > 0:
357+
href = response_data["member"][0].get("href", "")
358+
if href and "/" in href:
359+
resource_id = href.split("/")[-1]
360+
self.logger.info(f"Extracted resource_id: {resource_id} from create response")
361+
return resource_id, response_data
322362
else:
323-
# Fetch the newly created user from Core API
363+
# Fetch the newly created user
324364
return self.get_user(user_id)
325365
else:
326366
# For earlier versions, use the Core API v3/users endpoint
@@ -338,7 +378,8 @@ def get_or_create_user(self, payload):
338378
verify=self.core_internal_ca_pem_file_path
339379
)
340380
if response.status_code == 201:
341-
return response.json()
381+
# For version < 9.1, resource_id is None
382+
return None, response.json()
342383

343384
# if response.status_code == 409:
344385
# json = response.json()
@@ -504,7 +545,7 @@ def link_user_to_local_idp(self, user_id, email_password=False):
504545
"""
505546

506547
# For the sake of idempotency, check if the user already has a local identity
507-
user = self.get_user(user_id)
548+
resource_id, user = self.get_user(user_id)
508549
if user is None:
509550
raise Exception(f"User {user_id} was not found")
510551

@@ -725,7 +766,7 @@ def check_user_sync(self, user_id, application_id, timeout_secs=60 * 10, retry_i
725766
t_end = time.time() + timeout_secs
726767
self.logger.info(f"Awaiting user {user_id} sync status \"SUCCESS\" for app {application_id}: {t_end - time.time():.2f} seconds remaining")
727768
while time.time() < t_end:
728-
user = self.get_user(user_id)
769+
resource_id, user = self.get_user(user_id)
729770

730771
if "applications" not in user or application_id not in user["applications"] or "sync" not in user["applications"][application_id] or "state" not in user["applications"][application_id]["sync"]:
731772
self.logger.warning(f"User {user_id} does not have any sync state for application {application_id}, triggering resync")
@@ -771,7 +812,7 @@ def resync_users(self, user_ids):
771812
# which reduces the impact of concurrent updates leading to race conditions)
772813

773814
for user_id in user_ids:
774-
user = self.get_user(user_id)
815+
resource_id, user = self.get_user(user_id)
775816
self.update_user_display_name(user_id, user["displayName"])
776817

777818
def create_or_get_manage_api_key_for_user(self, user_id, temporary=False):
@@ -1621,20 +1662,10 @@ def create_initial_user_for_saas(self, user, user_type, groupreassign=None):
16211662
}
16221663

16231664
self.logger.info(f"User def - {user_def}")
1624-
user_info = self.get_or_create_user(user_def)
1665+
resource_id, user_info = self.get_or_create_user(user_def)
1666+
self.logger.info(f"Resource ID - {resource_id}")
16251667
self.logger.info(f"User info - {user_info}")
16261668

1627-
# Parse resource_id from user_info for version >= 9.1
1628-
resource_id = None
1629-
if Version(self.mas_version) >= Version('9.1') and user_info:
1630-
# Check if user_info has member array with href
1631-
if "member" in user_info and len(user_info["member"]) > 0:
1632-
href = user_info["member"][0].get("href", "")
1633-
# Extract resource_id from href (e.g., "api/os/masperuser/<resource_id>")
1634-
if href and "/" in href:
1635-
resource_id = href.split("/")[-1]
1636-
self.logger.info(f"Extracted resource_id: {resource_id} from user_info")
1637-
16381669
self.link_user_to_local_idp(user_id, email_password=True)
16391670
self.add_user_to_workspace(user_id, is_workspace_admin=is_workspace_admin)
16401671

0 commit comments

Comments
 (0)