mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 13:29:41 +02:00
feat(waiting-room): choose relations when associated endpoint [EE-5187] (#8720)
This commit is contained in:
parent
511adabce2
commit
365316971b
53 changed files with 1712 additions and 303 deletions
72
api/http/handler/endpoints/utils_update_edge_groups.go
Normal file
72
api/http/handler/endpoints/utils_update_edge_groups.go
Normal file
|
@ -0,0 +1,72 @@
|
|||
package endpoints
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
"github.com/portainer/portainer/api/internal/set"
|
||||
"github.com/portainer/portainer/api/internal/slices"
|
||||
)
|
||||
|
||||
func updateEnvironmentEdgeGroups(tx dataservices.DataStoreTx, newEdgeGroups []portainer.EdgeGroupID, environmentID portainer.EndpointID) (bool, error) {
|
||||
edgeGroups, err := tx.EdgeGroup().EdgeGroups()
|
||||
if err != nil {
|
||||
return false, errors.WithMessage(err, "Unable to retrieve edge groups from the database")
|
||||
}
|
||||
|
||||
newEdgeGroupsSet := set.ToSet(newEdgeGroups)
|
||||
|
||||
environmentEdgeGroupsSet := set.Set[portainer.EdgeGroupID]{}
|
||||
for _, edgeGroup := range edgeGroups {
|
||||
for _, eID := range edgeGroup.Endpoints {
|
||||
if eID == environmentID {
|
||||
environmentEdgeGroupsSet[edgeGroup.ID] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
union := set.Union(newEdgeGroupsSet, environmentEdgeGroupsSet)
|
||||
intersection := set.Intersection(newEdgeGroupsSet, environmentEdgeGroupsSet)
|
||||
|
||||
if len(union) <= len(intersection) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
updateSet := func(groupIDs set.Set[portainer.EdgeGroupID], updateItem func(*portainer.EdgeGroup)) error {
|
||||
for groupID := range groupIDs {
|
||||
group, err := tx.EdgeGroup().EdgeGroup(groupID)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "Unable to find a Edge group inside the database")
|
||||
}
|
||||
|
||||
updateItem(group)
|
||||
|
||||
err = tx.EdgeGroup().UpdateEdgeGroup(groupID, group)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "Unable to persist Edge group changes inside the database")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
removeEdgeGroups := environmentEdgeGroupsSet.Difference(newEdgeGroupsSet)
|
||||
err = updateSet(removeEdgeGroups, func(edgeGroup *portainer.EdgeGroup) {
|
||||
edgeGroup.Endpoints = slices.RemoveItem(edgeGroup.Endpoints, func(eID portainer.EndpointID) bool {
|
||||
return eID == environmentID
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
addToEdgeGroups := newEdgeGroupsSet.Difference(environmentEdgeGroupsSet)
|
||||
err = updateSet(addToEdgeGroups, func(edgeGroup *portainer.EdgeGroup) {
|
||||
edgeGroup.Endpoints = append(edgeGroup.Endpoints, environmentID)
|
||||
})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue