It's all about the answers!

Ask a question

How to bulk add one user to 400 project areas?


Jorge Alarcon (411022) | asked Apr 27 '16, 8:03 p.m.
edited May 04 '16, 5:13 a.m. by Krzysztof Kaźmierczyk (7.4k373103)
Do you know if there is a way to automate importing and assigning licenses to users into JTS?

Regards

3 answers



permanent link
Ralph Schoon (63.1k33645) | answered May 04 '16, 7:53 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
 Too big for a comment. The code would look like:

/*******************************************************************************
 * Licensed Materials - Property of IBM
 * (c) Copyright IBM Corporation 2013. All Rights Reserved. 
 * 
 * ServerSetup
 *
 * Note to U.S. Government Users Restricted Rights:  Use, duplication or 
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *******************************************************************************/
package com.ibm.js.team.admin.automation;

import java.net.URI;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

import com.ibm.team.process.client.IClientProcess;
import com.ibm.team.process.client.IProcessItemService;
import com.ibm.team.process.common.IProcessArea;
import com.ibm.team.process.common.IProcessItem;
import com.ibm.team.process.common.IProjectArea;
import com.ibm.team.process.common.IRole;
import com.ibm.team.repository.client.ITeamRepository;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;
import com.ibm.team.repository.client.TeamPlatform;
import com.ibm.team.repository.common.IContributor;
import com.ibm.team.repository.common.TeamRepositoryException;

/**
 * Automates setting up the RTC Extension workshop. Class to set up a project,
 * users and the SCM component for a project Extracts given files into the SCM
 * system.
 * 
 */
public class AddUserToProjectArea {


/**
* Basic constructor.
*/
public AddUserToProjectArea() {
super();
}

/**
* Main Entry Point run the whole server setup
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
boolean result;
System.out.println("Starting Team Platform");
if (!TeamPlatform.isStarted())
TeamPlatform.startup();
try {
System.out.println("Team Platform started");

AddUserToProjectArea setupParea = new AddUserToProjectArea();
result = setupParea.run(args);
} catch (TeamRepositoryException x) {
x.printStackTrace();
result = false;
} finally {
TeamPlatform.shutdown();
}

if (!result)
System.exit(1);
}

/**
* This actually does the work
* @param args
* @return
* @throws Exception
*/
private boolean run(String[] args) throws Exception {
if (args.length != 5) {
System.out
.println("Usage: CreateProjectArea [repositoryURI] [adminUserId] [adminPassword] [projectAreaName] [addUserID] [addRoleID]");
return false;
}

// Get the parameters
String repositoryURI = args[0];
String adminUserId = args[1];
String adminPassword = args[2];
String projectAreaName = args[3];
String addUserID = args[4];
String addRoleID = args[5];
IProgressMonitor monitor = new NullProgressMonitor();

ITeamRepository teamRepository = logIntoTeamRepository(repositoryURI,
adminUserId, adminPassword, monitor);

IProcessItemService service = (IProcessItemService) teamRepository
.getClientLibrary(IProcessItemService.class);

URI uri = URI.create(projectAreaName.replaceAll(" ", "%20"));
IProjectArea area = (IProjectArea) service.findProcessArea(uri,
IProcessItemService.ALL_PROPERTIES, monitor);
if (area == null) {
System.out.println("Project Area not found: " + projectAreaName);
return false;
}
addUsersAndRoles(teamRepository, area, addUserID, addRoleID, monitor);

System.out.println("Success");
return true;
}


/**
* Adds users and roles to a project area.
* @param teamRepository
* @param area
* @param monitor
* @return
* @throws TeamRepositoryException
*/
private IProjectArea addUsersAndRoles(ITeamRepository teamRepository,
IProjectArea area, String userID, String roleID, IProgressMonitor monitor)
throws TeamRepositoryException {

IProcessItemService service = (IProcessItemService) teamRepository
.getClientLibrary(IProcessItemService.class);
area = (IProjectArea) service.getMutableCopy(area);
System.out.println("Trying to add member with roles: "
+ roleID + " to Project Area" + area.getName());

IContributor user = teamRepository.contributorManager().fetchContributorByUserId(userID, monitor);

IRole role = getRole(area, roleID, monitor);

area.addAdministrator(user);
area.addMember(user);
area.addRoleAssignments(user, new IRole[] { role });
IProcessItem[] items = service.save(new IProcessItem[] { area },
monitor);
System.out.println("Users with Roles added to " + area.getName());
return area;
}

/**
* Gets a role by its ID.
* @param area
* @param roleID
* @param monitor
* @return
* @throws TeamRepositoryException
*/
private IRole getRole(IProcessArea area, String roleID,
IProgressMonitor monitor) throws TeamRepositoryException {
ITeamRepository repo = (ITeamRepository) area.getOrigin();
IProcessItemService service = (IProcessItemService) repo
.getClientLibrary(IProcessItemService.class);
IClientProcess clientProcess = service.getClientProcess(area, monitor);
IRole[] availableRoles = clientProcess.getRoles(area, monitor);
for (int i = 0; i < availableRoles.length; i++) {
IRole role = availableRoles[i];
// IRole2 role2 = (IRole2)role;
// role2.getRoleName();
if (role.getId().equalsIgnoreCase(roleID))
return role;
}
throw new IllegalArgumentException("Couldn't find roles");
}

/**
* Manages the login process.
* @param repositoryURI
* @param userId
* @param password
* @param monitor
* @return
* @throws TeamRepositoryException
*/
private ITeamRepository logIntoTeamRepository(String repositoryURI,
String userId, String password, IProgressMonitor monitor)
throws TeamRepositoryException {
System.out.println("Trying to log into repository: " + repositoryURI);
ITeamRepository teamRepository = TeamPlatform
.getTeamRepositoryService().getTeamRepository(repositoryURI);
teamRepository.registerLoginHandler(new LoginHandler(userId, password));
teamRepository.login(monitor);
System.out.println("Login succeeded.");
return teamRepository;
}

/**
* Login Handler implementation
*/
private static class LoginHandler implements ILoginHandler, ILoginInfo {

private String fUserId;
private String fPassword;

private LoginHandler(String userId, String password) {
fUserId = userId;
fPassword = password;
}

public String getUserId() {
return fUserId;
}

public String getPassword() {
return fPassword;
}

public ILoginInfo challenge(ITeamRepository repository) {
return this;
}
}
}


