@@ -10,28 +10,21 @@ import (
1010 "github.com/hashicorp/go-cty/cty"
1111 ctyconvert "github.com/hashicorp/go-cty/cty/convert"
1212 "github.com/hashicorp/go-cty/cty/msgpack"
13- "github.com/hashicorp/terraform-plugin-log/tflog"
14- "github.com/hashicorp/terraform-plugin-log/tfsdklog"
1513
1614 "github.com/hashicorp/terraform-plugin-go/tfprotov5"
1715 "github.com/hashicorp/terraform-plugin-go/tftypes"
1816 "github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema"
1917 "github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/hcl2shim"
18+ "github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
2019 "github.com/hashicorp/terraform-plugin-sdk/v2/internal/plans/objchange"
2120 "github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugin/convert"
2221 "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
2322)
2423
2524const (
26- newExtraKey = "_new_extra_shim"
27- tflogSubsystemName = "helper_schema"
25+ newExtraKey = "_new_extra_shim"
2826)
2927
30- func withLogger (ctx context.Context ) context.Context {
31- return tfsdklog .NewSubsystem (ctx , tflogSubsystemName ,
32- tfsdklog .WithLevelFromEnv ("TF_LOG_SDK_HELPER_SCHEMA" ))
33- }
34-
3528func NewGRPCProviderServer (p * Provider ) * GRPCProviderServer {
3629 return & GRPCProviderServer {
3730 provider : p ,
@@ -62,7 +55,7 @@ func mergeStop(ctx context.Context, cancel context.CancelFunc, stopCh chan struc
6255// It creates a goroutine to wait for the server stop and propagates
6356// cancellation to the derived grpc context.
6457func (s * GRPCProviderServer ) StopContext (ctx context.Context ) context.Context {
65- ctx = withLogger (ctx )
58+ ctx = logging . InitContext (ctx )
6659 s .stopMu .Lock ()
6760 defer s .stopMu .Unlock ()
6861
@@ -72,7 +65,9 @@ func (s *GRPCProviderServer) StopContext(ctx context.Context) context.Context {
7265}
7366
7467func (s * GRPCProviderServer ) GetProviderSchema (ctx context.Context , req * tfprotov5.GetProviderSchemaRequest ) (* tfprotov5.GetProviderSchemaResponse , error ) {
75- ctx = withLogger (ctx )
68+ ctx = logging .InitContext (ctx )
69+
70+ logging .HelperSchemaTrace (ctx , "Getting provider schema" )
7671
7772 resp := & tfprotov5.GetProviderSchemaResponse {
7873 ResourceSchemas : make (map [string ]* tfprotov5.Schema ),
@@ -88,13 +83,17 @@ func (s *GRPCProviderServer) GetProviderSchema(ctx context.Context, req *tfproto
8883 }
8984
9085 for typ , res := range s .provider .ResourcesMap {
86+ logging .HelperSchemaTrace (ctx , "Found resource type" , logging .KeyResourceType , typ )
87+
9188 resp .ResourceSchemas [typ ] = & tfprotov5.Schema {
9289 Version : int64 (res .SchemaVersion ),
9390 Block : convert .ConfigSchemaToProto (res .CoreConfigSchema ()),
9491 }
9592 }
9693
9794 for typ , dat := range s .provider .DataSourcesMap {
95+ logging .HelperSchemaTrace (ctx , "Found data source type" , logging .KeyDataSourceType , typ )
96+
9897 resp .DataSourceSchemas [typ ] = & tfprotov5.Schema {
9998 Version : int64 (dat .SchemaVersion ),
10099 Block : convert .ConfigSchemaToProto (dat .CoreConfigSchema ()),
@@ -123,9 +122,11 @@ func (s *GRPCProviderServer) getDatasourceSchemaBlock(name string) *configschema
123122}
124123
125124func (s * GRPCProviderServer ) PrepareProviderConfig (ctx context.Context , req * tfprotov5.PrepareProviderConfigRequest ) (* tfprotov5.PrepareProviderConfigResponse , error ) {
126- ctx = withLogger (ctx )
125+ ctx = logging . InitContext (ctx )
127126 resp := & tfprotov5.PrepareProviderConfigResponse {}
128127
128+ logging .HelperSchemaTrace (ctx , "Preparing provider configuration" )
129+
129130 schemaBlock := s .getProviderSchemaBlock ()
130131
131132 configVal , err := msgpack .Unmarshal (req .Config .MsgPack , schemaBlock .ImpliedType ())
@@ -212,7 +213,9 @@ func (s *GRPCProviderServer) PrepareProviderConfig(ctx context.Context, req *tfp
212213
213214 config := terraform .NewResourceConfigShimmed (configVal , schemaBlock )
214215
216+ logging .HelperSchemaTrace (ctx , "Calling downstream" )
215217 resp .Diagnostics = convert .AppendProtoDiag (resp .Diagnostics , s .provider .Validate (config ))
218+ logging .HelperSchemaTrace (ctx , "Called downstream" )
216219
217220 preparedConfigMP , err := msgpack .Marshal (configVal , schemaBlock .ImpliedType ())
218221 if err != nil {
@@ -226,7 +229,7 @@ func (s *GRPCProviderServer) PrepareProviderConfig(ctx context.Context, req *tfp
226229}
227230
228231func (s * GRPCProviderServer ) ValidateResourceTypeConfig (ctx context.Context , req * tfprotov5.ValidateResourceTypeConfigRequest ) (* tfprotov5.ValidateResourceTypeConfigResponse , error ) {
229- ctx = withLogger (ctx )
232+ ctx = logging . InitContext (ctx )
230233 resp := & tfprotov5.ValidateResourceTypeConfigResponse {}
231234
232235 schemaBlock := s .getResourceSchemaBlock (req .TypeName )
@@ -239,13 +242,15 @@ func (s *GRPCProviderServer) ValidateResourceTypeConfig(ctx context.Context, req
239242
240243 config := terraform .NewResourceConfigShimmed (configVal , schemaBlock )
241244
245+ logging .HelperSchemaTrace (ctx , "Calling downstream" )
242246 resp .Diagnostics = convert .AppendProtoDiag (resp .Diagnostics , s .provider .ValidateResource (req .TypeName , config ))
247+ logging .HelperSchemaTrace (ctx , "Called downstream" )
243248
244249 return resp , nil
245250}
246251
247252func (s * GRPCProviderServer ) ValidateDataSourceConfig (ctx context.Context , req * tfprotov5.ValidateDataSourceConfigRequest ) (* tfprotov5.ValidateDataSourceConfigResponse , error ) {
248- ctx = withLogger (ctx )
253+ ctx = logging . InitContext (ctx )
249254 resp := & tfprotov5.ValidateDataSourceConfigResponse {}
250255
251256 schemaBlock := s .getDatasourceSchemaBlock (req .TypeName )
@@ -264,13 +269,15 @@ func (s *GRPCProviderServer) ValidateDataSourceConfig(ctx context.Context, req *
264269
265270 config := terraform .NewResourceConfigShimmed (configVal , schemaBlock )
266271
272+ logging .HelperSchemaTrace (ctx , "Calling downstream" )
267273 resp .Diagnostics = convert .AppendProtoDiag (resp .Diagnostics , s .provider .ValidateDataSource (req .TypeName , config ))
274+ logging .HelperSchemaTrace (ctx , "Called downstream" )
268275
269276 return resp , nil
270277}
271278
272279func (s * GRPCProviderServer ) UpgradeResourceState (ctx context.Context , req * tfprotov5.UpgradeResourceStateRequest ) (* tfprotov5.UpgradeResourceStateResponse , error ) {
273- ctx = withLogger (ctx )
280+ ctx = logging . InitContext (ctx )
274281 resp := & tfprotov5.UpgradeResourceStateResponse {}
275282
276283 res , ok := s .provider .ResourcesMap [req .TypeName ]
@@ -289,6 +296,8 @@ func (s *GRPCProviderServer) UpgradeResourceState(ctx context.Context, req *tfpr
289296 // We first need to upgrade a flatmap state if it exists.
290297 // There should never be both a JSON and Flatmap state in the request.
291298 case len (req .RawState .Flatmap ) > 0 :
299+ logging .HelperSchemaTrace (ctx , "Upgrading flatmap state" )
300+
292301 jsonMap , version , err = s .upgradeFlatmapState (ctx , version , req .RawState .Flatmap , res )
293302 if err != nil {
294303 resp .Diagnostics = convert .AppendProtoDiag (resp .Diagnostics , err )
@@ -306,11 +315,13 @@ func (s *GRPCProviderServer) UpgradeResourceState(ctx context.Context, req *tfpr
306315 return resp , nil
307316 }
308317 default :
309- tfsdklog . SubsystemDebug (ctx , tflogSubsystemName , "no state provided to upgrade" )
318+ logging . HelperSchemaDebug (ctx , "no state provided to upgrade" )
310319 return resp , nil
311320 }
312321
313322 // complete the upgrade of the JSON states
323+ logging .HelperSchemaTrace (ctx , "Upgrading JSON state" )
324+
314325 jsonMap , err = s .upgradeJSONState (ctx , version , jsonMap , res )
315326 if err != nil {
316327 resp .Diagnostics = convert .AppendProtoDiag (resp .Diagnostics , err )
@@ -471,22 +482,22 @@ func (s *GRPCProviderServer) removeAttributes(ctx context.Context, v interface{}
471482 }
472483
473484 if ty == cty .DynamicPseudoType {
474- tflog . SubsystemDebug (ctx , tflogSubsystemName , "ignoring dynamic block" , "block" , v )
485+ logging . HelperSchemaDebug (ctx , "ignoring dynamic block" , "block" , v )
475486 return
476487 }
477488
478489 if ! ty .IsObjectType () {
479490 // This shouldn't happen, and will fail to decode further on, so
480491 // there's no need to handle it here.
481- tflog . SubsystemWarn (ctx , tflogSubsystemName , "unexpected type for map in JSON state" , "type" , ty )
492+ logging . HelperSchemaWarn (ctx , "unexpected type for map in JSON state" , "type" , ty )
482493 return
483494 }
484495
485496 attrTypes := ty .AttributeTypes ()
486497 for attr , attrV := range v {
487498 attrTy , ok := attrTypes [attr ]
488499 if ! ok {
489- tflog . SubsystemDebug (ctx , tflogSubsystemName , "attribute no longer present in schema" , "attribute" , attr )
500+ logging . HelperSchemaDebug (ctx , "attribute no longer present in schema" , "attribute" , attr )
490501 delete (v , attr )
491502 continue
492503 }
@@ -497,7 +508,10 @@ func (s *GRPCProviderServer) removeAttributes(ctx context.Context, v interface{}
497508}
498509
499510func (s * GRPCProviderServer ) StopProvider (ctx context.Context , _ * tfprotov5.StopProviderRequest ) (* tfprotov5.StopProviderResponse , error ) {
500- ctx = withLogger (ctx )
511+ ctx = logging .InitContext (ctx )
512+
513+ logging .HelperSchemaTrace (ctx , "Stopping provider" )
514+
501515 s .stopMu .Lock ()
502516 defer s .stopMu .Unlock ()
503517
@@ -506,11 +520,13 @@ func (s *GRPCProviderServer) StopProvider(ctx context.Context, _ *tfprotov5.Stop
506520 // reset the stop signal
507521 s .stopCh = make (chan struct {})
508522
523+ logging .HelperSchemaTrace (ctx , "Stopped provider" )
524+
509525 return & tfprotov5.StopProviderResponse {}, nil
510526}
511527
512528func (s * GRPCProviderServer ) ConfigureProvider (ctx context.Context , req * tfprotov5.ConfigureProviderRequest ) (* tfprotov5.ConfigureProviderResponse , error ) {
513- ctx = withLogger (ctx )
529+ ctx = logging . InitContext (ctx )
514530 resp := & tfprotov5.ConfigureProviderResponse {}
515531
516532 schemaBlock := s .getProviderSchemaBlock ()
@@ -536,14 +552,18 @@ func (s *GRPCProviderServer) ConfigureProvider(ctx context.Context, req *tfproto
536552 // function. Ideally a provider should migrate to the context aware API that receives
537553 // request scoped contexts, however this is a large undertaking for very large providers.
538554 ctxHack := context .WithValue (ctx , StopContextKey , s .StopContext (context .Background ()))
555+
556+ logging .HelperSchemaTrace (ctx , "Calling downstream" )
539557 diags := s .provider .Configure (ctxHack , config )
558+ logging .HelperSchemaTrace (ctx , "Called downstream" )
559+
540560 resp .Diagnostics = convert .AppendProtoDiag (resp .Diagnostics , diags )
541561
542562 return resp , nil
543563}
544564
545565func (s * GRPCProviderServer ) ReadResource (ctx context.Context , req * tfprotov5.ReadResourceRequest ) (* tfprotov5.ReadResourceResponse , error ) {
546- ctx = withLogger (ctx )
566+ ctx = logging . InitContext (ctx )
547567 resp := & tfprotov5.ReadResourceResponse {
548568 // helper/schema did previously handle private data during refresh, but
549569 // core is now going to expect this to be maintained in order to
@@ -636,7 +656,7 @@ func (s *GRPCProviderServer) ReadResource(ctx context.Context, req *tfprotov5.Re
636656}
637657
638658func (s * GRPCProviderServer ) PlanResourceChange (ctx context.Context , req * tfprotov5.PlanResourceChangeRequest ) (* tfprotov5.PlanResourceChangeResponse , error ) {
639- ctx = withLogger (ctx )
659+ ctx = logging . InitContext (ctx )
640660 resp := & tfprotov5.PlanResourceChangeResponse {}
641661
642662 // This is a signal to Terraform Core that we're doing the best we can to
@@ -752,6 +772,11 @@ func (s *GRPCProviderServer) PlanResourceChange(ctx context.Context, req *tfprot
752772 // now we need to apply the diff to the prior state, so get the planned state
753773 plannedAttrs , err := diff .Apply (priorState .Attributes , schemaBlock )
754774
775+ if err != nil {
776+ resp .Diagnostics = convert .AppendProtoDiag (resp .Diagnostics , err )
777+ return resp , nil
778+ }
779+
755780 plannedStateVal , err := hcl2shim .HCL2ValueFromFlatmap (plannedAttrs , schemaBlock .ImpliedType ())
756781 if err != nil {
757782 resp .Diagnostics = convert .AppendProtoDiag (resp .Diagnostics , err )
@@ -873,7 +898,7 @@ func (s *GRPCProviderServer) PlanResourceChange(ctx context.Context, req *tfprot
873898}
874899
875900func (s * GRPCProviderServer ) ApplyResourceChange (ctx context.Context , req * tfprotov5.ApplyResourceChangeRequest ) (* tfprotov5.ApplyResourceChangeResponse , error ) {
876- ctx = withLogger (ctx )
901+ ctx = logging . InitContext (ctx )
877902 resp := & tfprotov5.ApplyResourceChangeResponse {
878903 // Start with the existing state as a fallback
879904 NewState : req .PriorState ,
@@ -1053,7 +1078,7 @@ func (s *GRPCProviderServer) ApplyResourceChange(ctx context.Context, req *tfpro
10531078}
10541079
10551080func (s * GRPCProviderServer ) ImportResourceState (ctx context.Context , req * tfprotov5.ImportResourceStateRequest ) (* tfprotov5.ImportResourceStateResponse , error ) {
1056- ctx = withLogger (ctx )
1081+ ctx = logging . InitContext (ctx )
10571082 resp := & tfprotov5.ImportResourceStateResponse {}
10581083
10591084 info := & terraform.InstanceInfo {
@@ -1112,7 +1137,7 @@ func (s *GRPCProviderServer) ImportResourceState(ctx context.Context, req *tfpro
11121137}
11131138
11141139func (s * GRPCProviderServer ) ReadDataSource (ctx context.Context , req * tfprotov5.ReadDataSourceRequest ) (* tfprotov5.ReadDataSourceResponse , error ) {
1115- ctx = withLogger (ctx )
1140+ ctx = logging . InitContext (ctx )
11161141 resp := & tfprotov5.ReadDataSourceResponse {}
11171142
11181143 schemaBlock := s .getDatasourceSchemaBlock (req .TypeName )
0 commit comments