Skip to content

Commit 45c1959

Browse files
committed
Deprecate Exists
1 parent 3a14f25 commit 45c1959

File tree

2 files changed

+13
-50
lines changed

2 files changed

+13
-50
lines changed

helper/schema/resource.go

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,20 @@ type Resource struct {
103103
Read ReadFunc
104104
Update UpdateFunc
105105
Delete DeleteFunc
106+
107+
// Exists is a function that is called to check if a resource still
108+
// exists. If this returns false, then this will affect the diff
109+
// accordingly. If this function isn't set, it will not be called. You
110+
// can also signal existence in the Read method by calling d.SetId("")
111+
// if the Resource is no longer present and should be removed from state.
112+
// The *ResourceData passed to Exists should _not_ be modified.
113+
//
114+
// Deprecated: ReadContext should be able to encapsulate the logic of Exists
106115
Exists ExistsFunc
107116

108117
// The functions below are the CRUD operations for this resource.
109118
//
110-
// The only optional operation is Update and Exists. If Update is not
119+
// The only optional operation is Update. If Update is not
111120
// implemented, then updates will not be supported for this resource.
112121
//
113122
// The ResourceData parameter in the functions below are used to
@@ -120,20 +129,11 @@ type Resource struct {
120129
// to store API clients, configuration structures, etc.
121130
//
122131
// If any errors occur during each of the operation, an error should be
123-
// returned. If a resource was partially updated, be careful to enable
124-
// partial state mode for ResourceData and use it accordingly.
125-
//
126-
// Exists is a function that is called to check if a resource still
127-
// exists. If this returns false, then this will affect the diff
128-
// accordingly. If this function isn't set, it will not be called. You
129-
// can also signal existence in the Read method by calling d.SetId("")
130-
// if the Resource is no longer present and should be removed from state.
131-
// The *ResourceData passed to Exists should _not_ be modified.
132+
// returned.
132133
CreateContext CreateContextFunc
133134
ReadContext ReadContextFunc
134135
UpdateContext UpdateContextFunc
135136
DeleteContext DeleteContextFunc
136-
ExistsContext ExistsContextFunc
137137

138138
// CustomizeDiff is a custom function for working with the diff that
139139
// Terraform has created for this resource - it can be used to customize the
@@ -233,9 +233,6 @@ type UpdateContextFunc func(context.Context, *ResourceData, interface{}) error
233233
// See Resource documentation.
234234
type DeleteContextFunc func(context.Context, *ResourceData, interface{}) error
235235

236-
// See Resource documentation.
237-
type ExistsContextFunc func(context.Context, *ResourceData, interface{}) (bool, error)
238-
239236
// See Resource documentation.
240237
type StateMigrateFunc func(
241238
int, *terraform.InstanceState, interface{}) (*terraform.InstanceState, error)
@@ -300,15 +297,6 @@ func (r *Resource) delete(ctx context.Context, d *ResourceData, meta interface{}
300297
return r.DeleteContext(ctx, d, meta)
301298
}
302299

303-
func (r *Resource) exists(ctx context.Context, d *ResourceData, meta interface{}) (bool, error) {
304-
if r.Exists != nil {
305-
return r.Exists(d, meta)
306-
}
307-
ctx, cancel := context.WithTimeout(ctx, d.Timeout(TimeoutRead))
308-
defer cancel()
309-
return r.ExistsContext(ctx, d, meta)
310-
}
311-
312300
// Apply creates, updates, and/or deletes a resource.
313301
func (r *Resource) Apply(
314302
ctx context.Context,
@@ -505,7 +493,7 @@ func (r *Resource) RefreshWithoutUpgrade(
505493
}
506494
}
507495

508-
if r.existsFuncSet() {
496+
if r.Exists != nil {
509497
// Make a copy of data so that if it is modified it doesn't
510498
// affect our Read later.
511499
data, err := schemaMap(r.Schema).Data(s, nil)
@@ -515,7 +503,7 @@ func (r *Resource) RefreshWithoutUpgrade(
515503
return s, err
516504
}
517505

518-
exists, err := r.exists(ctx, data, meta)
506+
exists, err := r.Exists(data, meta)
519507

520508
if err != nil {
521509
return s, err
@@ -557,10 +545,6 @@ func (r *Resource) deleteFuncSet() bool {
557545
return (r.Delete != nil || r.DeleteContext != nil)
558546
}
559547

560-
func (r *Resource) existsFuncSet() bool {
561-
return (r.Exists != nil || r.ExistsContext != nil)
562-
}
563-
564548
// InternalValidate should be called to validate the structure
565549
// of the resource.
566550
//
@@ -688,9 +672,6 @@ func (r *Resource) InternalValidate(topSchemaMap schemaMap, writable bool) error
688672
if r.DeleteContext != nil && r.Delete != nil {
689673
return fmt.Errorf("DeleteContext and Delete should not both be set")
690674
}
691-
if r.ExistsContext != nil && r.Exists != nil {
692-
return fmt.Errorf("ExistsContext and Exists should not both be set")
693-
}
694675

695676
return schemaMap(r.Schema).InternalValidate(tsm)
696677
}

helper/schema/resource_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -928,24 +928,6 @@ func TestResourceInternalValidate(t *testing.T) {
928928
true,
929929
true,
930930
},
931-
19: { // Exists and ExistsContext should not both be set
932-
&Resource{
933-
Create: Noop,
934-
Read: Noop,
935-
Update: Noop,
936-
Delete: Noop,
937-
Exists: func(*ResourceData, interface{}) (bool, error) { return false, nil },
938-
ExistsContext: func(context.Context, *ResourceData, interface{}) (bool, error) { return false, nil },
939-
Schema: map[string]*Schema{
940-
"foo": {
941-
Type: TypeString,
942-
Required: true,
943-
},
944-
},
945-
},
946-
true,
947-
true,
948-
},
949931
}
950932

951933
for i, tc := range cases {

0 commit comments

Comments
 (0)