mirror of
https://github.com/hashicorp/terraform.git
synced 2026-05-28 04:03:27 -04:00
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
85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
// Copyright IBM Corp. 2014, 2026
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package callback
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
"github.com/zclconf/go-cty/cty/msgpack"
|
|
"google.golang.org/grpc"
|
|
|
|
"github.com/hashicorp/terraform/internal/policy/proto"
|
|
)
|
|
|
|
var (
|
|
_ proto.CallbackServiceServer = (*Server)(nil)
|
|
)
|
|
|
|
type Server struct {
|
|
ID uint32
|
|
Registry Registry
|
|
Grpc *grpc.Server
|
|
proto.UnimplementedCallbackServiceServer
|
|
}
|
|
|
|
func (s *Server) GetResources(_ context.Context, request *proto.GetResourcesRequest) (*proto.GetResourcesResponse, error) {
|
|
attrs, err := msgpack.Unmarshal(request.Attributes, cty.DynamicPseudoType)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to unserialize attributes: %w", err)
|
|
}
|
|
functions, ok := s.Registry.Get(request.EvaluationRequestId)
|
|
if !ok {
|
|
return nil, fmt.Errorf("no callback registered for ID %d (request type: %s)", request.EvaluationRequestId, request.Type)
|
|
}
|
|
resources, err := functions.GetResources(request.Type, attrs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
results := make([][]byte, 0, len(resources))
|
|
for _, resource := range resources {
|
|
result, err := msgpack.Marshal(resource, cty.DynamicPseudoType)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to serialize resource: %w", err)
|
|
}
|
|
results = append(results, result)
|
|
}
|
|
|
|
return &proto.GetResourcesResponse{
|
|
Results: results,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Server) GetDataSource(_ context.Context, request *proto.GetDataSourceRequest) (*proto.GetDataSourceResponse, error) {
|
|
config, err := msgpack.Unmarshal(request.Config, cty.DynamicPseudoType)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to unserialize config: %w", err)
|
|
}
|
|
|
|
functions, ok := s.Registry.Get(request.EvaluationRequestId)
|
|
if !ok {
|
|
return nil, fmt.Errorf("no callback registered for ID %d (request type: %s)", request.EvaluationRequestId, request.Type)
|
|
}
|
|
datasource, err := functions.GetDataSource(request.Type, config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result, err := msgpack.Marshal(datasource, cty.DynamicPseudoType)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to serialize datasource: %w", err)
|
|
}
|
|
|
|
return &proto.GetDataSourceResponse{
|
|
Result: result,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Server) Stop() {
|
|
if s.Grpc != nil {
|
|
s.Grpc.GracefulStop()
|
|
}
|
|
}
|