Skip to content

Commit 7c2d22e

Browse files
committed
feat: add regional schema helper for Plugin Framework actions
1 parent aad59f1 commit 7c2d22e

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package regional
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
5+
"github.com/hashicorp/terraform-plugin-framework/action/schema"
6+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
7+
"github.com/scaleway/scaleway-sdk-go/scw"
8+
)
9+
10+
// AllRegions returns all valid Scaleway regions as strings
11+
func AllRegions() []string {
12+
regions := make([]string, 0, len(scw.AllRegions))
13+
for _, r := range scw.AllRegions {
14+
regions = append(regions, r.String())
15+
}
16+
17+
return regions
18+
}
19+
20+
// SchemaAttribute returns a Plugin Framework schema attribute for a region field
21+
func SchemaAttribute() schema.StringAttribute {
22+
return schema.StringAttribute{
23+
Optional: true,
24+
Description: "The region you want to attach the resource to",
25+
Validators: []validator.String{
26+
stringvalidator.OneOf(AllRegions()...),
27+
},
28+
}
29+
}

internal/services/cockpit/action_trigger_test_alert_action.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-framework/types"
1010
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
1111
"github.com/scaleway/scaleway-sdk-go/scw"
12+
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1213
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
1314
)
1415

@@ -62,10 +63,7 @@ func (a *TriggerTestAlertAction) Schema(ctx context.Context, req action.SchemaRe
6263
Required: true,
6364
Description: "ID of the Project",
6465
},
65-
"region": schema.StringAttribute{
66-
Optional: true,
67-
Description: "Region to target. If not provided, will use the default region from the provider configuration",
68-
},
66+
"region": regional.SchemaAttribute(),
6967
},
7068
}
7169
}
@@ -99,8 +97,19 @@ func (a *TriggerTestAlertAction) Invoke(ctx context.Context, req action.InvokeRe
9997
}
10098

10199
var region scw.Region
100+
102101
if !data.Region.IsNull() && data.Region.ValueString() != "" {
103-
region = scw.Region(data.Region.ValueString())
102+
parsedRegion, err := scw.ParseRegion(data.Region.ValueString())
103+
if err != nil {
104+
resp.Diagnostics.AddError(
105+
"Invalid region",
106+
fmt.Sprintf("The region attribute must be a valid Scaleway region. Got %q: %s", data.Region.ValueString(), err),
107+
)
108+
109+
return
110+
}
111+
112+
region = parsedRegion
104113
} else {
105114
// Use default region from provider configuration
106115
defaultRegion, exists := a.meta.ScwClient().GetDefaultRegion()

0 commit comments

Comments
 (0)