Skip to content

Commit

Permalink
change files
Browse files Browse the repository at this point in the history
  • Loading branch information
Pivnoy committed Oct 25, 2024
1 parent 5bea9b4 commit eea9958
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
43 changes: 42 additions & 1 deletion cluster_ha_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package proxmox

import (
"context"
"errors"
"fmt"
"strconv"
"strings"
)

func (cl *Cluster) CreateHAResource(ctx context.Context, haResource HAResource) error {
Expand Down Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit eea9958

Please sign in to comment.