-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4475 from terraform-providers/f/custom-timeouts
Groundwork for Custom Timeouts
- Loading branch information
Showing
6 changed files
with
184 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package features | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
// SupportsCustomTimeouts returns whether Custom Timeouts are supported | ||
// | ||
// This feature allows Resources to define Custom Timeouts for Creation, Updating and Deletion | ||
// which helps work with Azure resources that take longer to provision/delete. | ||
// When this feature is disabled, all resources have a hard-coded timeout of 3 hours. | ||
// | ||
// This feature-toggle defaults to off in 1.x versions of the Azure Provider, however this will | ||
// become the default behaviour in version 2.0 of the Azure Provider. As outlined in the announcement | ||
// for v2.0 of the Azure Provider: https://github.com/terraform-providers/terraform-provider-azurerm/issues/2807 | ||
// | ||
// Operators wishing to adopt this behaviour can opt-into this behaviour in 1.x versions of the | ||
// Azure Provider by setting the Environment Variable 'ARM_PROVIDER_CUSTOM_TIMEOUTS' to 'true' | ||
func SupportsCustomTimeouts() bool { | ||
return strings.EqualFold(os.Getenv("ARM_PROVIDER_CUSTOM_TIMEOUTS"), "true") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package features | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestCustomTimeouts(t *testing.T) { | ||
testData := []struct { | ||
name string | ||
value string | ||
expected bool | ||
}{ | ||
{ | ||
name: "unset", | ||
value: "", | ||
expected: false, | ||
}, | ||
{ | ||
name: "disabled lower-case", | ||
value: "false", | ||
expected: false, | ||
}, | ||
{ | ||
name: "disabled upper-case", | ||
value: "FALSE", | ||
expected: false, | ||
}, | ||
{ | ||
name: "enabled lower-case", | ||
value: "true", | ||
expected: true, | ||
}, | ||
{ | ||
name: "enabled upper-case", | ||
value: "TRUE", | ||
expected: true, | ||
}, | ||
{ | ||
name: "invalid", | ||
value: "pandas", | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Test %q..", v.name) | ||
|
||
os.Setenv("ARM_PROVIDER_CUSTOM_TIMEOUTS", v.value) | ||
actual := SupportsCustomTimeouts() | ||
if v.expected != actual { | ||
t.Fatalf("Expected %t but got %t", v.expected, actual) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package timeouts | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform/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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters