By jbhase
I am trying to create multiple groups via the Okta group API using requests. I have the following working to make a test group but now I need to figure out a way to pass 40+ group names into the body mapped to separate descriptions. I was thinking of using a yaml file with pyyaml but I just dont quite understand how to do that in this case. Any tips would be greatly appreciated.
#!/usr/bin/env python3
import requests
url = "https://{org}.okta.com/api/v1/groups"
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization" : "SSWS TESTKEY"
}
body = {
"profile": {
"name": "aws#test#api#00000000",
"description": "API Test Group"
}
}
r = requests.post(url, headers=headers, json=body)
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hello,
If you have many groups to create, it’s a good idea to define them in a structured format such as YAML or JSON. This will make it easier to manage and iterate over the groups.
Here’s an example of how you might do this with a YAML file:
# groups.yaml
groups:
- name: "group1"
description: "description1"
- name: "group2"
description: "description2"
# add as many groups as you need
You can then use PyYAML to read this file and loop through the groups to create them:
import requests
import yaml
url = "https://{org}.okta.com/api/v1/groups"
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization" : "SSWS TESTKEY"
}
# Load groups from YAML file
with open('groups.yaml', 'r') as file:
groups_data = yaml.load(file, Loader=yaml.FullLoader)
# Loop through groups and create each one
for group in groups_data['groups']:
body = {
"profile": {
"name": group['name'],
"description": group['description']
}
}
r = requests.post(url, headers=headers, json=body)
# You might want to check the status of the request here
r.raise_for_status()
This script will open the groups.yaml file, load its content, and for each group in the groups list, make a POST request to create the group. The group’s name and description are taken from the YAML file.
This approach can be easily expanded to create as many groups as needed, just by adding them to the YAML file. Also, you can catch and handle exceptions from requests.post or r.raise_for_status() to deal with possible issues during group creation.
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.