-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
determine.go
66 lines (55 loc) · 2.45 KB
/
determine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package timeouts
import (
"context"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
)
// TODO: tests for this
// ForCreate returns the context wrapped with the timeout for an Create operation
//
// If the 'SupportsCustomTimeouts' feature toggle is enabled - this is wrapped with a context
// Otherwise this returns the default context
func ForCreate(ctx context.Context, d *schema.ResourceData) (context.Context, context.CancelFunc) {
return buildWithTimeout(ctx, d.Timeout(schema.TimeoutCreate))
}
// ForCreateUpdate returns the context wrapped with the timeout for an combined Create/Update operation
//
// If the 'SupportsCustomTimeouts' feature toggle is enabled - this is wrapped with a context
// Otherwise this returns the default context
func ForCreateUpdate(ctx context.Context, d *schema.ResourceData) (context.Context, context.CancelFunc) {
if d.IsNewResource() {
return ForCreate(ctx, d)
}
return ForUpdate(ctx, d)
}
// ForDelete returns the context wrapped with the timeout for an Delete operation
//
// If the 'SupportsCustomTimeouts' feature toggle is enabled - this is wrapped with a context
// Otherwise this returns the default context
func ForDelete(ctx context.Context, d *schema.ResourceData) (context.Context, context.CancelFunc) {
return buildWithTimeout(ctx, d.Timeout(schema.TimeoutDelete))
}
// ForRead returns the context wrapped with the timeout for an Read operation
//
// If the 'SupportsCustomTimeouts' feature toggle is enabled - this is wrapped with a context
// Otherwise this returns the default context
func ForRead(ctx context.Context, d *schema.ResourceData) (context.Context, context.CancelFunc) {
return buildWithTimeout(ctx, d.Timeout(schema.TimeoutRead))
}
// ForUpdate returns the context wrapped with the timeout for an Update operation
//
// If the 'SupportsCustomTimeouts' feature toggle is enabled - this is wrapped with a context
// Otherwise this returns the default context
func ForUpdate(ctx context.Context, d *schema.ResourceData) (context.Context, context.CancelFunc) {
return buildWithTimeout(ctx, d.Timeout(schema.TimeoutUpdate))
}
func buildWithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
if features.SupportsCustomTimeouts() {
return context.WithTimeout(ctx, timeout)
}
nullFunc := func() {
// do nothing on cancel since timeouts aren't enabled
}
return ctx, nullFunc
}