Skip to content

Commit

Permalink
Merge pull request #27 from franklupo/master
Browse files Browse the repository at this point in the history
Bump 8.3
  • Loading branch information
franklupo authored Dec 19, 2024
2 parents 4ea8098 + b2b8abc commit 58648f5
Show file tree
Hide file tree
Showing 41 changed files with 1,040 additions and 312 deletions.
3 changes: 2 additions & 1 deletion common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>8.2.5</Version>
<Version>8.3.0</Version>
<!--<NoWarn>$(NoWarn);CS1591;CS0436</NoWarn>-->

<Company>Corsinvest Srl</Company>
Expand All @@ -19,6 +19,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<OutputType>Library</OutputType>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<!-- Include symbol files (*.pdb) in the built .nupkg -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>
$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
Expand Down
15 changes: 15 additions & 0 deletions src/Corsinvest.ProxmoxVE.Api.Extension/ClientExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,21 @@ public static async Task<VmBaseStatusCurrent> GetVmStatusAsync(this PveClient cl
_ => throw new InvalidEnumArgumentException(),
};

/// <summary>
/// Get Vm Config
/// </summary>
/// <param name="client"></param>
/// <param name="vm"></param>
/// <returns></returns>
/// <exception cref="InvalidEnumArgumentException"></exception>
public static async Task<VmConfig> GetVmConfigAsync(this PveClient client, IClusterResourceVm vm)
=> vm.VmType switch
{
VmType.Qemu => await client.Nodes[vm.Node].Qemu[vm.VmId].Config.GetAsync(),
VmType.Lxc => await client.Nodes[vm.Node].Lxc[vm.VmId].Config.GetAsync(),
_ => throw new InvalidEnumArgumentException(),
};

/// <summary>
/// Get Vm RrdData
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static class ModelsExtensionsAutoGen
/// Resources index (cluster wide).
/// </summary>
/// <param name="item"></param>
/// <param name="type">
/// <param name="type">Resource type.
/// Enum: vm,storage,node,sdn</param>
/// <returns></returns>
public static async Task<IEnumerable<Corsinvest.ProxmoxVE.Api.Shared.Models.Cluster.ClusterResource>> GetAsync(this Corsinvest.ProxmoxVE.Api.PveClient.PveCluster.PveResources item, string type = null)
Expand Down Expand Up @@ -254,7 +254,7 @@ public static class ModelsExtensionsAutoGen
/// <param name="verbose">If disabled, does only print the PCI IDs. Otherwise, additional information like vendor and device will be returned.</param>
/// <returns></returns>
public static async Task<IEnumerable<Corsinvest.ProxmoxVE.Api.Shared.Models.Node.NodeHardwarePci>> GetAsync(this Corsinvest.ProxmoxVE.Api.PveClient.PveNodes.PveNodeItem.PveHardware.PvePci item, string pci_class_blacklist = null, bool? verbose = null)
=> (await item.Pciscan(pci_class_blacklist, verbose)).ToModel<IEnumerable<Corsinvest.ProxmoxVE.Api.Shared.Models.Node.NodeHardwarePci>>();
=> (await item.PciScan(pci_class_blacklist, verbose)).ToModel<IEnumerable<Corsinvest.ProxmoxVE.Api.Shared.Models.Node.NodeHardwarePci>>();

