diff --git a/src/Enterspeed.Cli/Api/Domain/CreateDomain.cs b/src/Enterspeed.Cli/Api/Domain/CreateDomain.cs index 832c6b5..3854212 100644 --- a/src/Enterspeed.Cli/Api/Domain/CreateDomain.cs +++ b/src/Enterspeed.Cli/Api/Domain/CreateDomain.cs @@ -1,5 +1,36 @@ -namespace Enterspeed.Cli.Api.Domain; +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; +using MediatR; +using RestSharp; -internal class CreateDomain +namespace Enterspeed.Cli.Api.Domain; + +public class CreateDomainRequest : IRequest +{ + public string Name { get; set; } + public string[] Hostnames { get; set; } +} + +public class CreateDomainResponse +{ + public DomainId DomainId { get; set; } +} + +public class CreateDomainRequestRequestHandler : IRequestHandler { + private readonly IEnterspeedClient _enterspeedClient; + + public CreateDomainRequestRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(CreateDomainRequest createDomainRequest, CancellationToken cancellationToken) + { + var request = new RestRequest("tenant/domains", Method.Post) + .AddJsonBody(createDomainRequest); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } } \ No newline at end of file diff --git a/src/Enterspeed.Cli/Api/Domain/DeleteDomain.cs b/src/Enterspeed.Cli/Api/Domain/DeleteDomain.cs index a250073..b1513cf 100644 --- a/src/Enterspeed.Cli/Api/Domain/DeleteDomain.cs +++ b/src/Enterspeed.Cli/Api/Domain/DeleteDomain.cs @@ -1,5 +1,38 @@ -namespace Enterspeed.Cli.Api.Domain; +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; +using MediatR; +using RestSharp; -internal class DeleteDomain +namespace Enterspeed.Cli.Api.Domain; + +public class DeleteDomainRequest : IRequest { + public DomainId DomainId { get; } + + public DeleteDomainRequest(DomainId domainId) + { + DomainId = domainId; + } +} + +public class DeleteDomainResponse +{ +} + +public class DeleteEnvironmentRequestRequestHandler : IRequestHandler +{ + private readonly IEnterspeedClient _enterspeedClient; + + public DeleteEnvironmentRequestRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(DeleteDomainRequest deleteDomainRequest, CancellationToken cancellationToken) + { + var request = new RestRequest($"tenant/domains/{deleteDomainRequest.DomainId.DomainGuid}", Method.Delete); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } } \ No newline at end of file diff --git a/src/Enterspeed.Cli/Api/Domain/UpdateDomain.cs b/src/Enterspeed.Cli/Api/Domain/UpdateDomain.cs index f07f1e7..36c3131 100644 --- a/src/Enterspeed.Cli/Api/Domain/UpdateDomain.cs +++ b/src/Enterspeed.Cli/Api/Domain/UpdateDomain.cs @@ -1,12 +1,22 @@ -using Enterspeed.Cli.Domain.Models; +using System.Text.Json.Serialization; +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; using MediatR; +using RestSharp; namespace Enterspeed.Cli.Api.Domain; -public class UpdateDomainRequest : IRequest +public class UpdateDomainRequest : IRequest { - public DomainId DomainId { get; set; } + [JsonIgnore] + public DomainId DomainId { get; } public string Name { get; set; } + public string[] Hostnames { get; set; } + + public UpdateDomainRequest(DomainId domainId) + { + DomainId = domainId; + } } public class UpdateDomainResponse @@ -18,4 +28,24 @@ public class UpdateDomainResponse public string Name { get; set; } public string[] Hostnames { get; set; } +} + + +public class UpdateDomainRequestRequestHandler : IRequestHandler +{ + private readonly IEnterspeedClient _enterspeedClient; + + public UpdateDomainRequestRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(UpdateDomainRequest updateDomainRequest, CancellationToken cancellationToken) + { + var request = new RestRequest($"tenant/domains/{updateDomainRequest.DomainId.DomainGuid}", Method.Put) + .AddJsonBody(updateDomainRequest); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } } \ No newline at end of file diff --git a/src/Enterspeed.Cli/Api/Environment/CreateEnvironment.cs b/src/Enterspeed.Cli/Api/Environment/CreateEnvironment.cs index 9a9b199..46f09dc 100644 --- a/src/Enterspeed.Cli/Api/Environment/CreateEnvironment.cs +++ b/src/Enterspeed.Cli/Api/Environment/CreateEnvironment.cs @@ -1,4 +1,5 @@ -using Enterspeed.Cli.Services.EnterspeedClient; +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; using MediatR; using RestSharp; @@ -11,6 +12,7 @@ public class CreateEnvironmentRequest : IRequest public class CreateEnvironmentResponse { + public EnvironmentId EnvironmentId { get; set; } } public class CreateEnvironmentRequestRequestHandler : IRequestHandler @@ -22,10 +24,10 @@ public CreateEnvironmentRequestRequestHandler(IEnterspeedClient enterspeedClient _enterspeedClient = enterspeedClient; } - public async Task Handle(CreateEnvironmentRequest createEnvironmentsRequest, CancellationToken cancellationToken) + public async Task Handle(CreateEnvironmentRequest createEnvironmentRequest, CancellationToken cancellationToken) { var request = new RestRequest("tenant/environments", Method.Post) - .AddJsonBody(createEnvironmentsRequest); + .AddJsonBody(createEnvironmentRequest); var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); return response; diff --git a/src/Enterspeed.Cli/Api/Environment/DeleteEnvironment.cs b/src/Enterspeed.Cli/Api/Environment/DeleteEnvironment.cs index 08bfc11..6184e73 100644 --- a/src/Enterspeed.Cli/Api/Environment/DeleteEnvironment.cs +++ b/src/Enterspeed.Cli/Api/Environment/DeleteEnvironment.cs @@ -1,14 +1,39 @@ using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; using MediatR; +using RestSharp; namespace Enterspeed.Cli.Api.Environment { public class DeleteEnvironmentRequest : IRequest { - public EnvironmentId EnvironmentId { get; set; } + public EnvironmentId EnvironmentId { get; } + + public DeleteEnvironmentRequest(EnvironmentId environmentId) + { + EnvironmentId = environmentId; + } } public class DeleteEnvironmentResponse { } + + public class DeleteEnvironmentRequestRequestHandler : IRequestHandler + { + private readonly IEnterspeedClient _enterspeedClient; + + public DeleteEnvironmentRequestRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(DeleteEnvironmentRequest deleteEnvironmentsRequest, CancellationToken cancellationToken) + { + var request = new RestRequest($"tenant/environments/{deleteEnvironmentsRequest.EnvironmentId.EnvironmentGuid}", Method.Delete); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } + } } diff --git a/src/Enterspeed.Cli/Api/Environment/UpdateEnvironment.cs b/src/Enterspeed.Cli/Api/Environment/UpdateEnvironment.cs index 79e9425..65afb06 100644 --- a/src/Enterspeed.Cli/Api/Environment/UpdateEnvironment.cs +++ b/src/Enterspeed.Cli/Api/Environment/UpdateEnvironment.cs @@ -1,15 +1,43 @@ -using Enterspeed.Cli.Domain.Models; +using System.Text.Json.Serialization; +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; using MediatR; +using RestSharp; namespace Enterspeed.Cli.Api.Environment { public class UpdateEnvironmentRequest : IRequest { - public EnvironmentId EnvironmentId { get; set; } + [JsonIgnore] + public EnvironmentId EnvironmentId { get; } public string Name { get; set; } + + public UpdateEnvironmentRequest(EnvironmentId environmentId) + { + EnvironmentId = environmentId; + } } public class UpdateEnvironmentResponse { } + + public class UpdateEnvironmentRequestRequestHandler : IRequestHandler + { + private readonly IEnterspeedClient _enterspeedClient; + + public UpdateEnvironmentRequestRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(UpdateEnvironmentRequest updateEnvironmentRequest, CancellationToken cancellationToken) + { + var request = new RestRequest($"tenant/environments/{updateEnvironmentRequest.EnvironmentId.EnvironmentGuid}", Method.Put) + .AddJsonBody(updateEnvironmentRequest); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } + } } diff --git a/src/Enterspeed.Cli/Api/EnvironmentClient/CreateEnvironmentClient.cs b/src/Enterspeed.Cli/Api/EnvironmentClient/CreateEnvironmentClient.cs new file mode 100644 index 0000000..0f73ec4 --- /dev/null +++ b/src/Enterspeed.Cli/Api/EnvironmentClient/CreateEnvironmentClient.cs @@ -0,0 +1,48 @@ +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; +using MediatR; +using RestSharp; +using System.Text.Json.Serialization; + +namespace Enterspeed.Cli.Api.EnvironmentClient +{ + public class CreateEnvironmentClientRequest : IRequest + { + public string Name { get; set; } + public string[] AllowedDomains { get; set; } + + public bool RegenerateAccessKey { get; set; } + + [JsonIgnore] + public EnvironmentId EnvironmentId { get; } + + public CreateEnvironmentClientRequest(EnvironmentId environmentId) + { + EnvironmentId = environmentId; + } + } + + public class CreateEnvironmentClientResponse + { + public EnvironmentClientId EnvironmentClientId { get; set; } + } + + public class CreateEnvironmentClientRequestRequestHandler : IRequestHandler + { + private readonly IEnterspeedClient _enterspeedClient; + + public CreateEnvironmentClientRequestRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(CreateEnvironmentClientRequest createEnvironmentClientRequest, CancellationToken cancellationToken) + { + var request = new RestRequest($"tenant/environment-clients/{createEnvironmentClientRequest.EnvironmentId.EnvironmentGuid}", Method.Post) + .AddJsonBody(createEnvironmentClientRequest); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } + } +} diff --git a/src/Enterspeed.Cli/Api/EnvironmentClient/DeleteEnvironmentClient.cs b/src/Enterspeed.Cli/Api/EnvironmentClient/DeleteEnvironmentClient.cs new file mode 100644 index 0000000..fccf36b --- /dev/null +++ b/src/Enterspeed.Cli/Api/EnvironmentClient/DeleteEnvironmentClient.cs @@ -0,0 +1,44 @@ +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; +using MediatR; +using RestSharp; +using System.Text.Json.Serialization; + +namespace Enterspeed.Cli.Api.EnvironmentClient +{ + public class DeleteEnvironmentClientRequest : IRequest + { + [JsonIgnore] + public EnvironmentClientId EnvironmentClientId { get; } + + public DeleteEnvironmentClientRequest(EnvironmentClientId environmentClientId) + { + EnvironmentClientId = environmentClientId; + } + } + + public class DeleteEnvironmentClientResponse + { + } + + public class DeleteEnvironmentClientRequestRequestHandler : IRequestHandler + { + private readonly IEnterspeedClient _enterspeedClient; + + public DeleteEnvironmentClientRequestRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(DeleteEnvironmentClientRequest deleteEnvironmentClientRequest, CancellationToken cancellationToken) + { + var request = + new RestRequest( + $"tenant/environment-clients/{deleteEnvironmentClientRequest.EnvironmentClientId.EnvironmentGuid}/{deleteEnvironmentClientRequest.EnvironmentClientId.ClientGuid}/", + Method.Delete); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } + } +} diff --git a/src/Enterspeed.Cli/Api/EnvironmentClient/UpdateEnvironmentClient.cs b/src/Enterspeed.Cli/Api/EnvironmentClient/UpdateEnvironmentClient.cs new file mode 100644 index 0000000..614be76 --- /dev/null +++ b/src/Enterspeed.Cli/Api/EnvironmentClient/UpdateEnvironmentClient.cs @@ -0,0 +1,48 @@ +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; +using MediatR; +using RestSharp; +using System.Text.Json.Serialization; + +namespace Enterspeed.Cli.Api.EnvironmentClient +{ + public class UpdateEnvironmentClientRequest : IRequest + { + public string Name { get; set; } + public string[] AllowedDomains { get; set; } + + public bool RegenerateAccessKey { get; set; } + + [JsonIgnore] + public EnvironmentClientId EnvironmentClientId { get; } + + public UpdateEnvironmentClientRequest(EnvironmentClientId environmentClientId) + { + EnvironmentClientId = environmentClientId; + } + } + + public class UpdateEnvironmentClientResponse + { + public EnvironmentClientId EnvironmentClientId { get; set; } + } + + public class UpdateEnvironmentClientRequestRequestHandler : IRequestHandler + { + private readonly IEnterspeedClient _enterspeedClient; + + public UpdateEnvironmentClientRequestRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(UpdateEnvironmentClientRequest updateEnvironmentClientRequest, CancellationToken cancellationToken) + { + var request = new RestRequest($"tenant/environment-clients/{updateEnvironmentClientRequest.EnvironmentClientId.EnvironmentGuid}/{updateEnvironmentClientRequest.EnvironmentClientId.ClientGuid}/", Method.Put) + .AddJsonBody(updateEnvironmentClientRequest); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } + } +} diff --git a/src/Enterspeed.Cli/Api/Source/CreateSource.cs b/src/Enterspeed.Cli/Api/Source/CreateSource.cs new file mode 100644 index 0000000..a60d0fd --- /dev/null +++ b/src/Enterspeed.Cli/Api/Source/CreateSource.cs @@ -0,0 +1,41 @@ +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; +using MediatR; +using RestSharp; + +namespace Enterspeed.Cli.Api.Source; +public class CreateSourceRequest : IRequest +{ + public string SourceGroupId { get; } + public string Name { get; set; } + public string Type { get; set; } + + public CreateSourceRequest(SourceGroupId sourceGroupId) + { + SourceGroupId = sourceGroupId.SourceGroupGuid; + } +} + +public class CreateSourceResponse +{ + public SourceId SourceId { get; set; } +} + +public class CreateSourceGroupRequestHandler : IRequestHandler +{ + private readonly IEnterspeedClient _enterspeedClient; + + public CreateSourceGroupRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(CreateSourceRequest createRequest, CancellationToken cancellationToken) + { + var request = new RestRequest("tenant/sources", Method.Post) + .AddJsonBody(createRequest); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } +} \ No newline at end of file diff --git a/src/Enterspeed.Cli/Api/Source/DeleteSource.cs b/src/Enterspeed.Cli/Api/Source/DeleteSource.cs new file mode 100644 index 0000000..0dd7ade --- /dev/null +++ b/src/Enterspeed.Cli/Api/Source/DeleteSource.cs @@ -0,0 +1,38 @@ +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; +using MediatR; +using RestSharp; + +namespace Enterspeed.Cli.Api.Source; + +public class DeleteSourceRequest : IRequest +{ + public SourceId SourceId { get; } + + public DeleteSourceRequest(SourceId sourceId) + { + SourceId = sourceId; + } +} + +public class DeleteSourceResponse +{ +} + +public class DeleteSourceRequestHandler : IRequestHandler +{ + private readonly IEnterspeedClient _enterspeedClient; + + public DeleteSourceRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(DeleteSourceRequest deleteRequest, CancellationToken cancellationToken) + { + var request = new RestRequest($"tenant/sources/{deleteRequest.SourceId.SourceGuid}", Method.Delete); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } +} \ No newline at end of file diff --git a/src/Enterspeed.Cli/Api/Source/UpdateSource.cs b/src/Enterspeed.Cli/Api/Source/UpdateSource.cs new file mode 100644 index 0000000..245b7e4 --- /dev/null +++ b/src/Enterspeed.Cli/Api/Source/UpdateSource.cs @@ -0,0 +1,48 @@ +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; +using MediatR; +using RestSharp; +using System.Text.Json.Serialization; + +namespace Enterspeed.Cli.Api.Source; + +public class UpdateSourceRequest : IRequest +{ + [JsonIgnore] public SourceId SourceId { get; } + + public UpdateSourceRequest(SourceId id) + { + SourceId = id; + } + + public string Name { get; set; } + public string Type { get; set; } + public string[] EnvironmentIds { get; set; } + + public string SourceGroupId { get; set; } + public bool RegenerateAccessKey { get; set; } +} + +public class UpdateSourceResponse +{ + public SourceId SourceId { get; set; } +} + +public class UpdateSourceRequestHandler : IRequestHandler +{ + private readonly IEnterspeedClient _enterspeedClient; + + public UpdateSourceRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(UpdateSourceRequest updateRequest, CancellationToken cancellationToken) + { + var request = new RestRequest($"tenant/sources/{updateRequest.SourceId.SourceGuid}", Method.Put) + .AddJsonBody(updateRequest); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } +} \ No newline at end of file diff --git a/src/Enterspeed.Cli/Api/SourceGroup/CreateSourceGroup.cs b/src/Enterspeed.Cli/Api/SourceGroup/CreateSourceGroup.cs new file mode 100644 index 0000000..8d0b043 --- /dev/null +++ b/src/Enterspeed.Cli/Api/SourceGroup/CreateSourceGroup.cs @@ -0,0 +1,37 @@ +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; +using MediatR; +using RestSharp; + +namespace Enterspeed.Cli.Api.SourceGroup; + +public class CreateSourceGroupRequest : IRequest +{ + public string Name { get; set; } + public string Alias { get; set; } + public string Type { get; set; } +} + +public class CreateSourceGroupResponse +{ + public SourceGroupId SourceGroupId { get; set; } +} + +public class CreateSourceGroupRequestHandler : IRequestHandler +{ + private readonly IEnterspeedClient _enterspeedClient; + + public CreateSourceGroupRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(CreateSourceGroupRequest createRequest, CancellationToken cancellationToken) + { + var request = new RestRequest("tenant/source-groups", Method.Post) + .AddJsonBody(createRequest); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } +} \ No newline at end of file diff --git a/src/Enterspeed.Cli/Api/SourceGroup/DeleteSourceGroup.cs b/src/Enterspeed.Cli/Api/SourceGroup/DeleteSourceGroup.cs new file mode 100644 index 0000000..c7c8c1c --- /dev/null +++ b/src/Enterspeed.Cli/Api/SourceGroup/DeleteSourceGroup.cs @@ -0,0 +1,39 @@ +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; +using MediatR; +using RestSharp; + +namespace Enterspeed.Cli.Api.SourceGroup; + +public class DeleteSourceGroupRequest : IRequest +{ + public SourceGroupId SourceGroupId { get; } + + public DeleteSourceGroupRequest(SourceGroupId sourceGroupId) + { + SourceGroupId = sourceGroupId; + } +} + +public class DeleteSourceGroupResponse +{ +} + +public class DeleteSourceGroupRequestHandler : IRequestHandler +{ + private readonly IEnterspeedClient _enterspeedClient; + + public DeleteSourceGroupRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(DeleteSourceGroupRequest deleteRequest, CancellationToken cancellationToken) + { + var request = new RestRequest($"tenant/source-groups/{deleteRequest.SourceGroupId.SourceGroupGuid}", + Method.Delete); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } +} \ No newline at end of file diff --git a/src/Enterspeed.Cli/Api/SourceGroup/GetSourceGroups.cs b/src/Enterspeed.Cli/Api/SourceGroup/GetSourceGroup.cs similarity index 97% rename from src/Enterspeed.Cli/Api/SourceGroup/GetSourceGroups.cs rename to src/Enterspeed.Cli/Api/SourceGroup/GetSourceGroup.cs index f5d4d20..7745f8d 100644 --- a/src/Enterspeed.Cli/Api/SourceGroup/GetSourceGroups.cs +++ b/src/Enterspeed.Cli/Api/SourceGroup/GetSourceGroup.cs @@ -8,7 +8,6 @@ namespace Enterspeed.Cli.Api.SourceGroup; public class GetSourceGroupsRequest : IRequest { - public string[] TenantIds { get; set; } } public class GetSourceGroupResponse diff --git a/src/Enterspeed.Cli/Api/SourceGroup/UpdateSourceGroup.cs b/src/Enterspeed.Cli/Api/SourceGroup/UpdateSourceGroup.cs new file mode 100644 index 0000000..f382f1d --- /dev/null +++ b/src/Enterspeed.Cli/Api/SourceGroup/UpdateSourceGroup.cs @@ -0,0 +1,47 @@ +using System.Text.Json.Serialization; +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.EnterspeedClient; +using MediatR; +using RestSharp; + +namespace Enterspeed.Cli.Api.SourceGroup; + +public class UpdateSourceGroupRequest : IRequest +{ + [JsonIgnore] + public SourceGroupId SourceGroupId { get; } + public UpdateSourceGroupRequest(SourceGroupId id) + { + SourceGroupId = id; + } + + public string Name { get; set; } + public string Type { get; set; } + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string[] EnvironmentIds { get; set; } + public bool RegenerateAccessKey { get; set; } +} + +public class UpdateSourceGroupResponse +{ + public SourceGroupId SourceGroupId { get; set; } +} + +public class UpdateSourceGroupRequestHandler : IRequestHandler +{ + private readonly IEnterspeedClient _enterspeedClient; + + public UpdateSourceGroupRequestHandler(IEnterspeedClient enterspeedClient) + { + _enterspeedClient = enterspeedClient; + } + + public async Task Handle(UpdateSourceGroupRequest updateRequest, CancellationToken cancellationToken) + { + var request = new RestRequest($"tenant/source-groups/{updateRequest.SourceGroupId.SourceGroupGuid}", Method.Put) + .AddJsonBody(updateRequest); + + var response = await _enterspeedClient.ExecuteAsync(request, cancellationToken); + return response; + } +} \ No newline at end of file diff --git a/src/Enterspeed.Cli/Commands/Domain/CreateDomainCommand.cs b/src/Enterspeed.Cli/Commands/Domain/CreateDomainCommand.cs index a7fad65..0c85a2f 100644 --- a/src/Enterspeed.Cli/Commands/Domain/CreateDomainCommand.cs +++ b/src/Enterspeed.Cli/Commands/Domain/CreateDomainCommand.cs @@ -1,4 +1,5 @@ -using Enterspeed.Cli.Services.ConsoleOutput; +using Enterspeed.Cli.Api.Domain; +using Enterspeed.Cli.Services.ConsoleOutput; using MediatR; using System.CommandLine; using System.CommandLine.Invocation; @@ -10,12 +11,15 @@ internal class CreateDomainCommand : Command public CreateDomainCommand() : base(name: "create", "Create domain") { AddOption(new Option(new[] { "--name", "-n" }, "Name of domain") {IsRequired = true}); + AddOption(new Option(new[] { "--hostnames", "-h" }, "List of hostnames, separated by semicolon.")); } public new class Handler : BaseCommandHandler, ICommandHandler { private readonly IMediator _mediator; private readonly IOutputService _outputService; + public string Name { get; set; } + public string Hostnames { get; set; } public Handler(IMediator mediator, IOutputService outputService) { @@ -25,6 +29,21 @@ public Handler(IMediator mediator, IOutputService outputService) public async Task InvokeAsync(InvocationContext context) { + var request = new CreateDomainRequest + { + Name = Name + }; + + if (!string.IsNullOrEmpty(Hostnames)) + { + var hostnames = Hostnames.Split(';'); + request.Hostnames = hostnames.Select(host => host.Trim()).ToArray(); + } + + var domain = await _mediator.Send(request); + + _outputService.Write(domain); + return 0; } } diff --git a/src/Enterspeed.Cli/Commands/Domain/DeleteDomainCommand.cs b/src/Enterspeed.Cli/Commands/Domain/DeleteDomainCommand.cs index f89441c..0db6b90 100644 --- a/src/Enterspeed.Cli/Commands/Domain/DeleteDomainCommand.cs +++ b/src/Enterspeed.Cli/Commands/Domain/DeleteDomainCommand.cs @@ -1,7 +1,9 @@ -using Enterspeed.Cli.Services.ConsoleOutput; +using Enterspeed.Cli.Api.Domain; +using Enterspeed.Cli.Services.ConsoleOutput; using MediatR; using System.CommandLine; using System.CommandLine.Invocation; +using Enterspeed.Cli.Domain.Models; namespace Enterspeed.Cli.Commands.Domain; @@ -9,7 +11,7 @@ public class DeleteDomainCommand : ConfirmCommand { public DeleteDomainCommand() : base(name: "delete", "Delete domain") { - AddArgument(new Argument("id", "Id of the domain") { Arity = ArgumentArity.ZeroOrOne }); + AddArgument(new Argument("id", "Id of the domain") { Arity = ArgumentArity.ExactlyOne }); } public new class Handler : BaseCommandHandler, ICommandHandler @@ -17,7 +19,7 @@ public DeleteDomainCommand() : base(name: "delete", "Delete domain") private readonly IMediator _mediator; private readonly IOutputService _outputService; - public string Id { get; set; } + public Guid Id { get; set; } public Handler(IMediator mediator, IOutputService outputService) @@ -33,6 +35,9 @@ public async Task InvokeAsync(InvocationContext context) return 0; } + var response = await _mediator.Send(new DeleteDomainRequest(DomainId.Parse(DomainId.From(Id.ToString())))); + _outputService.Write(response); + return 0; } } diff --git a/src/Enterspeed.Cli/Commands/Domain/UpdateDomainCommand.cs b/src/Enterspeed.Cli/Commands/Domain/UpdateDomainCommand.cs index e1cef91..6811a63 100644 --- a/src/Enterspeed.Cli/Commands/Domain/UpdateDomainCommand.cs +++ b/src/Enterspeed.Cli/Commands/Domain/UpdateDomainCommand.cs @@ -1,7 +1,9 @@ -using Enterspeed.Cli.Services.ConsoleOutput; +using Enterspeed.Cli.Api.Domain; +using Enterspeed.Cli.Services.ConsoleOutput; using MediatR; using System.CommandLine; using System.CommandLine.Invocation; +using Enterspeed.Cli.Domain.Models; namespace Enterspeed.Cli.Commands.Domain; @@ -9,6 +11,9 @@ internal class UpdateDomainCommand : Command { public UpdateDomainCommand() : base(name: "update", "Update domain") { + AddArgument(new Argument("id", "Id of the domain") { Arity = ArgumentArity.ExactlyOne }); + AddOption(new Option(new[] { "--name", "-n" }, "Name of domain")); + AddOption(new Option(new[] { "--hostnames", "-h" }, "List of hostnames, separated by semicolon.")); } public new class Handler : BaseCommandHandler, ICommandHandler @@ -16,6 +21,11 @@ public UpdateDomainCommand() : base(name: "update", "Update domain") private readonly IMediator _mediator; private readonly IOutputService _outputService; + public Guid Id { get; set; } + public string Name { get; set; } + public string Hostnames { get; set; } + + public Handler(IMediator mediator, IOutputService outputService) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); @@ -24,6 +34,21 @@ public Handler(IMediator mediator, IOutputService outputService) public async Task InvokeAsync(InvocationContext context) { + var request = new UpdateDomainRequest(DomainId.Parse(DomainId.From(Id.ToString()))); + if (!string.IsNullOrEmpty(Name)) + { + request.Name = Name; + } + + if (!string.IsNullOrEmpty(Hostnames)) + { + var hostnames = Hostnames.Split(';'); + request.Hostnames = hostnames.Select(host => host.Trim()).ToArray(); + } + + var response = await _mediator.Send(request); + + _outputService.Write(response); return 0; } } diff --git a/src/Enterspeed.Cli/Commands/Environment/CreateEnvironmentCommand.cs b/src/Enterspeed.Cli/Commands/Environment/CreateEnvironmentCommand.cs index c53d4e8..123a524 100644 --- a/src/Enterspeed.Cli/Commands/Environment/CreateEnvironmentCommand.cs +++ b/src/Enterspeed.Cli/Commands/Environment/CreateEnvironmentCommand.cs @@ -2,6 +2,7 @@ using MediatR; using System.CommandLine; using System.CommandLine.Invocation; +using Enterspeed.Cli.Api.Environment; namespace Enterspeed.Cli.Commands.Environment; @@ -9,6 +10,7 @@ internal class CreateEnvironmentCommand : Command { public CreateEnvironmentCommand() : base(name: "create", "Create environment") { + AddOption(new Option(new[] { "--name", "-n" }, "Name of environment") { IsRequired = true }); } public new class Handler : BaseCommandHandler, ICommandHandler @@ -16,6 +18,8 @@ public CreateEnvironmentCommand() : base(name: "create", "Create environment") private readonly IMediator _mediator; private readonly IOutputService _outputService; + public string Name { get; set; } + public Handler(IMediator mediator, IOutputService outputService) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); @@ -24,6 +28,13 @@ public Handler(IMediator mediator, IOutputService outputService) public async Task InvokeAsync(InvocationContext context) { + var domain = await _mediator.Send(new CreateEnvironmentRequest + { + Name = Name + }); + + _outputService.Write(domain); + return 0; } } diff --git a/src/Enterspeed.Cli/Commands/Environment/DeleteEnvironmentCommand.cs b/src/Enterspeed.Cli/Commands/Environment/DeleteEnvironmentCommand.cs index 2fd7d7e..9c48d1e 100644 --- a/src/Enterspeed.Cli/Commands/Environment/DeleteEnvironmentCommand.cs +++ b/src/Enterspeed.Cli/Commands/Environment/DeleteEnvironmentCommand.cs @@ -1,7 +1,10 @@ -using Enterspeed.Cli.Services.ConsoleOutput; +using Enterspeed.Cli.Api.Domain; +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.ConsoleOutput; using MediatR; using System.CommandLine; using System.CommandLine.Invocation; +using Enterspeed.Cli.Api.Environment; namespace Enterspeed.Cli.Commands.Environment; @@ -9,12 +12,15 @@ internal class DeleteEnvironmentCommand : Command { public DeleteEnvironmentCommand() : base(name: "delete", "Delete environment") { + AddArgument(new Argument("id", "Id of the environment") { Arity = ArgumentArity.ExactlyOne }); } public new class Handler : BaseCommandHandler, ICommandHandler { private readonly IMediator _mediator; private readonly IOutputService _outputService; + + public Guid Id { get; set; } public Handler(IMediator mediator, IOutputService outputService) { @@ -24,6 +30,14 @@ public Handler(IMediator mediator, IOutputService outputService) public async Task InvokeAsync(InvocationContext context) { + if (!Yes && !GetConfirmation()) + { + return 0; + } + + var response = await _mediator.Send(new DeleteEnvironmentRequest(EnvironmentId.Parse(EnvironmentId.From(Id.ToString())))); + _outputService.Write(response); + return 0; } } diff --git a/src/Enterspeed.Cli/Commands/Environment/GetEnvironmentCommand.cs b/src/Enterspeed.Cli/Commands/Environment/GetEnvironmentCommand.cs index 4f29cae..277ee79 100644 --- a/src/Enterspeed.Cli/Commands/Environment/GetEnvironmentCommand.cs +++ b/src/Enterspeed.Cli/Commands/Environment/GetEnvironmentCommand.cs @@ -3,6 +3,7 @@ using System.CommandLine; using System.CommandLine.Invocation; using Enterspeed.Cli.Api.Environment; +using Enterspeed.Cli.Exceptions; namespace Enterspeed.Cli.Commands.Environment; @@ -10,6 +11,8 @@ internal class GetEnvironmentCommand : Command { public GetEnvironmentCommand() : base(name: "get", "Get an environment") { + AddArgument(new Argument("id", "Id of the environment") { Arity = ArgumentArity.ZeroOrOne }); + AddOption(new Option(new[] { "--name", "-n" }, "Name of environment")); } public new class Handler : BaseCommandHandler, ICommandHandler @@ -17,6 +20,9 @@ public GetEnvironmentCommand() : base(name: "get", "Get an environment") private readonly IMediator _mediator; private readonly IOutputService _outputService; + public Guid Id { get; set; } + public string Name { get; set; } + public Handler(IMediator mediator, IOutputService outputService) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); @@ -25,9 +31,26 @@ public Handler(IMediator mediator, IOutputService outputService) public async Task InvokeAsync(InvocationContext context) { + if (Id == Guid.Empty) + { + if (string.IsNullOrEmpty(Name)) + { + throw new ConsoleArgumentException("Please specify either id or name option"); + } + } + var environments = await _mediator.Send(new GetEnvironmentsRequest()); + GetEnvironmentsResponse result; + if (Id != Guid.Empty) + { + result = environments?.FirstOrDefault(x => x.Id.EnvironmentGuid == Id); + } + else + { + result = environments?.FirstOrDefault(x => x.Name.Equals(Name, StringComparison.InvariantCulture)); + } - _outputService.Write(environments); + _outputService.Write(result); return 0; } } diff --git a/src/Enterspeed.Cli/Commands/Environment/UpdateEnvironmentCommand.cs b/src/Enterspeed.Cli/Commands/Environment/UpdateEnvironmentCommand.cs index df2135d..9e5e57c 100644 --- a/src/Enterspeed.Cli/Commands/Environment/UpdateEnvironmentCommand.cs +++ b/src/Enterspeed.Cli/Commands/Environment/UpdateEnvironmentCommand.cs @@ -1,7 +1,9 @@ -using Enterspeed.Cli.Services.ConsoleOutput; +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.ConsoleOutput; using MediatR; using System.CommandLine; using System.CommandLine.Invocation; +using Enterspeed.Cli.Api.Environment; namespace Enterspeed.Cli.Commands.Environment; @@ -9,6 +11,8 @@ internal class UpdateEnvironmentCommand : Command { public UpdateEnvironmentCommand() : base(name: "update", "Update environment") { + AddArgument(new Argument("id", "Id of the environment") { Arity = ArgumentArity.ExactlyOne }); + AddOption(new Option(new[] { "--name", "-n" }, "Name of environment")); } public new class Handler : BaseCommandHandler, ICommandHandler @@ -16,6 +20,10 @@ public UpdateEnvironmentCommand() : base(name: "update", "Update environment") private readonly IMediator _mediator; private readonly IOutputService _outputService; + public Guid Id { get; set; } + public string Name { get; set; } + + public Handler(IMediator mediator, IOutputService outputService) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); @@ -24,6 +32,15 @@ public Handler(IMediator mediator, IOutputService outputService) public async Task InvokeAsync(InvocationContext context) { + var request = new UpdateEnvironmentRequest(EnvironmentId.Parse(EnvironmentId.From(Id.ToString()))); + if (!string.IsNullOrEmpty(Name)) + { + request.Name = Name; + } + + var response = await _mediator.Send(request); + + _outputService.Write(response); return 0; } } diff --git a/src/Enterspeed.Cli/Commands/EnvironmentClient/CreateEnvironmentClientCommand.cs b/src/Enterspeed.Cli/Commands/EnvironmentClient/CreateEnvironmentClientCommand.cs index 0d8c4c4..54a192e 100644 --- a/src/Enterspeed.Cli/Commands/EnvironmentClient/CreateEnvironmentClientCommand.cs +++ b/src/Enterspeed.Cli/Commands/EnvironmentClient/CreateEnvironmentClientCommand.cs @@ -2,6 +2,9 @@ using MediatR; using System.CommandLine; using System.CommandLine.Invocation; +using Enterspeed.Cli.Api.Environment; +using Enterspeed.Cli.Api.EnvironmentClient; +using Microsoft.Extensions.Logging; namespace Enterspeed.Cli.Commands.EnvironmentClient; @@ -9,21 +12,47 @@ internal class CreateEnvironmentClientCommand : Command { public CreateEnvironmentClientCommand() : base(name: "create", "Create environment client") { + AddOption(new Option(new[] { "--environment", "-e" }, "Name of environment") { IsRequired = true }); + AddOption(new Option(new[] { "--name", "-n" }, "Name of environment client") { IsRequired = true }); } public new class Handler : BaseCommandHandler, ICommandHandler { private readonly IMediator _mediator; private readonly IOutputService _outputService; + private readonly ILogger _logger; - public Handler(IMediator mediator, IOutputService outputService) + public string Name { get; set; } + public string Environment { get; set; } + + public Handler(IMediator mediator, IOutputService outputService, ILogger logger) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); _outputService = outputService; + _logger = logger; } public async Task InvokeAsync(InvocationContext context) { + var environments = await _mediator.Send(new GetEnvironmentsRequest()); + + var targetEnvironment = environments.FirstOrDefault(env => env.Name == Environment); + + if (targetEnvironment == null) + { + _logger.LogError("Environment not found: {0}", Environment); + return 1; + } + + var request = new CreateEnvironmentClientRequest(targetEnvironment.Id) + { + Name = Name + }; + + var envClient = await _mediator.Send(request); + + _outputService.Write(envClient); + return 0; } } diff --git a/src/Enterspeed.Cli/Commands/EnvironmentClient/DeleteEnvironmentClientCommand.cs b/src/Enterspeed.Cli/Commands/EnvironmentClient/DeleteEnvironmentClientCommand.cs index c7d03d7..f97371f 100644 --- a/src/Enterspeed.Cli/Commands/EnvironmentClient/DeleteEnvironmentClientCommand.cs +++ b/src/Enterspeed.Cli/Commands/EnvironmentClient/DeleteEnvironmentClientCommand.cs @@ -2,6 +2,8 @@ using MediatR; using System.CommandLine; using System.CommandLine.Invocation; +using Enterspeed.Cli.Api.EnvironmentClient; +using Microsoft.Extensions.Logging; namespace Enterspeed.Cli.Commands.EnvironmentClient; @@ -9,21 +11,44 @@ internal class DeleteEnvironmentClientCommand : Command { public DeleteEnvironmentClientCommand() : base(name: "delete", "Delete environment client") { + AddArgument(new Argument("name", "Name of the environment client") { Arity = ArgumentArity.ExactlyOne }); } public new class Handler : BaseCommandHandler, ICommandHandler { private readonly IMediator _mediator; private readonly IOutputService _outputService; + private readonly ILogger _logger; - public Handler(IMediator mediator, IOutputService outputService) + public string Name { get; set; } + + public Handler(IMediator mediator, IOutputService outputService, ILogger logger) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); _outputService = outputService; + _logger = logger; } public async Task InvokeAsync(InvocationContext context) { + if (!Yes && !GetConfirmation()) + { + return 0; + } + + var environmentClients = await _mediator.Send(new GetEnvironmentClientsRequest()); + + var client = environmentClients.FirstOrDefault(envClient => envClient.Name == Name); + + if (client == null) + { + _logger.LogError("Environment client with name: {0} not found", Name); + return 1; + } + + var response = await _mediator.Send(new DeleteEnvironmentClientRequest(client.Id)); + _outputService.Write(response); + return 0; } } diff --git a/src/Enterspeed.Cli/Commands/EnvironmentClient/GetEnvironmentClientCommand.cs b/src/Enterspeed.Cli/Commands/EnvironmentClient/GetEnvironmentClientCommand.cs index 5492f85..e641aad 100644 --- a/src/Enterspeed.Cli/Commands/EnvironmentClient/GetEnvironmentClientCommand.cs +++ b/src/Enterspeed.Cli/Commands/EnvironmentClient/GetEnvironmentClientCommand.cs @@ -2,7 +2,7 @@ using MediatR; using System.CommandLine; using System.CommandLine.Invocation; -using Enterspeed.Cli.Api.Environment; +using Enterspeed.Cli.Api.EnvironmentClient; namespace Enterspeed.Cli.Commands.EnvironmentClient; @@ -10,6 +10,7 @@ internal class GetEnvironmentClientCommand : Command { public GetEnvironmentClientCommand() : base(name: "get", "Get an environment client") { + AddArgument(new Argument("name", "Name of the environment client") { Arity = ArgumentArity.ExactlyOne }); } public new class Handler : BaseCommandHandler, ICommandHandler @@ -17,6 +18,8 @@ public GetEnvironmentClientCommand() : base(name: "get", "Get an environment cli private readonly IMediator _mediator; private readonly IOutputService _outputService; + public string Name { get; set; } + public Handler(IMediator mediator, IOutputService outputService) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); @@ -25,9 +28,11 @@ public Handler(IMediator mediator, IOutputService outputService) public async Task InvokeAsync(InvocationContext context) { - var environments = await _mediator.Send(new GetEnvironmentsRequest()); + var environmentClients = await _mediator.Send(new GetEnvironmentClientsRequest()); + + var client = environmentClients.FirstOrDefault(envClient => envClient.Name == Name); - _outputService.Write(environments); + _outputService.Write(client); return 0; } } diff --git a/src/Enterspeed.Cli/Commands/EnvironmentClient/UpdateEnvironmentClientCommand.cs b/src/Enterspeed.Cli/Commands/EnvironmentClient/UpdateEnvironmentClientCommand.cs index 9b8b9d2..8881c1a 100644 --- a/src/Enterspeed.Cli/Commands/EnvironmentClient/UpdateEnvironmentClientCommand.cs +++ b/src/Enterspeed.Cli/Commands/EnvironmentClient/UpdateEnvironmentClientCommand.cs @@ -1,20 +1,33 @@ -using Enterspeed.Cli.Services.ConsoleOutput; +using Enterspeed.Cli.Api.EnvironmentClient; +using Enterspeed.Cli.Services.ConsoleOutput; using MediatR; +using Microsoft.Extensions.Logging; using System.CommandLine; using System.CommandLine.Invocation; +using Enterspeed.Cli.Api.Domain; namespace Enterspeed.Cli.Commands.EnvironmentClient; internal class UpdateEnvironmentClientCommand : Command { - public UpdateEnvironmentClientCommand() : base(name: "Update", "Update an environment client") + public UpdateEnvironmentClientCommand() : base(name: "update", "Update an environment client") { + AddArgument(new Argument("name", "Name of the environment client") { Arity = ArgumentArity.ExactlyOne }); + AddOption(new Option(new[] { "--new-name", "-n" }, "New name of environment client")); + AddOption(new Option(new[] { "--domains", "-d" }, "Domains separated by semicolon")); + AddOption(new Option(new[] { "--regenerate-access-key", "-regen" }, "Regenerate access key for environment client")); } public new class Handler : BaseCommandHandler, ICommandHandler { private readonly IMediator _mediator; private readonly IOutputService _outputService; + private readonly ILogger _logger; + + public string Name { get; set; } + public string NewName { get; set; } + public string Domains { get; set; } + public bool RegenerateAccessKey { get; set; } public Handler(IMediator mediator, IOutputService outputService) { @@ -24,7 +37,52 @@ public Handler(IMediator mediator, IOutputService outputService) public async Task InvokeAsync(InvocationContext context) { + var environmentClients = await _mediator.Send(new GetEnvironmentClientsRequest()); + + var client = environmentClients.FirstOrDefault(envClient => envClient.Name == Name); + if (client == null) + { + _logger.LogError("Environment client with name: {0} not found", Name); + return 1; + } + + var updateRequest = new UpdateEnvironmentClientRequest(client.Id); + + if (!string.IsNullOrEmpty(NewName)) + { + updateRequest.Name = NewName; + } + + if (!string.IsNullOrEmpty(Domains)) + { + var selectedDomains = Domains.Split(';').Select(domain => domain.Trim()); + var allDomains = await _mediator.Send(new GetDomainsRequest()); + + var allowedDomainIds = new List(); + foreach (var selectedDomain in selectedDomains) + { + var foundDomain = allDomains.FirstOrDefault(domain => domain.Name == selectedDomain); + if (foundDomain == null) + { + _logger.LogError("Domain with name: {0} not found", selectedDomain); + return 1; + } + + allowedDomainIds.Add(foundDomain.Id.IdValue); + } + + updateRequest.AllowedDomains = allowedDomainIds.ToArray(); + } + + if (RegenerateAccessKey) + { + updateRequest.RegenerateAccessKey = true; + } + + var response = await _mediator.Send(updateRequest); + _outputService.Write(response); + return 0; } } -} \ No newline at end of file +} diff --git a/src/Enterspeed.Cli/Commands/Source/CreateSourceCommand.cs b/src/Enterspeed.Cli/Commands/Source/CreateSourceCommand.cs new file mode 100644 index 0000000..5a21543 --- /dev/null +++ b/src/Enterspeed.Cli/Commands/Source/CreateSourceCommand.cs @@ -0,0 +1,48 @@ +using Enterspeed.Cli.Services.ConsoleOutput; +using MediatR; +using System.CommandLine.Invocation; +using System.CommandLine; +using Enterspeed.Cli.Api.Source; +using Enterspeed.Cli.Domain.Models; + +namespace Enterspeed.Cli.Commands.Source; +public class CreateSourceCommand : Command +{ + public CreateSourceCommand() : base(name: "create", "Create source in source group") + { + AddArgument(new Argument("id", "Id of the source group") { Arity = ArgumentArity.ExactlyOne }); + AddOption(new Option(new[] { "--name", "-n" }, "Name of source group") { IsRequired = true }); + AddOption(new Option(new[] { "--type", "-t" }, "Source group type")); + } + + public new class Handler : BaseCommandHandler, ICommandHandler + { + private readonly IMediator _mediator; + private readonly IOutputService _outputService; + + public Guid Id { get; set; } + public string Name { get; set; } + public string Type { get; set; } + + public Handler(IMediator mediator, IOutputService outputService) + { + _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + _outputService = outputService; + } + + public async Task InvokeAsync(InvocationContext context) + { + var request = new CreateSourceRequest(SourceGroupId.Parse(SourceGroupId.From(Id.ToString()))) + { + Name = Name, + Type = Type + }; + + var domain = await _mediator.Send(request); + + _outputService.Write(domain); + + return 0; + } + } +} \ No newline at end of file diff --git a/src/Enterspeed.Cli/Commands/Source/DeleteSourceCommand.cs b/src/Enterspeed.Cli/Commands/Source/DeleteSourceCommand.cs new file mode 100644 index 0000000..12b6fc0 --- /dev/null +++ b/src/Enterspeed.Cli/Commands/Source/DeleteSourceCommand.cs @@ -0,0 +1,44 @@ +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.ConsoleOutput; +using MediatR; +using System.CommandLine.Invocation; +using System.CommandLine; +using Enterspeed.Cli.Api.Source; + +namespace Enterspeed.Cli.Commands.Source; + +public class DeleteSourceCommand : ConfirmCommand +{ + public DeleteSourceCommand() : base(name: "delete", "Delete source") + { + AddArgument(new Argument("id", "Id of the source group") { Arity = ArgumentArity.ExactlyOne }); + } + + public new class Handler : BaseCommandHandler, ICommandHandler + { + private readonly IMediator _mediator; + private readonly IOutputService _outputService; + + public Guid Id { get; set; } + + + public Handler(IMediator mediator, IOutputService outputService) + { + _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + _outputService = outputService; + } + + public async Task InvokeAsync(InvocationContext context) + { + if (!Yes && !GetConfirmation()) + { + return 0; + } + + var response = await _mediator.Send(new DeleteSourceRequest(SourceId.Parse(SourceId.From(Id.ToString())))); + _outputService.Write(response); + + return 0; + } + } +} \ No newline at end of file diff --git a/src/Enterspeed.Cli/Commands/Source/SourceCommands.cs b/src/Enterspeed.Cli/Commands/Source/SourceCommands.cs new file mode 100644 index 0000000..36fa5d4 --- /dev/null +++ b/src/Enterspeed.Cli/Commands/Source/SourceCommands.cs @@ -0,0 +1,17 @@ +using System.CommandLine; + +namespace Enterspeed.Cli.Commands.Source; + +public static class SourceCommands +{ + public static Command BuildCommands() + { + var command = new Command("source", "Source") + { + new CreateSourceCommand(), + new UpdateSourceCommand(), + new DeleteSourceCommand() + }; + return command; + } +} diff --git a/src/Enterspeed.Cli/Commands/Source/UpdateSourceCommand.cs b/src/Enterspeed.Cli/Commands/Source/UpdateSourceCommand.cs new file mode 100644 index 0000000..90201e8 --- /dev/null +++ b/src/Enterspeed.Cli/Commands/Source/UpdateSourceCommand.cs @@ -0,0 +1,60 @@ +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.ConsoleOutput; +using MediatR; +using System.CommandLine.Invocation; +using System.CommandLine; +using Enterspeed.Cli.Api.Source; + +namespace Enterspeed.Cli.Commands.Source; + +public class UpdateSourceCommand : Command +{ + public UpdateSourceCommand() : base(name: "update", "Update source") + { + AddArgument(new Argument("id", "Id of the source group") { Arity = ArgumentArity.ExactlyOne }); + AddOption(new Option(new[] { "--name", "-n" }, "Name of source group")); + AddOption(new Option(new[] { "--type", "-t" }, "Source group type")); + AddOption(new Option(new[] { "--regenerate-access-key", "-regen" }, "Regenerate access key")); + } + + public new class Handler : BaseCommandHandler, ICommandHandler + { + private readonly IMediator _mediator; + private readonly IOutputService _outputService; + + public Guid Id { get; set; } + public string Name { get; set; } + public string Type { get; set; } + public bool RegenerateAccessKey { get; set; } + + + public Handler(IMediator mediator, IOutputService outputService) + { + _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + _outputService = outputService; + } + + public async Task InvokeAsync(InvocationContext context) + { + var request = new UpdateSourceRequest(SourceId.Parse(SourceId.From(Id.ToString()))) + { + RegenerateAccessKey = RegenerateAccessKey + }; + + if (!string.IsNullOrEmpty(Name)) + { + request.Name = Name; + } + + if (!string.IsNullOrEmpty(Type)) + { + request.Type = Type; + } + + var response = await _mediator.Send(request); + + _outputService.Write(response); + return 0; + } + } +} \ No newline at end of file diff --git a/src/Enterspeed.Cli/Commands/SourceGroup/CreateSourceGroupCommand.cs b/src/Enterspeed.Cli/Commands/SourceGroup/CreateSourceGroupCommand.cs new file mode 100644 index 0000000..4b010bc --- /dev/null +++ b/src/Enterspeed.Cli/Commands/SourceGroup/CreateSourceGroupCommand.cs @@ -0,0 +1,49 @@ +using Enterspeed.Cli.Services.ConsoleOutput; +using MediatR; +using System.CommandLine; +using System.CommandLine.Invocation; +using Enterspeed.Cli.Api.SourceGroup; + +namespace Enterspeed.Cli.Commands.SourceGroup; + +public class CreateSourceGroupCommand : Command +{ + public CreateSourceGroupCommand() : base(name: "create", "Create source group") + { + AddOption(new Option(new[] { "--name", "-n" }, "Name of source group") { IsRequired = true }); + AddOption(new Option(new[] { "--alias", "-a" }, "Alias of source group") { IsRequired = true }); + AddOption(new Option(new[] { "--type", "-t" }, "Source group type")); + } + + public new class Handler : BaseCommandHandler, ICommandHandler + { + private readonly IMediator _mediator; + private readonly IOutputService _outputService; + public string Name { get; set; } + public string Alias { get; set; } + public string Type { get; set; } + + public Handler(IMediator mediator, IOutputService outputService) + { + _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + _outputService = outputService; + } + + public async Task InvokeAsync(InvocationContext context) + { + var request = new CreateSourceGroupRequest + { + Name = Name, + Alias = Alias, + Type = Type + }; + + var domain = await _mediator.Send(request); + + _outputService.Write(domain); + + return 0; + } + } + +} \ No newline at end of file diff --git a/src/Enterspeed.Cli/Commands/SourceGroup/DeleteSourceGroupCommand.cs b/src/Enterspeed.Cli/Commands/SourceGroup/DeleteSourceGroupCommand.cs new file mode 100644 index 0000000..ca268f3 --- /dev/null +++ b/src/Enterspeed.Cli/Commands/SourceGroup/DeleteSourceGroupCommand.cs @@ -0,0 +1,44 @@ +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.ConsoleOutput; +using MediatR; +using System.CommandLine.Invocation; +using System.CommandLine; +using Enterspeed.Cli.Api.SourceGroup; + +namespace Enterspeed.Cli.Commands.SourceGroup; + +public class DeleteSourceGroupCommand : ConfirmCommand +{ + public DeleteSourceGroupCommand() : base(name: "delete", "Delete source group") + { + AddArgument(new Argument("id", "Id of the source group") { Arity = ArgumentArity.ExactlyOne }); + } + + public new class Handler : BaseCommandHandler, ICommandHandler + { + private readonly IMediator _mediator; + private readonly IOutputService _outputService; + + public Guid Id { get; set; } + + + public Handler(IMediator mediator, IOutputService outputService) + { + _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + _outputService = outputService; + } + + public async Task InvokeAsync(InvocationContext context) + { + if (!Yes && !GetConfirmation()) + { + return 0; + } + + var response = await _mediator.Send(new DeleteSourceGroupRequest(SourceGroupId.Parse(SourceGroupId.From(Id.ToString())))); + _outputService.Write(response); + + return 0; + } + } +} \ No newline at end of file diff --git a/src/Enterspeed.Cli/Commands/SourceGroup/ListSourceGroupsCommand.cs b/src/Enterspeed.Cli/Commands/SourceGroup/ListSourceGroupsCommand.cs index 7e53009..3f5f1d6 100644 --- a/src/Enterspeed.Cli/Commands/SourceGroup/ListSourceGroupsCommand.cs +++ b/src/Enterspeed.Cli/Commands/SourceGroup/ListSourceGroupsCommand.cs @@ -6,9 +6,9 @@ namespace Enterspeed.Cli.Commands.SourceGroup; -public class ListSourceGroupsCommand : Command +public class ListSourceGroupCommand : Command { - public ListSourceGroupsCommand() : base(name: "list", "List source groups") + public ListSourceGroupCommand() : base(name: "list", "List source groups") { } diff --git a/src/Enterspeed.Cli/Commands/SourceGroup/SourceGroupCommands.cs b/src/Enterspeed.Cli/Commands/SourceGroup/SourceGroupCommands.cs index 3a620fe..bc151d2 100644 --- a/src/Enterspeed.Cli/Commands/SourceGroup/SourceGroupCommands.cs +++ b/src/Enterspeed.Cli/Commands/SourceGroup/SourceGroupCommands.cs @@ -6,9 +6,12 @@ public static class SourceGroupCommands { public static Command BuildCommands() { - var command = new Command("source-groups", "Source groups") + var command = new Command("source-group", "Source group") { - new ListSourceGroupsCommand() + new ListSourceGroupCommand(), + new CreateSourceGroupCommand(), + new UpdateSourceGroupCommand(), + new DeleteSourceGroupCommand() }; return command; } diff --git a/src/Enterspeed.Cli/Commands/SourceGroup/UpdateSourceGroupCommand.cs b/src/Enterspeed.Cli/Commands/SourceGroup/UpdateSourceGroupCommand.cs new file mode 100644 index 0000000..c05f48f --- /dev/null +++ b/src/Enterspeed.Cli/Commands/SourceGroup/UpdateSourceGroupCommand.cs @@ -0,0 +1,54 @@ +using Enterspeed.Cli.Domain.Models; +using Enterspeed.Cli.Services.ConsoleOutput; +using MediatR; +using System.CommandLine; +using System.CommandLine.Invocation; +using Enterspeed.Cli.Api.SourceGroup; + +namespace Enterspeed.Cli.Commands.SourceGroup; + +public class UpdateSourceGroupCommand : Command +{ + public UpdateSourceGroupCommand() : base(name: "update", "Update source group") + { + AddArgument(new Argument("id", "Id of the source group") { Arity = ArgumentArity.ExactlyOne }); + AddOption(new Option(new[] { "--name", "-n" }, "Name of source group")); + AddOption(new Option(new[] { "--type", "-t" }, "Source group type")); + } + + public new class Handler : BaseCommandHandler, ICommandHandler + { + private readonly IMediator _mediator; + private readonly IOutputService _outputService; + + public Guid Id { get; set; } + public string Name { get; set; } + public string Type { get; set; } + + + public Handler(IMediator mediator, IOutputService outputService) + { + _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + _outputService = outputService; + } + + public async Task InvokeAsync(InvocationContext context) + { + var request = new UpdateSourceGroupRequest(SourceGroupId.Parse(SourceGroupId.From(Id.ToString()))); + if (!string.IsNullOrEmpty(Name)) + { + request.Name = Name; + } + + if (!string.IsNullOrEmpty(Type)) + { + request.Type = Type; + } + + var response = await _mediator.Send(request); + + _outputService.Write(response); + return 0; + } + } +} \ No newline at end of file diff --git a/src/Enterspeed.Cli/Domain/Models/EnvironmentClientId.cs b/src/Enterspeed.Cli/Domain/Models/EnvironmentClientId.cs index 50e56b0..0352e2a 100644 --- a/src/Enterspeed.Cli/Domain/Models/EnvironmentClientId.cs +++ b/src/Enterspeed.Cli/Domain/Models/EnvironmentClientId.cs @@ -48,9 +48,9 @@ public static EnvironmentClientId Parse(string environmentClientId) var guid = GetValidatedGuid(guidAsString); - return new EnvironmentClientId(From(environment.EnvironmentGuid, guid.ToString())) + return new EnvironmentClientId(From(environment.EnvironmentGuid.ToString(), guid.ToString())) { - EnvironmentGuid = environment.EnvironmentGuid, + EnvironmentGuid = environment.EnvironmentGuid.ToString(), ClientGuid = guid.ToString() }; } diff --git a/src/Enterspeed.Cli/Domain/Models/EnvironmentId.cs b/src/Enterspeed.Cli/Domain/Models/EnvironmentId.cs index 95e82ca..876028d 100644 --- a/src/Enterspeed.Cli/Domain/Models/EnvironmentId.cs +++ b/src/Enterspeed.Cli/Domain/Models/EnvironmentId.cs @@ -9,7 +9,7 @@ public EnvironmentId(string idValue) { } - public string EnvironmentGuid { get; set; } + public Guid EnvironmentGuid { get; set; } public static EnvironmentId Default() => Parse(From(Guid.Empty.ToString())); @@ -50,7 +50,7 @@ public static EnvironmentId Parse(string environmentId) return new EnvironmentId(From(guid.ToString())) { - EnvironmentGuid = guid.ToString() + EnvironmentGuid = guid }; } } \ No newline at end of file diff --git a/src/Enterspeed.Cli/Domain/Models/RouteHandleId.cs b/src/Enterspeed.Cli/Domain/Models/RouteHandleId.cs index 138886a..594fa27 100644 --- a/src/Enterspeed.Cli/Domain/Models/RouteHandleId.cs +++ b/src/Enterspeed.Cli/Domain/Models/RouteHandleId.cs @@ -55,9 +55,9 @@ public static RouteHandleId Parse(string routeId) throw new InvalidIdFormatException("Missing handle"); } - return new RouteHandleId(From(environment.EnvironmentGuid, handle)) + return new RouteHandleId(From(environment.EnvironmentGuid.ToString(), handle)) { - EnvironmentGuid = environment.EnvironmentGuid, + EnvironmentGuid = environment.EnvironmentGuid.ToString(), Handle = handle }; } diff --git a/src/Enterspeed.Cli/Domain/Models/RouteUrlId.cs b/src/Enterspeed.Cli/Domain/Models/RouteUrlId.cs index 87cdfd2..6a8e82f 100644 --- a/src/Enterspeed.Cli/Domain/Models/RouteUrlId.cs +++ b/src/Enterspeed.Cli/Domain/Models/RouteUrlId.cs @@ -63,9 +63,9 @@ public static RouteUrlId Parse(string routeId) throw new InvalidIdFormatException("Missing url"); } - return new RouteUrlId(From(environment.EnvironmentGuid, domainGuid, urlPath)) + return new RouteUrlId(From(environment.EnvironmentGuid.ToString(), domainGuid, urlPath)) { - EnvironmentGuid = environment.EnvironmentGuid, + EnvironmentGuid = environment.EnvironmentGuid.ToString(), UrlPath = urlPath, DomainGuid = domainGuid }; diff --git a/src/Enterspeed.Cli/Domain/Models/ViewId.cs b/src/Enterspeed.Cli/Domain/Models/ViewId.cs index a169efc..12fe6fd 100644 --- a/src/Enterspeed.Cli/Domain/Models/ViewId.cs +++ b/src/Enterspeed.Cli/Domain/Models/ViewId.cs @@ -58,9 +58,9 @@ public static ViewId Parse(string viewId) ValidateOrder(gidValues, "Environment", "Source", "Entity", "View"); - return new ViewId(From(environment.EnvironmentGuid, sourceEntity.SourceGuid, sourceEntity.OriginId, viewHandle)) + return new ViewId(From(environment.EnvironmentGuid.ToString(), sourceEntity.SourceGuid, sourceEntity.OriginId, viewHandle)) { - EnvironmentGuid = environment.EnvironmentGuid, + EnvironmentGuid = environment.EnvironmentGuid.ToString(), SourceGuid = sourceEntity.SourceGuid, OriginId = sourceEntity.OriginId, ViewHandle = viewHandle diff --git a/src/Enterspeed.Cli/Extensions/ServiceCollectionExtensions.cs b/src/Enterspeed.Cli/Extensions/ServiceCollectionExtensions.cs index ae1ee50..83b262a 100644 --- a/src/Enterspeed.Cli/Extensions/ServiceCollectionExtensions.cs +++ b/src/Enterspeed.Cli/Extensions/ServiceCollectionExtensions.cs @@ -30,7 +30,11 @@ public static LoggerConfiguration ConfigureSerilog(this LoggerConfiguration logg var logEventLevel = verbose ? LogEventLevel.Verbose : LogEventLevel.Warning; loggerConfiguration.MinimumLevel.Override("Microsoft", logEventLevel); - loggerConfiguration.WriteTo.File(new CompactJsonFormatter(), "log.json", logEventLevel); + + var userFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + var logFilePath = Path.Combine(userFolderPath, ".enterspeed", "cli.log.json"); + + loggerConfiguration.WriteTo.File(new CompactJsonFormatter(), logFilePath, logEventLevel); loggerConfiguration.WriteTo.Console(logEventLevel); loggerConfiguration.Enrich.WithExceptionDetails(); diff --git a/src/Enterspeed.Cli/Program.cs b/src/Enterspeed.Cli/Program.cs index 37b5abd..47ea2d6 100644 --- a/src/Enterspeed.Cli/Program.cs +++ b/src/Enterspeed.Cli/Program.cs @@ -1,5 +1,4 @@ -using System.Buffers; -using System.CommandLine; +using System.CommandLine; using System.CommandLine.Builder; using System.CommandLine.Hosting; using System.CommandLine.Parsing; @@ -9,6 +8,7 @@ using Enterspeed.Cli.Commands.EnvironmentClient; using Enterspeed.Cli.Commands.Login; using Enterspeed.Cli.Commands.Schema; +using Enterspeed.Cli.Commands.Source; using Enterspeed.Cli.Commands.SourceEntity; using Enterspeed.Cli.Commands.SourceGroup; using Enterspeed.Cli.Commands.Tenant; @@ -65,6 +65,7 @@ private static CommandLineBuilder BuildCommandLine() root.AddCommand(EnvironmentClientCommands.BuildCommands()); root.AddCommand(DomainCommands.BuildCommands()); root.AddCommand(SourceGroupCommands.BuildCommands()); + root.AddCommand(SourceCommands.BuildCommands()); root.AddCommand(ViewCommands.BuildCommands()); root.AddCommand(SourceEntityCommands.BuildCommands()); root.AddCommand(SchemaCommands.BuildCommands()); diff --git a/src/Enterspeed.Cli/Services/StateService/StateService.cs b/src/Enterspeed.Cli/Services/StateService/StateService.cs index 0b2585b..33fe663 100644 --- a/src/Enterspeed.Cli/Services/StateService/StateService.cs +++ b/src/Enterspeed.Cli/Services/StateService/StateService.cs @@ -8,11 +8,15 @@ namespace Enterspeed.Cli.Services.StateService; public class StateService : IStateService { private readonly ILogger _logger; - private const string Filename = ".escli-state.json"; + private readonly string _stateFilePath; public StateService(ILogger logger) { _logger = logger; + + var userFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); + _stateFilePath = Path.Combine(userFolderPath, ".enterspeed", "cli.state.json"); + LoadState(); } @@ -38,7 +42,8 @@ public void SaveState(Token token, IdentityUser user) User = user, ActiveTenantId = ActiveTenantId.IdValue }); - File.WriteAllText(Filename, jsonString); + Directory.CreateDirectory(Path.GetDirectoryName(_stateFilePath)!); + File.WriteAllText(_stateFilePath, jsonString); _logger.LogInformation("State saved"); } @@ -62,7 +67,7 @@ private void LoadState() { try { - var jsonString = File.ReadAllText(Filename); + var jsonString = File.ReadAllText(_stateFilePath); var state = JsonSerializer.Deserialize(jsonString); if (state != null) { diff --git a/src/Enterspeed.Cli/appsettings.Development.json b/src/Enterspeed.Cli/appsettings.Development.json index 29872e8..43022e1 100644 --- a/src/Enterspeed.Cli/appsettings.Development.json +++ b/src/Enterspeed.Cli/appsettings.Development.json @@ -1,5 +1,5 @@ { "Settings": { - "EnterspeedApiUri": "https://management.dev.enterspeed.io/api/v1/" + //"EnterspeedApiUri": "https://management.dev.enterspeed.io/api/v1/" } }