-
Notifications
You must be signed in to change notification settings - Fork 300
/
import.go
36 lines (29 loc) · 1.39 KB
/
import.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
package tf
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// ResourceIDValidator takes a Resource ID and confirms that it's Valid
type ResourceIDValidator func(resourceId string) error
// ValidateResourceIDPriorToImport parses the Resource ID to confirm it's
// valid for this Resource prior to performing an import - allowing for incorrect
// Resource ID's to be caught prior to Import and subsequent crashes
func ValidateResourceIDPriorToImport(idParser ResourceIDValidator) *schema.ResourceImporter {
return ValidateResourceIDPriorToImportThen(idParser, schema.ImportStatePassthroughContext)
}
// ValidateResourceIDPriorToImportThen parses the Resource ID to confirm it's
// valid for this Resource prior to calling the importer - allowing for incorrect
// Resource ID's to be caught prior to Import and subsequent crashes
func ValidateResourceIDPriorToImportThen(idParser ResourceIDValidator, importer schema.StateContextFunc) *schema.ResourceImporter {
return &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
log.Printf("[DEBUG] Importing Resource - parsing %q", d.Id())
if err := idParser(d.Id()); err != nil {
return []*schema.ResourceData{d}, fmt.Errorf("parsing Resource ID %q: %+v", d.Id(), err)
}
return importer(ctx, d, meta)
},
}
}