opentofu/internal/command/jsonprovider/provider.go
Nathan Baulch ea558d9d4b
Fix typos (#1905)
Signed-off-by: Nathan Baulch <nathan.baulch@gmail.com>
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Co-authored-by: Christian Mesh <christianmesh1@gmail.com>
2024-08-29 13:20:33 -04:00

68 lines
2.0 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package jsonprovider
import (
"encoding/json"
"github.com/opentofu/opentofu/internal/providers"
"github.com/opentofu/opentofu/internal/tofu"
)
// 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 = "1.0"
// Providers is the top-level object returned when exporting provider schemas
type Providers struct {
FormatVersion string `json:"format_version"`
Schemas map[string]*Provider `json:"provider_schemas,omitempty"`
}
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"`
Functions map[string]*Function `json:"functions,omitempty"`
}
func newProviders() *Providers {
schemas := make(map[string]*Provider)
return &Providers{
FormatVersion: FormatVersion,
Schemas: schemas,
}
}
// MarshalForRenderer converts the provided internal representation of the
// schema into the public structured JSON versions.
//
// This is a format that can be read by the structured plan renderer.
func MarshalForRenderer(s *tofu.Schemas) map[string]*Provider {
schemas := make(map[string]*Provider, len(s.Providers))
for k, v := range s.Providers {
schemas[k.String()] = marshalProvider(v)
}
return schemas
}
func Marshal(s *tofu.Schemas) ([]byte, error) {
providers := newProviders()
providers.Schemas = MarshalForRenderer(s)
ret, err := json.Marshal(providers)
return ret, err
}
func marshalProvider(tps providers.ProviderSchema) *Provider {
return &Provider{
Provider: marshalSchema(tps.Provider),
ResourceSchemas: marshalSchemas(tps.ResourceTypes),
DataSourceSchemas: marshalSchemas(tps.DataSources),
Functions: marshalFunctions(tps.Functions),
}
}