Skip to content

Commit

Permalink
Merge pull request #17 from eryph-org/pull/api_update
Browse files Browse the repository at this point in the history
update identity client to autorest v3
  • Loading branch information
fw2568 authored Oct 22, 2023
2 parents 0862bb2 + 8be56e7 commit 806fb9f
Show file tree
Hide file tree
Showing 35 changed files with 2,060 additions and 2,439 deletions.
2 changes: 1 addition & 1 deletion gen/generate_local.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ $location = Get-Location
$tag = $settings.tag
$spec = $settings.spec

autorest ..\eryph-api-spec\specification\$spec --tag=$tag --csharp-src-folder=$location --use=..\autorest.csharp --csharp --debug --legacy
autorest ..\eryph-api-spec\specification\$spec --tag=$tag --csharp-src-folder=$location --v3 --csharp --verbose
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Eryph.ClientRuntime.Powershell" Version="0.4.1" />
<PackageReference Include="GitVersion.MsBuild" Version="5.10.3">
<PackageReference Include="Eryph.ClientRuntime.Powershell" Version="0.5.0" />
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Eryph.ClientRuntime.Authentication" Version="0.4.1" />
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
Expand Down
38 changes: 20 additions & 18 deletions src/Eryph.IdentityClient.Commands/GetEryphClientCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Management.Automation;
using Eryph.ClientRuntime.OData;
using System.Linq;
using System.Management.Automation;
using Eryph.IdentityClient.Models;
using JetBrains.Annotations;

Expand All @@ -26,29 +26,31 @@ public class GetEryphClientCommand : IdentityCmdLet

