Skip to content

Commit

Permalink
Migrate to TF 0.12.3 (#33)
Browse files Browse the repository at this point in the history
Migrate to TF 0.12.3
  • Loading branch information
xescugc authored Oct 29, 2019
2 parents 30556e6 + c077327 commit 79e0348
Show file tree
Hide file tree
Showing 31 changed files with 1,216 additions and 561 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
## [Unreleased]

This version changes the format of the TFState to the Terraform 0.12+ [format](https://www.terraform.io/upgrade-guides/0-12.html)

### Fixed

- HCL formatter to ignore some special keys that fail on the `fmtcmd` of HCL
([Issue #36](https://github.com/cycloidio/terracognita/issues/36))

### Changed

- The Terraform version from 0.11 to 0.12 with all the implications (file formats) https://www.terraform.io/upgrade-guides/0-12.html
([PR #33](https://github.com/cycloidio/terracognita/pull/33))

## [0.1.6] _2019-07-18_

### Added
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ lint: $(GOLINT) $(GOIMPORTS) ## Runs the linter

.PHONY: generate
generate: $(MOCKGEN) ## Generates the needed code
@GO111MODULE=on go generate ./...
@GO111MODULE=on goimports -w ./mock
@GO111MODULE=on rm -rf ./mock/a && \
go generate ./... && \
goimports -w ./mock

.PHONY: test
test: ## Runs the tests
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ $> make install
Terracognita currently imports only AWS cloud provider as terraform resource/state.
Please see the following versions as follow:

Terraform: 0.11.14
Terraform: 0.12.7
Providers:
* AWS: 2.12.0
* AWS: 2.31.0

## Usage

Expand Down
10 changes: 5 additions & 5 deletions aws/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func getIAMGroupNames(ctx context.Context, a *aws, rt string, tags []tag.Tag) ([
// TODO cach this result too
names := make([]string, 0, len(rs))
for _, i := range rs {
names = append(names, i.Data().Get("name").(string))
names = append(names, i.ID())
}

return names, nil
Expand Down Expand Up @@ -77,7 +77,7 @@ func getIAMRoleNames(ctx context.Context, a *aws, rt string, tags []tag.Tag) ([]
// TODO cach this result too
names := make([]string, 0, len(rs))
for _, i := range rs {
names = append(names, i.Data().Get("name").(string))
names = append(names, i.ID())
}

return names, nil
Expand Down Expand Up @@ -114,7 +114,7 @@ func getIAMUserNames(ctx context.Context, a *aws, rt string, tags []tag.Tag) ([]
// TODO cach this result too
names := make([]string, 0, len(rs))
for _, i := range rs {
names = append(names, i.Data().Get("name").(string))
names = append(names, i.ID())
}

return names, nil
Expand Down Expand Up @@ -151,7 +151,7 @@ func getRoute53ZoneIDs(ctx context.Context, a *aws, rt string, tags []tag.Tag) (
// TODO cach this result too
ids := make([]string, 0, len(rs))
for _, i := range rs {
ids = append(ids, i.Data().Id())
ids = append(ids, i.ID())
}

return ids, nil
Expand Down Expand Up @@ -188,7 +188,7 @@ func getSESDomainIdentityDomains(ctx context.Context, a *aws, rt string, tags []
// TODO cach this result too
domains := make([]string, 0, len(rs))
for _, i := range rs {
domains = append(domains, i.Data().Get("domain").(string))
domains = append(domains, i.ID())
}

return domains, nil
Expand Down
9 changes: 8 additions & 1 deletion aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ func NewProvider(ctx context.Context, accessKey, secretKey, region string) (prov
return nil, fmt.Errorf("could not initialize 'terraform/aws.Config.Client()' because: %s", err)
}

tfp := tfaws.Provider().(*schema.Provider)
tfp.SetMeta(awsClient)

return &aws{
awsr: awsr,
tfAWSClient: awsClient,
tfProvider: tfaws.Provider().(*schema.Provider),
tfProvider: tfp,
cache: cache.New(),
}, nil
}
Expand Down Expand Up @@ -78,6 +81,10 @@ func (a *aws) TFClient() interface{} {
return a.tfAWSClient
}

func (a *aws) TFProvider() *schema.Provider {
return a.tfProvider
}

func (a *aws) String() string { return "aws" }

func (a *aws) Region() string { return a.awsr.GetRegions()[0] }
Expand Down
90 changes: 5 additions & 85 deletions aws/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"github.com/aws/aws-sdk-go/service/ses"
"github.com/cycloidio/terracognita/provider"
"github.com/cycloidio/terracognita/tag"
"github.com/hashicorp/terraform/helper/schema"
"github.com/pkg/errors"
)

// ResourceType is the type used to define all the Resources
Expand Down Expand Up @@ -154,19 +152,7 @@ var (
)

func initializeResource(a *aws, ID, t string) (provider.Resource, error) {
tfr, ok := a.tfProvider.ResourcesMap[t]
if !ok {
return nil, errors.Errorf("the resource %q does not exists on Terraform", t)
}

data := tfr.Data(nil)
data.SetId(ID)
data.SetType(t)

return provider.NewResource(
ID, t, tfr,
data, a,
), nil
return provider.NewResource(ID, t, a), nil
}

func instances(ctx context.Context, a *aws, resourceType string, tags []tag.Tag) ([]provider.Resource, error) {
Expand Down Expand Up @@ -505,10 +491,6 @@ func iamAccountAliases(ctx context.Context, a *aws, resourceType string, tags []
if err != nil {
return nil, err
}
err = r.Data().Set("account_alias", *i)
if err != nil {
return nil, err
}
resources = append(resources, r)
}

Expand All @@ -517,7 +499,7 @@ func iamAccountAliases(ctx context.Context, a *aws, resourceType string, tags []

func iamAccountPasswordPolicy(ctx context.Context, a *aws, resourceType string, tags []tag.Tag) ([]provider.Resource, error) {
// As it's for the full account we'll tell TF to fetch it directly with a "" id
r, err := initializeResource(a, NoID, resourceType)
r, err := initializeResource(a, "iam-account-password-policy", resourceType)
if err != nil {
return nil, err
}
Expand All @@ -536,11 +518,6 @@ func iamGroups(ctx context.Context, a *aws, resourceType string, tags []tag.Tag)
if err != nil {
return nil, err
}
// Needed for the cache
err = r.Data().Set("name", *i.GroupName)
if err != nil {
return nil, err
}
resources = append(resources, r)
}

Expand Down Expand Up @@ -620,15 +597,6 @@ func iamGroupPolicyAttachments(ctx context.Context, a *aws, resourceType string,
if err != nil {
return nil, err
}
err = r.Data().Set("group", gn)
if err != nil {
return nil, err
}

err = r.Data().Set("policy_arn", *i.PolicyArn)
if err != nil {
return nil, err
}
resources = append(resources, r)
}
}
Expand Down Expand Up @@ -705,11 +673,6 @@ func iamRoles(ctx context.Context, a *aws, resourceType string, tags []tag.Tag)
if err != nil {
return nil, err
}
// Needed for cache
err = r.Data().Set("name", *i.RoleName)
if err != nil {
return nil, err
}
resources = append(resources, r)
}

Expand Down Expand Up @@ -765,14 +728,6 @@ func iamRolePolicyAttachments(ctx context.Context, a *aws, resourceType string,
if err != nil {
return nil, err
}
err = r.Data().Set("role", rn)
if err != nil {
return nil, err
}
err = r.Data().Set("policy_arn", *i.PolicyArn)
if err != nil {
return nil, err
}
resources = append(resources, r)
}
}
Expand Down Expand Up @@ -810,10 +765,6 @@ func iamServerCertificates(ctx context.Context, a *aws, resourceType string, tag
if err != nil {
return nil, err
}
err = r.Data().Set("name", *i.ServerCertificateName)
if err != nil {
return nil, err
}
resources = append(resources, r)
}

Expand All @@ -832,11 +783,6 @@ func iamUsers(ctx context.Context, a *aws, resourceType string, tags []tag.Tag)
if err != nil {
return nil, err
}
// Needed for cache
err = r.Data().Set("name", *i.UserName)
if err != nil {
return nil, err
}
resources = append(resources, r)
}

Expand All @@ -854,23 +800,10 @@ func iamUserGroupMemberships(ctx context.Context, a *aws, resourceType string, t
return nil, err
}

gni := make([]interface{}, len(groupNames))
for i, gn := range groupNames {
gni[i] = gn
}
groupSet := schema.NewSet(schema.HashString, gni)

resources := make([]provider.Resource, 0)
for _, un := range userNames {
r, err := initializeResource(a, NoID, resourceType)
if err != nil {
return nil, err
}
err = r.Data().Set("user", un)
if err != nil {
return nil, err
}
err = r.Data().Set("groups", groupSet)
// The format expected by TF is <user-name>/<group-name1>/...
r, err := initializeResource(a, strings.Join(append([]string{un}, groupNames...), "/"), resourceType)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -925,15 +858,7 @@ func iamUserPolicyAttachments(ctx context.Context, a *aws, resourceType string,
}

for _, i := range userPolicies[a.Region()].AttachedPolicies {
r, err := initializeResource(a, NoID, resourceType)
if err != nil {
return nil, err
}
err = r.Data().Set("user", un)
if err != nil {
return nil, err
}
err = r.Data().Set("policy_arn", *i.PolicyArn)
r, err := initializeResource(a, fmt.Sprintf("%s/%s", un, *i.PolicyArn), resourceType)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1142,11 +1067,6 @@ func sesDomainIdentities(ctx context.Context, a *aws, resourceType string, tags
if err != nil {
return nil, err
}
// For the cache
err = r.Data().Set("domain", *i)
if err != nil {
return nil, err
}
resources = append(resources, r)
}

Expand Down
2 changes: 1 addition & 1 deletion cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestSetGet(t *testing.T) {
t.Run("Success", func(t *testing.T) {
c := cache.New()
r := provider.NewResource("id", "", nil, nil, nil)
r := provider.NewResource("id", "", nil)
err := c.Set("k", []provider.Resource{r})
require.NoError(t, err)

Expand Down
10 changes: 6 additions & 4 deletions cmd/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (

"github.com/cycloidio/terracognita/aws"
"github.com/cycloidio/terracognita/filter"
"github.com/cycloidio/terracognita/hcl"
"github.com/cycloidio/terracognita/log"
"github.com/cycloidio/terracognita/provider"
"github.com/cycloidio/terracognita/state"
"github.com/cycloidio/terracognita/tag"
"github.com/cycloidio/terracognita/writer"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -60,14 +62,14 @@ var (

var hclW, stateW writer.Writer

if hcl != nil {
if hclOut != nil {
logger.Log("msg", "initialzing HCL writer")
hclW = writer.NewHCLWriter(hcl)
hclW = hcl.NewWriter(hclOut)
}

if tfstate != nil {
if stateOut != nil {
logger.Log("msg", "initialzing TFState writer")
stateW = writer.NewTFStateWriter(tfstate)
stateW = state.NewWriter(stateOut)
}

logger.Log("msg", "importing")
Expand Down
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

var (
hcl io.Writer
tfstate io.Writer
hclOut io.Writer
stateOut io.Writer
closeOut []io.Closer
include, exclude []string
logsOut io.Writer
Expand Down Expand Up @@ -47,15 +47,15 @@ func preRunEOutput(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("could not OpenFile %s because: %s", viper.GetString("hcl"), err)
}
hcl = f
hclOut = f
closeOut = append(closeOut, f)
}
if viper.GetString("tfstate") != "" {
f, err := os.OpenFile(viper.GetString("tfstate"), os.O_APPEND|os.O_TRUNC|os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return fmt.Errorf("could not OpenFile %s because: %s", viper.GetString("tfstate"), err)
}
tfstate = f
stateOut = f
closeOut = append(closeOut, f)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.4.0
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20191003145700-f8707a46c6ec
github.com/zclconf/go-cty v1.0.1-0.20190708163926-19588f92a98f
k8s.io/apimachinery v0.0.0-20190213030929-f84a4639d8e8 // indirect
k8s.io/klog v0.2.0 // indirect
)
Expand Down
Loading

0 comments on commit 79e0348

Please sign in to comment.