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/cluster/ha/resources/resources.go
Pavel Boldyrev 5ecf135398
chore(code): fix proxmox package dependencies (#536)
move `types` back from `internal` to `proxmox` and adjust a few other types, to make sure `proxmox` package is not dependent on anything else, and therefore can be extracted to a separate repo (#423)
2023-09-03 00:40:47 +00:00

93 lines
2.8 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 resources
import (
"context"
"fmt"
"net/http"
"net/url"
"sort"
"github.com/bpg/terraform-provider-proxmox/proxmox/api"
types2 "github.com/bpg/terraform-provider-proxmox/proxmox/types"
)
type haResourceTypeListQuery struct {
ResType *types2.HAResourceType `url:"type"`
}
// List retrieves the list of HA resources. If the `resType` argument is `nil`, all resources will be returned;
// otherwise resources will be filtered by the specified type (either `ct` or `vm`).
func (c *Client) List(ctx context.Context, resType *types2.HAResourceType) ([]*HAResourceListResponseData, error) {
options := &haResourceTypeListQuery{resType}
resBody := &HAResourceListResponseBody{}
err := c.DoRequest(ctx, http.MethodGet, c.ExpandPath(""), options, resBody)
if err != nil {
return nil, fmt.Errorf("error listing HA resources: %w", err)
}
if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}
sort.Slice(resBody.Data, func(i, j int) bool {
return resBody.Data[i].ID.Type < resBody.Data[j].ID.Type ||
(resBody.Data[i].ID.Type == resBody.Data[j].ID.Type &&
resBody.Data[i].ID.Name < resBody.Data[j].ID.Name)
})
return resBody.Data, nil
}
// Get retrieves the configuration of a single HA resource.
func (c *Client) Get(ctx context.Context, id types2.HAResourceID) (*HAResourceGetResponseData, error) {
resBody := &HAResourceGetResponseBody{}
err := c.DoRequest(ctx, http.MethodGet, c.ExpandPath(url.PathEscape(id.String())), nil, resBody)
if err != nil {
return nil, fmt.Errorf("error reading HA resource: %w", err)
}
if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}
return resBody.Data, nil
}
// Create creates a new HA resource.
func (c *Client) Create(ctx context.Context, data *HAResourceCreateRequestBody) error {
err := c.DoRequest(ctx, http.MethodPost, c.ExpandPath(""), data, nil)
if err != nil {
return fmt.Errorf("error creating HA resource: %w", err)
}
return nil
}
// Update updates an existing HA resource.
func (c *Client) Update(ctx context.Context, id types2.HAResourceID, data *HAResourceUpdateRequestBody) error {
err := c.DoRequest(ctx, http.MethodPut, c.ExpandPath(url.PathEscape(id.String())), data, nil)
if err != nil {
return fmt.Errorf("error updating HA resource %v: %w", id, err)
}
return nil
}
// Delete deletes a HA resource.
func (c *Client) Delete(ctx context.Context, id types2.HAResourceID) error {
err := c.DoRequest(ctx, http.MethodDelete, c.ExpandPath(url.PathEscape(id.String())), nil, nil)
if err != nil {
return fmt.Errorf("error deleting HA resource %v: %w", id, err)
}
return nil
}