mirror of
https://github.com/GlueOps/autoglue.git
synced 2026-02-13 12:50:05 +01:00
97 lines
2.2 KiB
Go
97 lines
2.2 KiB
Go
package nodepools
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/glueops/autoglue/internal/db"
|
|
"github.com/glueops/autoglue/internal/db/models"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func toResp(ng models.NodePool, includeServers bool) nodePoolResponse {
|
|
out := nodePoolResponse{
|
|
ID: ng.ID,
|
|
Name: ng.Name,
|
|
}
|
|
if includeServers {
|
|
out.Servers = make([]serverBrief, 0, len(ng.Servers))
|
|
for _, s := range ng.Servers {
|
|
out.Servers = append(out.Servers, serverBrief{
|
|
ID: s.ID,
|
|
Hostname: s.Hostname,
|
|
IP: s.IPAddress,
|
|
Role: s.Role,
|
|
Status: s.Status,
|
|
})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func parseUUIDs(ids []string) ([]uuid.UUID, error) {
|
|
out := make([]uuid.UUID, 0, len(ids))
|
|
for _, raw := range ids {
|
|
id, err := uuid.Parse(raw)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, id)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func ensureServersBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
|
|
var count int64
|
|
if err := db.DB.Model(&models.Server{}).
|
|
Where("organization_id = ? AND id IN ?", orgID, ids).
|
|
Count(&count).Error; err != nil {
|
|
return err
|
|
}
|
|
if count != int64(len(ids)) {
|
|
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 {
|
|
var count int64
|
|
if err := db.DB.Model(&models.Taint{}).
|
|
Where("organization_id = ? AND id IN ?", orgID, ids).
|
|
Count(&count).Error; err != nil {
|
|
return err
|
|
}
|
|
if count != int64(len(ids)) {
|
|
return errors.New("some taints do not belong to org")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ensureAnnotationsBelongToOrg(orgID uuid.UUID, ids []uuid.UUID) error {
|
|
if len(ids) == 0 {
|
|
return errors.New("empty ids")
|
|
}
|
|
var count int64
|
|
if err := db.DB.Model(&models.Annotation{}).
|
|
Where("organization_id = ? AND id IN ?", orgID, ids).
|
|
Count(&count).Error; err != nil {
|
|
return err
|
|
}
|
|
if count != int64(len(ids)) {
|
|
return errors.New("some annotations not in org")
|
|
}
|
|
return nil
|
|
}
|