Saturday, August 21, 2010

### Script to create users from csv file

from java.io import FileInputStream


propInputStream = FileInputStream("domain.properties")
configProps = Properties()
configProps.load(propInputStream)

domainName=configProps.get("domain.name")
adminURL=configProps.get("admin.url")
adminUserName=configProps.get("admin.userName")
adminPassword=configProps.get("admin.password")
realmName=configProps.get("security.realmName")


connect(adminUserName, adminPassword, adminURL)
authenticatorPath= '/SecurityConfiguration/' + domainName + '/Realms/' + realmName + '/AuthenticationProviders/SQLAuthenticator'
#atnr=cmo.getSecurityConfiguration().getDefaultRealm().lookupAuthenticationProvider('DefaultAuthenticator')
print authenticatorPath
#print atnr
cd(authenticatorPath)
print ' '
print ' '


def createUser():
if cmo.userExists(UserName):
print "User already Exist = %s " % UserName
else:
cmo.createUser(UserName, Password, Description)
print "User Created   = %s " % UserName

import sys

### Read the command-line arguments
argslength = len(sys.argv)
if argslength < 2:
print '==>Insufficient arguments'
print '==>Syntax: java weblogic.WLST CreateUsers.py csv.file'
exit()
else:
### Read the csv file
fileName = sys.argv[1]
print('Reading File \"' + fileName + '\"' )
f = open(fileName)
try:
for line in f.readlines():
### Strip the comment lines
if line.strip().startswith('#'):
continue
else:
### Split the comma seperated values
items = line.split(',')
items = [item.strip() for item in items]
if len(items) != 3:
print "==>Bad line: %s" % line
print "==>Syntax: UserName, Password, Description"
continue
else:
(UserName, Password, Description) = items

### Call the create user
createUser()

except Exception, e:
print "==>Error Occured"
print e


exit()