Skip to content

Commit 348b6b0

Browse files
committed
Map new proto fields
Borrows some core code from 23acb02cb846fd002e7a0b52904ee88019b33211
1 parent 917ef1b commit 348b6b0

File tree

6 files changed

+148
-25
lines changed

6 files changed

+148
-25
lines changed

helper/schema/core_schema.go

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ import (
77
"github.com/zclconf/go-cty/cty"
88
)
99

10+
var (
11+
// DescriptionKind is the default StringKind of descriptions in this provider.
12+
// It defaults to StringPlain but can be globally switched to StringMarkdown.
13+
DescriptionKind = configschema.StringPlain
14+
15+
// SchemaDescriptionBuilder converts helper/schema.Schema Descriptions to configschema.Attribute
16+
// and Block Descriptions. This method can be used to modify the description text prior to it
17+
// being returned in the schema.
18+
SchemaDescriptionBuilder = func(s *Schema) string {
19+
return s.Description
20+
}
21+
22+
// ResourceDescriptionBuilder converts helper/schema.Resource Descriptions to configschema.Block
23+
// Descriptions at the resource top level. This method can be used to modify the description prior
24+
// to it being returned in the schema.
25+
ResourceDescriptionBuilder = func(r *Resource) string {
26+
return r.Description
27+
}
28+
)
29+
1030
// The functions and methods in this file are concerned with the conversion
1131
// of this package's schema model into the slightly-lower-level schema model
1232
// used by Terraform core for configuration parsing.
@@ -115,13 +135,22 @@ func (s *Schema) coreConfigSchemaAttribute() *configschema.Attribute {
115135
}
116136
}
117137

138+
desc := SchemaDescriptionBuilder(s)
139+
descKind := DescriptionKind
140+
if desc == "" {
141+
// fallback to plain text if empty
142+
descKind = configschema.StringPlain
143+
}
144+
118145
return &configschema.Attribute{
119-
Type: s.coreConfigSchemaType(),
120-
Optional: opt,
121-
Required: reqd,
122-
Computed: s.Computed,
123-
Sensitive: s.Sensitive,
124-
Description: s.Description,
146+
Type: s.coreConfigSchemaType(),
147+
Optional: opt,
148+
Required: reqd,
149+
Computed: s.Computed,
150+
Sensitive: s.Sensitive,
151+
Description: desc,
152+
DescriptionKind: descKind,
153+
Deprecated: s.Deprecated != "",
125154
}
126155
}
127156

