Skip to content

Commit b54d0fe

Browse files
committed
Log warnings for deprecated functions
tests for setting of both context and noncontext functions
1 parent 45c1959 commit b54d0fe

File tree

8 files changed

+68
-1
lines changed

8 files changed

+68
-1
lines changed

helper/resource/state.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,5 +276,6 @@ func (conf *StateChangeConf) WaitForStateContext(ctx context.Context) (interface
276276
//
277277
// Deprecated: Please use WaitForStateContext to ensure proper plugin shutdown
278278
func (conf *StateChangeConf) WaitForState() (interface{}, error) {
279+
log.Printf("[WARN] WaitForState is deprecated, please use WaitForStateContext")
279280
return conf.WaitForStateContext(context.Background())
280281
}

helper/resource/wait.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package resource
33
import (
44
"context"
55
"errors"
6+
"log"
67
"sync"
78
"time"
89
)
@@ -65,6 +66,7 @@ func RetryContext(ctx context.Context, timeout time.Duration, f RetryFunc) error
6566
//
6667
// Deprecated: Please use RetryContext to ensure proper plugin shutdown
6768
func Retry(timeout time.Duration, f RetryFunc) error {
69+
log.Printf("[WARN] Retry is deprecated, please use RetryContext")
6870
return RetryContext(context.Background(), timeout, f)
6971
}
7072

helper/schema/provider.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"log"
78
"sort"
89

910
"github.com/hashicorp/go-multierror"
@@ -89,6 +90,10 @@ func (p *Provider) InternalValidate() error {
8990
return errors.New("provider is nil")
9091
}
9192

93+
if p.ConfigureFunc != nil && p.ConfigureContextFunc != nil {
94+
return errors.New("ConfigureFunc and ConfigureContextFunc must not both be set")
95+
}
96+
9297
var validationErrors error
9398
sm := schemaMap(p.Schema)
9499
if err := sm.InternalValidate(sm); err != nil {
@@ -114,6 +119,11 @@ func (p *Provider) InternalValidate() error {
114119
}
115120
}
116121

122+
// Warn of deprecations
123+
if p.ConfigureFunc != nil && p.ConfigureContextFunc == nil {
124+
log.Printf("[WARN] ConfigureFunc is deprecated, please use ConfigureContextFunc")
125+
}
126+
117127
return validationErrors
118128
}
119129

helper/schema/provider_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,23 @@ func TestProvider_InternalValidate(t *testing.T) {
540540
},
541541
ExpectedErr: fmt.Errorf("%s is a reserved field name for a provider", "alias"),
542542
},
543+
{ // ConfigureFunc and ConfigureContext cannot both be set
544+
P: &Provider{
545+
Schema: map[string]*Schema{
546+
"foo": {
547+
Type: TypeString,
548+
Optional: true,
549+
},
550+
},
551+
ConfigureFunc: func(d *ResourceData) (interface{}, error) {
552+
return nil, nil
553+
},
554+
ConfigureContextFunc: func(ctx context.Context, d *ResourceData) (interface{}, error) {
555+
return nil, nil
556+
},
557+
},
558+
ExpectedErr: fmt.Errorf("ConfigureFunc and ConfigureContextFunc must not both be set"),
559+
},
543560
}
544561

545562
for i, tc := range cases {

helper/schema/resource.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,26 @@ func (r *Resource) InternalValidate(topSchemaMap schemaMap, writable bool) error
673673
return fmt.Errorf("DeleteContext and Delete should not both be set")
674674
}
675675

676+
// Warn of deprecations
677+
if r.Exists != nil {
678+
log.Printf("[WARN] Exists is deprecated, please encapsulate the logic in ReadContext")
679+
}
680+
if r.Create != nil && r.CreateContext == nil {
681+
log.Printf("[WARN] Create is deprecated, please use CreateContext")
682+
}
683+
if r.Read != nil && r.ReadContext == nil {
684+
log.Printf("[WARN] Read is deprecated, please use ReadContext")
685+
}
686+
if r.Update != nil && r.UpdateContext == nil {
687+
log.Printf("[WARN] Update is deprecated, please use UpdateContext")
688+
}
689+
if r.Delete != nil && r.DeleteContext == nil {
690+
log.Printf("[WARN] Delete is deprecated, please use DeleteContext")
691+
}
692+
if r.MigrateState != nil {
693+
log.Printf("[WARN] MigrateState is deprecated, please use StateUpgraders")
694+
}
695+
676696
return schemaMap(r.Schema).InternalValidate(tsm)
677697
}
678698

helper/schema/resource_importer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package schema
33
import (
44
"context"
55
"errors"
6+
"log"
67
)
78

89
// ResourceImporter defines how a resource is imported in Terraform. This
@@ -59,6 +60,9 @@ func (r *ResourceImporter) InternalValidate() error {
5960
if r.State != nil && r.StateContext != nil {
6061
return errors.New("Both State and StateContext cannot be set.")
6162
}
63+
if r.State != nil && r.StateContext == nil {
64+
log.Printf("[WARN] State is deprecated, please use StateContext")
65+
}
6266
return nil
6367
}
6468

@@ -67,6 +71,7 @@ func (r *ResourceImporter) InternalValidate() error {
6771
//
6872
// Deprecated: Please use the context aware ImportStatePassthroughContext instead
6973
func ImportStatePassthrough(d *ResourceData, m interface{}) ([]*ResourceData, error) {
74+
log.Printf("[WARN] ImportStatePassthrough is deprecated, please use ImportStatePassthroughContext")
7075
return []*ResourceData{d}, nil
7176
}
7277

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package schema
2+
3+
import "testing"
4+
5+
func TestInternalValidate(t *testing.T) {
6+
r := &ResourceImporter{
7+
State: ImportStatePassthrough,
8+
StateContext: ImportStatePassthroughContext,
9+
}
10+
if err := r.InternalValidate(); err == nil {
11+
t.Fatal("ResourceImporter should not allow State and StateContext to be set")
12+
}
13+
}

internal/helper/plugin/grpc_provider.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,6 @@ func (s *GRPCProviderServer) upgradeFlatmapState(ctx context.Context, version in
360360
"schema_version": strconv.Itoa(version),
361361
},
362362
}
363-
364363
is, err := res.MigrateState(version, is, s.provider.Meta())
365364
if err != nil {
366365
return nil, 0, err

0 commit comments

Comments
 (0)