Пример #1
0
        public async Task ComputeGroups(PerformContext performContext)
        {
            Logger.Info(performContext, "Getting the groups from the Portal...");
            var remoteGroups = PortalService.GetAllGroups().ToArray();

            Logger.Info(performContext, "Getting the groups from Active Directory...");
            var adGroups = _activeDirectoryManager.GetAllGroups().ToArray();

            foreach (var adGroup in adGroups)
            {
                performContext?.Cancel();

                var remoteGroup = remoteGroups.FirstOrDefault(rg => rg.Id == adGroup.Id);
                if (remoteGroup == null)
                {
                    var newGroup = LicenseGroup.Create(
                        adGroup,
                        AuthService.GetAccount());

                    await PortalService.AddGroupAsync(newGroup);

                    performContext?.WriteSuccessLine($"+ {newGroup}");
                    Logger.Info($"Created: {newGroup}");
                    Logger.Debug($"{JsonConvert.SerializeObject(newGroup, Formatting.Indented)}");

                    continue;
                }

                remoteGroup.UpdateValues(adGroup);
                await PortalService.UpdateGroupAsync(remoteGroup);

                performContext?.WriteSuccessLine($"^ {remoteGroup}");
                Logger.Info($"Updated:  {remoteGroup}");
                Logger.Debug($"{JsonConvert.SerializeObject(remoteGroup, Formatting.Indented)}");
            }

            var staleGroups = remoteGroups.Except(adGroups, _licenseGroupEqualityComparer).ToArray();

            foreach (var staleGroup in staleGroups)
            {
                performContext?.Cancel();

                if (staleGroup.IsDeleted)
                {
                    continue;
                }

                await PortalService.DeleteGroupAsync(staleGroup);

                performContext?.WriteWarnLine($"- {staleGroup}");
                Logger.Info($"Delete: {staleGroup}");
                Logger.Debug($"{JsonConvert.SerializeObject(staleGroup, Formatting.Indented)}");
            }
        }