Skip to content

Commit

Permalink
Merge pull request #1046 from tgstation/MoreAPIFixes2 [APIDeploy]
Browse files Browse the repository at this point in the history
More api fixes2
  • Loading branch information
Cyberboss committed Jun 11, 2020
2 parents a13dc63 + 4c008c2 commit 6b46999
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Tgstation.Server.Api/Models/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public enum ErrorCode : uint
/// <summary>
/// Indicates an API upgrade was required by the server.
/// </summary>
[Description("API Mismatch but no current API version provided!")]
[Description("API version mismatch!")]
ApiMismatch,

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,14 @@ public async Task<IActionResult> Read()
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> for the operation.</returns>
/// <response code="202">Update has been started successfully.</response>
/// <response code="410">The requested release version could not be found in the target GitHub repository.</response>
/// <response code="422">Upgrade operations are unavailable due to the launch configuration of TGS.</response>
/// <response code="424">A GitHub rate limit was encountered.</response>
/// <response code="429">A GitHub API error occurred.</response>
[HttpPost]
[TgsAuthorize(AdministrationRights.ChangeVersion)]
[ProducesResponseType(typeof(Administration), 202)]
[ProducesResponseType(typeof(ErrorMessage), 410)]
[ProducesResponseType(typeof(ErrorMessage), 422)]
[ProducesResponseType(typeof(ErrorMessage), 424)]
[ProducesResponseType(typeof(ErrorMessage), 429)]
Expand Down
12 changes: 3 additions & 9 deletions src/Tgstation.Server.Host/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.Extensions.Logging;
using Serilog.Context;
using System;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
Expand Down Expand Up @@ -87,7 +86,7 @@ public ApiController(IDatabaseContext databaseContext, IAuthenticationContextFac
/// Generic 404 response.
/// </summary>
/// <returns>An <see cref="ObjectResult"/> with <see cref="HttpStatusCode.NotFound"/>.</returns>
protected new ObjectResult NotFound() => StatusCode((int)HttpStatusCode.NotFound, new ErrorMessage(ErrorCode.ResourceNeverPresent));
protected new ObjectResult NotFound() => NotFound(new ErrorMessage(ErrorCode.ResourceNeverPresent));

/// <summary>
/// Generic 501 response.
Expand Down Expand Up @@ -190,14 +189,9 @@ public override async Task OnActionExecutionAsync(ActionExecutingContext context
{
if (ApiHeaders != null)
Logger.LogDebug(
"Starting API Request: Version: {1}. User-Agent: {2}",
AuthenticationContext?.User.Id.Value.ToString(CultureInfo.InvariantCulture),
"Starting API Request: Version: {0}. User-Agent: {1}",
ApiHeaders.ApiVersion.Semver(),
ApiHeaders.RawUserAgent,
Request.Method,
Request.Path,
Request.QueryString,
ApiHeaders.InstanceId);
ApiHeaders.RawUserAgent);
await base.OnActionExecutionAsync(context, next).ConfigureAwait(false);
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/Tgstation.Server.Host/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ public async Task<IActionResult> List(CancellationToken cancellationToken)
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> for the operation.</returns>
/// <response code="200">Retrieved <see cref="Api.Models.ChatBot"/> successfully.</response>
/// <response code="410">The <see cref="Api.Models.ChatBot"/> with the given ID does not exist in this instance.</response>
[HttpGet("{id}")]
[TgsAuthorize(ChatBotRights.Read)]
[ProducesResponseType(typeof(Api.Models.ChatBot), 200)]
[ProducesResponseType(typeof(ErrorMessage), 410)]
public async Task<IActionResult> GetId(long id, CancellationToken cancellationToken)
{
var query = DatabaseContext.ChatBots
Expand All @@ -216,11 +218,14 @@ public async Task<IActionResult> GetId(long id, CancellationToken cancellationTo
/// <param name="model">The <see cref="Api.Models.ChatBot"/> update to apply.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> for the operation.</returns>
/// <response code="200">Update applied successfully. <see cref="Api.Models.ChatBot"/> may or may not be returned based on user permissions.</response>
/// <response code="200">Update applied successfully.</response>
/// <response code="204">Update applied successfully. <see cref="Api.Models.ChatBot"/> not returned based on user permissions.</response>
/// <response code="410">The <see cref="Api.Models.ChatBot"/> with the given ID does not exist in this instance.</response>
[HttpPost]
[TgsAuthorize(ChatBotRights.WriteChannels | ChatBotRights.WriteConnectionString | ChatBotRights.WriteEnabled | ChatBotRights.WriteName | ChatBotRights.WriteProvider)]
[ProducesResponseType(200)]
[ProducesResponseType(typeof(Api.Models.ChatBot), 200)]
[ProducesResponseType(204)]
[ProducesResponseType(typeof(ErrorMessage), 410)]
#pragma warning disable CA1502, CA1506 // TODO: Decomplexify
public async Task<IActionResult> Update([FromBody] Api.Models.ChatBot model, CancellationToken cancellationToken)
#pragma warning restore CA1502, CA1506
Expand Down Expand Up @@ -314,7 +319,7 @@ bool CheckModified<T>(Expression<Func<Api.Models.Internal.ChatBot, T>> expressio
return Json(current.ToApi());
}

return Ok();
return NoContent();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,13 @@ public async Task<IActionResult> Update([FromBody] ConfigurationFile model, Canc
/// </summary>
/// <param name="filePath">The path of the file to get</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> for the operation</returns>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> for the operation</returns>>
/// <response code="200">File read successfully.</response>>
/// <response code="410">File does not currently exist.</response>
[HttpGet(Routes.File + "/{*filePath}")]
[TgsAuthorize(ConfigurationRights.Read)]
[ProducesResponseType(typeof(ConfigurationFile), 200)]
[ProducesResponseType(typeof(ErrorMessage), 410)]
public async Task<IActionResult> File(string filePath, CancellationToken cancellationToken)
{
if (ForbidDueToModeConflicts(filePath, out var systemIdentity))
Expand Down Expand Up @@ -153,9 +156,12 @@ public async Task<IActionResult> File(string filePath, CancellationToken cancell
/// <param name="directoryPath">The path of the directory to get</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> for the operation</returns>
/// <response code="200">Directory listed successfully.</response>>
/// <response code="410">Directory does not currently exist.</response>
[HttpGet(Routes.List + "/{*directoryPath}")]
[TgsAuthorize(ConfigurationRights.List)]
[ProducesResponseType(typeof(IReadOnlyList<ConfigurationFile>), 200)]
[ProducesResponseType(typeof(ErrorMessage), 410)]
public async Task<IActionResult> Directory(string directoryPath, CancellationToken cancellationToken)
{
if (ForbidDueToModeConflicts(directoryPath, out var systemIdentity))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ public async Task<IActionResult> Create(CancellationToken cancellationToken)
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> of the operation.</returns>
/// <response code="200">Read <see cref="DreamDaemon"/> information successfully.</response>
/// <response code="410">The database entity for the requested instance could not be retrieved. The instance was likely detached.</response>
[HttpGet]
[TgsAuthorize(DreamDaemonRights.ReadMetadata | DreamDaemonRights.ReadRevision)]
[ProducesResponseType(typeof(DreamDaemon), 200)]
[ProducesResponseType(typeof(ErrorMessage), 410)]
public Task<IActionResult> Read(CancellationToken cancellationToken) => ReadImpl(null, cancellationToken);

/// <summary>
Expand Down Expand Up @@ -171,9 +173,11 @@ public async Task<IActionResult> Delete(CancellationToken cancellationToken)
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> of the operation.</returns>
/// <response code="200">Settings applied successfully.</response>
/// <response code="410">The database entity for the requested instance could not be retrieved. The instance was likely detached.</response>
[HttpPost]
[TgsAuthorize(DreamDaemonRights.SetAutoStart | DreamDaemonRights.SetPorts | DreamDaemonRights.SetSecurity | DreamDaemonRights.SetWebClient | DreamDaemonRights.SoftRestart | DreamDaemonRights.SoftShutdown | DreamDaemonRights.Start | DreamDaemonRights.SetStartupTimeout | DreamDaemonRights.SetHeartbeatInterval)]
[ProducesResponseType(typeof(DreamDaemon), 200)]
[ProducesResponseType(typeof(ErrorMessage), 410)]
#pragma warning disable CA1502 // TODO: Decomplexify
#pragma warning disable CA1506
public async Task<IActionResult> Update([FromBody] DreamDaemon model, CancellationToken cancellationToken)
Expand Down
4 changes: 4 additions & 0 deletions src/Tgstation.Server.Host/Controllers/DreamMakerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ public async Task<IActionResult> Read(CancellationToken cancellationToken)
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> of the request.</returns>
/// <response code="200"><see cref="Api.Models.CompileJob"/> retrieved successfully.</response>
/// <response code="404">Specified <see cref="Api.Models.CompileJob"/> ID does not exist in this instance.</response>
[HttpGet("{id}")]
[TgsAuthorize(DreamMakerRights.CompileJobs)]
[ProducesResponseType(typeof(Api.Models.CompileJob), 200)]
[ProducesResponseType(typeof(ErrorMessage), 404)]
public async Task<IActionResult> GetId(long id, CancellationToken cancellationToken)
{
var compileJob = await DatabaseContext
Expand Down Expand Up @@ -156,10 +158,12 @@ public async Task<IActionResult> Create(CancellationToken cancellationToken)
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> of the request.</returns>
/// <response code="200">Changes applied successfully. The updated <see cref="DreamMaker"/> settings will be returned.</response>
/// <response code="204">Changes applied successfully. The updated <see cref="DreamMaker"/> settings will be not be returned due to permissions.</response>
/// <response code="410">The database entity for the requested instance could not be retrieved. The instance was likely detached.</response>
[HttpPost]
[TgsAuthorize(DreamMakerRights.SetDme | DreamMakerRights.SetApiValidationPort | DreamMakerRights.SetApiValidationPort)]
[ProducesResponseType(typeof(DreamMaker), 200)]
[ProducesResponseType(204)]
[ProducesResponseType(typeof(ErrorMessage), 410)]
public async Task<IActionResult> Update([FromBody] DreamMaker model, CancellationToken cancellationToken)
{
if (model == null)
Expand Down
6 changes: 6 additions & 0 deletions src/Tgstation.Server.Host/Controllers/InstanceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,11 @@ async Task<bool> DirExistsAndIsNotEmpty()
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> of the request.</returns>
/// <response code="204">Instance detatched successfully.</response>
/// <response code="410">The database entity for the requested instance could not be retrieved. The instance was likely detached.</response>
[HttpDelete("{id}")]
[TgsAuthorize(InstanceManagerRights.Delete)]
[ProducesResponseType(204)]
[ProducesResponseType(typeof(ErrorMessage), 410)]
public async Task<IActionResult> Delete(long id, CancellationToken cancellationToken)
{
var originalModel = await DatabaseContext
Expand Down Expand Up @@ -355,10 +357,12 @@ public async Task<IActionResult> Delete(long id, CancellationToken cancellationT
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> of the request.</returns>
/// <response code="200">Instance updated successfully.</response>
/// <response code="202">Instance updated successfully and relocation job created.</response>
/// <response code="410">The database entity for the requested instance could not be retrieved. The instance was likely detached.</response>
[HttpPost]
[TgsAuthorize(InstanceManagerRights.Relocate | InstanceManagerRights.Rename | InstanceManagerRights.SetAutoUpdate | InstanceManagerRights.SetConfiguration | InstanceManagerRights.SetOnline | InstanceManagerRights.SetChatBotLimit)]
[ProducesResponseType(typeof(Api.Models.Instance), 200)]
[ProducesResponseType(typeof(Api.Models.Instance), 202)]
[ProducesResponseType(typeof(ErrorMessage), 410)]
#pragma warning disable CA1502 // TODO: Decomplexify
public async Task<IActionResult> Update([FromBody] Api.Models.Instance model, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -583,9 +587,11 @@ IQueryable<Models.Instance> GetBaseQuery()
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> of the request.</returns>
/// <response code="200">Retrieved <see cref="Api.Models.Instance"/> successfully.</response>
/// <response code="410">The database entity for the requested instance could not be retrieved. The instance was likely detached.</response>
[HttpGet("{id}")]
[TgsAuthorize(InstanceManagerRights.List | InstanceManagerRights.Read)]
[ProducesResponseType(typeof(Api.Models.Instance), 200)]
[ProducesResponseType(typeof(ErrorMessage), 410)]
public async Task<IActionResult> GetId(long id, CancellationToken cancellationToken)
{
var cantList = !AuthenticationContext.User.InstanceManagerRights.Value.HasFlag(InstanceManagerRights.List);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ public async Task<IActionResult> Create([FromBody] Api.Models.InstanceUser model
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> of the request.</returns>
/// <response code="200"><see cref="Api.Models.InstanceUser"/> updated successfully.</response>
/// <response code="410">The requested <see cref="Api.Models.InstanceUser"/> does not currently exist.</response>
[HttpPost]
[TgsAuthorize(InstanceUserRights.WriteUsers)]
[ProducesResponseType(typeof(Api.Models.InstanceUser), 200)]
[ProducesResponseType(typeof(ErrorMessage), 410)]
#pragma warning disable CA1506 // TODO: Decomplexify
public async Task<IActionResult> Update([FromBody] Api.Models.InstanceUser model, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -163,9 +165,11 @@ public async Task<IActionResult> List(CancellationToken cancellationToken)
/// <param name="cancellationToken">The <see cref="CancellationToken"/> for the operation.</param>
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> of the request.</returns>
/// <response code="200">Retrieve <see cref="Api.Models.InstanceUser"/> successfully.</response>
/// <response code="410">The requested <see cref="Api.Models.InstanceUser"/> does not currently exist.</response>
[HttpGet("{id}")]
[TgsAuthorize(InstanceUserRights.ReadUsers)]
[ProducesResponseType(typeof(Api.Models.InstanceUser), 200)]
[ProducesResponseType(typeof(ErrorMessage), 410)]
public async Task<IActionResult> GetId(long id, CancellationToken cancellationToken)
{
// this functions as userId
Expand Down
3 changes: 3 additions & 0 deletions src/Tgstation.Server.Host/Controllers/JobController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ public async Task<IActionResult> List(CancellationToken cancellationToken)
/// <returns>A <see cref="Task{TResult}"/> resulting in the <see cref="IActionResult"/> of the request.</returns>
/// <response code="202"><see cref="Api.Models.Job"/> cancellation requested successfully.</response>
/// <response code="404"><see cref="Api.Models.Job"/> does not exist in this instance.</response>
/// <response code="410"><see cref="Api.Models.Job"/> could not be found in the job manager. Has it already completed?</response>
[HttpDelete("{id}")]
[TgsAuthorize]
[ProducesResponseType(typeof(Api.Models.Job), 202)]
[ProducesResponseType(typeof(ErrorMessage), 404)]
public async Task<IActionResult> Delete(long id, CancellationToken cancellationToken)
{
// don't care if an instance post or not at this point
Expand Down Expand Up @@ -129,6 +131,7 @@ public async Task<IActionResult> Delete(long id, CancellationToken cancellationT
[HttpGet("{id}")]
[TgsAuthorize]
[ProducesResponseType(typeof(Api.Models.Job), 200)]
[ProducesResponseType(typeof(ErrorMessage), 404)]
public async Task<IActionResult> GetId(long id, CancellationToken cancellationToken)
{
var job = await DatabaseContext
Expand Down

0 comments on commit 6b46999

Please sign in to comment.