terraform/internal/policy/callback/callback.go
Samsondeen 42fe4265f2
Some checks failed
build / Determine intended Terraform version (push) Has been cancelled
build / Determine Go toolchain version (push) Has been cancelled
Quick Checks / Unit Tests (push) Has been cancelled
Quick Checks / Race Tests (push) Has been cancelled
Quick Checks / End-to-end Tests (push) Has been cancelled
Quick Checks / Code Consistency Checks (push) Has been cancelled
Quick Checks / Automated defect checks (push) Has been cancelled
build / Generate release metadata (push) Has been cancelled
build / Build for freebsd_386 (push) Has been cancelled
build / Build for linux_386 (push) Has been cancelled
build / Build for openbsd_386 (push) Has been cancelled
build / Build for windows_386 (push) Has been cancelled
build / Build for darwin_amd64 (push) Has been cancelled
build / Build for freebsd_amd64 (push) Has been cancelled
build / Build for linux_amd64 (push) Has been cancelled
build / Build for openbsd_amd64 (push) Has been cancelled
build / Build for solaris_amd64 (push) Has been cancelled
build / Build for windows_amd64 (push) Has been cancelled
build / Build for freebsd_arm (push) Has been cancelled
build / Build for linux_arm (push) Has been cancelled
build / Build for darwin_arm64 (push) Has been cancelled
build / Build for linux_arm64 (push) Has been cancelled
build / Build for windows_arm64 (push) Has been cancelled
build / Build for linux_s390x (push) Has been cancelled
build / Build Docker image for linux_386 (push) Has been cancelled
build / Build Docker image for linux_amd64 (push) Has been cancelled
build / Build Docker image for linux_arm (push) Has been cancelled
build / Build Docker image for linux_arm64 (push) Has been cancelled
build / Build Docker image for linux_s390x (push) Has been cancelled
build / Build e2etest for linux_386 (push) Has been cancelled
build / Build e2etest for windows_386 (push) Has been cancelled
build / Build e2etest for darwin_amd64 (push) Has been cancelled
build / Build e2etest for linux_amd64 (push) Has been cancelled
build / Build e2etest for windows_amd64 (push) Has been cancelled
build / Build e2etest for linux_arm (push) Has been cancelled
build / Build e2etest for darwin_arm64 (push) Has been cancelled
build / Build e2etest for linux_arm64 (push) Has been cancelled
build / Run e2e test for linux_386 (push) Has been cancelled
build / Run e2e test for windows_386 (push) Has been cancelled
build / Run e2e test for darwin_amd64 (push) Has been cancelled
build / Run e2e test for linux_amd64 (push) Has been cancelled
build / Run e2e test for windows_amd64 (push) Has been cancelled
build / Run e2e test for linux_arm (push) Has been cancelled
build / Run e2e test for linux_arm64 (push) Has been cancelled
build / Run terraform-exec test for linux amd64 (push) Has been cancelled
Add policy client and protocol plumbing (#38514)
2026-05-21 13:25:15 +02:00

65 lines
1.5 KiB
Go

// Copyright IBM Corp. 2014, 2026
// SPDX-License-Identifier: BUSL-1.1
package callback
import (
"sync"
"sync/atomic"
"github.com/zclconf/go-cty/cty"
)
type Functions struct {
GetResources func(resource string, attrs cty.Value) ([]cty.Value, error)
GetDataSource func(datasource string, attrs cty.Value) (cty.Value, error)
}
// Registry is an interface for managing callback functions for resources and
// data sources during policy evaluation.
type Registry interface {
Get(id uint32) (Functions, bool)
NextID() uint32
Register(id uint32, fns Functions)
Unregister(id uint32)
}
var _ Registry = (*InternalRegistry)(nil)
// InternalRegistry stores a mapping of evaluation IDs to callback functions,
// allowing resources to register functions that will be called during their
// policy evaluation.
type InternalRegistry struct {
lock sync.RWMutex
provider map[uint32]Functions
counter uint32
}
func NewRegistry() *InternalRegistry {
return &InternalRegistry{
provider: make(map[uint32]Functions),
}
}
func (s *InternalRegistry) Register(id uint32, fns Functions) {
s.lock.Lock()
defer s.lock.Unlock()
s.provider[id] = fns
}
func (s *InternalRegistry) Unregister(id uint32) {
s.lock.Lock()
defer s.lock.Unlock()
delete(s.provider, id)
}
func (s *InternalRegistry) Get(id uint32) (Functions, bool) {
s.lock.RLock()
defer s.lock.RUnlock()
fns, ok := s.provider[id]
return fns, ok
}
func (s *InternalRegistry) NextID() uint32 {
return atomic.AddUint32(&s.counter, 1)
}