Normalize resource attributes for various formats

for:

- json
- toml
- xml
- and yaml

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
This commit is contained in:
Khosrow Moossavi
2021-03-15 17:50:14 -04:00
parent 8e9d7ac450
commit caebead84d
12 changed files with 113 additions and 105 deletions

View File

@@ -37,11 +37,8 @@
{{- if .Settings.ShowResources -}}
{{- with .Module.Resources }}
{{- range . }}
{{- if eq (len .URL) 0 }}
{{- printf "resource.%s (%s)" .Spec .GetMode | colorize "\033[36m" }}
{{- else -}}
{{- printf "resource.%s (%s)" .Spec .GetMode | colorize "\033[36m" }} ({{ .URL}})
{{- end }}
{{- $url := ternary .URL (printf " (%s)" .URL) "" }}
{{- printf "resource.%s (%s)" .Spec .GetMode | colorize "\033[36m" }}{{ $url }}
{{ end -}}
{{ end -}}
{{- printf "\n\n" -}}
@@ -77,4 +74,4 @@
{{ colorize "\033[90m" . }}
{{ end -}}
{{- printf "\n\n" -}}
{{ end -}}
{{ end -}}

View File

@@ -320,34 +320,34 @@
{
"type": "resource",
"name": "foo",
"providerName": "null",
"provicerSource": "hashicorp/null",
"provider": "null",
"source": "hashicorp/null",
"mode": "managed",
"version": "latest"
},
{
"type": "private_key",
"name": "baz",
"providerName": "tls",
"provicerSource": "hashicorp/tls",
"provider": "tls",
"source": "hashicorp/tls",
"mode": "managed",
"version": "latest"
},
{
"type": "caller_identity",
"name": "current",
"providerName": "aws",
"provicerSource": "hashicorp/aws",
"provider": "aws",
"source": "hashicorp/aws",
"mode": "data",
"version": "latest"
},
{
"type": "caller_identity",
"name": "ident",
"providerName": "aws",
"provicerSource": "hashicorp/aws",
"provider": "aws",
"source": "hashicorp/aws",
"mode": "data",
"version": "latest"
}
]
}
}

View File

@@ -320,34 +320,34 @@
{
"type": "resource",
"name": "foo",
"providerName": "null",
"provicerSource": "hashicorp/null",
"provider": "null",
"source": "hashicorp/null",
"mode": "managed",
"version": "latest"
},
{
"type": "private_key",
"name": "baz",
"providerName": "tls",
"provicerSource": "hashicorp/tls",
"provider": "tls",
"source": "hashicorp/tls",
"mode": "managed",
"version": "latest"
},
{
"type": "caller_identity",
"name": "current",
"providerName": "aws",
"provicerSource": "hashicorp/aws",
"provider": "aws",
"source": "hashicorp/aws",
"mode": "data",
"version": "latest"
},
{
"type": "caller_identity",
"name": "ident",
"providerName": "aws",
"provicerSource": "hashicorp/aws",
"provider": "aws",
"source": "hashicorp/aws",
"mode": "data",
"version": "latest"
}
]
}
}

View File

@@ -10,34 +10,34 @@
{
"type": "resource",
"name": "foo",
"providerName": "null",
"provicerSource": "hashicorp/null",
"provider": "null",
"source": "hashicorp/null",
"mode": "managed",
"version": "latest"
},
{
"type": "private_key",
"name": "baz",
"providerName": "tls",
"provicerSource": "hashicorp/tls",
"provider": "tls",
"source": "hashicorp/tls",
"mode": "managed",
"version": "latest"
},
{
"type": "caller_identity",
"name": "current",
"providerName": "aws",
"provicerSource": "hashicorp/aws",
"provider": "aws",
"source": "hashicorp/aws",
"mode": "data",
"version": "latest"
},
{
"type": "caller_identity",
"name": "ident",
"providerName": "aws",
"provicerSource": "hashicorp/aws",
"provider": "aws",
"source": "hashicorp/aws",
"mode": "data",
"version": "latest"
}
]
}
}

View File

