Skip to content
Merged
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
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/hashicorp/terraform-plugin-log v0.10.0
github.com/hashicorp/terraform-plugin-testing v1.16.0
github.com/stackitcloud/stackit-sdk-go/core v0.26.0
github.com/stackitcloud/stackit-sdk-go/services/alb v0.14.2
github.com/stackitcloud/stackit-sdk-go/services/alb v0.15.0
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.16.0
github.com/stackitcloud/stackit-sdk-go/services/certificates v1.7.0
github.com/stackitcloud/stackit-sdk-go/services/dns v0.20.2
Expand All @@ -23,15 +23,15 @@ require (
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.12.0
github.com/stackitcloud/stackit-sdk-go/services/intake v0.9.0
github.com/stackitcloud/stackit-sdk-go/services/kms v1.10.0
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.13.0
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.14.0
github.com/stackitcloud/stackit-sdk-go/services/logme v0.29.0
github.com/stackitcloud/stackit-sdk-go/services/logs v0.9.0
github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.29.0
github.com/stackitcloud/stackit-sdk-go/services/modelserving v0.10.0
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.10.0
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.9.0
github.com/stackitcloud/stackit-sdk-go/services/observability v0.23.0
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.28.0
github.com/stackitcloud/stackit-sdk-go/services/opensearch v1.0.0
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.8.0
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.30.0
github.com/stackitcloud/stackit-sdk-go/services/redis v0.28.2
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,8 @@ github.com/ssgreg/nlreturn/v2 v2.2.1 h1:X4XDI7jstt3ySqGU86YGAURbxw3oTDPK9sPEi6YE
github.com/ssgreg/nlreturn/v2 v2.2.1/go.mod h1:E/iiPB78hV7Szg2YfRgyIrk1AD6JVMTRkkxBiELzh2I=
github.com/stackitcloud/stackit-sdk-go/core v0.26.0 h1:jQEb9gkehfp6VCP6TcYk7BI10cz4l0KM2L6hqYBH2QA=
github.com/stackitcloud/stackit-sdk-go/core v0.26.0/go.mod h1:WU1hhxnjXw2EV7CYa1nlEvNpMiRY6CvmIOaHuL3pOaA=
github.com/stackitcloud/stackit-sdk-go/services/alb v0.14.2 h1:hGzfOJjlCRoFpri5eYIiwhE27qu02pKZLprKvbsTC/w=
github.com/stackitcloud/stackit-sdk-go/services/alb v0.14.2/go.mod h1:eK6oRB5Tmpt6KbXQ4UYBGg2LgW5bPtVoncL9E8JSRww=
github.com/stackitcloud/stackit-sdk-go/services/alb v0.15.0 h1:GDiNrm0A1KIHARcx6xI2I4PvNEWJuNysP3w1M/9WYps=
github.com/stackitcloud/stackit-sdk-go/services/alb v0.15.0/go.mod h1:eK6oRB5Tmpt6KbXQ4UYBGg2LgW5bPtVoncL9E8JSRww=
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.15.2 h1:b7WJ/vwxlVmNNX91kI3obqGcuoPAyaCbDL5aCMQ/sNg=
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.15.2/go.mod h1:T/JF25XGJ3GqER/1L2N//DgY8x5tY7gA3N+/0nvmOWY=
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.16.0 h1:Wqxx0PDTL2F5gqI5jjznuJY0TdqECltjA0aa/rHY63U=
Expand All @@ -692,8 +692,8 @@ github.com/stackitcloud/stackit-sdk-go/services/intake v0.9.0 h1:vtKZgDNsrZTr8jn
github.com/stackitcloud/stackit-sdk-go/services/intake v0.9.0/go.mod h1:UXhPs7JXjQvTp14d4ZffSLnFMQyB7y1cj100XWwQudI=
github.com/stackitcloud/stackit-sdk-go/services/kms v1.10.0 h1:/xPQvEaSbysmIKL/wtTYiweEe74P9iCzzyIA7ZHY8FU=
github.com/stackitcloud/stackit-sdk-go/services/kms v1.10.0/go.mod h1:pVaCmb1ZHAPGVRlSlBlVOjThp9Tb2sX9+nRX0M+d1KU=
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.13.0 h1:UuLNwFHjJCpL11y4F7B9oBKtZkxpu01VkNPILNkpex4=
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.13.0/go.mod h1:+Ld3dn648I+YKcBV3fEkYpDSr3fel421+LurJGywSBs=
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.14.0 h1:1dvL7tX91ziklayQmOupniE3jM4D5Nbtc0auNcx2p18=
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.14.0/go.mod h1:+Ld3dn648I+YKcBV3fEkYpDSr3fel421+LurJGywSBs=
github.com/stackitcloud/stackit-sdk-go/services/logme v0.29.0 h1:zsZaPr15UkjLuNhOiF+K7dikhenZAd+sQuEvTlGnkvg=
github.com/stackitcloud/stackit-sdk-go/services/logme v0.29.0/go.mod h1:JDOOYaGgcBts2x52nKPRMFgSZe7qqOFmfz1xIXCQgRY=
github.com/stackitcloud/stackit-sdk-go/services/logs v0.9.0 h1:m/FSPciEqsci1e7Ph2Nn+XyV+1bNPCLDfm1rm42xjAk=
Expand All @@ -708,8 +708,8 @@ github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.9.0 h1:T+ll3lS0
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.9.0/go.mod h1:QsPtoqAYvumyPU6ToX/5j1PbudN+VSTuvh6mp154ecM=
github.com/stackitcloud/stackit-sdk-go/services/observability v0.23.0 h1:kgD32fG3rkdGz0C+kZ1S7BEur7btTSeaX3vxHE346FY=
github.com/stackitcloud/stackit-sdk-go/services/observability v0.23.0/go.mod h1:0fEZQHm729mBdvg4sNrAhM6KmHROHJSeS2FwCMRk46k=
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.28.0 h1:Bu8/UzMCAN8VyaEnz5O15zK+8xSV0kf8ndYXvwkNwOk=
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.28.0/go.mod h1:L+NlfC1hilLOqlLLukCj/UDnxlnNrc/oMikcw3Ansyw=
github.com/stackitcloud/stackit-sdk-go/services/opensearch v1.0.0 h1:xJ/rhcMTV2pZ+s9PX/Q7+f4amNqn+kBl0t62cJ6xfko=
github.com/stackitcloud/stackit-sdk-go/services/opensearch v1.0.0/go.mod h1:L+NlfC1hilLOqlLLukCj/UDnxlnNrc/oMikcw3Ansyw=
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.8.0 h1:oWTviJKdlUxaaARJghTjOqBbarIK+7+nH3Kc3Wxn4rQ=
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.8.0/go.mod h1:yzlakB+f8ur4yAHR6lyCABO+HcEtZG3G2Faj6m5/uW8=
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.30.0 h1:zAFgiybClUjSaSKkGRfxKRiPmuJI7j5bnb+41/Zhtm8=
Expand Down
1 change: 0 additions & 1 deletion golang-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ linters:
- "-ST1021" # The documentation of an exported type should start with type's name.
- "-ST1022" # The documentation of an exported variable or constant should start with variable's name.
# customizations
- "-SA1019" # disable deprecation errors while we switch over to the SDK structure with multi API version support
- "-QF1001" # disable 'could apply De Morgan's law': readability of boolean expressions is subjective and should be decided on a case-by-case basis
- "-QF1012" # disable 'use fmt.Fprintf instead' Fprintf returns bytes written and an error, which we'd have to handle/ignore to appease some other linter
exclusions:
Expand Down
22 changes: 18 additions & 4 deletions stackit/internal/conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,16 @@ func StringValueToPointer(s basetypes.StringValue) *string {
if s.IsNull() || s.IsUnknown() {
return nil
}
value := s.ValueString()
return &value
return new(s.ValueString())
}

// StringValueToPointer converts basetypes.StringValue to a pointer to enum.
// It returns nil if the value is null or unknown.
func StringValueToEnumPointer[T ~string](s basetypes.StringValue) *T {
if s.IsNull() || s.IsUnknown() {
return nil
}
return new(T(s.ValueString()))
}

// Int32ValueToPointer converts basetypes.Int32Value to a pointer to int32.
Expand Down Expand Up @@ -153,18 +161,24 @@ func StringListToPointer(list basetypes.ListValue) (*[]string, error) {
// StringListToSlice converts basetypes.ListValue to a list of strings.
// It returns nil if the value is null or unknown.
func StringListToSlice(list basetypes.ListValue) ([]string, error) {
return StringListToEnumSlice[string](list)
}

// StringListToEnumSlice converts basetypes.ListValue to a list of enums.
// It returns nil if the value is null or unknown.
func StringListToEnumSlice[T ~string](list basetypes.ListValue) ([]T, error) {
if list.IsNull() || list.IsUnknown() {
return nil, nil
}

// Instantiate an empty slice to ensure the slice is not nil
listStr := []string{}
listStr := []T{}
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())
listStr = append(listStr, T(elStr.ValueString()))
}

return listStr, nil
Expand Down
153 changes: 153 additions & 0 deletions stackit/internal/conversion/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-framework/diag"
opensearch "github.com/stackitcloud/stackit-sdk-go/services/opensearch/v1api"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"

Expand Down Expand Up @@ -635,3 +636,155 @@ func TestTerraformStringSetToList(t *testing.T) {
})
}
}

