Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions internal/locality/regional/schemas_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package regional

import (
"github.com/hashicorp/terraform-plugin-framework/action/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

// AllRegions returns all valid Scaleway regions as strings
Expand All @@ -16,9 +18,17 @@ func AllRegions() []string {
}

// SchemaAttribute returns a Plugin Framework schema attribute for a region field
func SchemaAttribute() schema.StringAttribute {
func SchemaAttribute(description ...string) schema.StringAttribute {
desc := "The region you want to attach the resource to"
if len(description) > 0 {
desc = description[0]
}

return schema.StringAttribute{
Optional: true,
Description: "The region you want to attach the resource to",
Description: desc,
Validators: []validator.String{
verify.IsStringOneOfWithWarning(AllRegions()),
},
}
}
18 changes: 18 additions & 0 deletions internal/locality/zonal/schemas_framework.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package zonal

import (
"github.com/hashicorp/terraform-plugin-framework/action/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

// SchemaAttribute returns a Plugin Framework schema attribute for a zone field
func SchemaAttribute(description string) schema.StringAttribute {
return schema.StringAttribute{
Optional: true,
Description: description,
Validators: []validator.String{
verify.IsStringOneOfWithWarning(AllZones()),
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (

"github.com/hashicorp/terraform-plugin-framework/action"
"github.com/hashicorp/terraform-plugin-framework/action/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

var (
Expand Down Expand Up @@ -62,8 +64,11 @@ func (a *TriggerTestAlertAction) Schema(ctx context.Context, req action.SchemaRe
"project_id": schema.StringAttribute{
Required: true,
Description: "ID of the Project",
Validators: []validator.String{
verify.IsStringUUID(),
},
},
"region": regional.SchemaAttribute(),
"region": regional.SchemaAttribute("The region you want to attach the resource to"),
},
}
}
Expand Down
10 changes: 6 additions & 4 deletions internal/services/instance/action_server_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/zonal"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

var (
Expand Down Expand Up @@ -78,11 +80,11 @@ func (a *ServerAction) Schema(ctx context.Context, req action.SchemaRequest, res
"server_id": schema.StringAttribute{
Required: true,
Description: "Server id to send the action to",
Validators: []validator.String{
verify.IsStringUUID(),
},
},
"zone": schema.StringAttribute{
Optional: true,
Description: "Zone of server to send the action to",
},
"zone": zonal.SchemaAttribute("Zone of server to send the action to"),
"wait": schema.BoolAttribute{
Optional: true,
Description: "Wait for server to finish action",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/action"
"github.com/hashicorp/terraform-plugin-framework/action/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
Expand All @@ -14,6 +13,7 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

var (
Expand Down Expand Up @@ -69,13 +69,10 @@ func (a *StartJobDefinitionAction) Schema(ctx context.Context, req action.Schema
Required: true,
Description: "ID of the job definition to start. Can be a plain UUID or a regional ID.",
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
verify.IsStringUUIDOrUUIDWithLocality(),
},
},
"region": schema.StringAttribute{
Optional: true,
Description: "Region of the job definition. If not set, the region is derived from the job_definition_id when possible or from the provider configuration.",
},
"region": regional.SchemaAttribute("Region of the job definition. If not set, the region is derived from the job_definition_id when possible or from the provider configuration."),
"command": schema.StringAttribute{
Optional: true,
Description: "Contextual startup command for this specific job run.",
Expand Down
10 changes: 6 additions & 4 deletions internal/services/keymanager/action_rotate_key_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (

"github.com/hashicorp/terraform-plugin-framework/action"
"github.com/hashicorp/terraform-plugin-framework/action/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
key_manager "github.com/scaleway/scaleway-sdk-go/api/key_manager/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)

var (
Expand Down Expand Up @@ -60,13 +62,13 @@ func NewRotateKeyAction() action.Action {
func (a *RotateKeyAction) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"region": schema.StringAttribute{
Optional: true,
Description: "Region of the key. If not set, the region is derived from the key_id when possible or from the provider configuration.",
},
"region": regional.SchemaAttribute("Region of the key. If not set, the region is derived from the key_id when possible or from the provider configuration."),
"key_id": schema.StringAttribute{
Required: true,
Description: "ID of the key to rotate (UUID format)",
Validators: []validator.String{
verify.IsStringUUIDOrUUIDWithLocality(),
},
},
},
}
Expand Down
73 changes: 73 additions & 0 deletions internal/verify/framework.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package verify

import (
"context"
"regexp"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

// Validators for schema.StringAttribute{}

func IsStringUUID() validator.String {
return stringvalidator.RegexMatches(
regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`),
"must be a valid UUID",
)
}

func IsStringUUIDOrUUIDWithLocality() validator.String {
return stringvalidator.RegexMatches(
regexp.MustCompile(`^([a-zA-Z]{2}-[a-zA-Z]{3}/)?[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`),
"must be a valid UUID or UUID with locality prefix (format: aa-aaa-<uuid>)",
)
}

// IsStringOneOfWithWarning only raises a warning if the string is not oneOf validValues
func IsStringOneOfWithWarning(validValues []string) validator.String {
return ErrorToWarningValidator(
stringvalidator.OneOf(validValues...),
)
}

// Converts errors from a validator into warnings
func ErrorToWarningValidator(validator validator.String) validator.String {
return errorToWarningValidator{validator: validator}
}

type errorToWarningValidator struct {
validator validator.String
}

func (v errorToWarningValidator) Description(ctx context.Context) string {
return v.validator.Description(ctx)
}

func (v errorToWarningValidator) MarkdownDescription(ctx context.Context) string {
return v.validator.MarkdownDescription(ctx)
}

func (v errorToWarningValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
// Create a new response to capture the original diagnostics
validationResp := &validator.StringResponse{}

// Run the original validator
v.validator.ValidateString(ctx, req, validationResp)

// Convert any errors to warnings
for _, d := range validationResp.Diagnostics {
if d.Severity() == diag.SeverityError {
// Convert error to warning using the diag.NewWarningDiagnostic function
warningDiag := diag.NewWarningDiagnostic(
d.Summary(),
d.Detail(),
)
resp.Diagnostics = append(resp.Diagnostics, warningDiag)
} else {
// Keep existing warnings or info
resp.Diagnostics = append(resp.Diagnostics, d)
}
}
}
Loading
Loading