Skip to content

Commit f5383fb

Browse files
authored
feat: only return warnings for non public locality (#816)
1 parent e63d6ac commit f5383fb

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ require (
55
github.com/dnaeon/go-vcr v1.1.0
66
github.com/dustin/go-humanize v1.0.0
77
github.com/google/go-cmp v0.5.5
8+
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
89
github.com/hashicorp/go-retryablehttp v0.6.8
910
github.com/hashicorp/terraform-plugin-sdk/v2 v2.5.0
1011
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210414083420-b36f019ca892

scaleway/helpers.go

+30-18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/hashicorp/go-cty/cty"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1214
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1315
"github.com/scaleway/scaleway-sdk-go/namegenerator"
@@ -256,15 +258,12 @@ func zoneSchema() *schema.Schema {
256258
allZones = append(allZones, z.String())
257259
}
258260
return &schema.Schema{
259-
Type: schema.TypeString,
260-
Description: "The zone you want to attach the resource to",
261-
Optional: true,
262-
ForceNew: true,
263-
Computed: true,
264-
ValidateFunc: validation.StringInSlice(
265-
allZones,
266-
true,
267-
),
261+
Type: schema.TypeString,
262+
Description: "The zone you want to attach the resource to",
263+
Optional: true,
264+
ForceNew: true,
265+
Computed: true,
266+
ValidateDiagFunc: validateStringInSliceWithWarning(allZones, "zone"),
268267
}
269268
}
270269

@@ -275,15 +274,28 @@ func regionSchema() *schema.Schema {
275274
allRegions = append(allRegions, z.String())
276275
}
277276
return &schema.Schema{
278-
Type: schema.TypeString,
279-
Description: "The region you want to attach the resource to",
280-
Optional: true,
281-
ForceNew: true,
282-
Computed: true,
283-
ValidateFunc: validation.StringInSlice(
284-
allRegions,
285-
true,
286-
),
277+
Type: schema.TypeString,
278+
Description: "The region you want to attach the resource to",
279+
Optional: true,
280+
ForceNew: true,
281+
Computed: true,
282+
ValidateDiagFunc: validateStringInSliceWithWarning(allRegions, "region"),
283+
}
284+
}
285+
286+
// validateStringInSliceWithWarning helps to only returns warnings in case we got a non public locality passed
287+
func validateStringInSliceWithWarning(correctValues []string, field string) func(i interface{}, path cty.Path) diag.Diagnostics {
288+
return func(i interface{}, path cty.Path) diag.Diagnostics {
289+
_, rawErr := validation.StringInSlice(correctValues, true)(i, field)
290+
var res diag.Diagnostics
291+
for _, e := range rawErr {
292+
res = append(res, diag.Diagnostic{
293+
Severity: diag.Warning,
294+
Summary: e.Error(),
295+
AttributePath: path,
296+
})
297+
}
298+
return res
287299
}
288300
}
289301

0 commit comments

Comments
 (0)