From a2f209c378ff7c855e957e34573e511729d713a3 Mon Sep 17 00:00:00 2001 From: Scott Seaton Date: Fri, 4 Apr 2025 09:42:46 -0400 Subject: [PATCH] Adds filter to target specific cluster connectons --- KustoSchemaTools/KustoSchemaHandler.cs | 38 ++++++++++++++++++-------- KustoSchemaTools/Model/Cluster.cs | 1 + 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/KustoSchemaTools/KustoSchemaHandler.cs b/KustoSchemaTools/KustoSchemaHandler.cs index fcd17fc..1b8ef1f 100644 --- a/KustoSchemaTools/KustoSchemaHandler.cs +++ b/KustoSchemaTools/KustoSchemaHandler.cs @@ -25,11 +25,26 @@ public KustoSchemaHandler(ILogger> schemaHandlerLogger, Ya public YamlDatabaseHandlerFactory YamlDatabaseHandlerFactory { get; } public KustoDatabaseHandlerFactory KustoDatabaseHandlerFactory { get; } - public async Task<(string markDown, bool isValid)> GenerateDiffMarkdown(string path, string databaseName) + public Clusters FilterClusters(Clusters clusters, List? includedConnections) { + // Connections may be marked inactive, so we need to filter them out + // However, if includedConnections is provided we should only process those regardless of the IsActive flag + var includedConnectionsIsNullOrEmpty = includedConnections == null || includedConnections.Count == 0; + var filteredConnections = includedConnectionsIsNullOrEmpty + ? clusters.Connections.Where(cluster => cluster.IsActive).ToList() + : clusters.Connections.Where(cluster => includedConnections != null && includedConnections.Any(name => string.Equals(name, cluster.Name, StringComparison.OrdinalIgnoreCase))).ToList(); + + return new Clusters + { + Connections = filteredConnections + }; + } + public async Task<(string markDown, bool isValid)> GenerateDiffMarkdown(string path, string databaseName, List? includedConnections = null) + { var clustersFile = File.ReadAllText(Path.Combine(path, "clusters.yml")); var clusters = Serialization.YamlPascalCaseDeserializer.Deserialize(clustersFile); + clusters = FilterClusters(clusters, includedConnections); var sb = new StringBuilder(); bool isValid = true; @@ -51,7 +66,7 @@ public KustoSchemaHandler(ILogger> schemaHandlerLogger, Ya sb.AppendLine($"# {cluster.Name}/{escapedDbName} ({cluster.Url})"); - if(changes.Count == 0) + if (changes.Count == 0) { sb.AppendLine("No changes detected"); } @@ -64,7 +79,7 @@ public KustoSchemaHandler(ILogger> schemaHandlerLogger, Ya } var scriptSb = new StringBuilder(); - foreach(var script in changes.SelectMany(itm => itm.Scripts).Where(itm => itm.IsValid == true).OrderBy(itm => itm.Order)) + foreach (var script in changes.SelectMany(itm => itm.Scripts).Where(itm => itm.IsValid == true).OrderBy(itm => itm.Order)) { scriptSb.AppendLine(script.Text); } @@ -72,12 +87,10 @@ public KustoSchemaHandler(ILogger> schemaHandlerLogger, Ya Log.LogInformation($"Following scripts will be applied:\n{scriptSb}"); } - foreach(var follower in yamlDb.Followers) + foreach (var follower in yamlDb.Followers) { - Log.LogInformation($"Generating diff markdown for {Path.Combine(path, databaseName)} => {follower.Key}/{follower.Value.DatabaseName}"); - var followerClient = new KustoClient(follower.Key); var oldModel = FollowerLoader.LoadFollower(follower.Value.DatabaseName, followerClient); @@ -90,7 +103,7 @@ public KustoSchemaHandler(ILogger> schemaHandlerLogger, Ya foreach (var change in changes) { sb.AppendLine(change.Markdown); - sb.AppendLine(); + sb.AppendLine(); } } return (sb.ToString(), isValid); @@ -100,14 +113,15 @@ public async Task Import(string path, string databaseName, bool includeColumns) { var clustersFile = File.ReadAllText(Path.Combine(path, "clusters.yml")); var clusters = Serialization.YamlPascalCaseDeserializer.Deserialize(clustersFile); - + clusters = FilterClusters(clusters, null); + var escapedDbName = databaseName.BracketIfIdentifier(); var dbHandler = KustoDatabaseHandlerFactory.Create(clusters.Connections[0].Url, escapedDbName); var db = await dbHandler.LoadAsync(); if (includeColumns == false) { - foreach(var table in db.Tables.Values) + foreach (var table in db.Tables.Values) { table.Columns = new Dictionary(); } @@ -118,16 +132,16 @@ public async Task Import(string path, string databaseName, bool includeColumns) } - public async Task> Apply(string path, string databaseName) + public async Task> Apply(string path, string databaseName, List? includedConnections = null) { var clustersFile = File.ReadAllText(Path.Combine(path, "clusters.yml")); var clusters = Serialization.YamlPascalCaseDeserializer.Deserialize(clustersFile); - + clusters = FilterClusters(clusters, includedConnections); var escapedDbName = databaseName.BracketIfIdentifier(); var yamlHandler = YamlDatabaseHandlerFactory.Create(path, databaseName); var yamlDb = await yamlHandler.LoadAsync(); - var results = new ConcurrentDictionary(); + var results = new ConcurrentDictionary(); await Parallel.ForEachAsync(clusters.Connections, async (cluster, token) => { diff --git a/KustoSchemaTools/Model/Cluster.cs b/KustoSchemaTools/Model/Cluster.cs index 95e220a..64b4812 100644 --- a/KustoSchemaTools/Model/Cluster.cs +++ b/KustoSchemaTools/Model/Cluster.cs @@ -5,6 +5,7 @@ public class Cluster public string Name { get; set; } public string Url { get; set; } public List Scripts { get; set; } = new List(); + public bool IsActive { get; set; } = true; } }