diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b0ac2d970..f3f5817e07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## [Unreleased] +### Fixed + +- AWS IOpS attribute is only valid for io1 and io2 type + ([Issue #157](https://github.com/cycloidio/terracognita/issues/157)) + ## [0.6.0] _2020-12-22_ ### Added diff --git a/cmd/aws.go b/cmd/aws.go index 2780c07968..882162d970 100644 --- a/cmd/aws.go +++ b/cmd/aws.go @@ -85,7 +85,40 @@ var ( } var hclW, stateW writer.Writer - options := &writer.Options{Interpolate: viper.GetBool("interpolate")} + options := &writer.Options{ + Interpolate: viper.GetBool("interpolate"), + PreSync: func(i interface{}) error { + for rt, resource := range i.(map[string]map[string]interface{}) { + // we loop over a resource (e.g: aws_instance.oDSOj) + for _, block := range resource { + // we remove IOPS for GP2 volume types + // https://github.com/cycloidio/terracognita/issues/157 + if rt == "aws_instance" { + if bds, ok := block.(map[string]interface{})["root_block_device"]; ok { + for _, bd := range bds.([]interface{}) { + if vt, ok := bd.(map[string]interface{})["volume_type"].(string); ok && vt == "gp2" { + delete(bd.(map[string]interface{}), "iops") + } + } + } + if bds, ok := block.(map[string]interface{})["ebs_block_device"]; ok { + for _, bd := range bds.([]interface{}) { + if vt, ok := bd.(map[string]interface{})["volume_type"].(string); ok && vt == "gp2" { + delete(bd.(map[string]interface{}), "iops") + } + } + } + } + if rt == "aws_ebs_volume" { + if t, ok := block.(map[string]interface{})["type"]; ok && t == "gp2" { + delete(block.(map[string]interface{}), "iops") + } + } + } + } + return nil + }, + } if hclOut != nil { logger.Log("msg", "initializing HCL writer") diff --git a/hcl/writer.go b/hcl/writer.go index 383edd6564..5460a06d9c 100644 --- a/hcl/writer.go +++ b/hcl/writer.go @@ -99,6 +99,13 @@ func (w *Writer) Has(key string) (bool, error) { // Sync writes the content of the Config to the // internal w with the correct format func (w *Writer) Sync() error { + + if w.opts.PreSync != nil { + if err := w.opts.PreSync(w.Config["resource"]); err != nil { + return fmt.Errorf("unable to pre-sync the HCL configuration: %w", err) + } + } + logger := log.Get() logger = kitlog.With(logger, "func", "writer.Write(HCL)") diff --git a/writer/options.go b/writer/options.go index dd34446d2d..3d85a600af 100644 --- a/writer/options.go +++ b/writer/options.go @@ -6,4 +6,8 @@ type Options struct { // variables in HCL files or building dependencies // in a TFState Interpolate bool + + // PreSync is the method to call before the `Sync` + // of the writer in order to perform surgicals modification + PreSync func(interface{}) error }