From d58bb5609e6117b853562128d700c5e60e39c1b0 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 7 Nov 2023 11:51:39 +0000 Subject: [PATCH] Add default route for kiboards space --- .../Models/Settings/KibanaSettingsChanges.cs | 6 +++++- src/KiBoards/Services/KiBoardsKibanaClient.cs | 16 +++++++++++++--- src/KiBoards/Startup.cs | 7 ++++--- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/KiBoards/Models/Settings/KibanaSettingsChanges.cs b/src/KiBoards/Models/Settings/KibanaSettingsChanges.cs index 9d5720c..4bb3f08 100644 --- a/src/KiBoards/Models/Settings/KibanaSettingsChanges.cs +++ b/src/KiBoards/Models/Settings/KibanaSettingsChanges.cs @@ -5,6 +5,10 @@ namespace KiBoards.Models.Settings class KibanaSettingsChanges { [JsonPropertyName("theme:darkMode")] - public bool? ThemeDarkMode { get; set; } + public bool? ThemeDarkMode { get; set; } = null; + + [JsonPropertyName("defaultRoute")] + public string DefaultRoute { get; set; } = null; + } } \ No newline at end of file diff --git a/src/KiBoards/Services/KiBoardsKibanaClient.cs b/src/KiBoards/Services/KiBoardsKibanaClient.cs index 3ec00f3..4ad6867 100644 --- a/src/KiBoards/Services/KiBoardsKibanaClient.cs +++ b/src/KiBoards/Services/KiBoardsKibanaClient.cs @@ -19,14 +19,24 @@ public KiBoardsKibanaClient(Uri kibanaUri, HttpClient httpClinet) _httpClient.DefaultRequestHeaders.Add("kbn-xsrf", "true"); } - public async Task SetDarkModeAsync(bool darkMode, CancellationToken cancellationToken) + private string GetSpaceBaseUrl(string spaceId) => !string.IsNullOrEmpty(spaceId) ? $"/s/{spaceId.ToLower()}" : string.Empty; + + public async Task SetDarkModeAsync(bool darkMode, string spaceId, CancellationToken cancellationToken) { var content = JsonContent.Create(new KibanaSettingsRequest() { Changes = new KibanaSettingsChanges() { ThemeDarkMode = darkMode } }); - var response = await _httpClient.PostAsync("api/kibana/settings", content); + var response = await _httpClient.PostAsync($"{GetSpaceBaseUrl(spaceId)}/api/kibana/settings", content); response.EnsureSuccessStatusCode(); } + public async Task TrySetDefaultRoute(string defaultRoute, string spaceId, CancellationToken cancellationToken) + { + var content = JsonContent.Create(new KibanaSettingsRequest() { Changes = new KibanaSettingsChanges() { DefaultRoute = defaultRoute } }); + var response = await _httpClient.PostAsync($"{GetSpaceBaseUrl(spaceId)}/api/kibana/settings", content); + return response.IsSuccessStatusCode; + } + + public async Task ImportSavedObjectsAsync(string ndjsonFile, string spaceId) => await ImportSavedObjectsAsync(ndjsonFile, spaceId, false, CancellationToken.None); public async Task ImportSavedObjectsAsync(string ndjsonFile, string spaceId, bool overwrite) => await ImportSavedObjectsAsync(ndjsonFile, spaceId, overwrite, CancellationToken.None); public async Task ImportSavedObjectsAsync(string ndjsonFile, string spaceId, bool overwrite, CancellationToken cancellationToken) @@ -35,7 +45,7 @@ public async Task ImportSavedObjectsAsync(string ndjsonFi var streamContent = new StreamContent(File.Open(ndjsonFile, FileMode.Open)); multipartContent.Add(streamContent, "file", ndjsonFile); - var response = await _httpClient.PostAsync($"{(spaceId != null ? $"/s/{spaceId.ToLower()}" : "")}/api/saved_objects/_import?overwrite={overwrite.ToString().ToLower()}", multipartContent); + var response = await _httpClient.PostAsync($"{GetSpaceBaseUrl(spaceId)}/api/saved_objects/_import?overwrite={overwrite.ToString().ToLower()}", multipartContent); response.EnsureSuccessStatusCode(); var result = await response.Content.ReadFromJsonAsync(new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }, cancellationToken); diff --git a/src/KiBoards/Startup.cs b/src/KiBoards/Startup.cs index 3350442..8ae9932 100644 --- a/src/KiBoards/Startup.cs +++ b/src/KiBoards/Startup.cs @@ -50,9 +50,10 @@ public Startup(IMessageSink messageSink) var result = await kibanaClient.TryCreateSpaceAsync(Space.KiBoards); if (result) - { messageSink.WriteMessage($"KiBoards space created successfully."); - } + + await kibanaClient.TrySetDefaultRoute("/app/dashboards", Space.KiBoards.Id, CancellationToken.None); + var ndjsonFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), attribute.SearchPattern); @@ -73,7 +74,7 @@ public Startup(IMessageSink messageSink) messageSink.WriteMessage("Warning: Some objects were not imported. Please ensure proper import order based on their dependencies."); } }); - } + } } } }