Skip to content

Commit

Permalink
Map new proto fields
Browse files Browse the repository at this point in the history
Borrows some core code from 23acb02cb846fd002e7a0b52904ee88019b33211
  • Loading branch information
paultyng committed Mar 12, 2020
1 parent e3ed6ab commit f25eeed
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 22 deletions.
43 changes: 37 additions & 6 deletions helper/schema/core_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema"
)

var (
// DescriptionKind is the default StringKind of descriptions in this provider.
// It defaults to StringPlain but can be globally switched to StringMarkdown.
DescriptionKind = configschema.StringPlain

// SchemaDescriptionBuilder converts helper/schema.Schema Descriptions to configschema.Attribute
// and Block Descriptions. This method can be used to modify the description text prior to it
// being returned in the schema.
SchemaDescriptionBuilder = func(s *Schema) string {
return s.Description
}

// ResourceDescriptionBuilder converts helper/schema.Resource Descriptions to configschema.Block
// Descriptions at the resource top level. This method can be used to modify the description prior
// to it being returned in the schema.
ResourceDescriptionBuilder = func(r *Resource) string {
return r.Description
}
)

// The functions and methods in this file are concerned with the conversion
// of this package's schema model into the slightly-lower-level schema model
// used by Terraform core for configuration parsing.
Expand Down Expand Up @@ -117,12 +137,14 @@ func (s *Schema) coreConfigSchemaAttribute() *configschema.Attribute {
}

return &configschema.Attribute{
Type: s.coreConfigSchemaType(),
Optional: opt,
Required: reqd,
Computed: s.Computed,
Sensitive: s.Sensitive,
Description: s.Description,
Type: s.coreConfigSchemaType(),
Optional: opt,
Required: reqd,
Computed: s.Computed,
Sensitive: s.Sensitive,
Description: SchemaDescriptionBuilder(s),
DescriptionKind: DescriptionKind,
Deprecated: s.Deprecated != "",
}
}

