public static async Task RunOrchestration(
            [OrchestrationTrigger] IDurableOrchestrationContext functionContext,
            ILogger log)
        {
            if (functionContext is null)
            {
                throw new ArgumentNullException(nameof(functionContext));
            }

            if (log is null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            var command       = functionContext.GetInput <OrchestratorTeamCloudUserCreateCommand>();
            var commandResult = command.CreateResult();
            var user          = command.Payload;

            using (log.BeginCommandScope(command))
            {
                try
                {
                    functionContext.SetCustomStatus($"Creating user.", log);

                    using (await functionContext.LockAsync <TeamCloudInstance>(TeamCloudInstance.DefaultId).ConfigureAwait(true))
                    {
                        var teamCloud = await functionContext
                                        .GetTeamCloudAsync()
                                        .ConfigureAwait(true);

                        if (teamCloud.Users.Any(u => u.Id == user.Id))
                        {
                            throw new OrchestratorCommandException($"User '{user.Id}' already exists.", command);
                        }

                        teamCloud.Users.Add(user);

                        teamCloud = await functionContext
                                    .SetTeamCloudAsync(teamCloud)
                                    .ConfigureAwait(true);
                    }

                    var projects = await functionContext
                                   .ListProjectsAsync()
                                   .ConfigureAwait(true);

                    foreach (var project in projects)
                    {
                        var projectUpdateCommand = new OrchestratorProjectUpdateCommand(command.User, project);

                        functionContext.StartNewOrchestration(nameof(OrchestratorProjectUpdateCommand), projectUpdateCommand);
                    }

                    commandResult.Result = user;

                    functionContext.SetCustomStatus($"User created.", log);
                }
                catch (Exception ex)
                {
                    functionContext.SetCustomStatus("Failed to create user.", log, ex);

                    commandResult ??= command.CreateResult();
                    commandResult.Errors.Add(ex);

                    throw;
                }
                finally
                {
                    functionContext.SetOutput(commandResult);
                }
            }
        }
コード例 #2
0
        public static async Task RunOrchestration(
            [OrchestrationTrigger] IDurableOrchestrationContext functionContext,
            ILogger log)
        {
            if (functionContext is null)
            {
                throw new ArgumentNullException(nameof(functionContext));
            }

            if (log is null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            var command       = functionContext.GetInput <OrchestratorTeamCloudUserDeleteCommand>();
            var commandResult = command.CreateResult();
            var user          = command.Payload;

            using (log.BeginCommandScope(command))
            {
                try
                {
                    functionContext.SetCustomStatus($"Deleting user.", log);

                    using (await functionContext.LockAsync <UserDocument>(user.Id.ToString()).ConfigureAwait(true))
                    {
                        await functionContext
                        .DeleteUserAsync(user.Id)
                        .ConfigureAwait(true);
                    }

                    var projects = default(IEnumerable <ProjectDocument>);

                    // TODO: this is totally wrong
                    // only update all projects if user was an admin
                    if (user.IsAdmin())
                    {
                        projects = await functionContext
                                   .ListProjectsAsync()
                                   .ConfigureAwait(true);
                    }
                    else if (user.ProjectMemberships.Any())
                    {
                        projects = await functionContext
                                   .ListProjectsAsync(user.ProjectMemberships.Select(m => m.ProjectId).ToList())
                                   .ConfigureAwait(true);
                    }

                    if (projects?.Any() ?? false)
                    {
                        foreach (var project in projects)
                        {
                            var projectUpdateCommand = new OrchestratorProjectUpdateCommand(command.BaseApi, command.User, project);

                            functionContext.StartNewOrchestration(nameof(OrchestratorProjectUpdateCommandOrchestration), projectUpdateCommand);
                        }
                    }

                    commandResult.Result = user;

                    functionContext.SetCustomStatus($"User deleted.", log);
                }
                catch (Exception ex)
                {
                    functionContext.SetCustomStatus("Failed to delete user.", log, ex);

                    commandResult ??= command.CreateResult();
                    commandResult.Errors.Add(ex);

                    throw;
                }
                finally
                {
                    functionContext.SetOutput(commandResult);
                }
            }
        }
コード例 #3
0
        public static async Task RunOrchestration(
            [OrchestrationTrigger] IDurableOrchestrationContext functionContext,
            ILogger log)
        {
            if (functionContext is null)
            {
                throw new ArgumentNullException(nameof(functionContext));
            }

            if (log is null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            var command       = functionContext.GetInput <OrchestratorTeamCloudUserUpdateCommand>();
            var commandResult = command.CreateResult();
            var user          = command.Payload;

            using (log.BeginCommandScope(command))
            {
                try
                {
                    functionContext.SetCustomStatus($"Updating user.", log);

                    var existingUser = default(UserDocument);

                    using (await functionContext.LockAsync <UserDocument>(user.Id.ToString()).ConfigureAwait(true))
                    {
                        existingUser = await functionContext
                                       .GetUserAsync(user.Id)
                                       .ConfigureAwait(true);

                        if (existingUser is null)
                        {
                            throw new OrchestratorCommandException($"User '{user.Id}' does not exist.", command);
                        }

                        if (user.HasEqualTeamCloudInfo(existingUser))
                        {
                            throw new OrchestratorCommandException($"User '{user.Id}' TeamCloud details have not changed.", command);
                        }

                        if (!user.HasEqualMemberships(existingUser))
                        {
                            throw new OrchestratorCommandException($"User '{user.Id}' Project Memberships cannot be changed using the TeamCloudUserUpdateCommand. Project Memebership details must be changed using the ProjectUserUpdateCommand.", command);
                        }

                        user = await functionContext
                               .SetUserTeamCloudInfoAsync(user)
                               .ConfigureAwait(true);
                    }

                    var projects = default(IEnumerable <ProjectDocument>);

                    // only update all projects if the updated user is an admin
                    // or the user was an admin before the update, otherwise
                    // only update member projects if user's teamcloud level properties changed
                    if (user.IsAdmin() || existingUser.IsAdmin())
                    {
                        projects = await functionContext
                                   .ListProjectsAsync()
                                   .ConfigureAwait(true);
                    }
                    else if (user.ProjectMemberships.Any() && !user.Properties.SequenceEqual(existingUser.Properties))
                    {
                        projects = await functionContext
                                   .ListProjectsAsync(user.ProjectMemberships.Select(m => m.ProjectId).ToList())
                                   .ConfigureAwait(true);
                    }

                    if (projects?.Any() ?? false)
                    {
                        foreach (var project in projects)
                        {
                            var projectUpdateCommand = new OrchestratorProjectUpdateCommand(command.BaseApi, command.User, project);

                            functionContext.StartNewOrchestration(nameof(OrchestratorProjectUpdateCommandOrchestration), projectUpdateCommand);
                        }
                    }

                    commandResult.Result = user;

                    functionContext.SetCustomStatus($"User updated.", log);
                }
                catch (Exception ex)
                {
                    functionContext.SetCustomStatus("Failed to update user.", log, ex);

                    commandResult ??= command.CreateResult();
                    commandResult.Errors.Add(ex);

                    throw;
                }
                finally
                {
                    functionContext.SetOutput(commandResult);
                }
            }
        }
        public static async Task RunOrchestration(
            [OrchestrationTrigger] IDurableOrchestrationContext orchestrationContext,
            ILogger log)
        {
            if (orchestrationContext is null)
            {
                throw new ArgumentNullException(nameof(orchestrationContext));
            }

            if (log is null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            var command       = orchestrationContext.GetInput <OrchestratorTeamCloudUserDeleteCommand>();
            var commandResult = command.CreateResult();
            var user          = command.Payload;

            using (log.BeginCommandScope(command))
            {
                try
                {
                    orchestrationContext.SetCustomStatus($"Deleting user.", log);

                    using (await orchestrationContext.LockAsync <UserDocument>(user.Id.ToString()).ConfigureAwait(true))
                    {
                        await orchestrationContext
                        .DeleteUserAsync(user.Id)
                        .ConfigureAwait(true);
                    }

                    var projects = default(IEnumerable <ProjectDocument>);

                    // TODO: this is totally wrong
                    // only update all projects if user was an admin
                    if (user.IsAdmin())
                    {
                        projects = await orchestrationContext
                                   .ListProjectsAsync()
                                   .ConfigureAwait(true);
                    }
                    else if (user.ProjectMemberships.Any())
                    {
                        projects = await orchestrationContext
                                   .ListProjectsAsync(user.ProjectMemberships.Select(m => m.ProjectId).ToList())
                                   .ConfigureAwait(true);
                    }

                    if (projects?.Any() ?? false)
                    {
                        foreach (var project in projects)
                        {
                            var projectUpdateCommand = new OrchestratorProjectUpdateCommand(command.User, project);

                            orchestrationContext.StartNewOrchestration(nameof(OrchestratorProjectUpdateCommandOrchestration), projectUpdateCommand);
                        }
                    }

                    var providerCommand = new ProviderTeamCloudUserDeleteCommand
                                          (
                        command.User.PopulateExternalModel(),
                        command.Payload.PopulateExternalModel(),
                        command.CommandId
                                          );

                    var providerResult = await orchestrationContext
                                         .SendProviderCommandAsync <ProviderTeamCloudUserDeleteCommand, ProviderTeamCloudUserDeleteCommandResult>(providerCommand)
                                         .ConfigureAwait(true);

                    providerResult.Errors.ToList().ForEach(e => commandResult.Errors.Add(e));

                    commandResult.Result = user;

                    orchestrationContext.SetCustomStatus($"User deleted.", log);
                }
                catch (Exception ex)
                {
                    commandResult ??= command.CreateResult();
                    commandResult.Errors.Add(ex);
                }
                finally
                {
                    var commandException = commandResult.Errors?.ToException();

                    if (commandException is null)
                    {
                        orchestrationContext.SetCustomStatus($"Command succeeded", log);
                    }
                    else
                    {
                        orchestrationContext.SetCustomStatus($"Command failed: {commandException.Message}", log, commandException);
                    }

                    orchestrationContext.SetOutput(commandResult);
                }
            }
        }
コード例 #5
0
        public static async Task RunOrchestration(
            [OrchestrationTrigger] IDurableOrchestrationContext functionContext,
            ILogger log)
        {
            if (functionContext is null)
            {
                throw new ArgumentNullException(nameof(functionContext));
            }

            if (log is null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            var command       = functionContext.GetInput <OrchestratorTeamCloudUserCreateCommand>();
            var commandResult = command.CreateResult();
            var user          = command.Payload;

            using (log.BeginCommandScope(command))
            {
                try
                {
                    functionContext.SetCustomStatus($"Creating user.", log);

                    using (await functionContext.LockContainerDocumentAsync(user).ConfigureAwait(true))
                    {
                        var existingUser = await functionContext
                                           .GetUserAsync(user.Id)
                                           .ConfigureAwait(true);

                        if (existingUser != null)
                        {
                            throw new OrchestratorCommandException($"User '{user.Id}' already exist.", command);
                        }

                        user = await functionContext
                               .SetUserTeamCloudInfoAsync(user)
                               .ConfigureAwait(true);
                    }

                    // this will only be called on a newly created user with teamcloud (system)
                    // properties. providers only care if the user is an admin, so we check if
                    // the user is an admin, and if so, we send the providers teamcloud user created
                    // commands (or project update connamds depending on the provider's mode)

                    if (user.IsAdmin())
                    {
                        var projects = await functionContext
                                       .ListProjectsAsync()
                                       .ConfigureAwait(true);

                        // TODO: change this
                        foreach (var project in projects)
                        {
                            var projectUpdateCommand = new OrchestratorProjectUpdateCommand(command.User, project);

                            functionContext.StartNewOrchestration(nameof(OrchestratorProjectUpdateCommandOrchestration), projectUpdateCommand);
                        }
                    }

                    commandResult.Result = user;

                    functionContext.SetCustomStatus($"User created.", log);
                }
                catch (Exception ex)
                {
                    functionContext.SetCustomStatus("Failed to create user.", log, ex);

                    commandResult ??= command.CreateResult();
                    commandResult.Errors.Add(ex);

                    throw;
                }
                finally
                {
                    functionContext.SetOutput(commandResult);
                }
            }
        }
        public static async Task RunOrchestration(
            [OrchestrationTrigger] IDurableOrchestrationContext functionContext,
            ILogger log)
        {
            if (functionContext is null)
            {
                throw new ArgumentNullException(nameof(functionContext));
            }

            var orchestratorCommand = functionContext.GetInput <OrchestratorCommandMessage>();

            var command       = (OrchestratorTeamCloudUserUpdateCommand)orchestratorCommand.Command;
            var commandResult = command.CreateResult();

            var user = command.Payload;

            try
            {
                functionContext.SetCustomStatus($"Updating user.", log);

                var teamCloud = await functionContext
                                .GetTeamCloudAsync()
                                .ConfigureAwait(true);

                using (await functionContext.LockAsync(teamCloud).ConfigureAwait(true))
                {
                    teamCloud = await functionContext
                                .GetTeamCloudAsync()
                                .ConfigureAwait(true);

                    var userDelete = teamCloud.Users.SingleOrDefault(u => u.Id == user.Id);

                    if (userDelete is null)
                    {
                        throw new OrchestratorCommandException($"User '{user.Id}' does not exist.", command);
                    }
                    else
                    {
                        teamCloud.Users.Remove(userDelete);
                    }

                    teamCloud.Users.Add(user);

                    teamCloud = await functionContext
                                .SetTeamCloudAsync(teamCloud)
                                .ConfigureAwait(true);
                }

                var projects = await functionContext
                               .ListProjectsAsync()
                               .ConfigureAwait(true);

                foreach (var project in projects)
                {
                    var projectUpdateCommand = new OrchestratorProjectUpdateCommand(command.User, project);

                    functionContext.StartNewOrchestration(nameof(OrchestratorProjectUpdateCommand), new OrchestratorCommandMessage(projectUpdateCommand));
                }
                commandResult.Result = user;

                functionContext.SetCustomStatus($"User updated.", log);
            }
            catch (Exception ex)
            {
                functionContext.SetCustomStatus("Failed to update user.", log, ex);

                commandResult.Errors.Add(ex);

                throw;
            }
            finally
            {
                functionContext.SetOutput(commandResult);
            }
        }