@@ -296,31 +296,31 @@ footer = "## This is an example of a footer\n\nIt looks exactly like a header, b
[[resources]]
type = "resource"
name = "foo"
providerName = "null"
providerSource = "hashicorp/null"
provider = "null"
source = "hashicorp/null"
mode = "managed"
version = "latest"
[[resources]]
type = "private_key"
name = "baz"
providerName = "tls"
providerSource = "hashicorp/tls"
provider = "tls"
source = "hashicorp/tls"
mode = "managed"
version = "latest"
[[resources]]
type = "caller_identity"
name = "current"
providerName = "aws"
providerSource = "hashicorp/aws"
provider = "aws"
source = "hashicorp/aws"
mode = "data"
version = "latest"
[[resources]]
type = "caller_identity"
name = "ident"
providerName = "aws"
providerSource = "hashicorp/aws"
provider = "aws"
source = "hashicorp/aws"
mode = "data"
version = "latest"

View File

@@ -9,31 +9,31 @@ requirements = []
[[resources]]
type = "resource"
name = "foo"
providerName = "null"
providerSource = "hashicorp/null"
provider = "null"
source = "hashicorp/null"
mode = "managed"
version = "latest"
[[resources]]
type = "private_key"
name = "baz"
providerName = "tls"
providerSource = "hashicorp/tls"
provider = "tls"
source = "hashicorp/tls"
mode = "managed"
version = "latest"
[[resources]]
type = "caller_identity"
name = "current"
providerName = "aws"
providerSource = "hashicorp/aws"
provider = "aws"
source = "hashicorp/aws"
mode = "data"
version = "latest"
[[resources]]
type = "caller_identity"
name = "ident"
providerName = "aws"
providerSource = "hashicorp/aws"
provider = "aws"
source = "hashicorp/aws"
mode = "data"
version = "latest"
version = "latest"

View File

@@ -320,32 +320,32 @@
<resource>
<type>resource</type>
<name>foo</name>
<providerName>null</providerName>
<providerSource>hashicorp/null</providerSource>
<provider>null</provider>
<source>hashicorp/null</source>
<mode>managed</mode>
<version>latest</version>
</resource>
<resource>
<type>private_key</type>
<name>baz</name>
<providerName>tls</providerName>
<providerSource>hashicorp/tls</providerSource>
<provider>tls</provider>
<source>hashicorp/tls</source>
<mode>managed</mode>
<version>latest</version>
</resource>
<resource>
<type>caller_identity</type>
<name>current</name>
<providerName>aws</providerName>
<providerSource>hashicorp/aws</providerSource>
<provider>aws</provider>
<source>hashicorp/aws</source>
<mode>data</mode>
<version>latest</version>
</resource>
<resource>
<type>caller_identity</type>
<name>ident</name>
<providerName>aws</providerName>
<providerSource>hashicorp/aws</providerSource>
<provider>aws</provider>
<source>hashicorp/aws</source>
<mode>data</mode>
<version>latest</version>
</resource>

View File

@@ -10,34 +10,34 @@
<resource>
<type>resource</type>
<name>foo</name>
<providerName>null</providerName>
<providerSource>hashicorp/null</providerSource>
<provider>null</provider>
<source>hashicorp/null</source>
<mode>managed</mode>
<version>latest</version>
</resource>
<resource>
<type>private_key</type>
<name>baz</name>
<providerName>tls</providerName>
<providerSource>hashicorp/tls</providerSource>
<provider>tls</provider>
<source>hashicorp/tls</source>
<mode>managed</mode>
<version>latest</version>
</resource>
<resource>
<type>caller_identity</type>
<name>current</name>
<providerName>aws</providerName>
<providerSource>hashicorp/aws</providerSource>
<provider>aws</provider>
<source>hashicorp/aws</source>
<mode>data</mode>
<version>latest</version>
</resource>
<resource>
<type>caller_identity</type>
<name>ident</name>
<providerName>aws</providerName>
<providerSource>hashicorp/aws</providerSource>
<provider>aws</provider>
<source>hashicorp/aws</source>
<mode>data</mode>
<version>latest</version>
</resource>
</resources>
</module>
</module>

View File

@@ -266,25 +266,25 @@ requirements:
resources:
- type: resource
name: foo
providerName: "null"
providerSource: hashicorp/null
provider: "null"
source: hashicorp/null
mode: managed
version: latest
- type: private_key
name: baz
providerName: tls
providerSource: hashicorp/tls
provider: tls
source: hashicorp/tls
mode: managed
version: latest
- type: caller_identity
name: current
providerName: aws
providerSource: hashicorp/aws
provider: aws
source: hashicorp/aws
mode: data
version: latest
- type: caller_identity
name: ident
providerName: aws
providerSource: hashicorp/aws
provider: aws
source: hashicorp/aws
mode: data
version: latest
version: latest

View File

