/// <summary> /// Compare the list of users on the Server Site with the provision list of users and make the necessary updates /// </summary> /// <param name="existingSiteUsers"></param> private WorkingListSiteUsers Execute_ProvisionUsers(TableauServerSignIn siteSignIn) { //If there are no provisioning instructions, then there is nothing we need to do here if (_provisionInstructions.UsersToProvision == null) { _statusLogs.AddStatus("Skipping users/roles provisioning, because there are no instructions for this..."); return(null); } _statusLogs.AddStatusHeader("Provision users/roles in site"); //================================================================================= //Load the set of users for the site... //================================================================================= var existingUsers = DownloadUsersList.CreateAndExecute(siteSignIn); //Keep a list of the remaining users var workingListUnexaminedUsers = new WorkingListSiteUsers(existingUsers.Users); var workingList_allKnownUsers = new WorkingListSiteUsers(existingUsers.Users); //============================================================================== //Go through each user in our "to provision" list and see if we need to // 1. Add them as a new user // 2. Update an exisitng user //============================================================================== _statusLogs.AddStatusHeader("Process explicit users list"); foreach (var userToProvision in _provisionInstructions.UsersToProvision) { try { Execute_ProvisionUsers_SingleUser(userToProvision, siteSignIn, workingListUnexaminedUsers, workingList_allKnownUsers); } catch (Exception ex) { _statusLogs.AddError("Error provisioning user (913-1158)" + userToProvision.UserName + ", " + ex.Message); CSVRecord_ErrorUpdatingUser(userToProvision.UserName, userToProvision.UserRole, userToProvision.UserAuthentication, "Error provisioning user " + userToProvision.UserName + ", " + ex.Message); } //If the user was in the working list of users on the site, then remove them (since we have looked at them) workingListUnexaminedUsers.RemoveUser(userToProvision.UserName); } //============================================================================================ //Examine all the remaining users and decide if we need to unlicense or delete them //============================================================================================ _statusLogs.AddStatusHeader("Process unexpected users list"); foreach (var unexpectedUser in workingListUnexaminedUsers.GetUsers()) { try { Execute_UpdateUnexpectedUsersProvisioning_SingleUser(unexpectedUser, siteSignIn, workingList_allKnownUsers); } catch (Exception exUnxpectedUsers) { _statusLogs.AddError("Error processing unexpected user " + unexpectedUser.ToString() + ", " + exUnxpectedUsers.Message); CSVRecord_ErrorUpdatingUser(unexpectedUser.Name, unexpectedUser.SiteRole, unexpectedUser.SiteAuthentication, "Error processing unexpected user " + unexpectedUser.ToString() + ", " + exUnxpectedUsers.Message); } } return(workingList_allKnownUsers); }
/// <summary> /// Return the set of users in the site /// </summary> /// <param name="signIn"></param> /// <returns></returns> public static DownloadUsersList CreateAndExecute(TableauServerSignIn signIn) { var retObj = new DownloadUsersList(signIn); retObj.ExecuteRequest(); return(retObj); }
/// <summary> /// Provision the groups /// </summary> /// <param name="siteSignIn"></param> private void Execute_ProvisionGroups(TableauServerSignIn siteSignIn, WorkingListSiteUsers workingList_allKnownUsers) { //If there are no provisioning instructions, then there is nothing we need to do here if (_provisionInstructions.GroupsToProvision == null) { _statusLogs.AddStatus("Skipping groups provisioning, because there are no instructions for this..."); return; } _statusLogs.AddStatusHeader("Provision the specified groups in site"); //================================================================================= //If we do not have it already, load the set of users for the site...we will need this to look up users //================================================================================= if (workingList_allKnownUsers == null) { var existingUsers = DownloadUsersList.CreateAndExecute(siteSignIn); existingUsers.ExecuteRequest(); workingList_allKnownUsers = new WorkingListSiteUsers(existingUsers.Users); } //================================================================================= //Download the groups //================================================================================= var downloadGroups = new DownloadGroupsList(siteSignIn); downloadGroups.ExecuteRequest(false); //Download the list of groups, but not the membership of the groups (we will only do that for the groups we care about) //Go through each of the groups... foreach (var thisProvisionGroup in _provisionInstructions.GroupsToProvision) { Execute_ProvisionGroups_SingleGroup(siteSignIn, thisProvisionGroup, downloadGroups, workingList_allKnownUsers); } }
/// <summary> /// Download the users in the site /// </summary> /// <param name="onlineLogin"></param> /// <returns></returns> private IEnumerable <SiteUser> Execute_DownloadSiteUsers(TableauServerSignIn onlineLogin) { var onlineUrls = _onlineUrls; _statusLog.AddStatusHeader("Request site users"); DownloadUsersList users = null; //=================================================================================== //Users... //=================================================================================== try { users = new DownloadUsersList(onlineUrls, onlineLogin); users.ExecuteRequest(); //List all the users foreach (var singleUser in users.Users) { _statusLog.AddStatus("user: "******"/" + singleUser.SiteRole + "/" + singleUser.Id.ToString()); } return(users.Users); } catch (Exception exUsersQuery) { _statusLog.AddError("Error during users query, " + exUsersQuery.ToString()); return(null); } }
/// <summary> /// Follows the instructions for any content ownership changes we want to make /// </summary> /// <param name="siteSignIn"></param> /// <param name="workingList_allKnownUsers"></param> private void Execute_ProvisionContentOwnershipChanges(TableauServerSignIn siteSignIn, WorkingListSiteUsers workingList_allKnownUsers) { //=============================================================================================== //If there are no content ownership changes to perform, then just exit this step //=============================================================================================== if ((_provisionInstructions.ContentOwnershipToProvision == null) || (_provisionInstructions.ContentOwnershipToProvision.Count == 0)) { _statusLogs.AddStatus("Skipping content ownership mapping, because there are no instructions for this..."); return; } _statusLogs.AddStatusHeader("Provision content ownership changes"); //================================================================================= //If we do not have it already, load the set of users for the site...we will need this to look up users //================================================================================= if (workingList_allKnownUsers == null) { var existingUsers = DownloadUsersList.CreateAndExecute(siteSignIn); existingUsers.ExecuteRequest(); workingList_allKnownUsers = new WorkingListSiteUsers(existingUsers.Users); } //================================================================================= //Unlike Workbooks, there is not a simple URL to download the list of Datasources //for a single user. (There is a filter parameter that can be added to the REST //API that queries data sources, but that takes a "user name" not a User ID). //Here we can query for ALL the data sources pass that to all teh remappign request //================================================================================= var downloadDataSources = new DownloadDatasourcesList(siteSignIn); downloadDataSources.ExecuteRequest(); var allSiteDataSources = downloadDataSources.Datasources; //================================================================================= //Get the list of flows in the iste //================================================================================= var downloadFlows = new DownloadFlowsList(siteSignIn); downloadFlows.ExecuteRequest(); var allSiteFlows = downloadFlows.Flows; //Go through each of the ownership change instructions... foreach (var thisOwnershipChange in _provisionInstructions.ContentOwnershipToProvision) { Execute_ProvisionOwnership_SingleUserChange( siteSignIn, thisOwnershipChange, workingList_allKnownUsers, allSiteDataSources, allSiteFlows); } }
/// <summary> /// Provision the groups /// </summary> /// <param name="siteSignIn"></param> private void Execute_ProvisionGroups(TableauServerSignIn siteSignIn) { _statusLogs.AddStatusHeader("Provision the specified groups in site"); //================================================================================= //Load the set of users for the site...we will need this to look up users //================================================================================= var existingUsers = DownloadUsersList.CreateAndExecute(siteSignIn); existingUsers.ExecuteRequest(); //================================================================================= //Download the groups //================================================================================= var downloadGroups = new DownloadGroupsList(siteSignIn); downloadGroups.ExecuteRequest(false); //Download the list of groups, but not the membership of the groups (we will only do that for the groups we care about) //Go through each of the groups... foreach (var thisProvisionGroup in _provisionInstructions.GroupsToProvision) { Execute_ProvisionGroups_SingleGroup(siteSignIn, thisProvisionGroup, downloadGroups, existingUsers); } }
/// <summary> /// Download the ist of users/roles /// </summary> /// <param name="siteSignIn"></param> private void GenerateUsersRolesList_FromTableauSite(TableauServerSignIn siteSignIn) { var downloadUsers = new DownloadUsersList(siteSignIn); bool downloadSuccess = downloadUsers.ExecuteRequest(); if (!downloadSuccess) { throw new Exception("1012-358: Fatal error attempting to download users"); } var userRolesManager = this.SetManagerForRoles; foreach (var thisUser in downloadUsers.Users) { var thisProvisioningUser = new ProvisioningUser( thisUser.Name, thisUser.SiteRole, thisUser.SiteAuthentication, "Tableau Online site list", false); userRolesManager.AddUser(thisProvisioningUser); } }
/// <summary> /// Download the users in the site /// </summary> /// <param name="onlineLogin"></param> /// <returns></returns> private IEnumerable<SiteUser> Execute_DownloadSiteUsers(TableauServerSignIn onlineLogin) { var onlineUrls = _onlineUrls; _statusLog.AddStatusHeader("Request site users"); DownloadUsersList users = null; //=================================================================================== //Users... //=================================================================================== try { users = new DownloadUsersList(onlineUrls, onlineLogin); users.ExecuteRequest(); //List all the users foreach(var singleUser in users.Users) { _statusLog.AddStatus("user: "******"/" + singleUser.SiteRole + "/" + singleUser.Id.ToString()); } return users.Users; } catch (Exception exUsersQuery) { _statusLog.AddError("Error during users query, " + exUsersQuery.ToString()); return null; } }
/// <summary> /// Provisioning for a single group /// </summary> /// <param name="siteSignIn"></param> /// <param name="thisProvisionGroup"></param> /// <param name="existingGroups"></param> private void Execute_ProvisionGroups_SingleGroup( TableauServerSignIn siteSignIn, ProvisioningGroup thisProvisionGroup, DownloadGroupsList existingGroups, DownloadUsersList siteUsersList) { _statusLogs.AddStatusHeader("Provision the group: " + thisProvisionGroup.GroupName); var thisExistingGroup = existingGroups.FindGroupWithName(thisProvisionGroup.GroupName); ICollection <SiteUser> existingUsersInGroup = new List <SiteUser>(); //If the Group does not exist on server then create it if (thisExistingGroup == null) { var createGroup = new SendCreateGroup(siteSignIn, thisProvisionGroup.GroupName); thisExistingGroup = createGroup.ExecuteRequest(); CSVRecord_GroupModified(thisExistingGroup.Name, "created group", ""); _statusLogs.AddStatus("Created group: " + thisExistingGroup.Name); } else { //Download the members of the group var downloadGroupMembers = new DownloadUsersListInGroup(siteSignIn, thisExistingGroup.Id); downloadGroupMembers.ExecuteRequest(); existingUsersInGroup = downloadGroupMembers.Users; } //==================================================================================== //Keep a list of the remaining users in the Server Site's group //==================================================================================== var workingListUnexaminedUsers = new WorkingListSiteUsers(existingUsersInGroup); //==================================================================================== //Go through each of the users we need to provision, and see if they are in the group //already //==================================================================================== foreach (var provisionThisUser in thisProvisionGroup.Members) { var userInGroup = workingListUnexaminedUsers.FindUser(provisionThisUser); if (userInGroup != null) { //The user is already in the group, no need to add them workingListUnexaminedUsers.RemoveUser(userInGroup); } else { //Add the user to the group try { Execute_ProvisionGroups_SingleGroup_AddUser(siteSignIn, provisionThisUser, thisExistingGroup, siteUsersList); } catch (Exception exAddUserToGroup) //Unexpected error case { IwsDiagnostics.Assert(false, "811-700: Internal error adding user to group: " + exAddUserToGroup.Message); _statusLogs.AddError("811-700: Internal error adding user to group: " + exAddUserToGroup.Message); } } } //============================================================================== //Remove any remaining users that are in the Server Site's Group but not in //our provisioning list //============================================================================== foreach (var unexpectedUser in workingListUnexaminedUsers.GetUsers()) { try { Execute_ProvisionGroups_RemoveSingleUser(siteSignIn, unexpectedUser, thisExistingGroup); } catch (Exception exUnxpectedUsers) { _statusLogs.AddError("Error removing unexpected user in GROUP " + unexpectedUser.ToString() + ", " + exUnxpectedUsers.Message); CSVRecord_Error(unexpectedUser.Name, unexpectedUser.SiteRole, unexpectedUser.SiteAuthentication, "Error removing unexpected user in GROUP" + unexpectedUser.ToString() + ", " + exUnxpectedUsers.Message); } } }
/// <summary> /// Add a specific user into the Server Site's group /// </summary> /// <param name="userEmail"></param> /// <param name="thisProvisionGroup"></param> /// <param name="siteUsersList"></param> private void Execute_ProvisionGroups_SingleGroup_AddUser(TableauServerSignIn siteSignIn, string userEmail, SiteGroup siteGroup, DownloadUsersList siteUsersList) { var siteUserToAddToGroup = siteUsersList.FindUserByEmail(userEmail); //Sanity test. If the user is not a member of the site, they cannot be added to a group if (siteUserToAddToGroup == null) { CSVRecord_Error(userEmail, "", "", "User not on site. Cannot be added to group"); _statusLogs.AddError("User not on site. Cannot be added to group, " + userEmail); return; //FAILED } switch (_provisionInstructions.ActionForMissingGroupMembers) { case ProvisionUserInstructions.MissingGroupMemberAction.Add: //Call the server and add the user var addUserToGroup = new SendAddUserToGroup(siteSignIn, siteUserToAddToGroup.Id, siteGroup.Id); bool userGroupAddSuccess = addUserToGroup.ExecuteRequest(); if (userGroupAddSuccess) { //SUCCESS CSVRecord_GroupModified_WithUser(siteGroup.Name, "added member", siteUserToAddToGroup.Name, ""); _statusLogs.AddStatus("Group membership: Added " + siteUserToAddToGroup.Name + " to group " + siteGroup.Name); } else { CSVRecord_Error(userEmail, "", "", "User could not be added to group " + siteGroup.Name); _statusLogs.AddError("Group membership error: Failed to add " + siteUserToAddToGroup.Name + " to group " + siteGroup.Name); } return; case ProvisionUserInstructions.MissingGroupMemberAction.Report: //We are instructed to NOT REALLY add the user, jsut report CSVRecord_GroupModified_WithUser(siteGroup.Name, "SIMULATED added member", siteUserToAddToGroup.Name, ""); return; default: IwsDiagnostics.Assert(false, "814-433: Unknown action"); throw new Exception("814-433: Unknown action"); } }