diff --git a/migration/migration.go b/migration/migration.go index 7a9230b..e0906cf 100644 --- a/migration/migration.go +++ b/migration/migration.go @@ -7,7 +7,8 @@ import ( "text/template" tfjson "github.com/hashicorp/terraform-json" - "github.com/mitchellh/mapstructure" + "github.com/minamijoyo/tfedit/migration/schema" + _ "github.com/minamijoyo/tfedit/migration/schema/aws" ) var migrationTemplate string = `migration "state" "awsv4upgrade" { @@ -19,15 +20,6 @@ var migrationTemplate string = `migration "state" "awsv4upgrade" { } ` -type AWSS3BucketACLFilterResource struct { - Bucket string - ACL string -} - -func (r *AWSS3BucketACLFilterResource) ID() string { - return r.Bucket + "," + r.ACL -} - func Generate(planJSON []byte) ([]byte, error) { var plan tfjson.Plan if err := json.Unmarshal(planJSON, &plan); err != nil { @@ -39,9 +31,8 @@ func Generate(planJSON []byte) ([]byte, error) { if rc.Change.Actions.Create() { address := rc.Address after := rc.Change.After.(map[string]interface{}) - var r AWSS3BucketACLFilterResource - mapstructure.Decode(after, &r) - migrateAction := fmt.Sprintf("import %s %s", address, r.ID()) + importID := schema.ImportID(rc.Type, after) + migrateAction := fmt.Sprintf("import %s %s", address, importID) migrateActions = append(migrateActions, migrateAction) } } diff --git a/migration/schema/aws/s3.go b/migration/schema/aws/s3.go new file mode 100644 index 0000000..7598d7d --- /dev/null +++ b/migration/schema/aws/s3.go @@ -0,0 +1,19 @@ +package aws + +import "github.com/minamijoyo/tfedit/migration/schema" + +func init() { + RegisterSchema() +} + +func RegisterSchema() { + importIDMap := map[string]schema.ImportIDFunc{ + "aws_s3_bucket_acl": func(r schema.Resource) string { + return r["bucket"].(string) + "," + r["acl"].(string) + }, + } + + for k, v := range importIDMap { + schema.RegisterImportIDFunc(k, v) + } +} diff --git a/migration/schema/dictionary.go b/migration/schema/dictionary.go new file mode 100644 index 0000000..2535ba0 --- /dev/null +++ b/migration/schema/dictionary.go @@ -0,0 +1,33 @@ +package schema + +type Resource map[string]interface{} +type ImportIDFunc func(r Resource) string + +type Dictionary struct { + importIDMap map[string]ImportIDFunc +} + +func NewDictionary() *Dictionary { + return &Dictionary{ + importIDMap: make(map[string]ImportIDFunc), + } +} + +func (d *Dictionary) RegisterImportIDFunc(resourceType string, f ImportIDFunc) { + d.importIDMap[resourceType] = f +} + +func (d *Dictionary) ImportID(resourceType string, r Resource) string { + f := d.importIDMap[resourceType] + return f(r) +} + +var defaultDictionary = NewDictionary() + +func RegisterImportIDFunc(resourceType string, f ImportIDFunc) { + defaultDictionary.RegisterImportIDFunc(resourceType, f) +} + +func ImportID(resourceType string, r Resource) string { + return defaultDictionary.ImportID(resourceType, r) +}