Add source in JSON output

main
Elie 2021-08-03 10:26:38 +02:00
parent 75cd450a63
commit c1ea0a8067
No known key found for this signature in database
GPG Key ID: 399AF69092C727B6
6 changed files with 62 additions and 9 deletions

View File

@ -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{
{

View File

@ -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": [
{

View File

@ -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": [
{

View File

@ -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": [
{

View File

@ -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 {

View File

@ -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)