Expand All @@ -133,6 +155,10 @@ func (s *Schema) coreConfigSchemaBlock() *configschema.NestedBlock {
ret := &configschema.NestedBlock{}
if nested := s.Elem.(*Resource).coreConfigSchema(); nested != nil {
ret.Block = *nested
// set these on the block from the attribute Schema
ret.Block.Description = SchemaDescriptionBuilder(s)
ret.Block.DescriptionKind = DescriptionKind
ret.Block.Deprecated = s.Deprecated != ""
}
switch s.Type {
case TypeList:
Expand Down Expand Up @@ -232,6 +258,11 @@ func (s *Schema) coreConfigSchemaType() cty.Type {
func (r *Resource) CoreConfigSchema() *configschema.Block {
block := r.coreConfigSchema()

// Only apply Resource Description, Kind, Deprecation at top level
block.Description = ResourceDescriptionBuilder(r)
block.DescriptionKind = DescriptionKind
block.Deprecated = r.DeprecationMessage != ""

if block.Attributes == nil {
block.Attributes = map[string]*configschema.Attribute{}
}
Expand Down
5 changes: 5 additions & 0 deletions helper/schema/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ type Resource struct {
// actions (Create, Read, Update, Delete, Default) to the Resource struct, and
// accessing them in the matching methods.
Timeouts *ResourceTimeout

// Description is used as the description for docs, the language server and
// other user facing usage. It can be plain-text or markdown depending on the
// global DescriptionKind setting.
Description string
}

// ShimInstanceStateFromValue converts a cty.Value to a
Expand Down
6 changes: 3 additions & 3 deletions helper/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ type Schema struct {
Default interface{}
DefaultFunc SchemaDefaultFunc

// Description is used as the description for docs or asking for user
// input. It should be relatively short (a few sentences max) and should
// be formatted to fit a CLI.
// Description is used as the description for docs, the language server and
// other user facing usage. It can be plain-text or markdown depending on the
// global DescriptionKind setting.
Description string

// InputDefault is the default value to use for when inputs are requested.
Expand Down
27 changes: 26 additions & 1 deletion internal/configs/configschema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import (
"github.com/zclconf/go-cty/cty"
)

// StringKind represents the format a string is in.
type StringKind int

const (
// StringPlain indicates a string is plain-text and requires no processing for display.
StringPlain StringKind = iota
// StringMarkdown indicates a string is in markdown format and may
// require additional processing to display.
StringMarkdown
)

// Block represents a configuration block.
//
// "Block" here is a logical grouping construct, though it happens to map
Expand All @@ -21,6 +32,15 @@ type Block struct {
// BlockTypes describes any nested block types that may appear directly
// inside the block.
BlockTypes map[string]*NestedBlock

// Description and DescriptionKind contain a user facing description of the block
// and the format of that string.
Description string
DescriptionKind StringKind

// Deprecated indicates whether the block has been marked as deprecated in the
// provider and usage should be discouraged.
Deprecated bool
}

// Attribute represents a configuration attribute, within a block.
Expand All @@ -32,7 +52,8 @@ type Attribute struct {
// usage of the attribute. A description should be concise and use only
// one or two sentences, leaving full definition to longer-form
// documentation defined elsewhere.
Description string
Description string
DescriptionKind StringKind

// Required, if set to true, specifies that an omitted or null value is
// not permitted.
Expand All @@ -55,6 +76,10 @@ type Attribute struct {
// future to help Terraform mask sensitive information. (Terraform
// currently achieves this in a limited sense via other mechanisms.)
Sensitive bool

// Deprecated indicates whether the attribute has been marked as deprecated in the
// provider and usage should be discouraged.
Deprecated bool
}

// NestedBlock represents the embedding of one block within another.
Expand Down
55 changes: 43 additions & 12 deletions internal/plugin/convert/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@ import (
// ConfigSchemaToProto takes a *configschema.Block and converts it to a
// proto.Schema_Block for a grpc response.
func ConfigSchemaToProto(b *configschema.Block) *proto.Schema_Block {
block := &proto.Schema_Block{}
block := &proto.Schema_Block{
Description: b.Description,
DescriptionKind: protoStringKind(b.DescriptionKind),
Deprecated: b.Deprecated,
}

for _, name := range sortedKeys(b.Attributes) {
a := b.Attributes[name]

attr := &proto.Schema_Attribute{
Name: name,
Description: a.Description,
Optional: a.Optional,
Computed: a.Computed,
Required: a.Required,
Sensitive: a.Sensitive,
Name: name,
Description: a.Description,
DescriptionKind: protoStringKind(a.DescriptionKind),
Optional: a.Optional,
Computed: a.Computed,
Required: a.Required,
Sensitive: a.Sensitive,
Deprecated: a.Deprecated,
}

ty, err := json.Marshal(a.Type)
Expand All @@ -43,6 +50,15 @@ func ConfigSchemaToProto(b *configschema.Block) *proto.Schema_Block {
return block
}

func protoStringKind(k configschema.StringKind) proto.StringKind {
switch k {
default:
return proto.StringKind_PLAIN
case configschema.StringMarkdown:
return proto.StringKind_MARKDOWN
}
}

func protoSchemaNestedBlock(name string, b *configschema.NestedBlock) *proto.Schema_NestedBlock {
var nesting proto.Schema_NestedBlock_NestingMode
switch b.Nesting {
Expand Down Expand Up @@ -74,15 +90,21 @@ func ProtoToConfigSchema(b *proto.Schema_Block) *configschema.Block {
block := &configschema.Block{
Attributes: make(map[string]*configschema.Attribute),
BlockTypes: make(map[string]*configschema.NestedBlock),

Description: b.Description,
DescriptionKind: schemaStringKind(b.DescriptionKind),
Deprecated: b.Deprecated,
}

for _, a := range b.Attributes {
attr := &configschema.Attribute{
Description: a.Description,
Required: a.Required,
Optional: a.Optional,
Computed: a.Computed,
Sensitive: a.Sensitive,
Description: a.Description,
DescriptionKind: schemaStringKind(a.DescriptionKind),
Required: a.Required,
Optional: a.Optional,
Computed: a.Computed,
Sensitive: a.Sensitive,
Deprecated: a.Deprecated,
}

if err := json.Unmarshal(a.Type, &attr.Type); err != nil {
Expand All @@ -99,6 +121,15 @@ func ProtoToConfigSchema(b *proto.Schema_Block) *configschema.Block {
return block
}

func schemaStringKind(k proto.StringKind) configschema.StringKind {
switch k {
default:
return configschema.StringPlain
case proto.StringKind_MARKDOWN:
return configschema.StringMarkdown
}
}

func schemaNestedBlock(b *proto.Schema_NestedBlock) *configschema.NestedBlock {
var nesting configschema.NestingMode
switch b.Nesting {
Expand Down

0 comments on commit f25eeed

Please sign in to comment.