Search in sources :

Example 1 with UserGroupService

use of com.synopsys.integration.blackduck.service.dataservice.UserGroupService in project synopsys-detect by blackducksoftware.

the class BlackDuckConnectivityChecker method determineConnectivity.

public BlackDuckConnectivityResult determineConnectivity(BlackDuckServerConfig blackDuckServerConfig) throws DetectUserFriendlyException {
    logger.debug("Detect will check communication with the Black Duck server.");
    ConnectionResult connectionResult = blackDuckServerConfig.attemptConnection(new SilentIntLogger());
    if (connectionResult.isFailure()) {
        // TODO: For the logs, when connection result returns the client, can drop this.
        blackDuckServerConfig.attemptConnection(new Slf4jIntLogger(logger));
        logger.error("Failed to connect to the Black Duck server");
        return BlackDuckConnectivityResult.failure(connectionResult.getFailureMessage().orElse("Could not reach the Black Duck server or the credentials were invalid."));
    }
    BlackDuckServicesFactory blackDuckServicesFactory = blackDuckServerConfig.createBlackDuckServicesFactory(new Slf4jIntLogger(logger));
    BlackDuckRegistrationService blackDuckRegistrationService = blackDuckServicesFactory.createBlackDuckRegistrationService();
    UserService userService = blackDuckServicesFactory.createUserService();
    try {
        String version = blackDuckRegistrationService.getBlackDuckServerData().getVersion();
        logger.info(String.format("Successfully connected to Black Duck (version %s)!", version));
        if (logger.isDebugEnabled()) {
            // These (particularly fetching roles) can be very slow operations
            UserView userView = userService.findCurrentUser();
            logger.debug("Connected as: " + userView.getUserName());
            UserGroupService userGroupService = blackDuckServicesFactory.createUserGroupService();
            List<RoleAssignmentView> roles = userGroupService.getRolesForUser(userView);
            logger.debug("Roles: " + roles.stream().map(RoleAssignmentView::getName).distinct().collect(Collectors.joining(", ")));
            BlackDuckApiClient blackDuckApiClient = blackDuckServicesFactory.getBlackDuckApiClient();
            List<UserGroupView> groups = blackDuckApiClient.getAllResponses(userView.metaMultipleResponses(USERGROUPS));
            logger.debug("Group: " + groups.stream().map(UserGroupView::getName).distinct().collect(Collectors.joining(", ")));
        }
    } catch (IntegrationException e) {
        throw new DetectUserFriendlyException("Could not determine which version of Black Duck detect connected to or which user is connecting.", e, ExitCodeType.FAILURE_BLACKDUCK_CONNECTIVITY);
    }
    return BlackDuckConnectivityResult.success(blackDuckServicesFactory, blackDuckServerConfig);
}
Also used : IntegrationException(com.synopsys.integration.exception.IntegrationException) UserService(com.synopsys.integration.blackduck.service.dataservice.UserService) RoleAssignmentView(com.synopsys.integration.blackduck.api.generated.view.RoleAssignmentView) UserView(com.synopsys.integration.blackduck.api.generated.view.UserView) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) BlackDuckServicesFactory(com.synopsys.integration.blackduck.service.BlackDuckServicesFactory) BlackDuckRegistrationService(com.synopsys.integration.blackduck.service.dataservice.BlackDuckRegistrationService) SilentIntLogger(com.synopsys.integration.log.SilentIntLogger) UserGroupView(com.synopsys.integration.blackduck.api.generated.view.UserGroupView) DetectUserFriendlyException(com.synopsys.integration.detect.configuration.DetectUserFriendlyException) Slf4jIntLogger(com.synopsys.integration.log.Slf4jIntLogger) UserGroupService(com.synopsys.integration.blackduck.service.dataservice.UserGroupService) ConnectionResult(com.synopsys.integration.rest.client.ConnectionResult)

Aggregations

RoleAssignmentView (com.synopsys.integration.blackduck.api.generated.view.RoleAssignmentView)1 UserGroupView (com.synopsys.integration.blackduck.api.generated.view.UserGroupView)1 UserView (com.synopsys.integration.blackduck.api.generated.view.UserView)1 BlackDuckApiClient (com.synopsys.integration.blackduck.service.BlackDuckApiClient)1 BlackDuckServicesFactory (com.synopsys.integration.blackduck.service.BlackDuckServicesFactory)1 BlackDuckRegistrationService (com.synopsys.integration.blackduck.service.dataservice.BlackDuckRegistrationService)1 UserGroupService (com.synopsys.integration.blackduck.service.dataservice.UserGroupService)1 UserService (com.synopsys.integration.blackduck.service.dataservice.UserService)1 DetectUserFriendlyException (com.synopsys.integration.detect.configuration.DetectUserFriendlyException)1 IntegrationException (com.synopsys.integration.exception.IntegrationException)1 SilentIntLogger (com.synopsys.integration.log.SilentIntLogger)1 Slf4jIntLogger (com.synopsys.integration.log.Slf4jIntLogger)1 ConnectionResult (com.synopsys.integration.rest.client.ConnectionResult)1