tgstation-server 5.12.7
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ComponentInterfacingController.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4
5using Microsoft.AspNetCore.Mvc;
6using Microsoft.Extensions.Logging;
7using Serilog.Context;
8
15
17{
22 {
27
32
37
47 IDatabaseContext databaseContext,
48 IAuthenticationContextFactory authenticationContextFactory,
49 ILogger<ComponentInterfacingController> logger,
51 bool useInstanceRequestHeader = false)
52 : base(
53 databaseContext,
54 authenticationContextFactory,
55 logger,
56 true)
57 {
58 this.instanceManager = instanceManager ?? throw new ArgumentNullException(nameof(instanceManager));
59 this.useInstanceRequestHeader = useInstanceRequestHeader;
60 }
61
63 protected override async Task<IActionResult> ValidateRequest(CancellationToken cancellationToken)
64 {
66 return null;
67
68 if (!ApiHeaders.InstanceId.HasValue)
69 return BadRequest(new ErrorMessageResponse(ErrorCode.InstanceHeaderRequired));
70
72 return Forbid();
73
75 await DatabaseContext.Save(cancellationToken);
76
77 using var instanceReferenceCheck = instanceManager.GetInstanceReference(Instance);
78 if (instanceReferenceCheck == null)
79 return Conflict(new ErrorMessageResponse(ErrorCode.InstanceOffline));
80
81 return null;
82 }
83
89 protected bool ValidateInstanceOnlineStatus(Api.Models.Instance metadata)
90 {
91 ArgumentNullException.ThrowIfNull(metadata);
92
93 bool online;
94 using (var instanceReferenceCheck = instanceManager.GetInstanceReference(metadata))
95 online = instanceReferenceCheck != null;
96
97 if (metadata.Online.Value == online)
98 return false;
99
100 const string OfflineWord = "offline";
101 const string OnlineWord = "online";
102
103 Logger.LogWarning(
104 "Instance {instanceId} is says it's {databaseState} in the database, but it is actually {serviceState} in the service. Updating the database to reflect this...",
105 metadata.Id,
106 online ? OfflineWord : OnlineWord,
107 online ? OnlineWord : OfflineWord);
108
109 metadata.Online = online;
110 return true;
111 }
112
120 protected async Task<IActionResult> WithComponentInstance(Func<IInstanceCore, Task<IActionResult>> action, Models.Instance instance = null)
121 {
122 ArgumentNullException.ThrowIfNull(action);
123
124 instance ??= Instance;
125
126 using var instanceReference = instanceManager.GetInstanceReference(instance);
127 using (LogContext.PushProperty(SerilogContextHelper.InstanceReferenceContextProperty, instanceReference.Uid))
128 {
129 if (instanceReference == null)
130 return Conflict(new ErrorMessageResponse(ErrorCode.InstanceOffline));
131 return await action(instanceReference);
132 }
133 }
134 }
135}
Represents the header that must be present for every server request.
Definition: ApiHeaders.cs:22
long? InstanceId
The instance EntityId.Id being accessed.
Definition: ApiHeaders.cs:71
Metadata about a server instance.
Definition: Instance.cs:9
Represents an error message returned by the server.
Base Controller for API functions.
Models.Instance Instance
The Instance for the operation.
ILogger< ApiController > Logger
The ILogger for the ApiController.
override async Task< IActionResult > ValidateRequest(CancellationToken cancellationToken)
Performs validation a request. A Task<TResult> resulting in an appropriate IActionResult on validatio...
ComponentInterfacingController(IDatabaseContext databaseContext, IAuthenticationContextFactory authenticationContextFactory, ILogger< ComponentInterfacingController > logger, IInstanceManager instanceManager, bool useInstanceRequestHeader=false)
Initializes a new instance of the ComponentInterfacingController class.
async Task< IActionResult > WithComponentInstance(Func< IInstanceCore, Task< IActionResult > > action, Models.Instance instance=null)
Run a given action with the relevant IInstance.
readonly bool useInstanceRequestHeader
If the Api.ApiHeaders.InstanceId header should be checked and used to perform validation for every re...
IInstanceOperations InstanceOperations
Access the IInstanceOperations instance.
readonly IInstanceManager instanceManager
The IInstanceManager for the ComponentInterfacingController.
bool ValidateInstanceOnlineStatus(Api.Models.Instance metadata)
Corrects discrepencies between the Api.Models.Instance.Online status of IInstances in the database vs...
Backend abstract implementation of IDatabaseContext.
Task Save(CancellationToken cancellationToken)
Saves changes made to the IDatabaseContext. A Task representing the running operation.
InstancePermissionSet InstancePermissionSet
The User's effective Models.InstancePermissionSet if applicable.
Helpers for manipulating the Serilog.Context.LogContext.
const string InstanceReferenceContextProperty
The Serilog.Context.LogContext property name for Components.IInstanceReference.Uids.
For interacting with the instance services.
IInstanceReference GetInstanceReference(Api.Models.Instance metadata)
Get the IInstanceReference associated with given metadata .
Operations that can be performed on a given Models.Instance.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition: ErrorCode.cs:11