Forum Discussion

FrankG's avatar
3 years ago

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 - 


 

  • Anonymous's avatar
    Anonymous
    3 years ago

    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. 

    from lm import lm
    from datetime import datetime
    
    #datetime before which to consider users inactive
    month = 10
    day = 1
    year = 2021
    inactivedate = datetime(year,month,day,0,0,0)
    
    #these next few variables are for use in pagination
    size = 1000
    offset = 0
    results = []
    endfound = False
    while not endfound:
        users = lm.get_admin_list(size=size, offset=offset).items #fetch a page
        results.extend(users) #add the fetched data to the tally
        offset += size #figure out the next page range
        endfound = len(users) < size #check to see if we fetched an incomplete page (e.g. we're done)
    
    for user in results:
        lastlogin = datetime.utcfromtimestamp(user.last_login_on) #convert the string to a datetime object so we can compare it to our inactivedate
        if lastlogin < inactivedate and user.status != 'suspended':
            print(f"Disabling {user.username} (id={user.id}), last login time: {lastlogin.strftime('%Y-%m-%d')}")
            response = lm.patch_admin_by_id(user.id, body={'status':'suspended'})
            print(f"{user.username} (id={user.id}) is now {response.status}")