forked from TrueCloudLab/policy-engine
110 lines
2.9 KiB
Go
110 lines
2.9 KiB
Go
|
package inmemory
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"math/rand"
|
||
|
"strings"
|
||
|
|
||
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
||
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
||
|
"git.frostfs.info/TrueCloudLab/policy-engine/util"
|
||
|
)
|
||
|
|
||
|
type targetToChain map[string][]*chain.Chain
|
||
|
|
||
|
type inmemoryLocalStorage struct {
|
||
|
usedChainID map[chain.ID]struct{}
|
||
|
nameToResourceChains map[chain.Name]targetToChain
|
||
|
}
|
||
|
|
||
|
func NewInmemoryLocalStorage() engine.LocalOverrideStorage {
|
||
|
return &inmemoryLocalStorage{
|
||
|
usedChainID: map[chain.ID]struct{}{},
|
||
|
nameToResourceChains: make(map[chain.Name]targetToChain),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *inmemoryLocalStorage) generateChainID(name chain.Name, resource string) chain.ID {
|
||
|
var id chain.ID
|
||
|
for {
|
||
|
suffix := rand.Uint32() % 100
|
||
|
sid := fmt.Sprintf("%s:%s/%d", name, resource, suffix)
|
||
|
sid = strings.ReplaceAll(sid, "*", "")
|
||
|
sid = strings.ReplaceAll(sid, "/", ":")
|
||
|
sid = strings.ReplaceAll(sid, "::", ":")
|
||
|
id = chain.ID(sid)
|
||
|
_, ok := s.usedChainID[id]
|
||
|
if ok {
|
||
|
continue
|
||
|
}
|
||
|
s.usedChainID[id] = struct{}{}
|
||
|
break
|
||
|
}
|
||
|
return id
|
||
|
}
|
||
|
|
||
|
func (s *inmemoryLocalStorage) AddOverride(name chain.Name, resource string, c *chain.Chain) (chain.ID, error) {
|
||
|
// AddOverride assigns generated chain ID if it has not been assigned.
|
||
|
if c.ID == "" {
|
||
|
c.ID = s.generateChainID(name, resource)
|
||
|
}
|
||
|
if s.nameToResourceChains[name] == nil {
|
||
|
s.nameToResourceChains[name] = make(targetToChain)
|
||
|
}
|
||
|
rc := s.nameToResourceChains[name]
|
||
|
rc[resource] = append(rc[resource], c)
|
||
|
return c.ID, nil
|
||
|
}
|
||
|
|
||
|
func (s *inmemoryLocalStorage) GetOverride(name chain.Name, resource string, chainID chain.ID) (*chain.Chain, error) {
|
||
|
if _, ok := s.nameToResourceChains[name]; !ok {
|
||
|
return nil, engine.ErrChainNameNotFound
|
||
|
}
|
||
|
chains, ok := s.nameToResourceChains[name][resource]
|
||
|
if !ok {
|
||
|
return nil, engine.ErrResourceNotFound
|
||
|
}
|
||
|
for _, c := range chains {
|
||
|
if c.ID == chainID {
|
||
|
return c, nil
|
||
|
}
|
||
|
}
|
||
|
return nil, engine.ErrChainNotFound
|
||
|
}
|
||
|
|
||
|
func (s *inmemoryLocalStorage) RemoveOverride(name chain.Name, resource string, chainID chain.ID) error {
|
||
|
if _, ok := s.nameToResourceChains[name]; !ok {
|
||
|
return engine.ErrChainNameNotFound
|
||
|
}
|
||
|
chains, ok := s.nameToResourceChains[name][resource]
|
||
|
if !ok {
|
||
|
return engine.ErrResourceNotFound
|
||
|
}
|
||
|
for i, c := range chains {
|
||
|
if c.ID == chainID {
|
||
|
s.nameToResourceChains[name][resource] = append(chains[:i], chains[i+1:]...)
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
return engine.ErrChainNotFound
|
||
|
}
|
||
|
|
||
|
func (s *inmemoryLocalStorage) ListOverrides(name chain.Name, resource string) ([]*chain.Chain, error) {
|
||
|
rcs, ok := s.nameToResourceChains[name]
|
||
|
if !ok {
|
||
|
return []*chain.Chain{}, nil
|
||
|
}
|
||
|
for container, chains := range rcs {
|
||
|
if !util.GlobMatch(resource, container) {
|
||
|
continue
|
||
|
}
|
||
|
return chains, nil
|
||
|
}
|
||
|
return []*chain.Chain{}, nil
|
||
|
}
|
||
|
|
||
|
func (s *inmemoryLocalStorage) DropAllOverrides(name chain.Name) error {
|
||
|
s.nameToResourceChains[name] = make(targetToChain)
|
||
|
return nil
|
||
|
}
|