Skip to content

Commit

Permalink
Fix null handling in Get-CatletIp (#91)
Browse files Browse the repository at this point in the history
Closes #90
  • Loading branch information
ChristopherMann authored Nov 7, 2024
1 parent 5bdf61f commit 35217b1
Showing 1 changed file with 54 additions and 79 deletions.
133 changes: 54 additions & 79 deletions src/Eryph.ComputeClient.Commands/Catlets/GetCatletIpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,104 +4,79 @@
using Eryph.ComputeClient.Models;
using JetBrains.Annotations;

namespace Eryph.ComputeClient.Commands.Catlets
namespace Eryph.ComputeClient.Commands.Catlets;

[PublicAPI]
[Cmdlet(VerbsCommon.Get, "CatletIp", DefaultParameterSetName = "get")]
[OutputType(typeof(NetworkPortIp), ParameterSetName = ["get"])]
public class GetCatletIpCommand : CatletCmdLet
{
[PublicAPI]
[Cmdlet(VerbsCommon.Get, "CatletIp", DefaultParameterSetName = "get")]
[OutputType(typeof(NetworkPortIp), ParameterSetName = new[] { "get" })]
public class GetCatletIpCommand : CatletCmdLet
{
[Parameter(
ParameterSetName = "get",
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
[Parameter(
ParameterSetName = "get",
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]

public string[] Id { get; set; }
public string[] Id { get; set; }

[Parameter(
ParameterSetName = "get",
Mandatory = false)]
public SwitchParameter InternalIp { get; set; }
[Parameter(
ParameterSetName = "get",
Mandatory = false)]
public SwitchParameter InternalIp { get; set; }

[Parameter(
ParameterSetName = "get",
Mandatory = false)]
public string Network { get; set; }
[Parameter(
ParameterSetName = "get",
Mandatory = false)]
public string Network { get; set; }

protected override void ProcessRecord()
protected override void ProcessRecord()
{
if (Id != null)
{

if (Id != null)
{
foreach (var id in Id)
{
var catlet = GetSingleCatlet(id);

WriteIp(catlet);
}


return;
}


foreach (var catlet in Factory.CreateCatletsClient().List())
foreach (var id in Id)
{
if (Stopping) break;
var catlet = GetSingleCatlet(id);

WriteIp(catlet);

}


return;
}

private void WriteIp(Catlet catlet)
foreach (var catlet in Factory.CreateCatletsClient().List())
{
var addresses = catlet.Networks.Where(nw =>
{
if(!string.IsNullOrEmpty(Network))
return nw.Name == Network;
return true;

}).SelectMany(x =>
{
if (InternalIp.IsPresent)
{
return x.IpV4Addresses.Select(ip => new NetworkPortIp
{
Id = catlet.Id,
Name = catlet.Name,
IpAddress = ip,
AddressFamily = AddressFamily.InterNetwork
});
}

return x.FloatingPort.IpV4Addresses.Select(ip => new NetworkPortIp
{
Id = catlet.Id,
Name = catlet.Name,
IpAddress = ip,
AddressFamily = AddressFamily.InterNetwork

});
});

WriteObject(addresses, true);
if (Stopping) break;

WriteIp(catlet);
}

}


public class NetworkPortIp
private void WriteIp(Catlet catlet)
{
public string Id { get; set; }
public string Name { get; set; }
public string IpAddress { get; set; }
var addresses = (catlet.Networks ?? [])
.Where(n => string.IsNullOrEmpty(Network) || n.Name == Network)
.SelectMany(n => InternalIp ? n.IpV4Addresses ?? [] : n.FloatingPort?.IpV4Addresses ?? [])
.Select(ip => new NetworkPortIp
{
Id = catlet.Id,
Name = catlet.Name,
IpAddress = ip,
AddressFamily = AddressFamily.InterNetwork

})
.ToList();

public AddressFamily AddressFamily { get; set; }
WriteObject(addresses, true);
}
}

}
public class NetworkPortIp
{
public string Id { get; set; }

public string Name { get; set; }

public string IpAddress { get; set; }

public AddressFamily AddressFamily { get; set; }
}

0 comments on commit 35217b1

Please sign in to comment.