Comments
Jorge Alarcon commented May 04 '16, 12:41 p.m.

hi Ralph

Thanks so much for this, I don't have too much experience on java but I will take a look at this option.


permanent link
Ralph Schoon (63.1k33645) | answered May 04 '16, 7:05 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
  You have to do the import from LDAP first. Once the user is member of the project area, you could use the Plain Java Client Libraries to add the user to all project areas. 

I don't have code for everything, but most parts.

Setup API work: https://rsjazz.wordpress.com/2015/09/30/learning-to-fly-getting-started-with-the-rtc-java-apis/
Iterate all project areas: https://rsjazz.wordpress.com/2012/12/09/analyzing-a-aroject-areas-members-and-roles-using-the-plain-java-client-libraries/
Adding users to project areas: https://rsjazz.wordpress.com/2013/09/18/deploying-templates-and-creating-projects-using-the-plain-java-clients-library/
Also see snippet 3 shipped with the plain java client libraries.


permanent link
Krzysztof Kaźmierczyk (7.4k373103) | answered Apr 28 '16, 2:53 a.m.
Hi Jorge,
There is a repotools -importUsers command which allows you to set user roles and licenses. You can find more in the documentation: https://jazz.net/help-dev/clm/index.jsp?re=1&topic=/com.ibm.jazz.install.doc/topics/r_repotools_importusers.html&scope=null
Let us know if this is what you were looking for

Comments
Jorge Alarcon commented Apr 29 '16, 4:18 p.m. | edited Apr 29 '16, 4:49 p.m.

Hi

I know about this option and I find it useful, however our situation is the following:

We need to add 1 user, yeah, only 1, but it needs to be added on more than 400 project areas spread around on several servers, so I wanted to see If there was a way I could automate the import by only providing the JTS url and the license, similar to the option you mentioned to me on the other question to add users to project area.

Also this user needs to be imported from ldap

Do you think there is a way to do this?

Regards


Krzysztof Kaźmierczyk commented May 04 '16, 5:59 a.m.

Hi Jorge,
If the user needs to be added to 400 projects, it is possible that you are doing something wrong. What is the reason that this user needs to be added to so many project areas?


Jorge Alarcon commented May 04 '16, 12:40 p.m.

Hi

The request came from a client and apparently they will use the user as a functional/global id, it will be added as a project admin on several project areas so they can run some reports.


Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.