From c1ea0a8067be2526f0d10eb13ab911862bfdc206 Mon Sep 17 00:00:00 2001 From: Elie Date: Tue, 3 Aug 2021 10:26:38 +0200 Subject: [PATCH] Add source in JSON output --- pkg/analyser/analyzer_test.go | 7 ++++- pkg/analyser/testdata/output.json | 7 ++++- pkg/cmd/scan/output/testdata/output.json | 7 ++++- .../testdata/output_computed_fields.json | 7 ++++- pkg/resource/resource.go | 30 +++++++++++++++++-- test/resource/resource.go | 13 ++++++-- 6 files changed, 62 insertions(+), 9 deletions(-) diff --git a/pkg/analyser/analyzer_test.go b/pkg/analyser/analyzer_test.go index 158070d4..4c46684c 100644 --- a/pkg/analyser/analyzer_test.go +++ b/pkg/analyser/analyzer_test.go @@ -1089,9 +1089,14 @@ func TestAnalysis_MarshalJSON(t *testing.T) { }, ) analysis.AddDifference(Difference{ - Res: &testresource.FakeResource{ + Res: &resource.AbstractResource{ Id: "AKIA5QYBVVD25KFXJHYJ", Type: "aws_iam_access_key", + Source: &resource.TerraformStateSource{ + State: "tfstate://terraform.tfstate", + Module: "module", + Name: "my_name", + }, }, Changelog: []Change{ { diff --git a/pkg/analyser/testdata/output.json b/pkg/analyser/testdata/output.json index dcb50b06..832715a5 100644 --- a/pkg/analyser/testdata/output.json +++ b/pkg/analyser/testdata/output.json @@ -40,7 +40,12 @@ { "res": { "id": "AKIA5QYBVVD25KFXJHYJ", - "type": "aws_iam_access_key" + "type": "aws_iam_access_key", + "source": { + "source": "tfstate://terraform.tfstate", + "namespace": "module", + "internal_name": "my_name" + } }, "changelog": [ { diff --git a/pkg/cmd/scan/output/testdata/output.json b/pkg/cmd/scan/output/testdata/output.json index 1dda799b..afc25dbc 100644 --- a/pkg/cmd/scan/output/testdata/output.json +++ b/pkg/cmd/scan/output/testdata/output.json @@ -40,7 +40,12 @@ { "res": { "id": "diff-id-1", - "type": "aws_diff_resource" + "type": "aws_diff_resource", + "source": { + "source": "tfstate://state.tfstate", + "namespace": "module", + "internal_name": "name" + } }, "changelog": [ { diff --git a/pkg/cmd/scan/output/testdata/output_computed_fields.json b/pkg/cmd/scan/output/testdata/output_computed_fields.json index 6112f9dc..cdcb8eff 100644 --- a/pkg/cmd/scan/output/testdata/output_computed_fields.json +++ b/pkg/cmd/scan/output/testdata/output_computed_fields.json @@ -18,7 +18,12 @@ { "res": { "id": "diff-id-1", - "type": "aws_diff_resource" + "type": "aws_diff_resource", + "source": { + "source": "tfstate://state.tfstate", + "namespace": "module", + "internal_name": "name" + } }, "changelog": [ { diff --git a/pkg/resource/resource.go b/pkg/resource/resource.go index e5b7ddd6..b713f963 100644 --- a/pkg/resource/resource.go +++ b/pkg/resource/resource.go @@ -16,6 +16,7 @@ type Resource interface { TerraformType() string Attributes() *Attributes Schema() *Schema + Src() Source } type Source interface { @@ -24,6 +25,12 @@ type Source interface { InternalName() string } +type SerializableSource struct { + S string `json:"source"` + Ns string `json:"namespace"` + Name string `json:"internal_name"` +} + type TerraformStateSource struct { State string Module string @@ -70,6 +77,10 @@ func (a *AbstractResource) Attributes() *Attributes { return a.Attrs } +func (a *AbstractResource) Src() Source { + return a.Source +} + func (a *AbstractResource) SourceString() string { if a.Source.Namespace() == "" { return fmt.Sprintf("%s.%s", a.TerraformType(), a.Source.InternalName()) @@ -86,8 +97,9 @@ type SerializableResource struct { } type SerializedResource struct { - Id string `json:"id"` - Type string `json:"type"` + Id string `json:"id"` + Type string `json:"type"` + Source *SerializableSource `json:"source,omitempty"` } func (u *SerializedResource) TerraformId() string { @@ -106,6 +118,10 @@ func (u *SerializedResource) Schema() *Schema { return nil } +func (u *SerializedResource) Src() Source { + return nil +} + func (s *SerializableResource) UnmarshalJSON(bytes []byte) error { var res *SerializedResource @@ -117,7 +133,15 @@ func (s *SerializableResource) UnmarshalJSON(bytes []byte) error { } func (s SerializableResource) MarshalJSON() ([]byte, error) { - return json.Marshal(SerializedResource{Id: s.TerraformId(), Type: s.TerraformType()}) + var src *SerializableSource + if s.Src() != nil { + src = &SerializableSource{ + S: s.Src().Source(), + Ns: s.Src().Namespace(), + Name: s.Src().InternalName(), + } + } + return json.Marshal(SerializedResource{Id: s.TerraformId(), Type: s.TerraformType(), Source: src}) } type NormalizedResource interface { diff --git a/test/resource/resource.go b/test/resource/resource.go index 57ee1e7f..692cda28 100644 --- a/test/resource/resource.go +++ b/test/resource/resource.go @@ -31,9 +31,14 @@ func (d *FakeResource) Attributes() *resource.Attributes { return d.Attrs } +func (d *FakeResource) Src() resource.Source { + return nil +} + type FakeResourceStringer struct { - Id string - Attrs *resource.Attributes + Id string + Attrs *resource.Attributes + Source resource.Source } func (d *FakeResourceStringer) Schema() *resource.Schema { @@ -52,6 +57,10 @@ func (d *FakeResourceStringer) Attributes() *resource.Attributes { return d.Attrs } +func (d *FakeResourceStringer) Src() resource.Source { + return d.Source +} + func InitFakeSchemaRepository(provider, version string) resource.SchemaRepositoryInterface { repo := resource.NewSchemaRepository() schema := make(map[string]providers.Schema)