This commit is contained in:
allanice001
2025-09-03 22:49:28 +01:00
parent 816e11dbd4
commit 4e254fc569
9 changed files with 1115 additions and 193 deletions

View File

@@ -4,6 +4,27 @@ import (
"github.com/google/uuid"
)
type createNodePoolRequest struct {
Name string `json:"name"`
ServerIDs []string `json:"server_ids"`
}
type updateNodePoolRequest struct {
Name *string `json:"name"`
}
type attachServersRequest struct {
ServerIDs []string `json:"server_ids"`
}
type attachLabelsRequest struct {
LabelIDs []string `json:"label_ids"`
}
type attachTaintsRequest struct {
TaintIDs []string `json:"taint_ids"`
}
type nodePoolResponse struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
@@ -12,34 +33,10 @@ type nodePoolResponse struct {
type serverBrief struct {
ID uuid.UUID `json:"id"`
Hostname string `json:"hostname"`
IP string `json:"ip"`
Role string `json:"role"`
Status string `json:"status"`
}
type createNodePoolRequest struct {
Name string `json:"name"`
ServerIDs []string `json:"server_ids,omitempty"` // optional initial servers
}
type updateNodePoolRequest struct {
Name *string `json:"name,omitempty"`
}
type attachServersRequest struct {
ServerIDs []string `json:"server_ids"`
}
type taintBrief struct {
ID uuid.UUID `json:"id"`
Key string `json:"key"`
Value string `json:"value"`
Effect string `json:"effect"`
}
type attachTaintsRequest struct {
TaintIDs []string `json:"taint_ids"`
Hostname string `json:"hostname,omitempty"`
IP string `json:"ip,omitempty"`
Role string `json:"role,omitempty"`
Status string `json:"status,omitempty"`
}
type labelBrief struct {
@@ -48,16 +45,9 @@ type labelBrief struct {
Value string `json:"value"`
}
type attachLabelsRequest struct {
LabelIDs []string `json:"label_ids"`
}
type annotationBrief struct {
ID uuid.UUID `json:"id"`
Key string `json:"key"`
Value string `json:"value"`
}
type attachAnnotationsRequest struct {
AnnotationIDs []string `json:"annotation_ids"`
type taintBrief struct {
ID uuid.UUID `json:"id"`
Key string `json:"key"`
Value string `json:"value"`
Effect string `json:"effect"`
}

View File

@@ -2,8 +2,6 @@ package nodepools
import (
"errors"
"fmt"
"strings"
"github.com/glueops/autoglue/internal/db"
"github.com/glueops/autoglue/internal/db/models"
@@ -11,14 +9,14 @@ import (
)
func toResp(ng models.NodePool, includeServers bool) nodePoolResponse {
resp := nodePoolResponse{
out := nodePoolResponse{
ID: ng.ID,
Name: ng.Name,
}
if includeServers {
resp.Servers = make([]serverBrief, 0, len(ng.Servers))
out.Servers = make([]serverBrief, 0, len(ng.Servers))
for _, s := range ng.Servers {
resp.Servers = append(resp.Servers, serverBrief{
out.Servers = append(out.Servers, serverBrief{
ID: s.ID,
Hostname: s.Hostname,
IP: s.IPAddress,
@@ -27,17 +25,17 @@ func toResp(ng models.NodePool, includeServers bool) nodePoolResponse {
})
}
}
return resp
return out
}
func parseUUIDs(ids []string) ([]uuid.UUID, error) {
out := make([]uuid.UUID, 0, len(ids))
for _, s := range ids {
u, err := uuid.Parse(strings.TrimSpace(s))
for _, raw := range ids {
id, err := uuid.Parse(raw)
if err != nil {
return nil, err
}
out = append(out, u)
out = append(out, id)
}
return out, nil
}
@@ -50,15 +48,25 @@ func ensureServersBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
return err
}
if count != int64(len(ids)) {
return fmt.Errorf("some servers do not belong to this organization")
return errors.New("some servers do not belong to org")
}
return nil
}
func ensureLabelsBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
var count int64
if err := db.DB.Model(&models.Label{}).
Where("organization_id = ? AND id IN ?", orgID, ids).
Count(&count).Error; err != nil {
return err
}
if count != int64(len(ids)) {
return errors.New("some labels do not belong to org")
}
return nil
}
func ensureTaintsBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
if len(ids) == 0 {
return nil
}
var count int64
if err := db.DB.Model(&models.Taint{}).
Where("organization_id = ? AND id IN ?", orgID, ids).
@@ -66,39 +74,7 @@ func ensureTaintsBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
return err
}
if count != int64(len(ids)) {
return errors.New("some taints not in organization")
}
return nil
}
func ensureLabelsBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
if len(ids) == 0 {
return nil
}
var cnt int64
if err := db.DB.Model(&models.Label{}).
Where("organization_id = ? AND id IN ?", orgID, ids).
Count(&cnt).Error; err != nil {
return err
}
if cnt != int64(len(ids)) {
return errors.New("one or more labels not in organization")
}
return nil
}
func ensureAnnotationsBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
if len(ids) == 0 {
return nil
}
var cnt int64
if err := db.DB.Model(&models.Annotation{}).
Where("organization_id = ? AND id IN ?", orgID, ids).
Count(&cnt).Error; err != nil {
return err
}
if cnt != int64(len(ids)) {
return errors.New("one or more annotations not in organization")
return errors.New("some taints do not belong to org")
}
return nil
}