diff --git a/cluster_ha_resources.go b/cluster_ha_resources.go index 817f559..eb61c52 100644 --- a/cluster_ha_resources.go +++ b/cluster_ha_resources.go @@ -2,7 +2,10 @@ package proxmox import ( "context" + "errors" "fmt" + "strconv" + "strings" ) func (cl *Cluster) CreateHAResource(ctx context.Context, haResource HAResource) error { @@ -31,10 +34,48 @@ func (cl *Cluster) GetHAResource(ctx context.Context, sid SID) (HAResource, erro } func (cl *Cluster) ListHAResources(ctx context.Context, resourceType HAResourceType) ([]HAResource, error) { - var haResources []HAResource + var haResources []haResource if err := cl.client.Get(ctx, fmt.Sprintf("/cluster/ha/resources?type=%s", resourceType), &haResources); err != nil { return nil, err } + return fillHAResources(haResources) +} + +func fillHAResources(resources []haResource) ([]HAResource, error) { + haResources := make([]HAResource, 0, len(resources)) + for _, resource := range resources { + sid, err := parseSid(resource.Sid) + if err != nil { + return nil, fmt.Errorf("parse sid: %w", err) + } + + haResources = append(haResources, HAResource{ + ID: sid.ID, + Group: &resource.Group, + Comment: &resource.Comment, + MaxRelocate: &resource.MaxRelocate, + MaxRestart: &resource.MaxRestart, + State: &resource.State, + }) + } + return haResources, nil } + +func parseSid(sid string) (SID, error) { + sidParts := strings.Split(sid, ":") + if len(sidParts) != 2 { + return SID{}, errors.New("invalid sid parts count") + } + + vmid, err := strconv.Atoi(sidParts[1]) + if err != nil { + return SID{}, fmt.Errorf("parse sid vmid: %w", err) + } + + return SID{ + Type: HAResourceType(sidParts[0]), + ID: vmid, + }, nil +} diff --git a/types.go b/types.go index 405047e..63b5a25 100644 --- a/types.go +++ b/types.go @@ -1516,6 +1516,15 @@ type SID struct { ID int } +type haResource struct { + Group string `json:"group"` + Sid string `json:"sid"` + State HAResourceState `json:"state"` + Comment string `json:"comment"` + MaxRestart uint `json:"max_restart"` + MaxRelocate uint `json:"max_relocate"` +} + type HAResource struct { ID int `json:"sid"` Group *string `json:"group"`