diff --git a/Tests/servicediscovery.py b/Tests/servicediscovery.py index b7634ae..dcf2df8 100644 --- a/Tests/servicediscovery.py +++ b/Tests/servicediscovery.py @@ -1,12 +1,11 @@ import json import urllib.request -url = "http://localhost:5000/monitoring/ServiceDetection" +url = "http://localhost:5000/monitoring/service-discovery" payload = { - "server_id": 2, - "containers": [ - {"ContainerId": "hr1nm432143nkj", "Name": "Name des Containers", "Image": "ghcr.io/test/container:latest"} - ] + "server_id": 7, + "containers": ["{\"id\":\"6621c5b67c25\",\"image\":\"git.triggermeelmo.com/donpat1to/watcher-agent:v0.1.26\",\"name\":\"watcher-agent\"}", + "{\"id\":\"b8c86fb260bd\",\"image\":\"git.triggermeelmo.com/watcher/watcher-server:v0.1.10\",\"name\":\"watcher\"}"] } data = json.dumps(payload).encode("utf-8") diff --git a/Watcher/Controllers/MonitoringController.cs b/Watcher/Controllers/MonitoringController.cs index ad7ef3f..e956ed0 100644 --- a/Watcher/Controllers/MonitoringController.cs +++ b/Watcher/Controllers/MonitoringController.cs @@ -1,10 +1,12 @@ using System.ComponentModel.DataAnnotations; using System.Net; using System.Text.Json; +using System.Text.Json.Serialization; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; @@ -83,13 +85,7 @@ public class MetricDto public class ServiceDto { public required int server_id { get; set; } // Vom Watcher-Server zugewiesene ID des Hosts - public required JsonContent containers { get; set; } -} - -public class ServiceMetricDto -{ - public required JsonContent metrics { get; set; } // Array an Json-Objekten mit den Daten aller Container - + public required List containers { get; set; } } [ApiController] @@ -245,8 +241,33 @@ public class MonitoringController : Controller } // Json zu was brauchbarem machen - string containersJson = await dto.containers.ReadAsStringAsync(); - List newContainers = JsonSerializer.Deserialize>(containersJson) ?? new List(); ; + //string containersJson = await dto.containers.ReadAsStringAsync(); + //List newContainers = JsonSerializer.Deserialize>(containersJson)?? new List();; + + List newContainers = new List(); + + foreach (String s in dto.containers) + { + // JSON zu einem JsonDocument parsen + using JsonDocument doc = JsonDocument.Parse(s); + JsonElement root = doc.RootElement; + + // Einzelne Werte extrahieren + string id = root.GetProperty("id").GetString() ?? ""; + string image = root.GetProperty("image").GetString() ?? ""; + string name = root.GetProperty("name").GetString() ?? ""; + + Container newContainer = new Container + { + ServerId = dto.server_id, + ContainerId = id, + Image = image, + Name = name, + }; + + + newContainers.Add(newContainer); + } foreach (Container c in newContainers) { diff --git a/Watcher/Models/Container.cs b/Watcher/Models/Container.cs index a0a51a4..8dc084f 100644 --- a/Watcher/Models/Container.cs +++ b/Watcher/Models/Container.cs @@ -1,13 +1,20 @@ +using System.Text.Json.Serialization; + namespace Watcher.Models; public class Container { + [JsonPropertyName("id")] public int Id { get; set; } public int ServerId { get; set; } public String? ContainerId { get; set; } + + [JsonPropertyName("image")] public String? Image { get; set; } + + [JsonPropertyName("name")] public String? Name { get; set; } // keine Variable, die vom Agent übergeben wird. Ein container ist immer Running, die Variable dient nur für die Übersicht