Skip to content

Commit

Permalink
Importer function to normalize resource id
Browse files Browse the repository at this point in the history
Normalize the ID using the importer function to conform to what is
defined in the ID formatter.

This is to mitigate cases like users import resource ID with lowercase
'g' for the resource group. In this case, the current validation in the
importer raise no error, which results into the incorrect cased ID leak
into terraform state. This will cause the other referencing resource get
unexpected plan diff (see hashicorp#14854).

This commit adds the helper function for both untyped and typed sdk. The
users are expected to pass in a wrapper function around the parse
function generated via the resource id tool. While this wrapper might be
eliminated once we have generic in Go.
  • Loading branch information
magodo committed Jan 11, 2022
1 parent 303707d commit 276f86c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/sdk/wrapper_helpers.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package sdk

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/resourceid"
)

// combineSchema combines the arguments (user-configurable) and attributes (read-only) schema fields
Expand Down Expand Up @@ -55,3 +57,16 @@ func runArgs(d *schema.ResourceData, meta interface{}, logger Logger) ResourceMe

return metaData
}

// NormalizeIdImporter is a helper function which returns a ResourceRunFunc.
// This function is intended to be used as the CustomImporter for a ResourceWithCustomImporter.
func NormalizeIdImporter(parser func(string) (resourceid.Formatter, error)) ResourceRunFunc {
return func(ctx context.Context, metadata ResourceMetaData) error {
id, err := parser(metadata.ResourceData.Id())
if err != nil {
return err
}
metadata.SetID(id)
return nil
}
}
19 changes: 19 additions & 0 deletions internal/tf/pluginsdk/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -47,3 +48,21 @@ func ImporterValidatingResourceIdThen(validateFunc IDValidationFunc, thenFunc Im
},
}
}

type IDParseFunc func(string) (resourceids.Id, error)

func ImporterValidatingThenNormalizeId(parser IDParseFunc) *schema.ResourceImporter {
return &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *ResourceData, meta interface{}) ([]*ResourceData, error) {
log.Printf("[DEBUG] Importing Resource - parsing %q", d.Id())

id, err := parser(d.Id())
if err != nil {
return []*ResourceData{d}, fmt.Errorf("parsing Resource ID %q: %+v", d.Id(), err)
}

d.SetId(id.ID())
return []*ResourceData{d}, nil
},
}
}

0 comments on commit 276f86c

Please sign in to comment.