diff --git a/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs b/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs new file mode 100644 index 00000000..393c9bb7 --- /dev/null +++ b/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs @@ -0,0 +1,52 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Newtonsoft.Json; +using SeqCli.Cli.Features; +using SeqCli.Connection; +using Serilog; + +namespace SeqCli.Cli.Commands.ApiKey +{ + [Command("apikey", "list", "List of API Keys", Example = + "seqcli apikey list")] + class ListCommand : Command + { + private readonly SeqConnectionFactory _connectionFactory; + private readonly ConnectionFeature _connection; + + public ListCommand(SeqConnectionFactory connectionFactory) + { + _connectionFactory = connectionFactory; + _connection = Enable(); + } + + protected override async Task Run() + { + var connection = _connectionFactory.Connect(_connection); + + var apiKeys = await connection.ApiKeys.ListAsync(); + Log.Debug("Retrieved ApiKeys {@ApiKeys}", apiKeys); + var data = apiKeys.Select(a => new + { + a.Title, + a.Id, + a.Token, + a.MinimumLevel, + a.AppliedProperties, + a.CanActAsPrincipal, + a.InputFilter, + a.UseServerTimestamps, + a.IsDefault + }); + + foreach (var apiKey in data) + { + var apiKeyString = JsonConvert.SerializeObject(apiKey); + + Console.WriteLine(apiKeyString); + } + return 0; + } + } +} diff --git a/src/SeqCli/Cli/Commands/ApiKey/RemoveCommand.cs b/src/SeqCli/Cli/Commands/ApiKey/RemoveCommand.cs new file mode 100644 index 00000000..84b43b2f --- /dev/null +++ b/src/SeqCli/Cli/Commands/ApiKey/RemoveCommand.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Seq.Api.Model.Inputs; +using SeqCli.Cli.Features; +using SeqCli.Connection; + +namespace SeqCli.Cli.Commands.ApiKey +{ + [Command("apikey", "remove", "Remove API Key from the server", Example = + "seqcli apikey remove -t TestApiKey")] + class RemoveCommand : Command + { + private readonly SeqConnectionFactory _connectionFactory; + private readonly ConnectionFeature _connection; + private string _title; + private string _id; + + public RemoveCommand(SeqConnectionFactory connectionFactory) + { + _connectionFactory = connectionFactory; + _connection = Enable(); + Options.Add( + "t=|title=", + "Remove API Keys with the specified title", + (t) => _title = t); + + Options.Add( + "i=|id=", + "Remove API Keys with the specified Id", + (t) => _id = t); + } + + protected override async Task Run() + { + if (_title != default && _id != default) + { + Console.WriteLine("You can only specify \"title\" or \"id\" not both"); + return -1; + } + + var connection = _connectionFactory.Connect(_connection); + + var apiKeys = await connection.ApiKeys.ListAsync(); + var apiKeyToRemove = apiKeys.Where(ak => ak.Title == _title || ak.Id == _id).ToList(); + if (!apiKeyToRemove.Any()) + { + Console.WriteLine($"\"{_title}\" API Key doesn't exist"); + return -1; + } + + foreach (var apiKeyEntity in apiKeyToRemove) + { + await connection.ApiKeys.RemoveAsync(apiKeyEntity); + } + return 0; + } + } +}