Skip to content

Commit 1a1cbbb

Browse files
authored
fix(node): add CPU utilization to node data source (#2605)
Signed-off-by: 666Goofy666 <153852747+666Goofy666@users.noreply.github.com>
1 parent 32dfac8 commit 1a1cbbb

File tree

6 files changed

+21
-3
lines changed

6 files changed

+21
-3
lines changed

docs/data-sources/virtual_environment_node.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ output "data_proxmox_virtual_environment_node" {
4343
- `cpu_count` (Number) The total number of logical CPUs on the node (sockets * cores * threads)
4444
- `cpu_model` (String) The CPU model on the node
4545
- `cpu_sockets` (Number) The number of CPU sockets on the node
46+
- `cpu_utilization` (Number) The CPU utilization on the node (a value between `0.0` and `1.0`)
4647
- `id` (String) The ID of this resource.
4748
- `memory_available` (Number) The available memory in bytes on the node
4849
- `memory_total` (Number) The total memory in bytes on the node

docs/data-sources/virtual_environment_nodes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ output "data_proxmox_virtual_environment_nodes" {
3131
### Read-Only
3232

3333
- `cpu_count` (List of Number) The total number of logical CPUs on each node
34-
- `cpu_utilization` (List of Number) The CPU utilization on each node
34+
- `cpu_utilization` (List of Number) The CPU utilization on each node (values between `0.0` and `1.0`)
3535
- `id` (String) The ID of this resource.
3636
- `memory_available` (List of Number) The available memory in bytes on each node
3737
- `memory_used` (List of Number) The used memory in bytes on each node

fwprovider/test/datasource_node_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func TestAccDatasourceNode(t *testing.T) {
3131
"cpu_count",
3232
"cpu_sockets",
3333
"cpu_model",
34+
"cpu_utilization",
3435
"memory_available",
3536
"memory_used",
3637
"memory_total",

proxmox/nodes/nodes_types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ type GetInfoResponseBody struct {
4141

4242
// GetInfoResponseData contains the data from a node info response.
4343
type GetInfoResponseData struct {
44-
CPUInfo struct {
44+
CPUUtilization *float64 `json:"cpu,omitempty"`
45+
CPUInfo struct {
4546
CPUCores *int `json:"cores,omitempty"`
4647
CPUCount *int `json:"cpus,omitempty"`
4748
CPUSockets *int `json:"sockets,omitempty"`

proxmoxtf/datasource/node.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package datasource
88

99
import (
1010
"context"
11+
"math"
1112

1213
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -20,6 +21,7 @@ const (
2021
mkDataSourceVirtualEnvironmentNodeCPUCount = "cpu_count"
2122
mkDataSourceVirtualEnvironmentNodeCPUSockets = "cpu_sockets"
2223
mkDataSourceVirtualEnvironmentNodeCPUModel = "cpu_model"
24+
mkDataSourceVirtualEnvironmentNodeCPUUtilization = "cpu_utilization"
2325
mkDataSourceVirtualEnvironmentNodeMemoryAvailable = "memory_available"
2426
mkDataSourceVirtualEnvironmentNodeMemoryUsed = "memory_used"
2527
mkDataSourceVirtualEnvironmentNodeMemoryTotal = "memory_total"
@@ -52,6 +54,11 @@ func Node() *schema.Resource {
5254
Description: "The CPU model on the node",
5355
Computed: true,
5456
},
57+
mkDataSourceVirtualEnvironmentNodeCPUUtilization: {
58+
Type: schema.TypeFloat,
59+
Description: "The CPU utilization on the node (a value between `0.0` and `1.0`)",
60+
Computed: true,
61+
},
5562
mkDataSourceVirtualEnvironmentNodeMemoryAvailable: {
5663
Type: schema.TypeInt,
5764
Description: "The available memory in bytes on the node",
@@ -133,6 +140,14 @@ func nodeRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnosti
133140

134141
diags = append(diags, diag.FromErr(err)...)
135142

143+
if node.CPUUtilization != nil {
144+
err = d.Set(mkDataSourceVirtualEnvironmentNodeCPUUtilization, math.Round(*node.CPUUtilization*100)/100)
145+
} else {
146+
err = d.Set(mkDataSourceVirtualEnvironmentNodeCPUUtilization, 0)
147+
}
148+
149+
diags = append(diags, diag.FromErr(err)...)
150+
136151
if node.MemoryInfo.Total != nil {
137152
err = d.Set(mkDataSourceVirtualEnvironmentNodeMemoryAvailable, node.MemoryInfo.Free)
138153
diags = append(diags, diag.FromErr(err)...)

proxmoxtf/datasource/nodes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func Nodes() *schema.Resource {
4141
},
4242
mkDataSourceVirtualEnvironmentNodesCPUUtilization: {
4343
Type: schema.TypeList,
44-
Description: "The CPU utilization on each node",
44+
Description: "The CPU utilization on each node (values between `0.0` and `1.0`)",
4545
Computed: true,
4646
Elem: &schema.Schema{Type: schema.TypeFloat},
4747
},

0 commit comments

Comments
 (0)