示例#1
0
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var projectJson = JObject.ReadFrom(reader) as JObject;

        foreach (var nameToken in projectJson.SelectTokens("$..name").Reverse().ToArray())
        {
            // (nameToken.Parent.Parent as JObject)?.SetProperty("title", nameToken.ToString());
            (nameToken.Parent.Parent as JObject)?.SetProperty("displayName", nameToken.ToString());
            nameToken.Parent.Remove(); // get rid of the name token as we don't need it anymore
        }

        var inputJsonSchemaToken = GenerateInputJsonSchema(projectJson, serializer);

        projectJson.SetProperty(nameof(ComponentTemplate.InputJsonSchema), inputJsonSchemaToken);

        return(TeamCloudSerialize.MergeObject <ProjectTemplate>(projectJson.ToString(), projectTemplate));
    }
    public override async Task <ICommandResult> HandleAsync(OrganizationDeployCommand command, IAsyncCollector <ICommand> commandQueue, IDurableOrchestrationContext orchestrationContext, ILogger log)
    {
        if (command is null)
        {
            throw new ArgumentNullException(nameof(command));
        }

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

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

        var commandResult = command.CreateResult();

        using (await orchestrationContext.LockContainerDocumentAsync(command.Payload).ConfigureAwait(true))
        {
            // just to make sure we are dealing with the latest version
            // of the organization entity, we re-fetch the entity and
            // use the passed in one as a potential fallback.

            commandResult.Result = (await orchestrationContext
                                    .CallActivityWithRetryAsync <Organization>(nameof(OrganizationGetActivity), new OrganizationGetActivity.Input()
            {
                Id = command.Payload.Id
            })
                                    .ConfigureAwait(true)) ?? command.Payload;

            try
            {
                commandResult.Result = await orchestrationContext
                                       .CallActivityWithRetryAsync <Organization>(nameof(OrganizationSetActivity), new OrganizationSetActivity.Input()
                {
                    Organization = commandResult.Result, ResourceState = ResourceState.Initializing
                })
                                       .ConfigureAwait(true);

                var deploymentOutputEventName = orchestrationContext.NewGuid().ToString();

                _ = await orchestrationContext
                    .StartDeploymentAsync(nameof(OrganizationDeployActivity), new OrganizationDeployActivity.Input()
                {
                    Organization = commandResult.Result
                }, deploymentOutputEventName)
                    .ConfigureAwait(true);

                commandResult.Result = await orchestrationContext
                                       .CallActivityWithRetryAsync <Organization>(nameof(OrganizationSetActivity), new OrganizationSetActivity.Input()
                {
                    Organization = commandResult.Result, ResourceState = ResourceState.Provisioning
                })
                                       .ConfigureAwait(true);

                var deploymentOutput = await orchestrationContext
                                       .WaitForDeploymentOutput(deploymentOutputEventName, TimeSpan.FromMinutes(5))
                                       .ConfigureAwait(true);

                if (deploymentOutput.TryGetValue("organizationData", out var organizationData) && organizationData is JObject organizationDataJson)
                {
                    commandResult.Result = TeamCloudSerialize.MergeObject(organizationDataJson.ToString(), commandResult.Result);

                    commandResult.Result = await orchestrationContext
                                           .CallActivityWithRetryAsync <Organization>(nameof(OrganizationSetActivity), new OrganizationSetActivity.Input()
                    {
                        Organization = commandResult.Result, ResourceState = ResourceState.Provisioned
                    })
                                           .ConfigureAwait(true);
                }
                else
                {
                    throw new NullReferenceException($"Deployment output doesn't contain 'organizationData' output.");
                }
            }
            catch
            {
                commandResult.Result = await orchestrationContext
                                       .CallActivityWithRetryAsync <Organization>(nameof(OrganizationSetActivity), new OrganizationSetActivity.Input()
                {
                    Organization = commandResult.Result, ResourceState = ResourceState.Failed
                })
                                       .ConfigureAwait(true);

                throw;
            }
        }

        return(commandResult);
    }