Skip to content

Commit

Permalink
state: Add the module information to the State
Browse files Browse the repository at this point in the history
Now the resources will have the module information on it if it's imported as a module
  • Loading branch information
xescugc committed Sep 23, 2021
1 parent 729d342 commit f4092a9
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
([PR #236](https://github.com/cycloidio/terracognita/issues/237))
- Fix error to enable support for azurerm_resource_group
([Issue #232](https://github.com/cycloidio/terracognita/issues/232))
- TFState when generating modules now has the module on it too
([Issue #240](https://github.com/cycloidio/terracognita/issues/240))


## [0.7.3] _2021-08-30_
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ require (
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
golang.org/x/oauth2 v0.0.0-20210220000619-9bb904979d93 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/tools v0.1.5 // indirect
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/tools v0.1.6 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/api v0.41.0
google.golang.org/appengine v1.6.7 // indirect
Expand Down
19 changes: 10 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
Expand Down Expand Up @@ -953,8 +953,8 @@ golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d h1:20cMwl2fHAzkJMEA+8J4JgqBQcQGzbisXo31MIeenXI=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down Expand Up @@ -1030,9 +1030,9 @@ golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210305230114-8fe3ee5dd75b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand All @@ -1045,8 +1045,9 @@ golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db/go.mod h1:bEr9sfX3Q8Zfm5f
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down Expand Up @@ -1113,8 +1114,8 @@ golang.org/x/tools v0.0.0-20201201161351-ac6f37ff4c2a/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.0.0-20201208233053-a543418bbed2/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.6 h1:SIasE1FVIQOWz2GEAHFOmoW7xchJcqlucjSULTL0Ag4=
golang.org/x/tools v0.1.6/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
17 changes: 15 additions & 2 deletions state/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,17 @@ func (w *Writer) Write(key string, value interface{}) error {
return errors.Wrapf(errcode.ErrWriterInvalidTypeValue, "expected provider.Resource, found %T", value)
}

var md []addrs.ModuleInstanceStep = nil
if w.opts.HasModule() {
md = []addrs.ModuleInstanceStep{
addrs.ModuleInstanceStep{
Name: w.opts.Module,
},
}
}

absAddr := addrs.AbsResourceInstance{
Module: nil,
Module: md,
Resource: addrs.ResourceInstance{
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Expand Down Expand Up @@ -175,8 +184,12 @@ func (w *Writer) Interpolate(i map[string]string) {
s := strings.Split(dependency, ".")
rt := s[0]
rn := s[1]
var md []string = nil
if w.opts.HasModule() {
md = []string{w.opts.Module}
}
instance.Current.Dependencies = append(instance.Current.Dependencies, addrs.ConfigResource{
Module: nil,
Module: md,
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: rt,
Expand Down
226 changes: 225 additions & 1 deletion state/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,86 @@ func TestSync(t *testing.T) {
],
"mode":"managed",
"name":"name",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"type":"aws_iam_user"
}
],
"serial":0,
"terraform_version":"0.13.5",
"version":4
}`
)

defer ctrl.Finish()

tpt, err := util.HashicorpToZclonfType(aws.Provider().ResourcesMap[tp].CoreConfigSchema().ImpliedType())
require.NoError(t, err)

s, err := hcl2shim.HCL2ValueFromFlatmap(map[string]string{"name": "Pepito"}, tpt)
require.NoError(t, err)

res.EXPECT().Type().Return(tp)
res.EXPECT().Provider().Return(prv)
res.EXPECT().TFResource().Return(aws.Provider().ResourcesMap[tp])
res.EXPECT().ImpliedType().Return(aws.Provider().ResourcesMap[tp].CoreConfigSchema().ImpliedType())
res.EXPECT().ResourceInstanceObject().Return(providers.ImportedResource{
TypeName: tp,
State: s,
}.AsInstanceObject())

prv.EXPECT().String().Return("aws")

err = sw.Write("aws_iam_user.name", res)
require.NoError(t, err)

err = sw.Sync()
require.NoError(t, err)

var st map[string]interface{}
err = json.Unmarshal(b.Bytes(), &st)
require.NoError(t, err)

st["lineage"] = "lineage"

var est map[string]interface{}
err = json.Unmarshal([]byte(state), &est)
require.NoError(t, err)

assert.Equal(t, est, st)
})
t.Run("SuccessWithModule", func(t *testing.T) {
var (
ctrl = gomock.NewController(t)
b = &bytes.Buffer{}
sw = state.NewWriter(b, &writer.Options{Interpolate: true, Module: "cycloid"})
prv = mock.NewProvider(ctrl)
res = mock.NewResource(ctrl)
tp = "aws_iam_user"
state = `{
"lineage":"lineage",
"outputs":{},
"resources":[
{
"instances":[
{
"attributes":{
"arn":null,
"force_destroy":null,
"id":null,
"name":"Pepito",
"path":null,
"permissions_boundary":null,
"tags":null,
"tags_all":null,
"unique_id":null
},
"schema_version":0
}
],
"mode":"managed",
"name":"name",
"module": "module.cycloid",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"type":"aws_iam_user"
}
],
Expand Down Expand Up @@ -368,6 +447,151 @@ func TestDependencies(t *testing.T) {

assert.Equal(t, est, st)
})
t.Run("SuccessWitmModules", func(t *testing.T) {
var (
i = make(map[string]string)
ctrl = gomock.NewController(t)
b = &bytes.Buffer{}
sw = state.NewWriter(b, &writer.Options{Interpolate: true, Module: "cycloid"})
prv = mock.NewProvider(ctrl)
resSG = mock.NewResource(ctrl)
resSGR = mock.NewResource(ctrl)
sg = "aws_security_group"
sgr = "aws_security_group_rule"
state = `{
"version": 4,
"terraform_version": "0.13.5",
"serial": 0,
"lineage": "lineage",
"outputs": {},
"resources": [
{
"mode": "managed",
"module": "module.cycloid",
"type": "aws_security_group",
"name": "sg",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 1,
"attributes": {
"id": "sg-1234",
"name": "sg",
"description": null,
"egress": null,
"ingress": null,
"name_prefix": null,
"arn": null,
"owner_id": null,
"revoke_rules_on_delete": null,
"timeouts": {
"create": null,
"delete": null
},
"tags": null,
"tags_all": null,
"vpc_id": null
}
}
]
},
{
"mode": "managed",
"module": "module.cycloid",
"type": "aws_security_group_rule",
"name": "sgrule",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 2,
"attributes": {
"id": "sgrule-1234",
"security_group_id": "sg-1234",
"cidr_blocks": null,
"description": null,
"from_port": null,
"ipv6_cidr_blocks": null,
"prefix_list_ids": null,
"protocol": null,
"self": null,
"source_security_group_id": null,
"to_port": null,
"type": null
},
"dependencies": [
"module.cycloid.aws_security_group.sg"
]
}
]
}
]
}`
)

defer ctrl.Finish()

sgt, err := util.HashicorpToZclonfType(aws.Provider().ResourcesMap[sg].CoreConfigSchema().ImpliedType())
require.NoError(t, err)
sgrt, err := util.HashicorpToZclonfType(aws.Provider().ResourcesMap[sgr].CoreConfigSchema().ImpliedType())
require.NoError(t, err)

stateSG, err := hcl2shim.HCL2ValueFromFlatmap(map[string]string{"id": "sg-1234", "name": "sg"}, sgt)
stateSGR, err := hcl2shim.HCL2ValueFromFlatmap(map[string]string{"security_group_id": "sg-1234", "id": "sgrule-1234"}, sgrt)

require.NoError(t, err)

resSG.EXPECT().Type().Return(sg)
resSG.EXPECT().Provider().Return(prv)
resSG.EXPECT().TFResource().Return(aws.Provider().ResourcesMap[sg])
resSG.EXPECT().ImpliedType().Return(aws.Provider().ResourcesMap[sg].CoreConfigSchema().ImpliedType())
resSG.EXPECT().ResourceInstanceObject().Return(providers.ImportedResource{
TypeName: sg,
State: stateSG,
}.AsInstanceObject())
attrsSG := make(map[string]string)
resSG.EXPECT().InstanceState().Return(&terraform.InstanceState{
Attributes: attrsSG,
})

prv.EXPECT().String().Return("aws").AnyTimes()

resSGR.EXPECT().Type().Return(sgr).AnyTimes()
resSGR.EXPECT().Provider().Return(prv)
resSGR.EXPECT().TFResource().Return(aws.Provider().ResourcesMap[sgr])
resSGR.EXPECT().ImpliedType().Return(aws.Provider().ResourcesMap[sgr].CoreConfigSchema().ImpliedType())
resSGR.EXPECT().ResourceInstanceObject().Return(providers.ImportedResource{
TypeName: sgr,
State: stateSGR,
}.AsInstanceObject())
attrsSGR := make(map[string]string)
attrsSGR["security-group-id"] = "sg-1234"
resSGR.EXPECT().InstanceState().Return(&terraform.InstanceState{
Attributes: attrsSGR,
})

err = sw.Write("aws_security_group.sg", resSG)
require.NoError(t, err)
err = sw.Write("aws_security_group_rule.sgrule", resSGR)
require.NoError(t, err)

i["sg-1234"] = "${aws_security_group.sg.id}"
sw.Interpolate(i)

err = sw.Sync()
require.NoError(t, err)

var st map[string]interface{}
err = json.Unmarshal(b.Bytes(), &st)
require.NoError(t, err)

st["lineage"] = "lineage"

var est map[string]interface{}
err = json.Unmarshal([]byte(state), &est)
require.NoError(t, err)

assert.Equal(t, est, st)
})
t.Run("SuccessNoInterpolation", func(t *testing.T) {
var (
i = make(map[string]string)
Expand Down

0 comments on commit f4092a9

Please sign in to comment.