1a540f4f0a
Replace the CLI's Akka.NET ClusterClient transport with a simple HTTP client targeting a new POST /management endpoint on the Central Host. The endpoint handles Basic Auth, LDAP authentication, role resolution, and ManagementActor dispatch in a single round-trip — eliminating the CLI's Akka, LDAP, and Security dependencies. Also fixes DCL ReSubscribeAll losing subscriptions on repeated reconnect by deriving the tag list from _subscriptionsByInstance instead of _subscriptionIds.
119 lines
5.9 KiB
C#
119 lines
5.9 KiB
C#
using System.CommandLine;
|
|
using System.CommandLine.Parsing;
|
|
using ScadaLink.Commons.Messages.Management;
|
|
|
|
namespace ScadaLink.CLI.Commands;
|
|
|
|
public static class ApiMethodCommands
|
|
{
|
|
public static Command Build(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var command = new Command("api-method") { Description = "Manage inbound API methods" };
|
|
|
|
command.Add(BuildList(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildGet(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildCreate(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildUpdate(urlOption, formatOption, usernameOption, passwordOption));
|
|
command.Add(BuildDelete(urlOption, formatOption, usernameOption, passwordOption));
|
|
|
|
return command;
|
|
}
|
|
|
|
private static Command BuildList(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var cmd = new Command("list") { Description = "List all API methods" };
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption, new ListApiMethodsCommand());
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildGet(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "API method ID", Required = true };
|
|
var cmd = new Command("get") { Description = "Get an API method by ID" };
|
|
cmd.Add(idOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption, new GetApiMethodCommand(id));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildCreate(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var nameOption = new Option<string>("--name") { Description = "Method name", Required = true };
|
|
var scriptOption = new Option<string>("--script") { Description = "Script code", Required = true };
|
|
var timeoutOption = new Option<int>("--timeout") { Description = "Timeout in seconds" };
|
|
timeoutOption.DefaultValueFactory = _ => 30;
|
|
var parametersOption = new Option<string?>("--parameters") { Description = "Parameter definitions JSON" };
|
|
var returnDefOption = new Option<string?>("--return-def") { Description = "Return type definition" };
|
|
|
|
var cmd = new Command("create") { Description = "Create an API method" };
|
|
cmd.Add(nameOption);
|
|
cmd.Add(scriptOption);
|
|
cmd.Add(timeoutOption);
|
|
cmd.Add(parametersOption);
|
|
cmd.Add(returnDefOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var name = result.GetValue(nameOption)!;
|
|
var script = result.GetValue(scriptOption)!;
|
|
var timeout = result.GetValue(timeoutOption);
|
|
var parameters = result.GetValue(parametersOption);
|
|
var returnDef = result.GetValue(returnDefOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new CreateApiMethodCommand(name, script, timeout, parameters, returnDef));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildUpdate(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "API method ID", Required = true };
|
|
var scriptOption = new Option<string>("--script") { Description = "Script code", Required = true };
|
|
var timeoutOption = new Option<int>("--timeout") { Description = "Timeout in seconds" };
|
|
timeoutOption.DefaultValueFactory = _ => 30;
|
|
var parametersOption = new Option<string?>("--parameters") { Description = "Parameter definitions JSON" };
|
|
var returnDefOption = new Option<string?>("--return-def") { Description = "Return type definition" };
|
|
|
|
var cmd = new Command("update") { Description = "Update an API method" };
|
|
cmd.Add(idOption);
|
|
cmd.Add(scriptOption);
|
|
cmd.Add(timeoutOption);
|
|
cmd.Add(parametersOption);
|
|
cmd.Add(returnDefOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
var script = result.GetValue(scriptOption)!;
|
|
var timeout = result.GetValue(timeoutOption);
|
|
var parameters = result.GetValue(parametersOption);
|
|
var returnDef = result.GetValue(returnDefOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption,
|
|
new UpdateApiMethodCommand(id, script, timeout, parameters, returnDef));
|
|
});
|
|
return cmd;
|
|
}
|
|
|
|
private static Command BuildDelete(Option<string> urlOption, Option<string> formatOption, Option<string> usernameOption, Option<string> passwordOption)
|
|
{
|
|
var idOption = new Option<int>("--id") { Description = "API method ID", Required = true };
|
|
var cmd = new Command("delete") { Description = "Delete an API method" };
|
|
cmd.Add(idOption);
|
|
cmd.SetAction(async (ParseResult result) =>
|
|
{
|
|
var id = result.GetValue(idOption);
|
|
return await CommandHelpers.ExecuteCommandAsync(
|
|
result, urlOption, formatOption, usernameOption, passwordOption, new DeleteApiMethodCommand(id));
|
|
});
|
|
return cmd;
|
|
}
|
|
}
|