Skip to content

Commit ebe4720

Browse files
committed
improve documentation around diagnostics and context aware methods
1 parent 9a3f2bf commit ebe4720

File tree

5 files changed

+45
-18
lines changed

5 files changed

+45
-18
lines changed

diag/diagnostic.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,33 @@ type Diagnostic struct {
4545
// Severity indicates the level of the Diagnostic. Currently can be set to
4646
// either Error or Warning
4747
Severity Severity
48+
4849
// Summary is a short description of the problem, rendered above location
4950
// information
5051
Summary string
52+
5153
// Detail is an optional second message rendered below location information
5254
// typically used to communicate a potential fix to the user.
5355
Detail string
56+
5457
// AttributePath is a representation of the path starting from the root of
5558
// block (resource, datasource, provider) under evaluation by the SDK, to
56-
// the attribute that the Diagnostic should be associated to.
59+
// the attribute that the Diagnostic should be associated to. Terraform will
60+
// use this information to render information on where the problem took
61+
// place in the user's configuration.
5762
//
58-
// It is represented with cty.Path, the SDK currently only supports path
59-
// steps of either cty.GetAttrStep (an actual attribute) or cty.IndexStep
60-
// (a step with Key of cty.StringVal for map indexes, and cty.NumberVal
61-
// list indexes, pathing into sets is not currently supported). Please see
62-
// go-cty documentation for more information.
63+
// It is represented with cty.Path, which is a list of steps of either
64+
// cty.GetAttrStep (an actual attribute) or cty.IndexStep (a step with Key
65+
// of cty.StringVal for map indexes, and cty.NumberVal for list indexes).
6366
//
64-
// Terraform will use this information to render information on where the
65-
// problem took place in the user's configuration.
67+
// PLEASE NOTE: While cty can support indexing into sets, the SDK and
68+
// protocol currently do not. For any Diagnostic related to a schema.TypeSet
69+
// or a child of that type, please terminate the path at the schema.TypeSet
70+
// and opt for more verbose Summary and Detail to help guide the user.
71+
//
72+
// Validity of the AttributePath is currently the responsibility of the
73+
// developer, Terraform should render the root block (provider, resource,
74+
// datasource) in cases where the attribute path is invalid.
6675
AttributePath cty.Path
6776
}
6877

@@ -93,10 +102,6 @@ func FromErr(err error) Diagnostic {
93102
}
94103

95104
// Severity is an enum type marking the severity level of a Diagnostic
96-
//
97-
// Valid levels are
98-
// - Error
99-
// - Warning
100105
type Severity int
101106

102107
const (

helper/schema/provider.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ type Provider struct {
5959
ConfigureFunc ConfigureFunc
6060

6161
// ConfigureContextFunc is a function for configuring the provider. If the
62-
// provider doesn't need to be configured, this can be omitted.
62+
// provider doesn't need to be configured, this can be omitted. This function
63+
// receives a context.Context that will cancel when Terraform sends a
64+
// cancellation signal. This function can yield Diagnostics
6365
ConfigureContextFunc ConfigureContextFunc
6466

6567
meta interface{}
@@ -232,8 +234,6 @@ func (p *Provider) ValidateResource(
232234
// given. This is useful for setting things like access keys.
233235
//
234236
// This won't be called at all if no provider configuration is given.
235-
//
236-
// Configure returns Diagnostics.
237237
func (p *Provider) Configure(ctx context.Context, c *terraform.ResourceConfig) diag.Diagnostics {
238238

239239
var diags diag.Diagnostics

helper/schema/resource.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,15 @@ type Resource struct {
129129
// a ConfigureFunc, this will be nil. This parameter should be used
130130
// to store API clients, configuration structures, etc.
131131
//
132-
// If any errors occur during each of the operation, an error should be
133-
// returned.
132+
// These functions are passed a context configured to timeout with whatever
133+
// was set as the timeout for this operation. Useful for forwarding on to
134+
// backend SDK's that accept context. The context will also cancel if
135+
// Terraform sends a cancellation signal.
136+
//
137+
// These functions return diagnostics, allowing developers to build
138+
// a list of warnings and errors to be presented to the Terraform user.
139+
// The AttributePath of those diagnostics should be built within these
140+
// functions, please consult go-cty documentation for building a cty.Path
134141
CreateContext CreateContextFunc
135142
ReadContext ReadContextFunc
136143
UpdateContext UpdateContextFunc

helper/schema/resource_importer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ type ResourceImporter struct {
2424

2525
// StateContext is called to convert an ID to one or more InstanceState to
2626
// insert into the Terraform state. If this isn't specified, then
27-
// the ID is passed straight through.
27+
// the ID is passed straight through. This function receives a context
28+
// that will cancel if Terraform sends a cancellation signal.
2829
StateContext StateContextFunc
2930
}
3031

helper/schema/schema.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ type Schema struct {
234234
//
235235
// ValidateDiagFunc is honored only when the schema's Type is set to TypeInt,
236236
// TypeFloat, TypeString, TypeBool, or TypeMap. It is ignored for all other types.
237+
//
238+
// ValidateDiagFunc is also yielded the cty.Path the SDK has built up to this
239+
// attribute. The SDK will automatically set the AttributePath of any returned
240+
// Diagnostics to this path. Therefore the developer does not need to set
241+
// the AttributePath for primitive types.
242+
//
243+
// In the case of TypeMap to provide the most precise information, please
244+
// set an AttributePath with the additional cty.IndexStep:
245+
//
246+
// AttributePath: cty.IndexStringPath("key_name")
247+
//
248+
// Or alternatively use the passed in path to create the absolute path:
249+
//
250+
// AttributePath: append(path, cty.IndexStep{Key: cty.StringVal("key_name")})
237251
ValidateDiagFunc SchemaValidateDiagFunc
238252

239253
// Sensitive ensures that the attribute's value does not get displayed in

0 commit comments

Comments
 (0)