Skip to content

Commit

Permalink
adding wrappers for state change funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Apr 21, 2021
1 parent cb110f3 commit acb6400
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
8 changes: 7 additions & 1 deletion azurerm/internal/tf/pluginsdk/resource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pluginsdk

import "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

// This file is intended to provide a transition from Plugin SDKv1 to Plugin SDKv2
// without introducing a merge conflict into every PR.
Expand All @@ -20,6 +23,9 @@ func ImportStatePassthrough(d *schema.ResourceData, m interface{}) ([]*schema.Re
return schema.ImportStatePassthrough(d, m)
}

type StateChangeConf = resource.StateChangeConf
type StateRefreshFunc = resource.StateRefreshFunc

type CreateFunc = schema.CreateFunc
type DeleteFunc = schema.DeleteFunc
type ExistsFunc = schema.ExistsFunc
Expand Down
20 changes: 10 additions & 10 deletions azurerm/internal/timeouts/determine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import (
"context"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
)

// 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))
func ForCreate(ctx context.Context, d *pluginsdk.ResourceData) (context.Context, context.CancelFunc) {
return buildWithTimeout(ctx, d.Timeout(pluginsdk.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) {
func ForCreateUpdate(ctx context.Context, d *pluginsdk.ResourceData) (context.Context, context.CancelFunc) {
if d.IsNewResource() {
return ForCreate(ctx, d)
}
Expand All @@ -31,24 +31,24 @@ func ForCreateUpdate(ctx context.Context, d *schema.ResourceData) (context.Conte
//
// 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))
func ForDelete(ctx context.Context, d *pluginsdk.ResourceData) (context.Context, context.CancelFunc) {
return buildWithTimeout(ctx, d.Timeout(pluginsdk.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))
func ForRead(ctx context.Context, d *pluginsdk.ResourceData) (context.Context, context.CancelFunc) {
return buildWithTimeout(ctx, d.Timeout(pluginsdk.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 ForUpdate(ctx context.Context, d *pluginsdk.ResourceData) (context.Context, context.CancelFunc) {
return buildWithTimeout(ctx, d.Timeout(pluginsdk.TimeoutUpdate))
}

func buildWithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
Expand Down
6 changes: 6 additions & 0 deletions scripts/plugin-sdk-fixerupper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function updateCode {

# update the import for validation, the alised/unaliased part can remain the same
find "$SERVICE_DIRECTORY" -type f -iname "*.go" -exec sed -i '' -e 's/github\.com\/hashicorp\/terraform-plugin-sdk\/helper\/validation/github\.com\/terraform-providers\/terraform-provider-azurerm\/azurerm\/internal\/tf\/validation/g' {} \;

# `resource.` can become `pluginsdk`.
find "$SERVICE_DIRECTORY" -type f -iname "*.go" -exec sed -i '' -e 's/github\.com\/hashicorp\/terraform-plugin-sdk\/helper\/resource/github\.com\/terraform-providers\/terraform-provider-azurerm\/azurerm\/internal\/tf\/pluginsdk/g' {} \;
find "$SERVICE_DIRECTORY" -type f -iname "*.go" -exec sed -i '' -e 's/schema\./pluginsdk\./g' {} \;
}

function revertValidationFuncs {
Expand All @@ -39,6 +43,8 @@ function revertValidationFuncs {
}

function format {
echo "Running fmt.."
gofmt -w "$SERVICE_DIRECTORY"
echo "Running goimports.."
goimports -w "$SERVICE_DIRECTORY"
}
Expand Down

0 comments on commit acb6400

Please sign in to comment.