0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-30 02:31:10 +00:00
terraform-provider-proxmox/proxmox/nodes/nodes.go
Serge 9fee8c1e2e
feat(vm): add proxmox_virtual_environment_node datasource (#1151)
* feat(vm): add proxmox_virtual_environment_node datasource

It helps to get CPU model, number of cores and sockets.

Signed-off-by: Serge Logvinov <serge.logvinov@sinextra.dev>

* fix node_name ref
* add acceptance test

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>

---------

Signed-off-by: Serge Logvinov <serge.logvinov@sinextra.dev>
Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
2024-03-23 20:17:09 -04:00

79 lines
2.0 KiB
Go

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package nodes
import (
"context"
"fmt"
"net/http"
"sort"
"github.com/bpg/terraform-provider-proxmox/proxmox/api"
)
// ListNodes retrieves a list of nodes.
func (c *Client) ListNodes(ctx context.Context) ([]*ListResponseData, error) {
resBody := &ListResponseBody{}
err := c.DoRequest(ctx, http.MethodGet, "nodes", nil, resBody)
if err != nil {
return nil, fmt.Errorf("failed to get nodes: %w", err)
}
if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}
sort.Slice(resBody.Data, func(i, j int) bool {
return resBody.Data[i].Name < resBody.Data[j].Name
})
return resBody.Data, nil
}
// GetTime retrieves the time information for a node.
func (c *Client) GetTime(ctx context.Context) (*GetTimeResponseData, error) {
resBody := &GetTimeResponseBody{}
err := c.DoRequest(ctx, http.MethodGet, c.ExpandPath("time"), nil, resBody)
if err != nil {
return nil, fmt.Errorf("failed to get time information for node \"%s\": %w", c.NodeName, err)
}
if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}
return resBody.Data, nil
}
// UpdateTime updates the time on a node.
func (c *Client) UpdateTime(ctx context.Context, d *UpdateTimeRequestBody) error {
err := c.DoRequest(ctx, http.MethodPut, c.ExpandPath("time"), d, nil)
if err != nil {
return fmt.Errorf("failed to update node time: %w", err)
}
return nil
}
// GetInfo retrieves the information of the node.
func (c *Client) GetInfo(ctx context.Context) (*GetInfoResponseData, error) {
resBody := &GetInfoResponseBody{}
err := c.DoRequest(ctx, http.MethodGet, c.ExpandPath("status"), nil, resBody)
if err != nil {
return nil, fmt.Errorf("failed to get status of the node \"%s\": %w", c.NodeName, err)
}
if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}
return resBody.Data, nil
}