Python SDK and creating multiple customProperties in one go
I’m trying to be able to supply a script to add some CustomProperties and I was wondering so that I don’t have to reinvent the wheel, if anyone has a quick method to building what is required to set in the BODY of the post.
Let’s say I have some different custom properties I want to add:
corp.building = “Some Building Name”
corp.department = “Some Department Name”
corp.role = “Some Role”
Can I wrap all these up into a single array/dictionary to pass into the “customProperties” attribute in the “body” of the post?
The idea could be I feed a CSV file into the script and the amount of customProperties can be 1 or 10 for any given line being processed.
Thanks and let me know if I need to shed more light on what I’m expecting to happen.
Jeff_W_
Posted 3 years ago·Last reply 3 years ago
6 comments
LM User
·3 years agoIt’s not actually a Python problem It’s an LM problem. LM is the one that decided to use a list of dictionaries instead of a dictionary.
Jeff_W_
OP3 years agoThis should be helpful for me. Once I set up my customProps as a list of dictionaries it worked like a charm (this is where my python lacks, coming more from a PHP background). I was close, just slightly off ;)
Thanks for the tips and snippets.
LM User
·3 years agoFWIW, I wouldn’t use argparse to pull in the csv data. I’d actually use the built in CSV handling capabilities of Python:
Given the following CSV file called “example.csv” in the current working directory:
The usage would look like this:
LM User
·3 years agoYep, I do something very similar, except that I’ve started using environment variables for the credentials (see input_method below). This function should facilitate creating your groups. Actually, it’s idempotent, so if you call it once, it’ll create the group. The second time you call it, it should just update the existing one:
Example usage:
Jeff_W_
OP3 years agoThanks Stuart. One of the libraries I was using is “argparse” from the CLI.
My test script right now looks something like:
./script.py --primary ”Folder Name” --location ”123 somewhere ln, here, there 11111” --custom “building:Some Building” --custom “department:Some Department” --custom “role:Some Role”
When I push via the SDK to the api.add_device_group() function, I would basically take the --location and all the --custom ones set via the CLI and “build” the customProperties that I want to POST in the body of the api call (along with some more common , one off variables I scrape).
I’ll mess with the listofDicts as you illustrated and go from there.
Thanks for your help.
LM User
·3 years agoYou’d have a couple different things going on. I suggest trying to break down your code into separate functions.
Example to translate properties into something that can be patched in: