Skip to content

Commit

Permalink
all: Initial support for protocol version 5.4 and 6.4
Browse files Browse the repository at this point in the history
Reference: #185
  • Loading branch information
bflad committed Aug 25, 2023
1 parent 17416fc commit 13d82bc
Show file tree
Hide file tree
Showing 38 changed files with 1,513 additions and 119 deletions.
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/google/go-cmp v0.5.9
github.com/hashicorp/terraform-plugin-go v0.18.0
github.com/hashicorp/terraform-plugin-go v0.18.1-0.20230824194237-31d190886564
github.com/hashicorp/terraform-plugin-log v0.9.0
)

Expand All @@ -14,7 +14,7 @@ require (
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-plugin v1.4.10 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/terraform-registry-address v0.2.1 // indirect
github.com/hashicorp/terraform-registry-address v0.2.2 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
Expand All @@ -23,10 +23,10 @@ require (
github.com/oklog/run v1.0.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.56.1 // indirect
golang.org/x/net v0.13.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ github.com/hashicorp/go-plugin v1.4.10 h1:xUbmA4jC6Dq163/fWcp8P3JuHilrHHMLNRxzGQ
github.com/hashicorp/go-plugin v1.4.10/go.mod h1:6/1TEzT0eQznvI/gV2CM29DLSkAK/e58mUWKVsPaph0=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/terraform-plugin-go v0.18.0 h1:IwTkOS9cOW1ehLd/rG0y+u/TGLK9y6fGoBjXVUquzpE=
github.com/hashicorp/terraform-plugin-go v0.18.0/go.mod h1:l7VK+2u5Kf2y+A+742GX0ouLut3gttudmvMgN0PA74Y=
github.com/hashicorp/terraform-plugin-go v0.18.1-0.20230824194237-31d190886564 h1:INRLdX7mCAVNzcTweLg85StUa0LmaAsl8eBBboOE+r8=
github.com/hashicorp/terraform-plugin-go v0.18.1-0.20230824194237-31d190886564/go.mod h1:OLYUIu74227VwdQiU14UBqqFXac3reHV2YrWTCMAWPk=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
github.com/hashicorp/terraform-registry-address v0.2.1 h1:QuTf6oJ1+WSflJw6WYOHhLgwUiQ0FrROpHPYFtwTYWM=
github.com/hashicorp/terraform-registry-address v0.2.1/go.mod h1:BSE9fIFzp0qWsJUUyGquo4ldV9k2n+psif6NYkBRS3Y=
github.com/hashicorp/terraform-registry-address v0.2.2 h1:lPQBg403El8PPicg/qONZJDC6YlgCVbWDtNmmZKtBno=
github.com/hashicorp/terraform-registry-address v0.2.2/go.mod h1:LtwNbCihUoUZ3RYriyS2wF/lGPB6gF9ICLRtuDk7hSo=
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M=
Expand All @@ -46,22 +46,22 @@ github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU=
golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ=
golang.org/x/net v0.13.0 h1:Nvo8UFsZ8X3BhAC9699Z1j7XQ3rsZnUUm7jfBEk1ueY=
golang.org/x/net v0.13.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 h1:KpwkzHKEF7B9Zxg18WzOa7djJ+Ha5DzthMyZYQfEn2A=
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1/go.mod h1:nKE/iIaLqn2bQwXBg8f1g2Ylh6r5MN5CmZvuzZCgsCU=
google.golang.org/grpc v1.56.1 h1:z0dNfjIl0VpaZ9iSVjA6daGatAYwPGstTjt5vkRMFkQ=
google.golang.org/grpc v1.56.1/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 h1:0nDDozoAU19Qb2HwhXadU8OcsiO/09cnTqhUtq2MEOM=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
Expand Down
13 changes: 13 additions & 0 deletions internal/tf5testserver/tf5testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type TestServer struct {
ConfigureProviderCalled bool
ConfigureProviderResponse *tfprotov5.ConfigureProviderResponse

GetMetadataCalled bool
GetMetadataResponse *tfprotov5.GetMetadataResponse

GetProviderSchemaCalled bool
GetProviderSchemaResponse *tfprotov5.GetProviderSchemaResponse

Expand Down Expand Up @@ -64,6 +67,16 @@ func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov5.Configure
return &tfprotov5.ConfigureProviderResponse{}, nil
}

func (s *TestServer) GetMetadata(_ context.Context, _ *tfprotov5.GetMetadataRequest) (*tfprotov5.GetMetadataResponse, error) {
s.GetMetadataCalled = true

if s.GetMetadataResponse != nil {
return s.GetMetadataResponse, nil
}

return &tfprotov5.GetMetadataResponse{}, nil
}

func (s *TestServer) GetProviderSchema(_ context.Context, _ *tfprotov5.GetProviderSchemaRequest) (*tfprotov5.GetProviderSchemaResponse, error) {
s.GetProviderSchemaCalled = true

Expand Down
13 changes: 13 additions & 0 deletions internal/tf6testserver/tf6testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type TestServer struct {
ConfigureProviderCalled bool
ConfigureProviderResponse *tfprotov6.ConfigureProviderResponse

GetMetadataCalled bool
GetMetadataResponse *tfprotov6.GetMetadataResponse

GetProviderSchemaCalled bool
GetProviderSchemaResponse *tfprotov6.GetProviderSchemaResponse

Expand Down Expand Up @@ -64,6 +67,16 @@ func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov6.Configure
return &tfprotov6.ConfigureProviderResponse{}, nil
}

func (s *TestServer) GetMetadata(_ context.Context, _ *tfprotov6.GetMetadataRequest) (*tfprotov6.GetMetadataResponse, error) {
s.GetMetadataCalled = true

if s.GetMetadataResponse != nil {
return s.GetMetadataResponse, nil
}

return &tfprotov6.GetMetadataResponse{}, nil
}

func (s *TestServer) GetProviderSchema(_ context.Context, _ *tfprotov6.GetProviderSchemaRequest) (*tfprotov6.GetProviderSchemaResponse, error) {
s.GetProviderSchemaCalled = true

Expand Down
65 changes: 60 additions & 5 deletions internal/tfprotov5tov6/tfprotov5tov6.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func ConfigureProviderResponse(in *tfprotov5.ConfigureProviderResponse) *tfproto
}
}

func DataSourceMetadata(in tfprotov5.DataSourceMetadata) tfprotov6.DataSourceMetadata {
return tfprotov6.DataSourceMetadata{
TypeName: in.TypeName,
}
}

func Diagnostics(in []*tfprotov5.Diagnostic) []*tfprotov6.Diagnostic {
if in == nil {
return nil
Expand Down Expand Up @@ -92,6 +98,37 @@ func DynamicValue(in *tfprotov5.DynamicValue) *tfprotov6.DynamicValue {
}
}

func GetMetadataRequest(in *tfprotov5.GetMetadataRequest) *tfprotov6.GetMetadataRequest {
if in == nil {
return nil
}

return &tfprotov6.GetMetadataRequest{}
}

func GetMetadataResponse(in *tfprotov5.GetMetadataResponse) *tfprotov6.GetMetadataResponse {
if in == nil {
return nil
}

resp := &tfprotov6.GetMetadataResponse{
DataSources: make([]tfprotov6.DataSourceMetadata, 0, len(in.DataSources)),
Diagnostics: Diagnostics(in.Diagnostics),
Resources: make([]tfprotov6.ResourceMetadata, 0, len(in.Resources)),
ServerCapabilities: ServerCapabilities(in.ServerCapabilities),
}

for _, datasource := range in.DataSources {
resp.DataSources = append(resp.DataSources, DataSourceMetadata(datasource))
}

for _, resource := range in.Resources {
resp.Resources = append(resp.Resources, ResourceMetadata(resource))
}

return resp
}

func GetProviderSchemaRequest(in *tfprotov5.GetProviderSchemaRequest) *tfprotov6.GetProviderSchemaRequest {
if in == nil {
return nil
Expand All @@ -118,11 +155,12 @@ func GetProviderSchemaResponse(in *tfprotov5.GetProviderSchemaResponse) *tfproto
}

return &tfprotov6.GetProviderSchemaResponse{
DataSourceSchemas: dataSourceSchemas,
Diagnostics: Diagnostics(in.Diagnostics),
Provider: Schema(in.Provider),
ProviderMeta: Schema(in.ProviderMeta),
ResourceSchemas: resourceSchemas,
DataSourceSchemas: dataSourceSchemas,
Diagnostics: Diagnostics(in.Diagnostics),
Provider: Schema(in.Provider),
ProviderMeta: Schema(in.ProviderMeta),
ResourceSchemas: resourceSchemas,
ServerCapabilities: ServerCapabilities(in.ServerCapabilities),
}
}

Expand Down Expand Up @@ -258,6 +296,12 @@ func ReadResourceResponse(in *tfprotov5.ReadResourceResponse) *tfprotov6.ReadRes
}
}

func ResourceMetadata(in tfprotov5.ResourceMetadata) tfprotov6.ResourceMetadata {
return tfprotov6.ResourceMetadata{
TypeName: in.TypeName,
}
}

func Schema(in *tfprotov5.Schema) *tfprotov6.Schema {
if in == nil {
return nil
Expand Down Expand Up @@ -336,6 +380,17 @@ func SchemaNestedBlock(in *tfprotov5.SchemaNestedBlock) *tfprotov6.SchemaNestedB
}
}

func ServerCapabilities(in *tfprotov5.ServerCapabilities) *tfprotov6.ServerCapabilities {
if in == nil {
return nil
}

return &tfprotov6.ServerCapabilities{
GetProviderSchemaOptional: in.GetProviderSchemaOptional,
PlanDestroy: in.PlanDestroy,
}
}

func StopProviderRequest(in *tfprotov5.StopProviderRequest) *tfprotov6.StopProviderRequest {
if in == nil {
return nil
Expand Down
54 changes: 54 additions & 0 deletions internal/tfprotov6tov5/tfprotov6tov5.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func ConfigureProviderResponse(in *tfprotov6.ConfigureProviderResponse) *tfproto
}
}

func DataSourceMetadata(in tfprotov6.DataSourceMetadata) tfprotov5.DataSourceMetadata {
return tfprotov5.DataSourceMetadata{
TypeName: in.TypeName,
}
}

func Diagnostics(in []*tfprotov6.Diagnostic) []*tfprotov5.Diagnostic {
if in == nil {
return nil
Expand Down Expand Up @@ -97,6 +103,37 @@ func DynamicValue(in *tfprotov6.DynamicValue) *tfprotov5.DynamicValue {
}
}

func GetMetadataRequest(in *tfprotov6.GetMetadataRequest) *tfprotov5.GetMetadataRequest {
if in == nil {
return nil
}

return &tfprotov5.GetMetadataRequest{}
}

func GetMetadataResponse(in *tfprotov6.GetMetadataResponse) *tfprotov5.GetMetadataResponse {
if in == nil {
return nil
}

resp := &tfprotov5.GetMetadataResponse{
DataSources: make([]tfprotov5.DataSourceMetadata, 0, len(in.DataSources)),
Diagnostics: Diagnostics(in.Diagnostics),
Resources: make([]tfprotov5.ResourceMetadata, 0, len(in.Resources)),
ServerCapabilities: ServerCapabilities(in.ServerCapabilities),
}

for _, datasource := range in.DataSources {
resp.DataSources = append(resp.DataSources, DataSourceMetadata(datasource))
}

for _, resource := range in.Resources {
resp.Resources = append(resp.Resources, ResourceMetadata(resource))
}

return resp
}

func GetProviderSchemaRequest(in *tfprotov6.GetProviderSchemaRequest) *tfprotov5.GetProviderSchemaRequest {
if in == nil {
return nil
Expand Down Expand Up @@ -307,6 +344,12 @@ func ReadResourceResponse(in *tfprotov6.ReadResourceResponse) *tfprotov5.ReadRes
}
}

func ResourceMetadata(in tfprotov6.ResourceMetadata) tfprotov5.ResourceMetadata {
return tfprotov5.ResourceMetadata{
TypeName: in.TypeName,
}
}

func Schema(in *tfprotov6.Schema) (*tfprotov5.Schema, error) {
if in == nil {
return nil, nil
Expand Down Expand Up @@ -413,6 +456,17 @@ func SchemaNestedBlock(in *tfprotov6.SchemaNestedBlock) (*tfprotov5.SchemaNested
}, nil
}

func ServerCapabilities(in *tfprotov6.ServerCapabilities) *tfprotov5.ServerCapabilities {
if in == nil {
return nil
}

return &tfprotov5.ServerCapabilities{
GetProviderSchemaOptional: in.GetProviderSchemaOptional,
PlanDestroy: in.PlanDestroy,
}
}

func StopProviderRequest(in *tfprotov6.StopProviderRequest) *tfprotov5.StopProviderRequest {
if in == nil {
return nil
Expand Down
21 changes: 21 additions & 0 deletions tf5muxserver/mux_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package tf5muxserver

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-go/tfprotov5"
)
Expand Down Expand Up @@ -37,6 +38,26 @@ func (s muxServer) ProviderServer() tfprotov5.ProviderServer {
return &s
}

func (s *muxServer) getDataSourceServer(_ context.Context, typeName string) (tfprotov5.ProviderServer, error) {
server, ok := s.dataSources[typeName]

if !ok {
return nil, fmt.Errorf("data source type %q is not implemented by any servers", typeName)
}

return server, nil
}

func (s *muxServer) getResourceServer(_ context.Context, typeName string) (tfprotov5.ProviderServer, error) {
server, ok := s.resources[typeName]

if !ok {
return nil, fmt.Errorf("resource type %q is not implemented by any servers", typeName)
}

return server, nil
}

// NewMuxServer returns a muxed server that will route gRPC requests between
// tfprotov5.ProviderServers specified. The GetProviderSchema method of each
// is called to verify that the overall muxed server is compatible by ensuring:
Expand Down
8 changes: 4 additions & 4 deletions tf5muxserver/mux_server_ApplyResourceChange.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package tf5muxserver

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-mux/internal/logging"
Expand All @@ -18,10 +17,11 @@ func (s muxServer) ApplyResourceChange(ctx context.Context, req *tfprotov5.Apply
rpc := "ApplyResourceChange"
ctx = logging.InitContext(ctx)
ctx = logging.RpcContext(ctx, rpc)
server, ok := s.resources[req.TypeName]

if !ok {
return nil, fmt.Errorf("%q isn't supported by any servers", req.TypeName)
server, err := s.getResourceServer(ctx, req.TypeName)

if err != nil {
return nil, err
}

ctx = logging.Tfprotov5ProviderServerContext(ctx, server)
Expand Down
Loading

0 comments on commit 13d82bc

Please sign in to comment.