示例#1
0
        public async Task <ICommandResult> HandleAsync(OrchestratorComponentCreateCommand orchestratorCommand, IDurableClient durableClient = null)
        {
            if (orchestratorCommand is null)
            {
                throw new ArgumentNullException(nameof(orchestratorCommand));
            }

            var commandResult = orchestratorCommand.CreateResult();

            try
            {
                commandResult.Result = await componentRepository
                                       .AddAsync(orchestratorCommand.Payload)
                                       .ConfigureAwait(false);

                commandResult.RuntimeStatus = CommandRuntimeStatus.Completed;
            }
            catch (Exception exc)
            {
                commandResult.Errors.Add(exc);
            }

            return(commandResult);
        }
    public override async Task <ICommandResult> HandleAsync(ComponentCreateCommand command, IAsyncCollector <ICommand> commandQueue, IDurableOrchestrationContext orchestrationContext, ILogger log)
    {
        if (command is null)
        {
            throw new ArgumentNullException(nameof(command));
        }

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

        var commandResult = command.CreateResult();

        try
        {
            commandResult.Result = await componentRepository
                                   .AddAsync(command.Payload)
                                   .ConfigureAwait(false);

            var deploymentScope = await deploymentScopeRepository
                                  .GetAsync(commandResult.Result.Organization, commandResult.Result.DeploymentScopeId)
                                  .ConfigureAwait(false);

            if (adapterProvider.GetAdapter(deploymentScope.Type) is IAdapterIdentity adapterIdentity)
            {
                var servicePrincipal = await adapterIdentity
                                       .GetServiceIdentityAsync(commandResult.Result)
                                       .ConfigureAwait(false);

                var servicePrincipalUser = await userRepository
                                           .GetAsync(commandResult.Result.Organization, servicePrincipal.Id.ToString())
                                           .ConfigureAwait(false);

                if (servicePrincipalUser is null)
                {
                    servicePrincipalUser ??= new User
                    {
                        Id               = servicePrincipal.Id.ToString(),
                        Role             = OrganizationUserRole.Adapter,
                        UserType         = Model.Data.UserType.Service,
                        Organization     = commandResult.Result.Organization,
                        OrganizationName = commandResult.Result.OrganizationName
                    };

                    servicePrincipalUser.EnsureProjectMembership(commandResult.Result.ProjectId, ProjectUserRole.Adapter);

                    await commandQueue
                    .AddAsync(new ProjectUserCreateCommand(command.User, servicePrincipalUser, commandResult.Result.ProjectId))
                    .ConfigureAwait(false);
                }
            }

            var componentTask = new ComponentTask
            {
                Organization     = commandResult.Result.Organization,
                OrganizationName = commandResult.Result.OrganizationName,
                ComponentId      = commandResult.Result.Id,
                ComponentName    = commandResult.Result.Slug,
                ProjectId        = commandResult.Result.ProjectId,
                ProjectName      = commandResult.Result.ProjectName,
                Type             = ComponentTaskType.Create,
                RequestedBy      = commandResult.Result.Creator,
                InputJson        = commandResult.Result.InputJson
            };

            await commandQueue
            .AddAsync(new ComponentTaskCreateCommand(command.User, componentTask))
            .ConfigureAwait(false);

            commandResult.RuntimeStatus = CommandRuntimeStatus.Completed;
        }
        catch (Exception exc)
        {
            commandResult.Errors.Add(exc);
        }

        return(commandResult);
    }