Skip to content

Commit

Permalink
Allow to inject definitions of resource type outside from package
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Apr 19, 2022
1 parent f8dbf09 commit 9575027
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
17 changes: 4 additions & 13 deletions migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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 {
Expand All @@ -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)
}
}
Expand Down
19 changes: 19 additions & 0 deletions migration/schema/aws/s3.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
33 changes: 33 additions & 0 deletions migration/schema/dictionary.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 9575027

Please sign in to comment.