Skip to content

Commit

Permalink
Add source in JSON output
Browse files Browse the repository at this point in the history
  • Loading branch information
Elie committed Aug 4, 2021
1 parent 75cd450 commit c1ea0a8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 deletions.
7 changes: 6 additions & 1 deletion pkg/analyser/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down
7 changes: 6 additions & 1 deletion pkg/analyser/testdata/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/scan/output/testdata/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
7 changes: 6 additions & 1 deletion pkg/cmd/scan/output/testdata/output_computed_fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
30 changes: 27 additions & 3 deletions pkg/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Resource interface {
TerraformType() string
Attributes() *Attributes
Schema() *Schema
Src() Source
}

type Source interface {
Expand All @@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -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 {
Expand All @@ -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

Expand All @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions test/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit c1ea0a8

Please sign in to comment.