@@ -132,6 +161,17 @@ func (s *Schema) coreConfigSchemaBlock() *configschema.NestedBlock {
132161
ret := &configschema.NestedBlock{}
133162
if nested := s.Elem.(*Resource).coreConfigSchema(); nested != nil {
134163
ret.Block = *nested
164+
165+
desc := SchemaDescriptionBuilder(s)
166+
descKind := DescriptionKind
167+
if desc == "" {
168+
// fallback to plain text if empty
169+
descKind = configschema.StringPlain
170+
}
171+
// set these on the block from the attribute Schema
172+
ret.Block.Description = desc
173+
ret.Block.DescriptionKind = descKind
174+
ret.Block.Deprecated = s.Deprecated != ""
135175
}
136176
switch s.Type {
137177
case TypeList:
@@ -231,6 +271,18 @@ func (s *Schema) coreConfigSchemaType() cty.Type {
231271
func (r *Resource) CoreConfigSchema() *configschema.Block {
232272
block := r.coreConfigSchema()
233273

274+
desc := ResourceDescriptionBuilder(r)
275+
descKind := DescriptionKind
276+
if desc == "" {
277+
// fallback to plain text if empty
278+
descKind = configschema.StringPlain
279+
}
280+
281+
// Only apply Resource Description, Kind, Deprecation at top level
282+
block.Description = desc
283+
block.DescriptionKind = descKind
284+
block.Deprecated = r.DeprecationMessage != ""
285+
234286
if block.Attributes == nil {
235287
block.Attributes = map[string]*configschema.Attribute{}
236288
}

helper/schema/core_schema_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ func testResource(block *configschema.Block) *configschema.Block {
3131
}
3232

3333
func TestSchemaMapCoreConfigSchema(t *testing.T) {
34+
// these are global so if new tests are written we should probably employ a mutex
35+
DescriptionKind = configschema.StringMarkdown
36+
SchemaDescriptionBuilder = func(s *Schema) string {
37+
if s.Required && s.Description != "" {
38+
return fmt.Sprintf("**Required** %s", s.Description)
39+
}
40+
return s.Description
41+
}
42+
3443
tests := map[string]struct {
3544
Schema map[string]*Schema
3645
Want *configschema.Block
@@ -63,9 +72,10 @@ func TestSchemaMapCoreConfigSchema(t *testing.T) {
6372
testResource(&configschema.Block{
6473
Attributes: map[string]*configschema.Attribute{
6574
"int": {
66-
Type: cty.Number,
67-
Required: true,
68-
Description: "foo bar baz",
75+
Type: cty.Number,
76+
Required: true,
77+
Description: "**Required** foo bar baz",
78+
DescriptionKind: configschema.StringMarkdown,
6979
},
7080
"float": {
7181
Type: cty.Number,

helper/schema/resource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ type Resource struct {
172172
// actions (Create, Read, Update, Delete, Default) to the Resource struct, and
173173
// accessing them in the matching methods.
174174
Timeouts *ResourceTimeout
175+
176+
// Description is used as the description for docs, the language server and
177+
// other user facing usage. It can be plain-text or markdown depending on the
178+
// global DescriptionKind setting.
179+
Description string
175180
}
176181

177182
// ShimInstanceStateFromValue converts a cty.Value to a

helper/schema/schema.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ type Schema struct {
138138
Default interface{}
139139
DefaultFunc SchemaDefaultFunc
140140

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

146146
// InputDefault is the default value to use for when inputs are requested.

internal/configs/configschema/schema.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import (
44
"github.com/zclconf/go-cty/cty"
55
)
66

7+
// StringKind represents the format a string is in.
8+
type StringKind int
9+
10+
const (
11+
// StringPlain indicates a string is plain-text and requires no processing for display.
12+
StringPlain StringKind = iota
13+
// StringMarkdown indicates a string is in markdown format and may
14+
// require additional processing to display.
15+
StringMarkdown
16+
)
17+
718
// Block represents a configuration block.
819
//
920
// "Block" here is a logical grouping construct, though it happens to map
@@ -21,6 +32,15 @@ type Block struct {
2132
// BlockTypes describes any nested block types that may appear directly
2233
// inside the block.
2334
BlockTypes map[string]*NestedBlock
35+
36+
// Description and DescriptionKind contain a user facing description of the block
37+
// and the format of that string.
38+
Description string
39+
DescriptionKind StringKind
40+
41+
// Deprecated indicates whether the block has been marked as deprecated in the
42+
// provider and usage should be discouraged.
43+
Deprecated bool
2444
}
2545

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

3758
// Required, if set to true, specifies that an omitted or null value is
3859
// not permitted.
@@ -55,6 +76,10 @@ type Attribute struct {
5576
// future to help Terraform mask sensitive information. (Terraform
5677
// currently achieves this in a limited sense via other mechanisms.)
5778
Sensitive bool
79+
80+
// Deprecated indicates whether the attribute has been marked as deprecated in the
81+
// provider and usage should be discouraged.
82+
Deprecated bool
5883
}
5984

6085
// NestedBlock represents the embedding of one block within another.

internal/plugin/convert/schema.go

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,24 @@ import (
1313
// ConfigSchemaToProto takes a *configschema.Block and converts it to a
1414
// proto.Schema_Block for a grpc response.
1515
func ConfigSchemaToProto(b *configschema.Block) *proto.Schema_Block {
16-
block := &proto.Schema_Block{}
16+
block := &proto.Schema_Block{
17+
Description: b.Description,
18+
DescriptionKind: protoStringKind(b.DescriptionKind),
19+
Deprecated: b.Deprecated,
20+
}
1721

1822
for _, name := range sortedKeys(b.Attributes) {
1923
a := b.Attributes[name]
24+
2025
attr := &proto.Schema_Attribute{
21-
Name: name,
22-
Description: a.Description,
23-
Optional: a.Optional,
24-
Computed: a.Computed,
25-
Required: a.Required,
26-
Sensitive: a.Sensitive,
26+
Name: name,
27+
Description: a.Description,
28+
DescriptionKind: protoStringKind(a.DescriptionKind),
29+
Optional: a.Optional,
30+
Computed: a.Computed,
31+
Required: a.Required,
32+
Sensitive: a.Sensitive,
33+
Deprecated: a.Deprecated,
2734
}
2835

2936
ty, err := json.Marshal(a.Type)
@@ -44,6 +51,15 @@ func ConfigSchemaToProto(b *configschema.Block) *proto.Schema_Block {
4451
return block
4552
}
4653

54+
func protoStringKind(k configschema.StringKind) proto.StringKind {
55+
switch k {
56+
default:
57+
return proto.StringKind_PLAIN
58+
case configschema.StringMarkdown:
59+
return proto.StringKind_MARKDOWN
60+
}
61+
}
62+
4763
func protoSchemaNestedBlock(name string, b *configschema.NestedBlock) *proto.Schema_NestedBlock {
4864
var nesting proto.Schema_NestedBlock_NestingMode
4965
switch b.Nesting {
@@ -83,15 +99,21 @@ func ProtoToConfigSchema(b *proto.Schema_Block) *configschema.Block {
8399
block := &configschema.Block{
84100
Attributes: make(map[string]*configschema.Attribute),
85101
BlockTypes: make(map[string]*configschema.NestedBlock),
102+
103+
Description: b.Description,
104+
DescriptionKind: schemaStringKind(b.DescriptionKind),
105+
Deprecated: b.Deprecated,
86106
}
87107

88108
for _, a := range b.Attributes {
89109
attr := &configschema.Attribute{
90-
Description: a.Description,
91-
Required: a.Required,
92-
Optional: a.Optional,
93-
Computed: a.Computed,
94-
Sensitive: a.Sensitive,
110+
Description: a.Description,
111+
DescriptionKind: schemaStringKind(a.DescriptionKind),
112+
Required: a.Required,
113+
Optional: a.Optional,
114+
Computed: a.Computed,
115+
Sensitive: a.Sensitive,
116+
Deprecated: a.Deprecated,
95117
}
96118

97119
if err := json.Unmarshal(a.Type, &attr.Type); err != nil {
@@ -108,6 +130,15 @@ func ProtoToConfigSchema(b *proto.Schema_Block) *configschema.Block {
108130
return block
109131
}
110132

133+
func schemaStringKind(k proto.StringKind) configschema.StringKind {
134+
switch k {
135+
default:
136+
return configschema.StringPlain
137+
case proto.StringKind_MARKDOWN:
138+
return configschema.StringMarkdown
139+
}
140+
}
141+
111142
func schemaNestedBlock(b *proto.Schema_NestedBlock) *configschema.NestedBlock {
112143
var nesting configschema.NestingMode
113144
switch b.Nesting {

0 commit comments

Comments
 (0)