chore: changed and refactored providers (#36)

## Description

<!-- **Please link some issue here describing what you are trying to achieve.**

In case there is no issue present for your PR, please consider creating one.
At least please give us some description what you are trying to achieve and why your change is needed. -->

relates to #1234

## Checklist

- [ ] Issue was linked above
- [ ] Code format was applied: `make fmt`
- [ ] Examples were added / adjusted (see `examples/` directory)
- [x] Docs are up-to-date: `make generate-docs` (will be checked by CI)
- [ ] Unit tests got implemented or updated
- [ ] Acceptance tests got implemented or updated (see e.g. [here](f5f99d1709/stackit/internal/services/dns/dns_acc_test.go))
- [x] Unit tests are passing: `make test` (will be checked by CI)
- [x] No linter issues: `make lint` (will be checked by CI)

Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Reviewed-on: #36
Reviewed-by: Marcel_Henselin <marcel.henselin@stackit.cloud>
Co-authored-by: Andre Harms <andre.harms@stackit.cloud>
Co-committed-by: Andre Harms <andre.harms@stackit.cloud>
This commit is contained in:
Andre_Harms 2026-02-10 08:10:02 +00:00 committed by Marcel_Henselin
parent b1b359f436
commit de019908d2
Signed by: tf-provider.git.onstackit.cloud
GPG key ID: 6D7E8A1ED8955A9C
70 changed files with 6250 additions and 2608 deletions

View file

@ -1,229 +0,0 @@
package utils
import (
"fmt"
"log/slog"
"reflect"
"slices"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/iancoleman/strcase"
"gopkg.in/yaml.v3"
)
type Field struct {
Name string `yaml:"name"`
Modifiers []*string `yaml:"modifiers"`
}
type Fields struct {
Fields []*Field `yaml:"fields"`
}
var validModifiers = []string{
"UseStateForUnknown",
"RequiresReplace",
}
func ReadModifiersConfig(content []byte) (*Fields, error) {
var fields Fields
err := yaml.Unmarshal(content, &fields)
if err != nil {
return nil, err
}
return &fields, nil
}
func AddPlanModifiersToResourceSchema(fields *Fields, s *schema.Schema) error {
err := validateFields(fields)
if err != nil {
return err
}
resAttr, err := handleAttributes("", s.Attributes, fields)
if err != nil {
return err
}
s.Attributes = resAttr
return nil
}
func handleAttributes(prefix string, attributes map[string]schema.Attribute, fields *Fields) (map[string]schema.Attribute, error) {
fieldMap := fieldListToMap(fields)
for attrName, attrValue := range attributes {
attrNameSnake := strcase.ToSnake(attrName)
if prefix != "" {
attrNameSnake = prefix + "." + attrNameSnake
}
switch reflect.TypeOf(attrValue).String() {
case "schema.BoolAttribute":
if field, ok := fieldMap[attrNameSnake]; ok {
res, err := handleBoolPlanModifiers(attrValue, field)
if err != nil {
return nil, err
}
attributes[attrName] = res
}
case "schema.Int64Attribute":
if field, ok := fieldMap[attrNameSnake]; ok {
res, err := handleInt64PlanModifiers(attrValue, field)
if err != nil {
return nil, err
}
attributes[attrName] = res
}
case "schema.StringAttribute":
if field, ok := fieldMap[attrNameSnake]; ok {
res, err := handleStringPlanModifiers(attrValue, field)
if err != nil {
return nil, err
}
attributes[attrName] = res
}
case "schema.ListAttribute":
if field, ok := fieldMap[attrNameSnake]; ok {
res, err := handleListPlanModifiers(attrValue, field)
if err != nil {
return nil, err
}
attributes[attrName] = res
}
case "schema.SingleNestedAttribute":
nested, ok := attrValue.(schema.SingleNestedAttribute)
if !ok {
if _, ok := attrValue.(interface {
GetAttributes() map[string]schema.Attribute
}); ok {
return nil, fmt.Errorf("unsupported type for single nested attribute")
}
}
res, err := handleAttributes(attrName, nested.Attributes, fields)
if err != nil {
return nil, err
}
nested.Attributes = res
attributes[attrName] = nested
default:
slog.Warn("type currently not supported", "type", reflect.TypeOf(attrValue).String())
}
}
return attributes, nil
}
func handleBoolPlanModifiers(
attr schema.Attribute,
fields []*string,
) (schema.Attribute, error) {
a, ok := attr.(schema.BoolAttribute)
if !ok {
return nil, fmt.Errorf("field is not a string attribute")
}
for _, v := range fields {
switch *v {
case "RequiresReplace":
a.PlanModifiers = append(a.PlanModifiers, boolplanmodifier.RequiresReplace())
case "UseStateForUnknown":
a.PlanModifiers = append(a.PlanModifiers, boolplanmodifier.UseStateForUnknown())
}
}
return a, nil
}
func handleStringPlanModifiers(
attr schema.Attribute,
fields []*string,
) (schema.Attribute, error) {
a, ok := attr.(schema.StringAttribute)
if !ok {
return nil, fmt.Errorf("field is not a string attribute")
}
for _, v := range fields {
switch *v {
case "RequiresReplace":
a.PlanModifiers = append(a.PlanModifiers, stringplanmodifier.RequiresReplace())
case "UseStateForUnknown":
a.PlanModifiers = append(a.PlanModifiers, stringplanmodifier.UseStateForUnknown())
}
}
return a, nil
}
func handleInt64PlanModifiers(
attr schema.Attribute,
fields []*string,
) (schema.Attribute, error) {
a, ok := attr.(schema.Int64Attribute)
if !ok {
return nil, fmt.Errorf("field is not a string attribute")
}
for _, v := range fields {
switch *v {
case "RequiresReplace":
a.PlanModifiers = append(a.PlanModifiers, int64planmodifier.RequiresReplace())
case "UseStateForUnknown":
a.PlanModifiers = append(a.PlanModifiers, int64planmodifier.UseStateForUnknown())
}
}
return a, nil
}
func handleListPlanModifiers(
attr schema.Attribute,
fields []*string,
) (schema.Attribute, error) {
a, ok := attr.(schema.ListAttribute)
if !ok {
return nil, fmt.Errorf("field is not a string attribute")
}
for _, v := range fields {
switch *v {
case "RequiresReplace":
a.PlanModifiers = append(a.PlanModifiers, listplanmodifier.RequiresReplace())
case "UseStateForUnknown":
a.PlanModifiers = append(a.PlanModifiers, listplanmodifier.UseStateForUnknown())
}
}
return a, nil
}
func validateFields(fields *Fields) error {
if fields == nil {
return nil
}
for _, field := range fields.Fields {
for _, modifier := range field.Modifiers {
if *modifier == "" {
return fmt.Errorf("modifier %s is required", *modifier)
}
if !slices.Contains(validModifiers, *modifier) {
return fmt.Errorf("modifier %s is invalid", *modifier)
}
}
}
return nil
}
func fieldListToMap(fields *Fields) map[string][]*string {
res := make(map[string][]*string)
if fields != nil {
for _, field := range fields.Fields {
res[field.Name] = field.Modifiers
}
} else {
slog.Warn("no fields available")
}
return res
}