Ejemplo n.º 1
0
        public async Task SetSingleVariableAsync(ScopedVariableJsonModel variable)
        {
            if (string.IsNullOrEmpty(variable.Server) && string.IsNullOrEmpty(variable.ServerRole) && string.IsNullOrEmpty(variable.Environment))
            {
                throw new InvalidOperationException("Specified variable requires at least one scope (server, role, or environment).");
            }

            using (var client = this.CreateClient())
            {
                string url = "api/variables/scoped/single";
                this.LogRequest(url);
                using (var stream = new SlimMemoryStream())
                    using (var writer = new StreamWriter(stream))
                    {
                        JsonSerializer.CreateDefault().Serialize(writer, variable);
                        await writer.FlushAsync().ConfigureAwait(false);

                        stream.Position = 0;

                        using (var content = new StreamContent(stream))
                            using (var response = await client.PostAsync(url, content).ConfigureAwait(false))
                            {
                                await HandleError(response).ConfigureAwait(false);
                            }
                    }
            }
        }
        public async override Task ExecuteAsync(IOperationExecutionContext context)
        {
            this.LogInformation($"Setting Otter variable {this.Name}...");

            var variable = new ScopedVariableJsonModel
            {
                Name        = this.Name,
                Value       = this.Value,
                Environment = this.Environment,
                Server      = this.Server,
                ServerRole  = this.Role,
                Sensitive   = this.Sensitive
            };

            var client = OtterClient.Create(this.Host, this.ApiKey, this, context.CancellationToken);

            try
            {
                if (string.IsNullOrEmpty(variable.Server) && string.IsNullOrEmpty(variable.ServerRole) && string.IsNullOrEmpty(variable.Environment))
                {
                    this.LogDebug($"Setting Otter global variable '{variable.Name}' = '{variable.Value}'...");
                    await client.SetGlobalVariableAsync(variable.Name, variable.Value).ConfigureAwait(false);
                }
                else
                {
                    this.LogDebug($"Setting Otter scoped variable '{variable.Name}' = '{variable.Value}'...");
                    await client.SetSingleVariableAsync(variable).ConfigureAwait(false);
                }

                this.LogInformation("Otter variable value set.");
            }
            catch (OtterException ex)
            {
                this.LogError(ex.FullMessage);
            }
        }