Confusing requirements and output get_admin_list using the SDK - Guidance requested
Hello all,
I'm trying to gather a list of users (role agnostic) and include only a specific set of fields in my output, filtered by a last_login_on timeframe. When I attempt to do this through the API via requests.get(url) via a python script (aka without invoking the SDK) it works just fine with no extra required fields in the output. However, when using the SDK API Class, I am required to include 'role,username,password,email' otherwise I receive an error. To make matters more confusing, when I add the required fields, the output then doesn't include just those specified fields, it includes all fields and ignores what I'm asking for.
This leads me to two questions:
- Am I missing something regarding the SDK's logic or use? ... paraphrased... Why can't I only request the fields I care about like with the regular requests.get API calls?
- What benefit is there to requiring field for a "get"?
Thanks for your responses in advance,
Frank
https://www.logicmonitor.com/support-files/rest-api-developers-guide/sdks/docs/#api-LM-getAdminList doc (for reference and websearchability)
From the above link - 
LM User
Oh right, I didn't think my code was buggy...
FrankG
OPIt is sorted out now. I made a typo at some point that didn't cause the script to fail and it basically made the comparison not work correctly. Thanks again for your stellar help @Stuart Weenig
LM User
You might try casting them both to integers, it's probable that log.happened_on is coming in as a string. Python should recast it, but you can't go wrong making sure.
Also, there may be some timezone funkiness going on there. I only had a handful of users in the portal i tested against. You might try outputting the human readable log.happened_on as compared to the human readable inactivedate value. That's where i'd start.
FrankG
OPDISREGARD THIS POST - I FOUND A TYPO
Seeing some strange behavior from that audit log {log.happened_on} > comparison. I added some code to output the test result. As the output from thefilter="_all~\"Add a new account\"- was also squirrelly from the API, I'm very glad I checked. Screenshot attached below. It seems like it can't compare epoch times correctly, but it's intermittent at first before failing completely beyond a certain user. Very strange - any ideas?FrankG
OPAmazingly done. Very straightforward now that I'm looking at it. Seemed like more work than that, but this is why you're the master. I see your note about the filtering returning an extra unrelated entry for some reason. I see that when using the API directly also. Strangeness.
I'll have to fold that bit in and give it a test. I've also done some enhancements from this end that may be useful for others (excluding certain users with specific role assignments, usernames, or API Only users). I'll do the needful, decruft, and share shortly. Thanks again Stuart!
LM User
I got some filtering working through the sdk, so i added the logic to look through the audit logs (don't worry, it only fetches the audit logs that indicate a new user was added). It checks, using username, to see if the user was added after the inactivedate and if so, doesn't disable that user.
https://github.com/sweenig/lmcommunity/blob/master/LMAutomation/disable_old_users.py
FrankG
OPThanks again @Stuart Weenig - I'll submit those enhancement requests.
LM User
¯\_(ツ)_/¯
I don't see another way. The id never decrements, so maybe if you're running it on a regular basis, you could keep track of what the max(id) was during a particular run and use that to determine which accounts are new since the last time you ran it. Would require local storage of that bit of data though.
I don't know if i've ever gotten filtering/fields to work through the SDK.
FrankG
OPThis works great @Stuart Weenig. Thank you.
Now to the second related issue... I wonder why there is a created_by field but not a created_on field for the user account. I see there is one for the api_tokens, but not the user itself.
For example, now that I want to suspend user access automagically... I want to confirm that the user who hasn't logged in ever (aka last_login_on = 0) wasn't recently created, say the last 30 days. To handle that automatically I believe I'll need to query the audit log for a "Add a new account" text from the prior 30 days to define which set of users who've never logged in should be suspended.
Do you know of another way to handle that @Stuart Weenig? I might submit an enhancement request if not. Seems useful to me, I'm sure it'd be useful for others. I'd rather not load the last 30 days of audit log entries looking for newly created users and mapping them to userIDs if possible. The audit log entries don't include the newly created user ID which also seems like an enhancement request.
I've tried everything I can think of to make the filtering by description work in the SDK. This is another example where it also seems to work via the API (albeit after stealing the formatting from the Dev Tools) but not via the SDK.
Thanks for your time!
LM User
Here you go. This uses my lmwrapper helper script, so if you're not using that, you'd need to setup the api object manually. Just set the month, day, and year variables to the date you'd want.
FrankG
OPI'm trying to gather user ID / email / last login and auto suspend inactive users and then notify them or account primary contact (manager) on the process to re-enable their account. It's all there on the requests.get side, so I will re-eval the SDK use for something else.
LM User
I think doing it via the SDK for this case is still the right way to do it. Just instead of trying to filter using the fetch request, fetch everything, then filter in your script. What are the fields you're looking to get for each user?
FrankG
OPThank you for confirming I'm not nuts at least... ?
I was intent on working with the SDK a bit and I chose a current need as an example and it looks like I picked the wrong one to start with. I'll jump back in the shallow end of the pool for now. Thanks Stuart!
LM User
This is likely a bi-product of generating the python sdk through swagger in an automated fashion rather than building it by hand. The advantage is that we have a mostly working SDK for all our endpoints. Downside is stuff like this that doesn't always make usability sense. Hopefully that answers both your questions.
As to what to do about it, is there any reason you can't just get all of it and filter through the response in python? It shouldn't be too hard and would still let you use the SDK.
For what it's worth, when i do:
I get all the fields back too, so it's not just you.