Check API for existence of a user - TrueNAS-SCALE-22.12.2

bothunbr

Cadet
Joined
Jul 12, 2012
Messages
2
Hello Everyone,

Is there a simple way to check if a local username exists with the TNScale API? The way I'm doing now is overly complicated. I'm calling /user and looping thru all usernames.

The goal is to check to see if a username already exists in the system before calling userPost as there are several tasks that need to be be done before I actually create the user.

I'm sure there is a very simple solution that I'm missing.

Thanks in advance for any assistance you can provide.
--Brad
 

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,399
midclt call user.query | jq -r '.[]|.username|select(. == "<username to match>")'

Returns the matching username if it already exists, or an empty string if not.
 

Samuel Tai

Never underestimate your own stupidity
Moderator
Joined
Apr 24, 2020
Messages
5,399
There's also the old reliable finger -m -p <username to match>.
 

bothunbr

Cadet
Joined
Jul 12, 2012
Messages
2
Samuel,

Thank you for the ultra fast response and thank the internet for people like you!

I found a much easier way to query username thru the API. I really shouldn't have spent hours figuring this out as it was so simple.

/api/v2.0/user?username=testuser2

Maybe this will help others.

Thanks again for your help!
--Brad
 
Top