Skip to content

Commit cade2af

Browse files
authored
Enable ineffassign linter and fix issues (#870)
Reference: #865 Reference: https://github.com/hashicorp/terraform-plugin-go/tree/main/internal/logging The majority of these issues were in the gRPC server handling for some recent inclusion of logging that did not include other context handling. This adds some logic handoff trace logs to the helper/schema logger and starts an internal logging package, similar to the terraform-plugin-go implementation. Previously: ```text internal/plugintest/util.go:13:4: ineffectual assignment to err (ineffassign) err = os.Chmod(dest, srcInfo.Mode()) ^ helper/schema/grpc_provider.go:75:2: ineffectual assignment to ctx (ineffassign) ctx = withLogger(ctx) ^ helper/schema/grpc_provider.go:126:2: ineffectual assignment to ctx (ineffassign) ctx = withLogger(ctx) ^ helper/schema/grpc_provider.go:230:2: ineffectual assignment to ctx (ineffassign) ctx = withLogger(ctx) ^ helper/schema/grpc_provider.go:249:2: ineffectual assignment to ctx (ineffassign) ctx = withLogger(ctx) ^ helper/schema/grpc_provider.go:501:2: ineffectual assignment to ctx (ineffassign) ctx = withLogger(ctx) ^ helper/schema/grpc_provider.go:754:16: ineffectual assignment to err (ineffassign) plannedAttrs, err := diff.Apply(priorState.Attributes, schemaBlock) ^ helper/schema/resource_data.go:531:2: ineffectual assignment to level (ineffassign) level := "set" ^ ```
1 parent 15ee89a commit cade2af

File tree

11 files changed

+167
-37
lines changed

11 files changed

+167
-37
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ linters:
1515
- exportloopref
1616
- gofmt
1717
- gosimple
18-
# - ineffassign # TODO: https://github.com/hashicorp/terraform-plugin-sdk/issues/865
18+
- ineffassign
1919
- makezero
2020
- nilerr
2121
# - paralleltest # Reference: https://github.com/kunwardeep/paralleltest/issues/14

helper/schema/grpc_provider.go

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2524
const (
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-
3528
func 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.
6457
func (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

7467
func (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

125124
func (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

228231
func (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

247252
func (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

272279
func (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

499510
func (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

512528
func (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

545565
func (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

638658
func (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

875900
func (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

10551080
func (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

11141139
func (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)

helper/schema/provider.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
1515
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1616
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema"
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
1718
"github.com/hashicorp/terraform-plugin-sdk/v2/meta"
1819
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1920
)
@@ -378,11 +379,15 @@ func (p *Provider) ImportState(
378379
results := []*ResourceData{data}
379380
if r.Importer.State != nil || r.Importer.StateContext != nil {
380381
var err error
382+
logging.HelperSchemaTrace(ctx, "Calling downstream")
383+
381384
if r.Importer.StateContext != nil {
382385
results, err = r.Importer.StateContext(ctx, data, p.meta)
383386
} else {
384387
results, err = r.Importer.State(data, p.meta)
385388
}
389+
logging.HelperSchemaTrace(ctx, "Called downstream")
390+
386391
if err != nil {
387392
return nil, err
388393
}

helper/schema/resource.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/go-cty/cty"
1111

1212
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/logging"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1415
)
1516

@@ -436,7 +437,10 @@ func (r *Resource) Apply(
436437
if d.Destroy || d.RequiresNew() {
437438
if s.ID != "" {
438439
// Destroy the resource since it is created
440+
logging.HelperSchemaTrace(ctx, "Calling downstream")
439441
diags = append(diags, r.delete(ctx, data, meta)...)
442+
logging.HelperSchemaTrace(ctx, "Called downstream")
443+
440444
if diags.HasError() {
441445
return r.recordCurrentSchemaVersion(data.State()), diags
442446
}
@@ -464,15 +468,19 @@ func (r *Resource) Apply(
464468
if data.Id() == "" {
465469
// We're creating, it is a new resource.
466470
data.MarkNewResource()
471+
logging.HelperSchemaTrace(ctx, "Calling downstream")
467472
diags = append(diags, r.create(ctx, data, meta)...)
473+
logging.HelperSchemaTrace(ctx, "Called downstream")
468474
} else {
469475
if !r.updateFuncSet() {
470476
return s, append(diags, diag.Diagnostic{
471477
Severity: diag.Error,
472478
Summary: "doesn't support update",
473479
})
474480
}
481+
logging.HelperSchemaTrace(ctx, "Calling downstream")
475482
diags = append(diags, r.update(ctx, data, meta)...)
483+
logging.HelperSchemaTrace(ctx, "Called downstream")
476484
}
477485

478486
return r.recordCurrentSchemaVersion(data.State()), diags
@@ -566,7 +574,10 @@ func (r *Resource) ReadDataApply(
566574
return nil, diag.FromErr(err)
567575
}
568576

577+
logging.HelperSchemaTrace(ctx, "Calling downstream")
569578
diags := r.read(ctx, data, meta)
579+
logging.HelperSchemaTrace(ctx, "Called downstream")
580+
570581
state := data.State()
571582
if state != nil && state.ID == "" {
572583
// Data sources can set an ID if they want, but they aren't
@@ -612,7 +623,10 @@ func (r *Resource) RefreshWithoutUpgrade(
612623
data.providerMeta = s.ProviderMeta
613624
}
614625

626+
logging.HelperSchemaTrace(ctx, "Calling downstream")
615627
exists, err := r.Exists(data, meta)
628+
logging.HelperSchemaTrace(ctx, "Called downstream")
629+
616630
if err != nil {
617631
return s, diag.FromErr(err)
618632
}
@@ -632,7 +646,10 @@ func (r *Resource) RefreshWithoutUpgrade(
632646
data.providerMeta = s.ProviderMeta
633647
}
634648

649+
logging.HelperSchemaTrace(ctx, "Calling downstream")
635650
diags := r.read(ctx, data, meta)
651+
logging.HelperSchemaTrace(ctx, "Called downstream")
652+
636653
state := data.State()
637654
if state != nil && state.ID == "" {
638655
state = nil

helper/schema/resource_data.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ func (d *ResourceData) getChange(
528528
func (d *ResourceData) get(addr []string, source getSource) getResult {
529529
d.once.Do(d.init)
530530

531-
level := "set"
531+
var level string
532532
flags := source & ^getSourceLevelMask
533533
exact := flags&getSourceExact != 0
534534
source = source & getSourceLevelMask

0 commit comments

Comments
 (0)