Skip to content

Commit

Permalink
connectkustocommand ongoing
Browse files Browse the repository at this point in the history
  • Loading branch information
santisq committed Dec 16, 2024
1 parent 6584823 commit 261603b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
75 changes: 70 additions & 5 deletions src/PowerShellKusto/ConnectKustoCommand.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,93 @@
using System.Management.Automation;
using System;
using System.Management.Automation;
using System.Security.Authentication;
using Kusto.Data;
using Kusto.Data.Common;

namespace PowerShellKusto;

[Cmdlet(VerbsCommunications.Connect, "Kusto")]
public sealed class ConnectKustoCommand : PSCmdlet
{
private const string CredentialSet = "Credential";

private const string IdentitySet = "Identity";

private static KustoConnectionDetails? s_connectionDetails;


[Parameter(Mandatory = true, Position = 0)]
public string Cluster { get; set; } = null!;

[Parameter(Mandatory = true, Position = 1)]
public string Database { get; set; } = null!;

[Parameter(Mandatory = true, ParameterSetName = "Credential")]
[Parameter(Mandatory = true, ParameterSetName = CredentialSet)]
[Credential]
public PSCredential Credential { get; set; } = null!;

[Parameter(Mandatory = true, ParameterSetName = "Credential")]
public Guid TenantId { get; set; }
[Parameter(Mandatory = true, ParameterSetName = CredentialSet)]
public string Authority { get; set; } = null!;

[Parameter(ParameterSetName = "Identity")]
[Parameter(ParameterSetName = IdentitySet)]
public SwitchParameter Identity { get; set; }

[Parameter]
public ClientRequestProperties? RequestProperties { get; set; }

[Parameter]
public TimeSpan ServerTimeout { get; set; } = TimeSpan.FromMinutes(10);

[Parameter]
public SwitchParameter NoTruncation { get; set; }

protected override void EndProcessing()
{
RequestProperties ??= new ClientRequestProperties([
new(ClientRequestProperties.OptionServerTimeout, ServerTimeout),
new(ClientRequestProperties.OptionNoTruncation, NoTruncation.IsPresent)],
null);

KustoConnectionStringBuilder builder = new(Cluster, Database);

try
{
s_connectionDetails = ParameterSetName switch
{
IdentitySet => new KustoConnectionDetails(
builder.WithAadSystemManagedIdentity(),
RequestProperties),

CredentialSet => new KustoConnectionDetails(
builder.WithAadApplicationKeyAuthentication(
Credential.UserName,
Credential.GetNetworkCredential().Password,
Authority),
RequestProperties),

_ => null
};
}
catch (Exception ex)
{
ErrorRecord error = new(
ex, "AuthFailed", ErrorCategory.AuthenticationError, null);

ThrowTerminatingError(error);
}
}

internal static void AssertConnected(PSCmdlet cmdlet)
{
if (s_connectionDetails is null)
{
ErrorRecord error = new(
new AuthenticationException("Authentication required. Please call 'Connect-Kust'."),
"AuthRequired",
ErrorCategory.AuthenticationError,
null);

cmdlet.ThrowTerminatingError(error);
}
}
}
8 changes: 8 additions & 0 deletions src/PowerShellKusto/KustoConnectionDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Kusto.Data;
using Kusto.Data.Common;

namespace PowerShellKusto;

internal record struct KustoConnectionDetails(
KustoConnectionStringBuilder Connection,
ClientRequestProperties RequestProperties);
3 changes: 2 additions & 1 deletion src/PowerShellKusto/PowerShellKusto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Kusto.Tools" Version="12.2.8" />
<PackageReference Include="Microsoft.Azure.Kusto.Data" Version="12.2.8" />
<PackageReference Include="Microsoft.Azure.Kusto.Ingest" Version="12.2.8" />
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1" PrivateAssets="all" />
</ItemGroup>

Expand Down

0 comments on commit 261603b

Please sign in to comment.