Swagger Configration and move Agent API Calls to APIController
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
// Get Methoden um Metrics abzugreifen
|
||||
// Get Methden um Informationen über den Status des Servers einzuholen
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using watcher_monitoring.Models;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
using watcher_monitoring.Models;
|
||||
using watcher_monitoring.Data;
|
||||
using watcher_monitoring.Attributes;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using watcher_monitoring.Payloads;
|
||||
|
||||
|
||||
|
||||
namespace watcher_monitoring.Controllers;
|
||||
|
||||
@@ -95,4 +97,106 @@ public class APIController : Controller
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
|
||||
// Agent Calls
|
||||
|
||||
// Registration Endpoint for watcher-agent
|
||||
[HttpPost("agent-register")]
|
||||
public async Task<IActionResult> Register([FromBody] RegistrationDto dto)
|
||||
{
|
||||
// payload check
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
var errors = ModelState.Values
|
||||
.SelectMany(v => v.Errors)
|
||||
.Select(e => e.ErrorMessage)
|
||||
.ToList();
|
||||
|
||||
_logger.LogError("Invalid registration payload");
|
||||
return BadRequest(new { error = "Invalid registration payload", details = errors });
|
||||
}
|
||||
|
||||
try {
|
||||
Server server = new Server
|
||||
{
|
||||
Name = "test",
|
||||
IPAddress = dto.IpAddress
|
||||
};
|
||||
|
||||
_context.Servers.Add(server);
|
||||
await _context.SaveChangesAsync();
|
||||
return Ok();
|
||||
} catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
_logger.LogError(ex.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
|
||||
// Hardware Configuration Endpoint for watcher-agent
|
||||
[HttpPost("agent-hardware")]
|
||||
public async Task<IActionResult> HardwareConfiguration ([FromBody] HardwareDto dto)
|
||||
{
|
||||
// payload check
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
var errors = ModelState.Values
|
||||
.SelectMany(v => v.Errors)
|
||||
.Select(e => e.ErrorMessage)
|
||||
.ToList();
|
||||
|
||||
_logger.LogError("Invalid hardware configuration");
|
||||
return BadRequest(new { error = "Invalid Hardware Configuration Payload", details = errors });
|
||||
}
|
||||
try
|
||||
{
|
||||
// Find Server in Database
|
||||
var server = await _context.Servers.FindAsync(dto.Id);
|
||||
|
||||
// Add Hardware Configuration
|
||||
try
|
||||
{
|
||||
server.CpuType = dto.CpuType;
|
||||
server.CpuCores = dto.CpuCores;
|
||||
server.GpuType = dto.GpuType;
|
||||
server.RamSize = dto.RamSize;
|
||||
// Diskspace fehlt
|
||||
|
||||
_logger.LogInformation("Harware configuration successfull for server {server}", server.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
}
|
||||
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
// Server-Metrics endpoint for watcher-agent
|
||||
[HttpPost("agent-server-metrics/{id}")]
|
||||
public async Task<IActionResult> ServerMetrics ([FromBody] HardwareDto dto)
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
|
||||
// Service-Detection endpoint for watcher-agent
|
||||
[HttpPost("agent-container-detection")]
|
||||
public async Task<IActionResult> ContainerDetection ([FromBody] HardwareDto dto)
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
|
||||
// Container-Metrics endpoint for watcher-agent
|
||||
[HttpPost("agent-container-metrics/{id}")]
|
||||
public async Task<IActionResult> ContainerMetrics ([FromBody] HardwareDto dto)
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
using watcher_monitoring.Payloads;
|
||||
using watcher_monitoring.Data;
|
||||
using watcher_monitoring.Models;
|
||||
using Microsoft.AspNetCore.Authorization.Infrastructure;
|
||||
|
||||
namespace watcher_monitoring.Controllers;
|
||||
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
public class MonitoringController : Controller
|
||||
{
|
||||
@@ -20,104 +19,10 @@ public class MonitoringController : Controller
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
// Registration Endpoint for watcher-agent
|
||||
[HttpPost("register")]
|
||||
public async Task<IActionResult> Register([FromBody] RegistrationDto dto)
|
||||
[HttpGet("container")]
|
||||
public async Task<IActionResult> ContainerIndex()
|
||||
{
|
||||
// payload check
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
var errors = ModelState.Values
|
||||
.SelectMany(v => v.Errors)
|
||||
.Select(e => e.ErrorMessage)
|
||||
.ToList();
|
||||
|
||||
_logger.LogError("Invalid registration payload");
|
||||
return BadRequest(new { error = "Invalid registration payload", details = errors });
|
||||
}
|
||||
|
||||
try {
|
||||
Server server = new Server
|
||||
{
|
||||
Name = "test",
|
||||
IPAddress = dto.IpAddress
|
||||
};
|
||||
|
||||
_context.Servers.Add(server);
|
||||
await _context.SaveChangesAsync();
|
||||
return Ok();
|
||||
} catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
_logger.LogError(ex.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
}
|
||||
|
||||
// Hardware Configuration Endpoint for watcher-agent
|
||||
[HttpPost("hardware-configuration")]
|
||||
public async Task<IActionResult> HardwareConfiguration ([FromBody] HardwareDto dto)
|
||||
{
|
||||
// payload check
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
var errors = ModelState.Values
|
||||
.SelectMany(v => v.Errors)
|
||||
.Select(e => e.ErrorMessage)
|
||||
.ToList();
|
||||
|
||||
_logger.LogError("Invalid hardware configuration");
|
||||
return BadRequest(new { error = "Invalid Hardware Configuration Payload", details = errors });
|
||||
}
|
||||
try
|
||||
{
|
||||
// Find Server in Database
|
||||
var server = await _context.Servers.FindAsync(dto.Id);
|
||||
|
||||
// Add Hardware Configuration
|
||||
try
|
||||
{
|
||||
server.CpuType = dto.CpuType;
|
||||
server.CpuCores = dto.CpuCores;
|
||||
server.GpuType = dto.GpuType;
|
||||
server.RamSize = dto.RamSize;
|
||||
// Diskspace fehlt
|
||||
|
||||
_logger.LogInformation("Harware configuration successfull for server {server}", server.Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message);
|
||||
}
|
||||
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
// Server-Metrics endpoint for watcher-agent
|
||||
[HttpPost("server-metrics/{id}")]
|
||||
public async Task<IActionResult> ServerMetrics ([FromBody] HardwareDto dto)
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
|
||||
// Service-Detection endpoint for watcher-agent
|
||||
[HttpPost("container-detection")]
|
||||
public async Task<IActionResult> ContainerDetection ([FromBody] HardwareDto dto)
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
|
||||
// Container-Metrics endpoint for watcher-agent
|
||||
[HttpPost("container-metrics/{id}")]
|
||||
public async Task<IActionResult> ContainerMetrics ([FromBody] HardwareDto dto)
|
||||
{
|
||||
return Ok();
|
||||
return View();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.OpenApi.Models;
|
||||
|
||||
@@ -65,6 +66,17 @@ builder.Services.AddHealthChecks();
|
||||
builder.Services.AddSwaggerGen(options =>
|
||||
{
|
||||
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Watcher-Server API", Version = "v1" });
|
||||
|
||||
// Nur API-Controller dokumentieren (mit [ApiController]-Attribut)
|
||||
options.DocInclusionPredicate((docName, apiDesc) =>
|
||||
{
|
||||
var controllerActionDescriptor = apiDesc.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;
|
||||
if (controllerActionDescriptor == null) return false;
|
||||
|
||||
// Nur Controller mit [ApiController]-Attribut einbeziehen
|
||||
return controllerActionDescriptor.ControllerTypeInfo
|
||||
.GetCustomAttributes(typeof(ApiControllerAttribute), true).Any();
|
||||
});
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
0
watcher-monitoring/Views/Monitoring/Index.cshtml
Normal file
0
watcher-monitoring/Views/Monitoring/Index.cshtml
Normal file
Reference in New Issue
Block a user