protected override void ProcessRecord()
{
using (var identityClient = new EryphIdentityClient(GetEndpointUri(), GetCredentials("identity:clients:read:all")))
var identityClient = Factory.CreateClientsClient();

if (Id != null)
{
if (Id != null)
foreach (var id in Id)
{
foreach (var id in Id)
{
WriteObject(identityClient.Clients.Get(id));
}

return;
var response = identityClient.Get(id);
if(response.HasValue)
WriteObject(response.Value);
}

if (Name != null)
{
foreach (var name in Name)
{
WriteObject(identityClient.Clients.List(), true);
}
return;
}

return;
foreach (var client in identityClient.List())
{
if (Stopping) break;

if(Name != null)
{
if (Name.Contains(client.Name))
WriteObject(client);
}

WriteObject(identityClient.Clients.List(), true);
WriteObject(client);

}

Expand Down
39 changes: 36 additions & 3 deletions src/Eryph.IdentityClient.Commands/IdentityCmdLet.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
using System;
using System.Management.Automation;
using Eryph.ClientRuntime.Configuration;
using Eryph.ClientRuntime.Powershell;
using JetBrains.Annotations;
using Microsoft.Rest;

namespace Eryph.IdentityClient.Commands
{
[PublicAPI]
public abstract class IdentityCmdLet : EryphCmdLet
{

protected ServiceClientCredentials GetCredentials(params string[] scopes)
protected IdentityClientsFactory Factory;

protected bool IsDebugEnabled
{
get
{
bool debug;
var containsDebug = MyInvocation.BoundParameters.ContainsKey("Debug");
if (containsDebug)
debug = ((SwitchParameter)MyInvocation.BoundParameters["Debug"]).ToBool();
else
debug = (ActionPreference)GetVariableValue("DebugPreference") != ActionPreference.SilentlyContinue;

return debug;
}
}

protected override void BeginProcessing()
{
return ServiceClientCredentialsCache.Instance.GetServiceCredentials(GetClientCredentials(), scopes).GetAwaiter().GetResult();
base.BeginProcessing();

var options = new EryphIdentityClientOptions(GetClientCredentials())
{
Diagnostics =
{
IsDistributedTracingEnabled = true,
IsLoggingEnabled = IsDebugEnabled,
IsLoggingContentEnabled = IsDebugEnabled
}
};


Factory = new IdentityClientsFactory(
options, GetEndpointUri());

}


protected Uri GetEndpointUri()
{

Expand Down
76 changes: 47 additions & 29 deletions src/Eryph.IdentityClient.Commands/NewEryphClientCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using Eryph.IdentityClient.Models;
using JetBrains.Annotations;

Expand Down Expand Up @@ -57,43 +56,62 @@ public class NewEryphClientCommand : IdentityCmdLet
protected override void ProcessRecord()
{
var clientCredentials = GetClientCredentials();
using (var identityClient = new EryphIdentityClient(GetEndpointUri(),GetCredentials("identity:clients:write:all")))
var identityClient = Factory.CreateClientsClient();
foreach (var name in Name)
{
foreach (var name in Name)
var eryphClient = new Client
{
var result = identityClient.Clients.Create(new Client
{
Name = name,
Description = Description,
AllowedScopes = AllowedScopes
});
Name = name,
Description = Description
};
foreach (var allowedScope in AllowedScopes)
{
eryphClient.AllowedScopes.Add(allowedScope);

if (AddToConfiguration)
{
var asDefault = !AsDefault ? "" : " -AsDefault";
}

var cmd =
$@"$args[0] | New-EryphClientCredentials -Id ""{result.Id}"" -IdentityEndpoint ""{clientCredentials.IdentityProvider}"" -Configuration ""{clientCredentials.Configuration}""| Add-EryphClientConfiguration -Name ""{result.Name}""{asDefault}";
var response = identityClient.Create(eryphClient);
if (!response.HasValue)
return;

var script = InvokeCommand.NewScriptBlock(cmd);
script.Invoke(result.Key);

WriteObject(new Client(result.Id, result.Name, result.Description, result.AllowedScopes));
}
else
WriteObject(new CreatedClient
{
Id = result.Id,
Name = result.Name,
AllowedScopes = result.AllowedScopes.ToArray(),
Description = result.Description,
IdentityProvider = clientCredentials.IdentityProvider,
PrivateKey = result.Key
});
var result = response.Value;

if (AddToConfiguration)
{
var asDefault = !AsDefault ? "" : " -AsDefault";

var cmd =
$@"$args[0] | New-EryphClientCredentials -Id ""{result.Id}"" -IdentityEndpoint ""{clientCredentials.IdentityProvider}"" -Configuration ""{clientCredentials.Configuration}""| Add-EryphClientConfiguration -Name ""{result.Name}""{asDefault}";

var script = InvokeCommand.NewScriptBlock(cmd);
script.Invoke(result.Key);

eryphClient = new Client
{
Id = result.Id,
Name = result.Name,
Description = result.Description
};
foreach (var allowedScope in result.AllowedScopes)
{
eryphClient.AllowedScopes.Add(allowedScope);

}

WriteObject(eryphClient);
}
else
WriteObject(new CreatedClient
{
Id = result.Id,
Name = result.Name,
AllowedScopes = result.AllowedScopes.ToArray(),
Description = result.Description,
IdentityProvider = clientCredentials.IdentityProvider,
PrivateKey = result.Key
});



}
}
Expand Down
9 changes: 3 additions & 6 deletions src/Eryph.IdentityClient.Commands/RemoveEryphClientCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ public class RemoveEryphClientCommand : IdentityCmdLet

protected override void ProcessRecord()
{
using (var identityClient = new EryphIdentityClient(GetEndpointUri(),GetCredentials("identity:clients:write:all")))
var identityClient = Factory.CreateClientsClient();
foreach (var id in Id)
{
foreach (var id in Id)
{
identityClient.Clients.Delete(id);

}
identityClient.Delete(id);

}
}
Expand Down
32 changes: 0 additions & 32 deletions src/Eryph.IdentityClient.Commands/ServiceClientCredentialsCache.cs

This file was deleted.

18 changes: 13 additions & 5 deletions src/Eryph.IdentityClient.Commands/SetEryphClientCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,31 @@ public class SetEryphClientCommand : IdentityCmdLet

protected override void ProcessRecord()
{
using (var identityClient = new EryphIdentityClient(GetEndpointUri(),GetCredentials("identity:clients:write:all")))
var identityClient = Factory.CreateClientsClient();
{
foreach (var id in Id)
{
var client =
identityClient.Clients.Get(id);
identityClient.Get(id)?.Value;

if(AllowedScopes != null)
client.AllowedScopes = AllowedScopes;
if (client == null)
return;

if (AllowedScopes != null)
{
client.AllowedScopes.Clear();
foreach (var scope in AllowedScopes)
client.AllowedScopes.Add(scope);
}

if(Name != null)
client.Name = Name;

if (Description != null)
client.Description = Description;

WriteObject(identityClient.Clients.Update(id, client));
var response = identityClient.Update(id, client);
WriteObject(response.Value);

}

Expand Down
18 changes: 11 additions & 7 deletions src/Eryph.IdentityClient/Eryph.IdentityClient.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Nullable>annotations</Nullable>
</PropertyGroup>

<ItemGroup>
<Folder Include="Generated\" />
</ItemGroup>
<PropertyGroup>
<LangVersion>11.0</LangVersion>
<IncludeGeneratorSharedCode>true</IncludeGeneratorSharedCode>
<RestoreAdditionalProjectSources>https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json</RestoreAdditionalProjectSources>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GitVersion.MsBuild" Version="5.10.3">
<PackageReference Include="Eryph.ClientRuntime.Authentication" Version="0.5.0" />
<PackageReference Include="GitVersion.MsBuild" Version="5.12.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Eryph.ClientRuntime" Version="0.4.1" />
<PackageReference Include="Microsoft.Azure.AutoRest.CSharp" Version="3.0.0-beta.20231020.1" PrivateAssets="All" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions src/Eryph.IdentityClient/EryphIdentityClientOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Runtime.CompilerServices;
using Eryph.IdentityModel.Clients;

namespace Eryph.IdentityClient
{
public partial class EryphIdentityClientOptions
{
public ClientCredentials ClientCredentials { get; }

public EryphIdentityClientOptions(ClientCredentials clientCredentials, ServiceVersion version = LatestVersion)
: this(version)
{
ClientCredentials = clientCredentials;
}
}
}
Loading

0 comments on commit 806fb9f

Please sign in to comment.