github.com/pulumi/terraform@v1.4.0/pkg/command/jsonprovider/provider.go (about)

     1  package jsonprovider
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/pulumi/terraform/pkg/terraform"
     7  )
     8  
     9  // FormatVersion represents the version of the json format and will be
    10  // incremented for any change to this format that requires changes to a
    11  // consuming parser.
    12  const FormatVersion = "1.0"
    13  
    14  // providers is the top-level object returned when exporting provider schemas
    15  type providers struct {
    16  	FormatVersion string               `json:"format_version"`
    17  	Schemas       map[string]*Provider `json:"provider_schemas,omitempty"`
    18  }
    19  
    20  type Provider struct {
    21  	Provider          *Schema            `json:"provider,omitempty"`
    22  	ResourceSchemas   map[string]*Schema `json:"resource_schemas,omitempty"`
    23  	DataSourceSchemas map[string]*Schema `json:"data_source_schemas,omitempty"`
    24  }
    25  
    26  func newProviders() *providers {
    27  	schemas := make(map[string]*Provider)
    28  	return &providers{
    29  		FormatVersion: FormatVersion,
    30  		Schemas:       schemas,
    31  	}
    32  }
    33  
    34  // MarshalForRenderer converts the provided internation representation of the
    35  // schema into the public structured JSON versions.
    36  //
    37  // This is a format that can be read by the structured plan renderer.
    38  func MarshalForRenderer(s *terraform.Schemas) map[string]*Provider {
    39  	schemas := make(map[string]*Provider, len(s.Providers))
    40  	for k, v := range s.Providers {
    41  		schemas[k.String()] = marshalProvider(v)
    42  	}
    43  	return schemas
    44  }
    45  
    46  func Marshal(s *terraform.Schemas) ([]byte, error) {
    47  	providers := newProviders()
    48  	providers.Schemas = MarshalForRenderer(s)
    49  	ret, err := json.Marshal(providers)
    50  	return ret, err
    51  }
    52  
    53  func marshalProvider(tps *terraform.ProviderSchema) *Provider {
    54  	if tps == nil {
    55  		return &Provider{}
    56  	}
    57  
    58  	var ps *Schema
    59  	var rs, ds map[string]*Schema
    60  
    61  	if tps.Provider != nil {
    62  		ps = marshalSchema(tps.Provider)
    63  	}
    64  
    65  	if tps.ResourceTypes != nil {
    66  		rs = marshalSchemas(tps.ResourceTypes, tps.ResourceTypeSchemaVersions)
    67  	}
    68  
    69  	if tps.DataSources != nil {
    70  		ds = marshalSchemas(tps.DataSources, tps.ResourceTypeSchemaVersions)
    71  	}
    72  
    73  	return &Provider{
    74  		Provider:          ps,
    75  		ResourceSchemas:   rs,
    76  		DataSourceSchemas: ds,
    77  	}
    78  }