terraform-provider-stackitp.../stackit/internal/conversion/conversion.go
Diogo Ferrão a35b887315
Onboard Argus alert configs (#449)
* Onboard Argus alert config receivers (#439)

* initial schema and to payload

* finish receiver resource implementation (schema, topayload and mapfields)

* fix toUpdate payload lists

* fix resource and datasource on alert config attr removal, add testing

* fix linting and testing

* initial test map fields

* improve testing, remove logging

* rename vars in acc testing

* refactor mapAlertConfig

* improve mock alert config, fix testing

* make the mock alert config receivers match the default

* generate docs

* Onboard Argus Alert Config global configuration (#446)

* initial implementation

* initial map fields

* extend datasource, finish resource impl and extend acc testing

* remove unmapped fields

* add all attributes back

* remove commented inhibit rules

* generate docs, fix testing

* address PR comments

* Onboard Alert Config route (#447)

* Initial implementation

* add schema to datasource, improve acc testing

* fix linting, generate docs

* address PR comments

* fix and improve acceptance tests

* update test titles (comments)

* address acceptance comments (#452)

* Onboard Argus Alert Config child routes in Route (#463)

* initial schema and map fields implementation

* initial working solution

* improve implementation, generate docs

* fix description, add more unit testing

* address PR comments

* add unit and acc testing, fix datasource schema, fix plan_id mapping

* add checks to acceptance testing

* update acceptance tests
2024-07-18 15:05:35 +01:00

135 lines
3.8 KiB
Go

package conversion
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
)
func ToString(ctx context.Context, v attr.Value) (string, error) {
if t := v.Type(ctx); t != types.StringType {
return "", fmt.Errorf("type mismatch. expected 'types.StringType' but got '%s'", t.String())
}
if v.IsNull() || v.IsUnknown() {
return "", fmt.Errorf("value is unknown or null")
}
tv, err := v.ToTerraformValue(ctx)
if err != nil {
return "", err
}
var s string
if err := tv.Copy().As(&s); err != nil {
return "", err
}
return s, nil
}
func ToOptStringMap(tfMap map[string]attr.Value) (*map[string]string, error) { //nolint: gocritic //pointer needed to map optional fields
labels := make(map[string]string, len(tfMap))
for l, v := range tfMap {
valueString, ok := v.(types.String)
if !ok {
return nil, fmt.Errorf("error converting map value: expected to string, got %v", v)
}
labels[l] = valueString.ValueString()
}
labelsPointer := &labels
if len(labels) == 0 {
labelsPointer = nil
}
return labelsPointer, nil
}
func ToTerraformStringMap(ctx context.Context, m map[string]string) (basetypes.MapValue, error) {
labels := make(map[string]attr.Value, len(m))
for l, v := range m {
stringValue := types.StringValue(v)
labels[l] = stringValue
}
res, diags := types.MapValueFrom(ctx, types.StringType, m)
if diags.HasError() {
return types.MapNull(types.StringType), fmt.Errorf("converting to MapValue: %v", diags.Errors())
}
return res, nil
}
// ToStringInterfaceMap converts a basetypes.MapValue of Strings to a map[string]interface{}.
func ToStringInterfaceMap(ctx context.Context, m basetypes.MapValue) (map[string]interface{}, error) {
labels := map[string]string{}
diags := m.ElementsAs(ctx, &labels, false)
if diags.HasError() {
return nil, fmt.Errorf("converting from MapValue: %w", core.DiagsToError(diags))
}
interfaceMap := make(map[string]interface{}, len(labels))
for k, v := range labels {
interfaceMap[k] = v
}
return interfaceMap, nil
}
// StringValueToPointer converts basetypes.StringValue to a pointer to string.
// It returns nil if the value is null or unknown.
func StringValueToPointer(s basetypes.StringValue) *string {
if s.IsNull() || s.IsUnknown() {
return nil
}
value := s.ValueString()
return &value
}
// Int64ValueToPointer converts basetypes.Int64Value to a pointer to int64.
// It returns nil if the value is null or unknown.
func Int64ValueToPointer(s basetypes.Int64Value) *int64 {
if s.IsNull() || s.IsUnknown() {
return nil
}
value := s.ValueInt64()
return &value
}
// Float64ValueToPointer converts basetypes.Float64Value to a pointer to float64.
// It returns nil if the value is null or unknown.
func Float64ValueToPointer(s basetypes.Float64Value) *float64 {
if s.IsNull() || s.IsUnknown() {
return nil
}
value := s.ValueFloat64()
return &value
}
// BoolValueToPointer converts basetypes.BoolValue to a pointer to bool.
// It returns nil if the value is null or unknown.
func BoolValueToPointer(s basetypes.BoolValue) *bool {
if s.IsNull() || s.IsUnknown() {
return nil
}
value := s.ValueBool()
return &value
}
// StringListToPointer converts basetypes.ListValue to a pointer to a list of strings.
// It returns nil if the value is null or unknown.
func StringListToPointer(list basetypes.ListValue) (*[]string, error) {
if list.IsNull() || list.IsUnknown() {
return nil, nil
}
listStr := []string{}
for i, el := range list.Elements() {
elStr, ok := el.(types.String)
if !ok {
return nil, fmt.Errorf("element %d is not a string", i)
}
listStr = append(listStr, elStr.ValueString())
}
return &listStr, nil
}