func TestStringValueToPointer(t *testing.T) {
type args struct {
s basetypes.StringValue
}
tests := []struct {
name string
args args
want *string
}{
{
name: "default",
args: args{
s: basetypes.NewStringValue("abc"),
},
want: new("abc"),
},
{
name: "unknown",
args: args{
s: basetypes.NewStringUnknown(),
},
want: nil,
},
{
name: "null",
args: args{
s: basetypes.NewStringNull(),
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := StringValueToPointer(tt.args.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("StringValueToPointer() = %v, want %v", got, tt.want)
}
})
}
}

func TestStringValueToEnumPointer(t *testing.T) {
type args struct {
s basetypes.StringValue
}
type testCase[T interface{ ~string }] struct {
name string
args args
want *T
}
tests := []testCase[opensearch.InstanceParametersJavaGarbageCollector]{
{
name: "default",
args: args{
s: basetypes.NewStringValue("UseG1GC"),
},
want: new(opensearch.INSTANCEPARAMETERSJAVAGARBAGECOLLECTOR_USE_G1_GC),
},
{
name: "unknown",
args: args{
s: basetypes.NewStringUnknown(),
},
want: nil,
},
{
name: "null",
args: args{
s: basetypes.NewStringNull(),
},
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := StringValueToEnumPointer[opensearch.InstanceParametersJavaGarbageCollector](tt.args.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("StringValueToEnumPointer() = %v, want %v", got, tt.want)
}
})
}
}

