Creation bulk users using API

tbaror

Contributor
Joined
Mar 20, 2013
Messages
105
Hi All,

I started to play with Freenas Api , the task i trying to accomplish is create bulk of users
While creating one user all seems to work fine using following below example
But when i am trying to make python read json file with bulk of users i get error
Code:
{"error_message": "Sorry, this request could not be processed. Please try again later."}

My question is what would be best way to write bulk of users to freenas
Thanks

create single user
Code:
import json
import requests
r = requests.post(
  'http://192.10.11.245/api/v1.0/account/users/',
  auth=('root', 'password'),
  headers={'Content-Type': 'application/json'},
  verify=False,
  data=json.dumps({
  "bsdusr_builtin": 'false',
  "bsdusr_email": "",
  "bsdusr_full_name": "testuser",
  "bsdusr_password": "password",
  "bsdusr_group": 14,
  "bsdusr_home": "/mnt/PO01/ftp_vol/FtpRoot/Customers/testuser/",
  "bsdusr_locked": 'false',
  "bsdusr_password_disabled": 'false',
  "bsdusr_uid": 1201,
  "bsdusr_username": "testuser",
  "bsdusr_sudo":'false',
} )
)
print(r.text)
 

anodos

Sambassador
iXsystems
Joined
Mar 6, 2014
Messages
9,554
Hi All,

I started to play with Freenas Api , the task i trying to accomplish is create bulk of users
While creating one user all seems to work fine using following below example
But when i am trying to make python read json file with bulk of users i get error
Code:
{"error_message": "Sorry, this request could not be processed. Please try again later."}

My question is what would be best way to write bulk of users to freenas
Thanks

create single user
Code:
import json
import requests
r = requests.post(
  'http://192.10.11.245/api/v1.0/account/users/',
  auth=('root', 'password'),
  headers={'Content-Type': 'application/json'},
  verify=False,
  data=json.dumps({
  "bsdusr_builtin": 'false',
  "bsdusr_email": "",
  "bsdusr_full_name": "testuser",
  "bsdusr_password": "password",
  "bsdusr_group": 14,
  "bsdusr_home": "/mnt/PO01/ftp_vol/FtpRoot/Customers/testuser/",
  "bsdusr_locked": 'false',
  "bsdusr_password_disabled": 'false',
  "bsdusr_uid": 1201,
  "bsdusr_username": "testuser",
  "bsdusr_sudo":'false',
} )
)
print(r.text)

You'll probably have to write your script so that you generate one post request per user that is added. This can probably be done pretty easily with arrays and a loop. You may have to limit the rate at which you're sending the post requests (not overload the middleware).
 

anodos

Sambassador
iXsystems
Joined
Mar 6, 2014
Messages
9,554
Hi All,

I started to play with Freenas Api , the task i trying to accomplish is create bulk of users
While creating one user all seems to work fine using following below example
But when i am trying to make python read json file with bulk of users i get error
Code:
{"error_message": "Sorry, this request could not be processed. Please try again later."}

My question is what would be best way to write bulk of users to freenas
Thanks

create single user
Code:
import json
import requests
r = requests.post(
  'http://192.10.11.245/api/v1.0/account/users/',
  auth=('root', 'password'),
  headers={'Content-Type': 'application/json'},
  verify=False,
  data=json.dumps({
  "bsdusr_builtin": 'false',
  "bsdusr_email": "",
  "bsdusr_full_name": "testuser",
  "bsdusr_password": "password",
  "bsdusr_group": 14,
  "bsdusr_home": "/mnt/PO01/ftp_vol/FtpRoot/Customers/testuser/",
  "bsdusr_locked": 'false',
  "bsdusr_password_disabled": 'false',
  "bsdusr_uid": 1201,
  "bsdusr_username": "testuser",
  "bsdusr_sudo":'false',
} )
)
print(r.text)
If you get this working, post your experience and code here. It'd be nice to have more examples of API usage. ;)
 

tbaror

Contributor
Joined
Mar 20, 2013
Messages
105
Hello ,
Yes i managed to make it work
I manage to copy Crushftp users to Freenas format by using Freenas Api as follows code below
finally what i did is to read each line from csv and update user predefined user template and create it in FreeNas API
Thanks

Code:
import json,csv
import requests



usertemplate = {
  "bsdusr_builtin": 'false',
  "bsdusr_email": "",
  "bsdusr_full_name": "",
  "bsdusr_password": "",
  "bsdusr_group": 14,
  "bsdusr_home": "",
  "bsdusr_locked": 'false',
  "bsdusr_password_disabled": 'false',
  "bsdusr_uid": 1201,
  "bsdusr_username": "",
  "bsdusr_sudo":'false',
}

def read_cell(x, y):
    with open('crushftp.csv', 'r') as f:
        reader = csv.reader(f)
        y_count = 0
        for n in reader:
            if y_count == y:
                cell = n[x]
                return cell
            y_count += 1

print (read_cell(1, 1))




i = 0
id = 1200
with open("crushftp.csv","r") as f:
    for line in f:
        usertemplate["bsdusr_full_name"] = read_cell(0, i)
        usertemplate["bsdusr_password"] = read_cell(1, i)
        usertemplate["bsdusr_home"] = read_cell(2, i)
        usertemplate["bsdusr_uid"] = id
        usertemplate["bsdusr_username"] = read_cell(0, i)

        r = requests.post(
          'http://192.10.11.245/api/v1.0/account/users/',
          auth=('root', 'password'),
          headers={'Content-Type': 'application/json'},
          verify=False,
          data=json.dumps(usertemplate)
        )
        print(r.text)

        i +=1
        id +=1
 

LiNixG33k

Cadet
Joined
Feb 21, 2019
Messages
1
I wasn't able to get this code working immediately. I've made a few changes and figured I'd provide that here for anyone who needs it.

If it doesn't work when you copy and paste you can grab it from https://matthewxspencer.com/freenas_bulk_user_add
Code:
import json,csv
import requests



usertemplate = {
  'bsdusr_uid': '1100',
  'bsdusr_username': '',
  'bsdusr_mode': '755',
  'bsdusr_creategroup': 'True',
  'bsdusr_password': '',
  'bsdusr_shell': '/usr/local/bin/bash',
  'bsdusr_full_name': '',
  'bsdusr_email': '',
}

def read_cell(x, y):
    with open('new_users.csv', 'r') as f:
        reader = csv.reader(f)
        y_count = 0
        for n in reader:
            if y_count == y:
                cell = n[x]
                return cell
            y_count += 1

print (read_cell(1, 0))




i = 0
id = 1200
with open("new_users.csv","r") as f:
    for line in f:
        usertemplate["bsdusr_full_name"] = read_cell(0, i)
        usertemplate["bsdusr_password"] = read_cell(1, i)
        usertemplate["bsdusr_home"] = read_cell(2, i)
        usertemplate["bsdusr_uid"] = id
        usertemplate['bsdusr_username'] = read_cell(0, i)
        r = requests.post(
          'http://192.168.168.138/api/v1.0/account/users/',
          auth=('root', 'password'),
          headers={'Content-Type': 'application/json'},
          verify=False,
          data=json.dumps(usertemplate)
        )
        print(r.text)

        i +=1
        id +=1
 
Top