Skip to content

Commit 9c80b0e

Browse files
committed
Add a custom command to open Kusto Explorer
1 parent d1648bf commit 9c80b0e

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/Aspire.Hosting.Azure.Kusto/AzureKustoBuilderExtensions.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#pragma warning disable AZPROVISION001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
55

6+
using System.Diagnostics;
7+
using System.Web;
68
using Aspire.Hosting.ApplicationModel;
79
using Aspire.Hosting.Azure;
810
using Azure.Identity;
@@ -12,6 +14,7 @@
1214
using Kusto.Data.Common;
1315
using Kusto.Data.Net.Client;
1416
using Microsoft.Extensions.DependencyInjection;
17+
using Microsoft.Extensions.Diagnostics.HealthChecks;
1518
using Microsoft.Extensions.Logging;
1619

1720
namespace Aspire.Hosting;
@@ -36,7 +39,7 @@ public static class AzureKustoBuilderExtensions
3639
/// </para>
3740
/// <para>
3841
/// By default references to the Azure Data Explorer database resources will be assigned the following roles:
39-
///
42+
///
4043
/// - <see cref="KustoDatabasePrincipalRole.User"/>
4144
/// </para>
4245
/// </remarks>
@@ -88,6 +91,7 @@ public static IResourceBuilder<AzureKustoClusterResource> AddAzureKustoCluster(t
8891
var resourceBuilder = builder.AddResource(resource);
8992

9093
AddKustoHealthChecksAndLifecycleManagement(resourceBuilder);
94+
AddKustoCustomCommands(resourceBuilder);
9195

9296
return resourceBuilder
9397
.WithAnnotation(new DefaultRoleAssignmentsAnnotation(new HashSet<RoleDefinition>()));
@@ -307,4 +311,45 @@ private static KustoConnectionStringBuilder GetConnectionStringBuilder(AzureKust
307311

308312
return builder;
309313
}
314+
315+
private static void AddKustoCustomCommands(IResourceBuilder<AzureKustoClusterResource> resourceBuilder)
316+
{
317+
var options = new CommandOptions
318+
{
319+
UpdateState = UpdateState,
320+
IconName = "ServerLink"
321+
};
322+
323+
resourceBuilder.WithCommand(
324+
name: "open-kusto-explorer",
325+
displayName: "Open in Kusto Explorer (Desktop)",
326+
executeCommand: context => OnOpenInKustoExplorer(resourceBuilder, context),
327+
commandOptions: options);
328+
329+
static ResourceCommandState UpdateState(UpdateCommandStateContext context)
330+
{
331+
return context.ResourceSnapshot.HealthStatus is HealthStatus.Healthy ? ResourceCommandState.Enabled : ResourceCommandState.Disabled;
332+
}
333+
334+
static async Task<ExecuteCommandResult> OnOpenInKustoExplorer(IResourceBuilder<AzureKustoClusterResource> resourceBuilder, ExecuteCommandContext context)
335+
{
336+
var connectionString = await resourceBuilder
337+
.Resource
338+
.ConnectionStringExpression
339+
.GetValueAsync(context.CancellationToken)
340+
.ConfigureAwait(false) ??
341+
throw new DistributedApplicationException($"Connection string for Kusto resource '{resourceBuilder.Resource.Name}' is not set.");
342+
343+
// TODO: This should be of the form http://<your_cluster>/?web=0, but the current redirect logic doesn't include the port. So for now we build it ourselves.
344+
var uri = $"https://explorer.kusto.io/app/Kusto.Explorer.application?uri={Uri.EscapeDataString(connectionString)}&svc=engine&web=0";
345+
346+
Process.Start(new ProcessStartInfo
347+
{
348+
FileName = uri,
349+
UseShellExecute = true
350+
});
351+
352+
return CommandResults.Success();
353+
}
354+
}
310355
}

0 commit comments

Comments
 (0)