Showing posts with label CSV. Show all posts
Showing posts with label CSV. Show all posts

Sunday, March 7, 2021

Sailpoint IdentityIQ Creating Business Role Using API

 Creating Business Role Using API

public static void buildRole(HashMap lineHash) {
		 
		String roleName = lineHash.get("RoleName").toString();
		String roleType = lineHash.get("RoleType").toString();
		String locCity = lineHash.get("locCity").toString();
		String coNumber = lineHash.get("coNumber").toString();
		
		System.out.println("locCity " + locCity );
		System.out.println("coNumber " + coNumber );
		
		//Added
		String displayName = lineHash.get("displayName").toString();
		String subRoleName = lineHash.get("requiredRole").toString();

		
		String roleOwner = "";
		String roleExists = "yes";
		Bundle role;
		System.out.println("Data " + lineHash );
		try {
		    role = context.getObject(Bundle.class, roleName);
			if (null == role) {
				role = new sailpoint.object.Bundle();
				roleExists = "no";
			}
			if (roleType.equalsIgnoreCase("business") && roleExists.equalsIgnoreCase("no")) {
				System.out.println("Creating Role :" + roleName );
				AccountSelectorRules rules = new AccountSelectorRules();
				role.setAccountSelectorRules(rules);
				
				HashMap mapDesc = new HashMap();
				mapDesc.put("en_US", "This is the BR " + roleName);
				
				Identity ownerId = context.getObject(Identity.class, roleOwner);
				if (null == ownerId) {
					ownerId = context.getObject(Identity.class, "spadmin");
				}
				role.setName(roleName);
				role.setDescriptions(mapDesc);
				role.setType("business");
				role.setAllowDuplicateAccounts(false);
				role.setAllowMultipleAssignments(false);
				role.setMergeTemplates(false);
				role.setOwner(ownerId);
				
				//Added
				role.setDisplayName(displayName);
				Bundle requiredRole = context.getObjectByName(Bundle.class, subRoleName);
				role.addRequirement(requiredRole);			

				IdentitySelector is = new IdentitySelector();
				MatchExpression me = new MatchExpression();
				me.setAnd(false);

				MatchTerm term = new MatchTerm();
				MatchTerm term1 = new MatchTerm();
				MatchTerm term2 = new MatchTerm();
				
				if((!(locCity.equalsIgnoreCase(""))) && (!(coNumber.equalsIgnoreCase("")))){
				
				term1.setName("locCity");
				term1.setValue(locCity);
				term.addChild(term1);
				
				term2.setName("coNumber");
				term2.setValue(coNumber);
				term.addChild(term2);
				
				term.setAnd(true);
				term.setContainer(true);
				me.addTerm(term);
				
				}else if(locCity.equalsIgnoreCase("")){
				
				term2.setName("coNumber");
				term2.setValue(coNumber);
				me.addTerm(term2);
				}else if(coNumber.equalsIgnoreCase("")){
				term2.setName("locCity");
				term2.setValue(locCity);
				me.addTerm(term2);
				}
				
				is.setMatchExpression(me);
				role.setSelector(is);
				context.saveObject(role);
				context.commitTransaction();
				context.decache();
			
			}else if(roleType.equalsIgnoreCase("business") && roleExists.equalsIgnoreCase("yes")){			
			    
				IdentitySelector is = role.getSelector();
				MatchExpression me = is.getMatchExpression();
				MatchTerm term = new MatchTerm();
				MatchTerm term1 = new MatchTerm();
				MatchTerm term2 = new MatchTerm();

				if((!(locCity.equalsIgnoreCase(""))) && (!(coNumber.equalsIgnoreCase("")))){
				
				term1.setName("locCity");
				term1.setValue(locCity);
				term.addChild(term1);
				
				term2.setName("coNumber");
				term2.setValue(coNumber);
				term.addChild(term2);
				
				term.setAnd(true);
				term.setContainer(true);
				me.addTerm(term);
				}else if(locCity.equalsIgnoreCase("")){		
				term2.setName("coNumber");
				term2.setValue(coNumber);
				me.addTerm(term2);
				}else if(coNumber.equalsIgnoreCase("")){
				term2.setName("locCity");
				term2.setValue(locCity);
				me.addTerm(term2);
				}
				
				is.setMatchExpression(me);
				role.setSelector(is);
				
				context.saveObject(role);
				context.commitTransaction();
				context.decache();
			} else{
			
			System.out.println("Doing Nothing !" );
			
			}
		} catch (GeneralException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		}

Friday, March 5, 2021

Reading CSV File Data

Sample data

RoleName,displayName,RoleType,locCity,coNumber
AAA1,AAA1 DN,business,AA,1
AAA1,AAA1 DN,business,BB,2
AAA1,AAA1 DN,business,CC,3
AAA2,AAA2 DN,business,AA,1
AAA2,AAA2 DN,business,BB,2
AAA2,AAA2 DN,business,CC,3
AAA2,AAA2 DN,business,DD,4
AAA2,AAA2 DN,business,EE,5
AAA2,AAA2 DN,,business,FF,6
AAA2,AAA2 DN,business,GG,7
AAA2,AAA2 DN,business,HH,8
AAA2,AAA2 DN,business,II,9
AAA2,AAA2 DN,business,JJ,10
AAA2,AAA2 DN,business,KK,11
AAA2,AAA2 DN,business,LL,12
AAA2,AAA2 DN,business,MM,13
AAA1,AAA1 DN,business,DD,4
AAA1,AAA1 DN,business,EE,5
AAA2,AAA2 DN,business,NN,14

Sample Code
 
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule  language="beanshell"  name="VIS CreateBundle">
  <Source>
  <![CDATA[
import java.io.File;
import java.util.List;
import java.util.HashMap;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import sailpoint.api.SailPointContext;
import sailpoint.api.SailPointFactory;
import sailpoint.object.Bundle;
import sailpoint.object.Identity;
import sailpoint.object.IdentitySelector;
import sailpoint.spring.SpringStarter;
import sailpoint.tools.GeneralException;
import sailpoint.tools.RFC4180LineParser;
import sailpoint.object.AccountSelectorRules;
import sailpoint.object.IdentitySelector.MatchTerm;
import sailpoint.object.IdentitySelector.MatchExpression;

		
		String dlm = ",";
		String csvFileName = "/tmp/bCopy/SampleBundle.csv";

		System.out.println("Role Creation Started ...");

		File bundleFile = new File(csvFileName);
		if ((!bundleFile.exists()) || (bundleFile.isDirectory())) {
			System.out.println("Unable to find the bundle csv file: " + csvFileName);
			return;
		}

		System.out.println("Reading Bundle Data from: '" + csvFileName);

		
		try {
			int lineCounter = 0;
			ArrayList headerStrings = new ArrayList();
			String thisLine = "";
		
			BufferedReader fileIn = new BufferedReader(new FileReader(csvFileName));

			RFC4180LineParser parser = new RFC4180LineParser(dlm);

			while (null != (thisLine = fileIn.readLine())) {
				ArrayList tokens = parser.parseLine(thisLine);
				if (lineCounter == 0) {
					for (int i = 0; i<tokens.size(); i++) {
						headerStrings.add((String) (tokens.get(i)));
					}
				}else {
					HashMap lineHash = new HashMap();
					for (int i = 0; i<tokens.size(); i++) {
						String headerString = headerStrings.get(i);
						String valueString = (String) tokens.get(i);
						if (null != valueString) {
							valueString = valueString.trim();
						}else{
							//Added Else Condition
							valueString = "";
						}
						lineHash.put(headerString, valueString);
					}
					try{ 
						//buildRole(lineHash);
					}catch (Exception e){
						System.out.println("Error While Creating Bundle " + lineHash);				
					}	
				}
				lineCounter++;				
			}
			fileIn.close();
			System.out.println("Role Creation Completed ...");					
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (GeneralException e) {
			e.printStackTrace();
		}
]]>
  </Source>
</Rule>