Skip to content

Commit

Permalink
UD Scripts
Browse files Browse the repository at this point in the history
UD scripts to demonstrated
  • Loading branch information
sohaibajmal-okta committed Mar 27, 2017
0 parents commit 7c3570d
Show file tree
Hide file tree
Showing 4 changed files with 483 additions and 0 deletions.
127 changes: 127 additions & 0 deletions activate_staged_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import requests
import json
import re
import sys
import csv

orgName = ""
apiKey = ""

api_token = "SSWS "+ apiKey

headers = {'Accept':'application/json','Content-Type':'application/json','Authorization':api_token}

def GetPaginatedResponse(url):

response = requests.request("GET", url, headers=headers)

returnResponseList = []

responseJSON = json.dumps(response.json())

responseList = json.loads(responseJSON)

returnResponseList = returnResponseList + responseList


if "errorCode" in responseJSON:

print "\nYou encountered following Error: \n"
print responseJSON
print "\n"

return "Error"

else:

headerLink= response.headers["Link"]

count = 1

while str(headerLink).find("rel=\"next\"") > -1:

linkItems = str(headerLink).split(",")

nextCursorLink = ""
for link in linkItems:

if str(link).find("rel=\"next\"") > -1:
nextCursorLink = str(link)


nextLink = str(nextCursorLink.split(";")[0]).strip()
nextLink = nextLink[1:]
nextLink = nextLink[:-1]

url = nextLink

print "\nCalling Paginated Url " + str(url) + " " + str(count) + " \n"
response = requests.request("GET", url, headers=headers)

responseJSON = json.dumps(response.json())

responseList = json.loads(responseJSON)

returnResponseList = returnResponseList + responseList

headerLink= response.headers["Link"]

count += 1


returnJSON = json.dumps(returnResponseList)

return returnResponseList

def ActivateUser(url):

response = requests.post(url, headers=headers)

responseJSON = response.json()

if "errorCode" in responseJSON:
print "\nYou encountered following Error: \n"
print responseJSON
print "\n"

return "Error"

else:

return responseJSON



def UpdateAssignment():


getStagedUsersUrl = "https://"+orgName+".com/api/v1/users?filter=status eq \"STAGED\""

stagedUsers = GetPaginatedResponse(getStagedUsersUrl)

count = 1

for stagedUser in stagedUsers:

userId = str(stagedUser[u"id"])

print "Activating " + stagedUser[u"profile"][u"firstName"] + " " + stagedUser[u"profile"][u"lastName"] + stagedUser[u"profile"][u"login"] + " " + str(count)

acticvateUserUrl = "https://"+orgName+".com/api/v1/users/" + userId + "/lifecycle/activate?sendEmail=false"


activationResponse = ActivateUser(acticvateUserUrl)

if "Error" != activationResponse:

print stagedUser[u"profile"][u"firstName"] + " " + stagedUser[u"profile"][u"lastName"] + stagedUser[u"profile"][u"login"] + " ACTIVATED\n"

count += 1





if __name__ == "__main__":

UpdateAssignment()
100 changes: 100 additions & 0 deletions create_random_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import requests
import json
import re
import sys
import csv

orgName = ""
apiKey = ""

def CreateUsers():

N = 3

firstName = "OktaSupp"
lastName = "TestUser"

## CSV file before user is created

beforeCreation = open("User-Info-Before-Creation.csv", "wb")
beforeWriter = csv.writer(beforeCreation)

beforeWriter.writerow(["firstName", "lastName", "email", "login"])

afterCreation = open("User-Info-After-Creation.csv", "wb")
afterWriter = csv.writer(afterCreation)
afterWriter.writerow(["id", "status", "email","Okta-Request-Id"])

for userNum in range(1, N):

try:

email = firstName.lower()+str(userNum)+lastName.lower()+str(userNum)+"@mailinator.com"

## First name of the user
print "\n Creating User " + email + "\n"

#Write to CSV before creating
beforeWriter.writerow([firstName,lastName,email,email])


user_info = {}

user_info['profile'] = {}
user_info['credentials'] = {}

user_info['profile'] ['firstName'] = firstName+str(userNum)
user_info['profile'] ['lastName'] = lastName+str(userNum)
user_info['profile'] ['email'] = email
user_info['profile'] ['login'] = email

user_info['credentials'] ['password'] = {}
user_info['credentials'] ['recovery_question'] = {}

user_info['credentials'] ['password']['value'] = "TestPass123$"
user_info['credentials']['recovery_question'] ['question'] = "Who's a major player"
user_info['credentials']['recovery_question'] ['answer'] = "Ev3ry1"

user_info_json = json.dumps(user_info)

url = "https://"+orgName+".com/api/v1/users"

api_token = "SSWS "+ apiKey
headers = {'Accept':'application/json','Content-Type':'application/json','Authorization':api_token}

print user_info
response = requests.post(url, data = user_info_json, headers = headers)

responseJSON = response.json()

##Read headers
oktaRequestId= response.headers["X-Okta-Request-Id"]

if "errorCode" in responseJSON:


print responseJSON

else:

userId = responseJSON["id"]
status = responseJSON["status"]
userEmail = responseJSON["profile"]["email"]



#Write to CSV after creating
afterWriter.writerow([userId,status,email,oktaRequestId])

print "\n User " + email + " Created Successfully"

except:

print "Unexpected error:", sys.exc_info()
print "\n"
pass

if __name__ == "__main__":
CreateUsers()


104 changes: 104 additions & 0 deletions get_all_active_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import requests
import json
import re
import sys
import csv


orgName = ""

apiKey = ""

api_token = "SSWS "+ apiKey

headers = {'Accept':'application/json','Content-Type':'application/json','Authorization':api_token}

def GetPaginatedResponse(url):

response = requests.request("GET", url, headers=headers)

returnResponseList = []

responseJSON = json.dumps(response.json())

responseList = json.loads(responseJSON)

returnResponseList = returnResponseList + responseList


if "errorCode" in responseJSON:

print "\nYou encountered following Error: \n"
print responseJSON
print "\n"

return "Error"

else:

headerLink= response.headers["Link"]

while str(headerLink).find("rel=\"next\"") > -1:

linkItems = str(headerLink).split(",")

nextCursorLink = ""
for link in linkItems:

if str(link).find("rel=\"next\"") > -1:
nextCursorLink = str(link)


nextLink = str(nextCursorLink.split(";")[0]).strip()
nextLink = nextLink[1:]
nextLink = nextLink[:-1]

url = nextLink

response = requests.request("GET", url, headers=headers)

responseJSON = json.dumps(response.json())

responseList = json.loads(responseJSON)

returnResponseList = returnResponseList + responseList

headerLink= response.headers["Link"]


returnJSON = json.dumps(returnResponseList)

return returnResponseList



def DownloadSFUsers():

url = "https://"+orgName+".com/api/v1/users"

responseJSON = GetPaginatedResponse(url)

if responseJSON != "Error":


userFile = open("All-Users-In-Okta.csv", "wb")

writer = csv.writer(userFile)

writer.writerow(["firstName", "lastName", "email", "login"])

for user in responseJSON:

firstName = user[u"profile"][u"firstName"]
lastName = user[u"profile"][u"lastName"]
email = user[u"profile"][u"email"]
login = user[u"profile"][u"login"]

row = firstName+","+lastName+","+email+","+login

writer.writerow([firstName,lastName,email,login])


if __name__ == "__main__":

DownloadSFUsers()
Loading

0 comments on commit 7c3570d

Please sign in to comment.