/// <summary>
/// List local USB devices.
Expand Down
2 changes: 1 addition & 1 deletion src/Corsinvest.ProxmoxVE.Api.Extension/Utils/InfoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ public static async Task<Info> CollectAsync(PveClient client,
private static async Task ReadClusterAsync(Info info, PveClient client)
{
info.Cluster.Status = await client.Cluster.Status.GetAsync();
info.IsCluster = !string.IsNullOrEmpty(info.Cluster.Status.FirstOrDefault(a => a.Type == Corsinvest.ProxmoxVE.Api.Shared.Utils.PveConstants.KeyApiCluster)?.Name);
info.IsCluster = !string.IsNullOrEmpty(info.Cluster.Status.FirstOrDefault(a => a.Type == Shared.Utils.PveConstants.KeyApiCluster)?.Name);

info.Cluster.Config.Nodes = await client.Cluster.Config.Nodes.GetAsync();
info.Cluster.Config.Qdevice = await client.Cluster.Config.Qdevice.GetAsync();
Expand Down
62 changes: 38 additions & 24 deletions src/Corsinvest.ProxmoxVE.Api.Extension/Utils/VmHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Corsinvest.ProxmoxVE.Api.Shared.Models.Cluster;
using Corsinvest.ProxmoxVE.Api.Shared.Models.Vm;
using Corsinvest.ProxmoxVE.Api.Shared.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
Expand All @@ -29,9 +28,26 @@ public static class VmHelper
/// <param name="vmId"></param>
/// <param name="vmName"></param>
/// <param name="vmType"></param>
/// <param name="noVnc"></param>
/// <param name="xtermJs"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static async Task<HttpResponseMessage> GetConsoleNoVncAsync(PveClient client, string node, long vmId, string vmName, VmType vmType)
=> await GetConsoleNoVncAsync(client, node, vmId, vmName, NoVncHelper.GetConsoleType(vmType));
public static async Task<HttpResponseMessage> GetConsoleNoVncAsync(PveClient client,
string node,
long vmId,
string vmName,
VmType vmType,
bool noVnc,
bool xtermJs,
string parameters = null)
=> await GetConsoleNoVncAsync(client,
node,
vmId,
vmName,
NoVncHelper.GetConsoleType(vmType),
noVnc,
xtermJs,
parameters);

/// <summary>
/// Get console NoVnc
Expand All @@ -41,16 +57,29 @@ public static async Task<HttpResponseMessage> GetConsoleNoVncAsync(PveClient cli
/// <param name="vmId"></param>
/// <param name="vmName"></param>
/// <param name="console"></param>
/// <param name="noVnc"></param>
/// <param name="xtermJs"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public static async Task<HttpResponseMessage> GetConsoleNoVncAsync(PveClient client, string node, long vmId, string vmName, string console)
public static async Task<HttpResponseMessage> GetConsoleNoVncAsync(PveClient client,
string node,
long vmId,
string vmName,
string console,
bool noVnc,
bool xtermJs,
string parameters = null)
{
using var httpClient = client.GetHttpClient();
return await httpClient.GetAsync(NoVncHelper.GetConsoleUrl(client.Host,
client.Port,
console,
node,
vmId,
vmName));
client.Port,
console,
node,
vmId,
vmName,
noVnc,
xtermJs,
parameters));
}
#endregion

Expand Down Expand Up @@ -133,21 +162,6 @@ public static async Task<Result> ChangeStatusVmAsync(PveClient client, string no
_ => throw new InvalidEnumArgumentException(),
};

/// <summary>
/// Get vm status
/// </summary>
/// <param name="client"></param>
/// <param name="vm"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static async Task<VmBaseStatusCurrent> GetVmStatusAsync(PveClient client, IClusterResourceVm vm)
=> vm.VmType switch
{
VmType.Qemu => await client.Nodes[vm.Node].Qemu[vm.VmId].Status.Current.GetAsync(),
VmType.Lxc => await client.Nodes[vm.Node].Lxc[vm.VmId].Status.Current.GetAsync(),
_ => throw new ArgumentOutOfRangeException("vm.VmType"),
};

/// <summary>
/// Get Vms Jolly Keys
/// </summary>
Expand Down
8 changes: 7 additions & 1 deletion src/Corsinvest.ProxmoxVE.Api.Metadata/ClassApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace Corsinvest.ProxmoxVE.Api.Metadata;

Expand All @@ -27,11 +28,16 @@ public class ClassApi
/// <param name="parent"></param>
public ClassApi(JToken token, ClassApi parent)
{
Name = token["text"].ToString();
Name = token["text"].ToString().Replace("-", "_");
IndexName = Name.Replace("{", "").Replace("}", "");
Parent = parent;
Resource = token["path"].ToString();

Resource = Regex.Replace(Resource, @"\{([^}]*)\}", match =>
{
return "{" + match.Groups[1].Value.Replace('-', '_') + "}";
});

Keys.AddRange(parent.Keys);
parent.SubClasses.Add(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static IEnumerable<TResource> CalculateHostUsage<TResource>(this IEnumera
foreach (var item in items.Where(a => a.ResourceType == ClusterResourceType.Vm))
{
var node = items.FirstOrDefault(a => a.ResourceType == ClusterResourceType.Node && a.Node == item.Node);
if (node != null) { item.CalculateHostUsage(node.MemorySize, node.CpuSize); }
if (node != null) { item.CalculateHostUsage(node.MemorySize, (ulong)node.CpuSize); }
}

return items;
Expand All @@ -40,12 +40,12 @@ public static IEnumerable<TResource> CalculateHostUsage<TResource>(this IEnumera
/// <param name="data"></param>
/// <param name="hostMemorySize"></param>
/// <param name="hostCpuSize"></param>
public static void CalculateHostUsage(this ClusterResource data, long hostMemorySize, long hostCpuSize)
public static void CalculateHostUsage(this ClusterResource data, ulong hostMemorySize, ulong hostCpuSize)
{
//calculate host usage
if (data.ResourceType == ClusterResourceType.Vm && data.Uptime > 0 && hostCpuSize > 0 && hostMemorySize > 0)
{
var per = Math.Round(data.CpuUsagePercentage / hostCpuSize * data.CpuSize, 1);
var per = Math.Round(data.CpuUsagePercentage / hostCpuSize * data.CpuSize * 100.0, 1);
data.HostCpuUsage = $"{per} % of {hostCpuSize} {(hostCpuSize > 1 ? "CPUs" : "CPU")}";

data.HostMemoryUsage = (double)data.MemoryUsage / hostMemorySize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class ClusterLog : ModelBase
/// </summary>
/// <value></value>
[JsonProperty("time")]
[DisplayFormat(DataFormatString = "{0:" + FormatHelper.FormatUnixTime + "}")]
[DisplayFormat(DataFormatString = FormatHelper.DataFormatUnixTime)]
public int Time { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public class ClusterResource : ModelBase, IClusterResourceNode, IClusterResource
/// </summary>
/// <value></value>
[JsonProperty("netin")]
[DisplayFormat(DataFormatString = "{0:" + FormatHelper.FormatBytes + "}")]
[DisplayFormat(DataFormatString = FormatHelper.DataFormatBytes)]
public long NetIn { get; set; }

/// <summary>
/// Net out
/// </summary>
/// <value></value>
[JsonProperty("netout")]
[DisplayFormat(DataFormatString = "{0:" + FormatHelper.FormatBytes + "}")]
[DisplayFormat(DataFormatString = FormatHelper.DataFormatBytes)]
public long NetOut { get; set; }

/// <summary>
Expand Down Expand Up @@ -89,15 +89,15 @@ public class ClusterResource : ModelBase, IClusterResourceNode, IClusterResource
/// </summary>
/// <value></value>
[JsonProperty("diskread")]
[DisplayFormat(DataFormatString = "{0:" + FormatHelper.FormatBytes + "}")]
[DisplayFormat(DataFormatString = FormatHelper.DataFormatBytes)]
public long DiskRead { get; set; }

/// <summary>
/// Disk write
/// </summary>
/// <value></value>
[JsonProperty("diskwrite")]
[DisplayFormat(DataFormatString = "{0:" + FormatHelper.FormatBytes + "}")]
[DisplayFormat(DataFormatString = FormatHelper.DataFormatBytes)]
public long DiskWrite { get; set; }

/// <summary>
Expand Down Expand Up @@ -130,7 +130,7 @@ public class ClusterResource : ModelBase, IClusterResourceNode, IClusterResource
/// </summary>
/// <value></value>
[Display(Name = "Host Memory Usage %")]
[DisplayFormat(DataFormatString = "{0:P1}")]
[DisplayFormat(DataFormatString = FormatHelper.DataFormatPercentage)]
public double HostMemoryUsage { get; set; }

/// <summary>
Expand Down Expand Up @@ -202,24 +202,24 @@ public class ClusterResource : ModelBase, IClusterResourceNode, IClusterResource
/// <value></value>
[JsonProperty("disk")]
[Display(Name = "Disk usage")]
[DisplayFormat(DataFormatString = "{0:" + FormatHelper.FormatBytes + "}")]
public long DiskUsage { get; set; }
[DisplayFormat(DataFormatString = FormatHelper.DataFormatBytes)]
public ulong DiskUsage { get; set; }

/// <summary>
/// Disk size
/// </summary>
/// <value></value>
[JsonProperty("maxdisk")]
[Display(Name = "Disk size")]
[DisplayFormat(DataFormatString = "{0:" + FormatHelper.FormatBytes + "}")]
public long DiskSize { get; set; }
[DisplayFormat(DataFormatString = FormatHelper.DataFormatBytes)]
public ulong DiskSize { get; set; }

/// <summary>
/// Disk usage percentage
/// </summary>
/// <value></value>
[Display(Name = "Disk usage %")]
[DisplayFormat(DataFormatString = "{0:P1}")]
[DisplayFormat(DataFormatString = FormatHelper.DataFormatPercentage)]
public double DiskUsagePercentage { get; set; }

/// <summary>
Expand All @@ -234,17 +234,17 @@ public class ClusterResource : ModelBase, IClusterResourceNode, IClusterResource
/// <value></value>
[JsonProperty("mem")]
[Display(Name = "Memory")]
[DisplayFormat(DataFormatString = "{0:" + FormatHelper.FormatBytes + "}")]
public long MemoryUsage { get; set; }
[DisplayFormat(DataFormatString = FormatHelper.DataFormatBytes)]
public ulong MemoryUsage { get; set; }

/// <summary>
///Memory size
/// </summary>
/// <value></value>
[JsonProperty("maxmem")]
[Display(Name = "Max Memory")]
[DisplayFormat(DataFormatString = "{0:" + FormatHelper.FormatBytes + "}")]
public long MemorySize { get; set; }
[DisplayFormat(DataFormatString = FormatHelper.DataFormatBytes)]
public ulong MemorySize { get; set; }

/// <summary>
/// Memory info
Expand All @@ -258,15 +258,15 @@ public class ClusterResource : ModelBase, IClusterResourceNode, IClusterResource
/// </summary>
/// <value></value>
[Display(Name = "Memory Usage %")]
[DisplayFormat(DataFormatString = "{0:P1}")]
[DisplayFormat(DataFormatString = FormatHelper.DataFormatPercentage)]
public double MemoryUsagePercentage { get; set; }

/// <summary>
/// Cpu usage
/// </summary>
/// <value></value>
[Display(Name = "CPU Usage %")]
[DisplayFormat(DataFormatString = "{0:P1}")]
[DisplayFormat(DataFormatString = FormatHelper.DataFormatPercentage)]
[JsonProperty("cpu")]
public double CpuUsagePercentage { get; set; }

Expand All @@ -289,7 +289,7 @@ public class ClusterResource : ModelBase, IClusterResourceNode, IClusterResource
/// </summary>
/// <value></value>
[JsonProperty("uptime")]
[DisplayFormat(DataFormatString = "{0:" + FormatHelper.FormatUptimeUnixTime + "}")]
[DisplayFormat(DataFormatString = FormatHelper.DataFormatUptimeUnixTime)]
public long Uptime { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ namespace Corsinvest.ProxmoxVE.Api.Shared.Models.Cluster;
public enum ClusterResourceType
{
/// <summary>
/// All
/// Sdn
/// </summary>
All,
Unknown = 0,

/// <summary>
/// Node
/// </summary>
Node,
Node = 1,

/// <summary>
/// Vm
/// </summary>
Vm,
Vm = 2,

/// <summary>
/// Storage
/// </summary>
Storage,
Storage = 4,

/// <summary>
/// Pool
/// </summary>
Pool,
Pool = 8,

/// <summary>
/// Sdn
/// </summary>
Sdn,
Sdn = 16,

/// <summary>
/// Sdn
/// All
/// </summary>
Unknown,
All = Node | Vm | Storage | Pool | Sdn,
}
Loading

0 comments on commit 58648f5

Please sign in to comment.