func TestStringListToEnumSlice(t *testing.T) {
type args struct {
list basetypes.ListValue
}
type testCase[T interface{ ~string }] struct {
name string
args args
want []T
wantErr bool
}
tests := []testCase[opensearch.InstanceParametersPluginsInner]{
{
name: "default",
args: args{
list: basetypes.NewListValueMust(types.StringType, []attr.Value{
types.StringValue("repository-s3"),
types.StringValue("repository-azure"),
}),
},
want: []opensearch.InstanceParametersPluginsInner{
opensearch.INSTANCEPARAMETERSPLUGINSINNER_REPOSITORY_S3,
opensearch.INSTANCEPARAMETERSPLUGINSINNER_REPOSITORY_AZURE,
},
wantErr: false,
},
{
name: "unknown",
args: args{
list: basetypes.NewListUnknown(types.StringType),
},
want: nil,
wantErr: false,
},
{
name: "null",
args: args{
list: basetypes.NewListNull(types.StringType),
},
want: nil,
wantErr: false,
},
{
name: "empty list",
args: args{
list: basetypes.NewListValueMust(types.StringType, []attr.Value{}),
},
want: []opensearch.InstanceParametersPluginsInner{},
wantErr: false,
},
{
name: "invalid type",
args: args{
list: basetypes.NewListValueMust(types.Int64Type, []attr.Value{types.Int64Value(123)}),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := StringListToEnumSlice[opensearch.InstanceParametersPluginsInner](tt.args.list)
if (err != nil) != tt.wantErr {
t.Errorf("StringListToEnumSlice() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("StringListToEnumSlice() got = %v, want %v", got, tt.want)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
legacyAlb "github.com/stackitcloud/stackit-sdk-go/services/alb"
albSdk "github.com/stackitcloud/stackit-sdk-go/services/alb/v2api"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
Expand Down Expand Up @@ -60,9 +59,9 @@ func (r *albDataSource) Configure(ctx context.Context, req datasource.ConfigureR

// Schema defines the schema for the resource.
func (r *albDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
protocolOptions := sdkUtils.EnumSliceToStringSlice(legacyAlb.AllowedListenerProtocolEnumValues)
roleOptions := sdkUtils.EnumSliceToStringSlice(legacyAlb.AllowedNetworkRoleEnumValues)
errorOptions := sdkUtils.EnumSliceToStringSlice(legacyAlb.AllowedLoadBalancerErrorTypesEnumValues)
protocolOptions := sdkUtils.EnumSliceToStringSlice(albSdk.AllowedListenerProtocolEnumValues)
roleOptions := sdkUtils.EnumSliceToStringSlice(albSdk.AllowedNetworkRoleEnumValues)
errorOptions := sdkUtils.EnumSliceToStringSlice(albSdk.AllowedLoadBalancerErrorTypeEnumValues)

descriptions := map[string]string{
"main": "Application Load Balancer resource schema.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils"
legacyAlb "github.com/stackitcloud/stackit-sdk-go/services/alb"
albSdk "github.com/stackitcloud/stackit-sdk-go/services/alb/v2api"
"github.com/stackitcloud/stackit-sdk-go/services/alb/v2api/wait"

Expand Down Expand Up @@ -390,9 +389,9 @@ func (r *applicationLoadBalancerResource) Configure(ctx context.Context, req res

// Schema defines the schema for the resource.
func (r *applicationLoadBalancerResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
protocolOptions := sdkUtils.EnumSliceToStringSlice(legacyAlb.AllowedListenerProtocolEnumValues)
roleOptions := sdkUtils.EnumSliceToStringSlice(legacyAlb.AllowedNetworkRoleEnumValues)
errorOptions := sdkUtils.EnumSliceToStringSlice(legacyAlb.AllowedLoadBalancerErrorTypesEnumValues)
protocolOptions := sdkUtils.EnumSliceToStringSlice(albSdk.AllowedListenerProtocolEnumValues)
roleOptions := sdkUtils.EnumSliceToStringSlice(albSdk.AllowedNetworkRoleEnumValues)
errorOptions := sdkUtils.EnumSliceToStringSlice(albSdk.AllowedLoadBalancerErrorTypeEnumValues)

descriptions := map[string]string{
"main": "Application Load Balancer resource schema.",
Expand Down Expand Up @@ -1464,7 +1463,7 @@ func toListenersPayload(ctx context.Context, model *Model) ([]albSdk.Listener, e
Https: httpsPayload,
Name: conversion.StringValueToPointer(listenerModel.Name),
Port: conversion.Int32ValueToPointer(listenerModel.Port),
Protocol: conversion.StringValueToPointer(listenerModel.Protocol),
Protocol: conversion.StringValueToEnumPointer[albSdk.ListenerProtocol](listenerModel.Protocol),
WafConfigName: conversion.StringValueToPointer(listenerModel.WafConfigName),
})
}
Expand Down Expand Up @@ -1718,7 +1717,7 @@ func toNetworksPayload(ctx context.Context, model *Model) ([]albSdk.Network, err
networkModel := networksModel[i]
payload = append(payload, albSdk.Network{
NetworkId: conversion.StringValueToPointer(networkModel.NetworkId),
Role: conversion.StringValueToPointer(networkModel.Role),
Role: conversion.StringValueToEnumPointer[albSdk.NetworkRole](networkModel.Role),
})
}

Expand Down
Loading
Loading