terraform-provider-mozout/internal/provider/helpers.go
2026-11-28 15:28:08 +02:00

124 lines
4.8 KiB
Go

package provider
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/mozout-com/terraform-provider-mozout/internal/brand"
"github.com/mozout-com/terraform-provider-mozout/internal/client"
)
// clientFrom turns the provider data into the API client.
func clientFrom(data interface{}, diags *diag.Diagnostics) *client.Client {
if data == nil {
return nil
}
c, ok := data.(*client.Client)
if !ok {
diags.AddError("Unexpected provider data", fmt.Sprintf("Expected *client.Client, got %T.", data))
return nil
}
return c
}
// apiError adds an API error as a diagnostic.
func apiError(diags *diag.Diagnostics, summary string, err error) {
diags.AddError(summary, err.Error())
}
func strOrNull(p *string) types.String {
if p == nil || *p == "" {
return types.StringNull()
}
return types.StringValue(*p)
}
func listOfStrings(items []string) types.List {
vals := make([]types.String, 0, len(items))
for _, s := range items {
vals = append(vals, types.StringValue(s))
}
l, _ := types.ListValueFrom(context.Background(), types.StringType, vals)
return l
}
var removedFromStateWarning = "The API cannot cancel services, so this only removed it from the Terraform state. " +
"It is still in your account and keeps renewing. To cancel it, use " + brand.PanelName + " (" + brand.PanelURL + ")."
// ---------------------------------------------------------------- validators
type oneOfString struct{ values []string }
func stringOneOf(values ...string) validator.String { return oneOfString{values: values} }
func (v oneOfString) Description(_ context.Context) string {
return "value must be one of: " + strings.Join(v.values, ", ")
}
func (v oneOfString) MarkdownDescription(ctx context.Context) string { return v.Description(ctx) }
func (v oneOfString) ValidateString(_ context.Context, req validator.StringRequest, resp *validator.StringResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}
got := req.ConfigValue.ValueString()
for _, s := range v.values {
if s == got {
return
}
}
resp.Diagnostics.AddAttributeError(req.Path, "Invalid value", fmt.Sprintf("%q is not valid: %s.", got, v.Description(context.Background())))
}
// ---------------------------------------------------------------- plan modifiers
// The purchase settings cannot change after the purchase: changing them would buy another service
// while the first one keeps running. These modifiers stop the plan with a clear message instead.
const purchaseLockedMsg = "This setting is only used when buying. Changing it would buy another service while the first one keeps " +
"running, so the provider refuses. Change the plan in " + brand.PanelName + ", or put the old value back."
type lockedInt64 struct{}
func (lockedInt64) Description(_ context.Context) string { return "cannot change after the purchase" }
func (m lockedInt64) MarkdownDescription(ctx context.Context) string { return m.Description(ctx) }
func (lockedInt64) PlanModifyInt64(_ context.Context, req planmodifier.Int64Request, resp *planmodifier.Int64Response) {
if req.State.Raw.IsNull() || req.Plan.Raw.IsNull() || req.PlanValue.IsUnknown() || req.StateValue.IsNull() {
return
}
if !req.PlanValue.Equal(req.StateValue) {
resp.Diagnostics.AddAttributeError(req.Path, "Cannot change after the purchase", purchaseLockedMsg)
}
}
type lockedString struct{}
func (lockedString) Description(_ context.Context) string { return "cannot change after the purchase" }
func (m lockedString) MarkdownDescription(ctx context.Context) string { return m.Description(ctx) }
func (lockedString) PlanModifyString(_ context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
if req.State.Raw.IsNull() || req.Plan.Raw.IsNull() || req.PlanValue.IsUnknown() || req.StateValue.IsNull() {
return
}
if !req.PlanValue.Equal(req.StateValue) {
resp.Diagnostics.AddAttributeError(req.Path, "Cannot change after the purchase", purchaseLockedMsg)
}
}
type lockedMap struct{}
func (lockedMap) Description(_ context.Context) string { return "cannot change after the purchase" }
func (m lockedMap) MarkdownDescription(ctx context.Context) string { return m.Description(ctx) }
func (lockedMap) PlanModifyMap(_ context.Context, req planmodifier.MapRequest, resp *planmodifier.MapResponse) {
if req.State.Raw.IsNull() || req.Plan.Raw.IsNull() || req.PlanValue.IsUnknown() || req.StateValue.IsNull() {
return
}
if !req.PlanValue.Equal(req.StateValue) {
resp.Diagnostics.AddAttributeError(req.Path, "Cannot change after the purchase", purchaseLockedMsg)
}
}