Skip to content

Commit b72105a

Browse files
committed
switch API to use diag.Diagnostics
1 parent a4e71f6 commit b72105a

File tree

12 files changed

+219
-258
lines changed

12 files changed

+219
-258
lines changed

diag/diagnostic.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
11
package diag
22

33
import (
4+
"errors"
45
"fmt"
56

7+
"github.com/hashicorp/go-multierror"
68
"github.com/zclconf/go-cty/cty"
79
)
810

911
type Diagnostics []*Diagnostic
1012

13+
func (diags Diagnostics) Append(i interface{}) Diagnostics {
14+
switch v := i.(type) {
15+
case Diagnostics:
16+
if len(v) != 0 {
17+
diags = append(diags, v...)
18+
}
19+
case *Diagnostic:
20+
diags = append(diags, v)
21+
case error:
22+
if v != nil {
23+
diags = append(diags, &Diagnostic{
24+
Severity: Error,
25+
Summary: v.Error(),
26+
})
27+
}
28+
}
29+
return diags
30+
}
31+
32+
func (diags Diagnostics) Err() error {
33+
return errors.New(multierror.ListFormatFunc(diags.Errors()))
34+
}
35+
1136
func (diags Diagnostics) HasErrors() bool {
1237
for _, d := range diags {
1338
if d.Severity == Error {
@@ -42,11 +67,14 @@ type Diagnostic struct {
4267
Summary string
4368
Detail string
4469
AttributePath cty.Path
45-
MapKey string
4670
}
4771

4872
func (d *Diagnostic) Error() string {
49-
return fmt.Sprintf("%s: %s: %s", d.Severity, d.Summary, d.Detail)
73+
s := fmt.Sprintf("%s: %s", d.Severity, d.Summary)
74+
if d.Detail != "" {
75+
s = fmt.Sprintf("%s: %s", s, d.Detail)
76+
}
77+
return s
5078
}
5179

5280
type Severity int

helper/schema/provider.go

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,15 @@ func (p *Provider) GetSchema(req *terraform.ProviderSchemaRequest) (*terraform.P
180180
}
181181

182182
// Validate is called once at the beginning with the raw configuration
183-
// (no interpolation done) and can return a list of warnings and/or
184-
// errors.
183+
// (no interpolation done) and can return diagnostics
185184
//
186185
// This is called once with the provider configuration only. It may not
187186
// be called at all if no provider configuration is given.
188187
//
189188
// This should not assume that any values of the configurations are valid.
190189
// The primary use case of this call is to check that required keys are
191190
// set.
192-
func (p *Provider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
193-
194-
diags := p.ValidateDiag(c)
195-
196-
return diags.Warnings(), diags.Errors()
197-
}
198-
199-
func (p *Provider) ValidateDiag(c *terraform.ResourceConfig) diag.Diagnostics {
191+
func (p *Provider) Validate(c *terraform.ResourceConfig) diag.Diagnostics {
200192
if err := p.InternalValidate(); err != nil {
201193
return []*diag.Diagnostic{
202194
{
@@ -209,12 +201,11 @@ func (p *Provider) ValidateDiag(c *terraform.ResourceConfig) diag.Diagnostics {
209201
}
210202
}
211203

212-
return schemaMap(p.Schema).ValidateDiag(c)
204+
return schemaMap(p.Schema).Validate(c)
213205
}
214206

215207
// ValidateResource is called once at the beginning with the raw
216-
// configuration (no interpolation done) and can return a list of warnings
217-
// and/or errors.
208+
// configuration (no interpolation done) and can return diagnostics.
218209
//
219210
// This is called once per resource.
220211
//
@@ -223,12 +214,6 @@ func (p *Provider) ValidateDiag(c *terraform.ResourceConfig) diag.Diagnostics {
223214
// The primary use case of this call is to check that the required keys
224215
// are set and that the general structure is correct.
225216
func (p *Provider) ValidateResource(
226-
t string, c *terraform.ResourceConfig) ([]string, []error) {
227-
diags := p.ValidateResourceDiag(t, c)
228-
return diags.Warnings(), diags.Errors()
229-
}
230-
231-
func (p *Provider) ValidateResourceDiag(
232217
t string, c *terraform.ResourceConfig) diag.Diagnostics {
233218
r, ok := p.ResourcesMap[t]
234219
if !ok {
@@ -240,7 +225,7 @@ func (p *Provider) ValidateResourceDiag(
240225
}
241226
}
242227

243-
return r.ValidateDiag(c)
228+
return r.Validate(c)
244229
}
245230

246231
// Configure configures the provider itself with the configuration
@@ -384,8 +369,7 @@ func (p *Provider) ImportState(
384369
}
385370

386371
// ValidateDataSource is called once at the beginning with the raw
387-
// configuration (no interpolation done) and can return a list of warnings
388-
// and/or errors.
372+
// configuration (no interpolation done) and can return diagnostics.
389373
//
390374
// This is called once per data source instance.
391375
//
@@ -394,12 +378,6 @@ func (p *Provider) ImportState(
394378
// The primary use case of this call is to check that the required keys
395379
// are set and that the general structure is correct.
396380
func (p *Provider) ValidateDataSource(
397-
t string, c *terraform.ResourceConfig) ([]string, []error) {
398-
diags := p.ValidateDataSourceDiag(t, c)
399-
return diags.Warnings(), diags.Errors()
400-
}
401-
402-
func (p *Provider) ValidateDataSourceDiag(
403381
t string, c *terraform.ResourceConfig) diag.Diagnostics {
404382
r, ok := p.DataSourcesMap[t]
405383
if !ok {
@@ -411,7 +389,7 @@ func (p *Provider) ValidateDataSourceDiag(
411389
}
412390
}
413391

414-
return r.ValidateDiag(c)
392+
return r.Validate(c)
415393
}
416394

417395
// DataSources returns all of the available data sources that this

helper/schema/provider_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ func TestProviderValidate(t *testing.T) {
282282

283283
for i, tc := range cases {
284284
c := terraform.NewResourceConfigRaw(tc.Config)
285-
_, es := tc.P.Validate(c)
286-
if len(es) > 0 != tc.Err {
287-
t.Fatalf("%d: %#v", i, es)
285+
diags := tc.P.Validate(c)
286+
if diags.HasErrors() != tc.Err {
287+
t.Fatalf("%d: %#v", i, diags.Err())
288288
}
289289
}
290290
}
@@ -395,9 +395,9 @@ func TestProviderValidateResource(t *testing.T) {
395395

396396
for i, tc := range cases {
397397
c := terraform.NewResourceConfigRaw(tc.Config)
398-
_, es := tc.P.ValidateResource(tc.Type, c)
399-
if len(es) > 0 != tc.Err {
400-
t.Fatalf("%d: %#v", i, es)
398+
diags := tc.P.ValidateResource(tc.Type, c)
399+
if diags.HasErrors() != tc.Err {
400+
t.Fatalf("%d: %#v", i, diags.Err())
401401
}
402402
}
403403
}

0 commit comments

Comments
 (0)