2019-02-25 22:32:47 +01:00
|
|
|
package jsonprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FormatVersion represents the version of the json format and will be
|
|
|
|
// incremented for any change to this format that requires changes to a
|
|
|
|
// consuming parser.
|
|
|
|
const FormatVersion = "0.1"
|
|
|
|
|
|
|
|
// providers is the top-level object returned when exporting provider schemas
|
|
|
|
type providers struct {
|
2019-03-14 22:52:07 +01:00
|
|
|
FormatVersion string `json:"format_version"`
|
|
|
|
Schemas map[string]*Provider `json:"provider_schemas,omitempty"`
|
2019-02-25 22:32:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Provider struct {
|
|
|
|
Provider *schema `json:"provider,omitempty"`
|
|
|
|
ResourceSchemas map[string]*schema `json:"resource_schemas,omitempty"`
|
|
|
|
DataSourceSchemas map[string]*schema `json:"data_source_schemas,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newProviders() *providers {
|
2019-03-14 22:52:07 +01:00
|
|
|
schemas := make(map[string]*Provider)
|
2019-02-25 22:32:47 +01:00
|
|
|
return &providers{
|
|
|
|
FormatVersion: FormatVersion,
|
|
|
|
Schemas: schemas,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Marshal(s *terraform.Schemas) ([]byte, error) {
|
|
|
|
providers := newProviders()
|
|
|
|
|
|
|
|
for k, v := range s.Providers {
|
2020-04-02 18:58:44 +02:00
|
|
|
providers.Schemas[k.String()] = marshalProvider(v)
|
2019-02-25 22:32:47 +01:00
|
|
|
}
|
|
|
|
|
2019-03-11 15:29:36 +01:00
|
|
|
ret, err := json.Marshal(providers)
|
2019-02-25 22:32:47 +01:00
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:52:07 +01:00
|
|
|
func marshalProvider(tps *terraform.ProviderSchema) *Provider {
|
2019-02-25 22:32:47 +01:00
|
|
|
if tps == nil {
|
2019-03-14 22:52:07 +01:00
|
|
|
return &Provider{}
|
2019-02-25 22:32:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var ps *schema
|
|
|
|
var rs, ds map[string]*schema
|
|
|
|
|
|
|
|
if tps.Provider != nil {
|
|
|
|
ps = marshalSchema(tps.Provider)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tps.ResourceTypes != nil {
|
|
|
|
rs = marshalSchemas(tps.ResourceTypes, tps.ResourceTypeSchemaVersions)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tps.DataSources != nil {
|
|
|
|
ds = marshalSchemas(tps.DataSources, tps.ResourceTypeSchemaVersions)
|
|
|
|
}
|
|
|
|
|
2019-03-14 22:52:07 +01:00
|
|
|
return &Provider{
|
2019-02-25 22:32:47 +01:00
|
|
|
Provider: ps,
|
|
|
|
ResourceSchemas: rs,
|
|
|
|
DataSourceSchemas: ds,
|
|
|
|
}
|
|
|
|
}
|