Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to TF 0.12.3 #33

Merged
merged 15 commits into from
Oct 29, 2019
Merged

Migrate to TF 0.12.3 #33

merged 15 commits into from
Oct 29, 2019

Conversation

xescugc
Copy link
Member

@xescugc xescugc commented Jul 9, 2019

Change all the workflow to work with the new 0.12 implementation of TF. This implementation uses the Plugins logic without using Plugins directly.

The HCL and State have been changed to work with the new format.

Closes #7

Copy link
Contributor

@xlr-8 xlr-8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple typo/question to solve/answer

CHANGELOG.md Outdated
### Changed

- The Terraform version from 0.11 to 0.12 with all the implications (file formats)
([PR #33](https://github.com/cycloidio/terracognita/pull/33))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could make sense to drop a link to https://www.terraform.io/upgrade-guides/0-12.html

},
{
// Add new lines before blocks
//match: regexp.MustCompile("\n(\t*)([\\w\\-_\\.]+\\s{)"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unecessary commented code

@@ -45,11 +56,15 @@ var (
}
)

// FormatHCL format the hcl to have a better formatter thatn the default one
// Format format the hcl to have a better formatter thatn the default one
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

than*
returned*

hcl/writer.go Outdated

return nil
}

// Has checks if the given key it's already present or not
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is already*

hcl/writer.go Outdated
// Has checks if the given key it's already present or not
func (w *Writer) Has(key string) (bool, error) {
keys := strings.Split(key, ".")
if len(keys) != 2 || (keys[0] == "" || keys[1] == "") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unecessary parenthesis

if err != nil {
return errors.Wrapf(err, "error while calculating the Config of resource %q", t)
cause := errors.Cause(err)
// All those ignored errors are types of errors to identify that something happend, but not that something whent wrong
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

went*

if tfstate != nil {
err = r.State(tfstate)
if err != nil {
return errors.Wrapf(err, "error while calculating the Satate of resource %q", t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

state*

func (w *Writer) Has(key string) (bool, error) {
_, ok := w.Config[key]
return ok, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this Has method or the other one are used anywhere

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/cycloidio/terracognita/pull/33/files#diff-9a4396fc8b469f87eb63a29771b3380fR330 also on the HCL.

The resource.go was collapsed on my diff maybe you forgot to review it.

@xescugc
Copy link
Member Author

xescugc commented Jul 16, 2019

Did the changes.

@xescugc xescugc mentioned this pull request Jul 18, 2019
// to the Resource and could return []Resource if
// it imported more than one state, this list does not
// include the actual Resource on parameters, so if
// len([]Resource) == 0 menas only the Resource it's imported
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

means*
is* imported

}
gpsKey = fmt.Sprintf("%s/%s", gpsKey, gp)
resources := make([]Resource, 0, len(newInstanceStates)-1)
// We assume that the first state it's the one for this Resource
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the one*

})
func (r *resource) ImportState() ([]Resource, error) {
// If it does not support import do not try
if r.TFResource().Importer == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it make sense to log that as a Warning/Debug info?

@xlr-8
Copy link
Contributor

xlr-8 commented Jul 29, 2019

The code generated doesn't quite seem to be valid for terraform, some blocks/quote and optional params are generating errors.

The resources that are imported do not require sets, with the ID it's enough
@xescugc
Copy link
Member Author

xescugc commented Oct 28, 2019

Ok done, some of the issues we are having now are related to TF not working on some form and we have to patch it on top normally. I would say to merge this 0.11 -> 0.12 and then fix the other issues in separated PRs.

return nil, err
}
err = r.Data().Set("groups", groupSet)
r, err := initializeResource(a, strings.Join(append([]string{un}, groupNames...), "/"), resourceType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if adding a small comment regarding each format could help

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each format could help

Not only this one

hcl/format.go Outdated
)

var (
// transformers are all the transformmations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transformations*

hcl/format.go Outdated
replaceFn func([]byte) []byte
}{
{
// Replace all the `"key" = "value"` for `key = "value"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is excluding key with special characters such as: cycloid.io, might be worth mentioning, as this is a "new element" from TC 0.12

hcl/format.go Outdated
replace: []byte("}\n\n"),
},
{
// Remove "" from resources definition
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More info on this?

hcl/format.go Outdated
}
)

// Format format the hcl to have a better formatter that the default one
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formats*

hcl/format.go Outdated
replace: []byte(`$1 =`),
},
{
// Replace all the `key = {` for `key {`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think that works for everything?
Because you can also have other ones such as: https://www.terraform.io/docs/providers/aws/r/cloudwatch_metric_alarm.html

Checked how the other handle it?
Could there be a way to generate many (all?) AWS resource and try to run our code against it to make sure it imports them properly?

util/retry.go Outdated
@@ -25,6 +26,13 @@ func Retry(rfn RetryFn, times int, interval time.Duration) error {
if times == 0 {
return err
}
// If the error it's from the stdlib we just continue with them
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is from*

@xescugc
Copy link
Member Author

xescugc commented Oct 29, 2019

Done

Copy link
Contributor

@xlr-8 xlr-8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the typos previously mentioned weren't fixed

return nil, err
}
err = r.Data().Set("groups", groupSet)
r, err := initializeResource(a, strings.Join(append([]string{un}, groupNames...), "/"), resourceType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each format could help

Not only this one

@@ -512,6 +513,12 @@ func mergeFullConfig(cfgr *schema.ResourceData, sch map[string]*schema.Schema, k
}
}

// If it's a type map, we'll prefix the key with '=tc=' to know
// that it's an attribute so we have to keep the '=' when formatting
// the HCL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea :)

Using all the plugin methods without directly using the Plugins.

Some of this methods had to be 'copied' from TF itself as those where private
so not beeing able to use them directly, the ones that match this caracteristics have comments
that mention it.
Also refactored the resources to use the new 'NewResource'
One for the HCL logic 'hcl/' and one for the TF State logic 'state/'.

This way the implementation of the interfaces it's not on the same package as the definition of the interface
and causes some cycle imports on other packages
Changed the 'New' statement of the Writers to use the new packages
This way if we removed some mock now it'll also be removed, or if we accidentally remove a generate frome some 'interface'
now it'll be detected.

It has happend with the generate from the 'writer.Writer', the '//go:generate' was removed by mistake and no error was triggered
anywhere, this will trigger an error now
Update the TF version to 0.12.3 and other dependencies
To have the right ID on the field, it's expected to be an specific value
So the users know which ones they have to have and we use
The AWS 'IsErrorRetryable' retries all std errors, so to prevent that (to prevent our own errors to be retried) we'll ignore std errors
so only errors that internally have an strucure will be passed
@xescugc xescugc merged commit 79e0348 into master Oct 29, 2019
@xescugc xescugc deleted the fg-tf-0.12 branch October 29, 2019 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aws: some default values are not properly imported
2 participants