@@ -8,25 +8,25 @@ requirements: []
resources:
- type: resource
name: foo
providerName: "null"
providerSource: hashicorp/null
provider: "null"
source: hashicorp/null
mode: managed
version: latest
- type: private_key
name: baz
providerName: tls
providerSource: hashicorp/tls
provider: tls
source: hashicorp/tls
mode: managed
version: latest
- type: caller_identity
name: current
providerName: aws
providerSource: hashicorp/aws
provider: aws
source: hashicorp/aws
mode: data
version: latest
- type: caller_identity
name: ident
providerName: aws
providerSource: hashicorp/aws
provider: aws
source: hashicorp/aws
mode: data
version: latest
version: latest

View File

@@ -428,12 +428,14 @@ func loadResources(tfmodule *tfconfig.Module) []*Resource {
if rv, ok := tfmodule.RequiredProviders[r.Provider.Name]; ok {
version = resourceVersion(rv.VersionConstraints)
}
var source string
if len(tfmodule.RequiredProviders[r.Provider.Name].Source) > 0 {
source = tfmodule.RequiredProviders[r.Provider.Name].Source
} else {
source = fmt.Sprintf("%s/%s", "hashicorp", r.Provider.Name)
}
rType := strings.TrimPrefix(r.Type, r.Provider.Name+"_")
key := fmt.Sprintf("%s.%s.%s.%s", r.Provider.Name, r.Mode, rType, r.Name)
discovered[key] = &Resource{
@@ -443,6 +445,10 @@ func loadResources(tfmodule *tfconfig.Module) []*Resource {
ProviderName: r.Provider.Name,
ProviderSource: source,
Version: types.String(version),
Position: Position{
Filename: r.Pos.Filename,
Line: r.Pos.Line,
},
}
}
}
@@ -532,7 +538,7 @@ func sortItems(tfmodule *Module, sortby *SortBy) {
sort.Sort(providersSortedByPosition(tfmodule.Providers))
}
// Always sort resources
// resources (always sorted)
sort.Sort(resourcesSortedByType(tfmodule.Resources))
// modules

View File

@@ -22,10 +22,11 @@ import (
type Resource struct {
Type string `json:"type" toml:"type" xml:"type" yaml:"type"`
Name string `json:"name" toml:"name" xml:"name" yaml:"name"`
ProviderName string `json:"providerName" toml:"providerName" xml:"providerName" yaml:"providerName"`
ProviderSource string `json:"provicerSource" toml:"providerSource" xml:"providerSource" yaml:"providerSource"`
ProviderName string `json:"provider" toml:"provider" xml:"provider" yaml:"provider"`
ProviderSource string `json:"source" toml:"source" xml:"source" yaml:"source"`
Mode string `json:"mode" toml:"mode" xml:"mode" yaml:"mode"`
Version types.String `json:"version" toml:"version" xml:"version" yaml:"version"`
Position Position `json:"-" toml:"-" xml:"-" yaml:"-"`
}
// Spec returns the resource spec addresses a specific resource in the config.
@@ -66,23 +67,6 @@ func (r *Resource) URL() string {
return fmt.Sprintf("https://registry.terraform.io/providers/%s/%s/docs/%s/%s", r.ProviderSource, r.Version, kind, r.Type)
}
type resources []*Resource
func (rr resources) convert() []*terraformsdk.Resource {
list := []*terraformsdk.Resource{}
for _, r := range rr {
list = append(list, &terraformsdk.Resource{
Type: r.Type,
Name: r.Name,
ProviderName: r.ProviderName,
ProviderSource: r.ProviderSource,
Version: fmt.Sprintf("%v", r.Version.Raw()),
})
}
return list
}
type resourcesSortedByType []*Resource
func (a resourcesSortedByType) Len() int { return len(a) }
@@ -96,3 +80,24 @@ func (a resourcesSortedByType) Less(i, j int) bool {
}
return a[i].Mode > a[j].Mode
}
type resources []*Resource
func (rr resources) convert() []*terraformsdk.Resource {
list := []*terraformsdk.Resource{}
for _, r := range rr {
list = append(list, &terraformsdk.Resource{
Type: r.Type,
Name: r.Name,
ProviderName: r.ProviderName,
ProviderSource: r.ProviderSource,
Version: fmt.Sprintf("%v", r.Version.Raw()),
Position: terraformsdk.Position{
Filename: r.Position.Filename,
Line: r.Position.Line,
},
})
}
return list
}