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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/hashicorp/terraform-plugin-log v0.10.0
github.com/hashicorp/terraform-plugin-testing v1.14.0
github.com/stackitcloud/stackit-sdk-go/core v0.23.0
github.com/stackitcloud/stackit-sdk-go/services/alb v0.9.3
github.com/stackitcloud/stackit-sdk-go/services/alb v0.12.1
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.13.0
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1
github.com/stackitcloud/stackit-sdk-go/services/edge v0.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ github.com/stackitcloud/stackit-sdk-go/core v0.23.0 h1:zPrOhf3Xe47rKRs1fg/AqKYUi
github.com/stackitcloud/stackit-sdk-go/core v0.23.0/go.mod h1:osMglDby4csGZ5sIfhNyYq1bS1TxIdPY88+skE/kkmI=
github.com/stackitcloud/stackit-sdk-go/services/alb v0.9.3 h1:X82TZfc6lg8ZoYdckiv5+OsV0d+81Q2TFMJh1TfxGWk=
github.com/stackitcloud/stackit-sdk-go/services/alb v0.9.3/go.mod h1:V6+MolxM/M2FWyWZA+FRFKEzzUe10MU9eEVfMvxHGi8=
github.com/stackitcloud/stackit-sdk-go/services/alb v0.12.1 h1:RKaxAymxlyxxE0Gta3yRuQWf07LnlcX+mfGnVB96NHA=
github.com/stackitcloud/stackit-sdk-go/services/alb v0.12.1/go.mod h1:FHkV5L9vCQha+5MX+NdMdYjQIHXcLr95+bu1FN91QOM=
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.12.0 h1:HxPgBu04j5tj6nfZ2r0l6v4VXC0/tYOGe4sA5Addra8=
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.12.0/go.mod h1:uYI9pHAA2g84jJN25ejFUxa0/JtfpPZqMDkctQ1BzJk=
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.13.0 h1:iRJK2d3I2QqWp8hqhxlkCtQDNb7fwKHkik9ogmcx2o8=
Expand Down
25 changes: 25 additions & 0 deletions stackit/internal/conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,31 @@ func StringSetToPointer(set basetypes.SetValue) (*[]string, error) {
return &result, nil
}

// StringSetToSlice converts basetypes.SetValue to a slice of strings.
// It returns nil if the value is null or unknown.
// Note: It sorts the resulting slice to ensure deterministic behavior.
func StringSetToSlice(set basetypes.SetValue) ([]string, error) {
Copy link
Member

@rubenhoenle rubenhoenle Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is basically a copy of StringSetToPointer, couldn't we adjust it to re-use this logic here? So we don't have duplicates?

// StringSetToPointer converts basetypes.SetValue to a pointer to a list of strings.
// It returns nil if the value is null or unknown.
// Note: It sorts the resulting slice to ensure deterministic behavior.
func StringSetToPointer(set basetypes.SetValue) (*[]string, error) {
   result := StringSetToSlice(set)
   return &result
}

if set.IsNull() || set.IsUnknown() {
return nil, nil
}
elements := set.Elements()
result := make([]string, 0, len(elements))

for i, el := range elements {
elStr, ok := el.(types.String)
if !ok {
return nil, fmt.Errorf("element %d in set is not a string (type: %T)", i, el)
}
result = append(result, elStr.ValueString())
}

// Because Sets are unordered in Terraform, we sort here to
// prevent non-deterministic behavior in the provider logic or API calls.
sort.Strings(result)

return result, nil
}

// ToJSONMApPartialUpdatePayload returns a map[string]interface{} to be used in a PATCH request payload.
// It takes a current map as it is in the terraform state and a desired map as it is in the user configuratiom
// and builds a map which sets to null keys that should be removed, updates the values of existing keys and adds new keys
Expand Down
56 changes: 56 additions & 0 deletions stackit/internal/conversion/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,59 @@ func TestParseEphemeralProviderData(t *testing.T) {
})
}
}

func TestStringSetToSlice(t *testing.T) {
t.Parallel()
tests := []struct {
name string
in basetypes.SetValue
want []string
wantErr bool
}{
{
name: "unknown",
in: basetypes.NewSetUnknown(types.StringType),
want: nil,
},
{
name: "null",
in: basetypes.NewSetNull(types.StringType),
want: nil,
},
{
name: "invalid type",
in: basetypes.NewSetValueMust(types.Int64Type, []attr.Value{types.Int64Value(123)}),
wantErr: true,
},
{
name: "some values, sorting",
in: basetypes.NewSetValueMust(
types.StringType,
[]attr.Value{
types.StringValue("xyz"),
types.StringValue("abc"),
},
),
want: []string{
"abc",
"xyz",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := StringSetToSlice(tt.in)
if tt.wantErr && err == nil {
t.Fatal("expected error")
}
if !tt.wantErr && err != nil {
t.Fatalf("expected no error, got: %v", err)
}
if d := cmp.Diff(got, tt.want); d != "" {
t.Fatalf("no match, diff: %s", d)
}
})
}
}
20 changes: 10 additions & 10 deletions stackit/internal/services/alb/alb_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/stackitcloud/stackit-sdk-go/services/alb/wait"
"github.com/stackitcloud/stackit-sdk-go/services/alb/v2api/wait"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"

stackitSdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"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/testutil"
)

Expand Down Expand Up @@ -621,12 +621,12 @@ func TestAccALBResourceMax(t *testing.T) {

func testAccCheckALBDestroy(s *terraform.State) error {
ctx := context.Background()
var client *alb.APIClient
var client *albSdk.APIClient
var err error
if testutil.ALBCustomEndpoint == "" {
client, err = alb.NewAPIClient()
client, err = albSdk.NewAPIClient()
} else {
client, err = alb.NewAPIClient(
client, err = albSdk.NewAPIClient(
stackitSdkConfig.WithEndpoint(testutil.ALBCustomEndpoint),
)
}
Expand All @@ -648,27 +648,27 @@ func testAccCheckALBDestroy(s *terraform.State) error {
loadbalancersToDestroy = append(loadbalancersToDestroy, loadbalancerName)
}

loadbalancersResp, err := client.ListLoadBalancers(ctx, testutil.ProjectId, region).Execute()
loadbalancersResp, err := client.DefaultAPI.ListLoadBalancers(ctx, testutil.ProjectId, region).Execute()
if err != nil {
return fmt.Errorf("getting loadbalancersResp: %w", err)
}

if loadbalancersResp.LoadBalancers == nil || (loadbalancersResp.LoadBalancers != nil && len(*loadbalancersResp.LoadBalancers) == 0) {
if loadbalancersResp.LoadBalancers == nil || (loadbalancersResp.LoadBalancers != nil && len(loadbalancersResp.LoadBalancers) == 0) {
fmt.Print("No load balancers found for project \n")
return nil
}

items := *loadbalancersResp.LoadBalancers
items := loadbalancersResp.LoadBalancers
for i := range items {
if items[i].Name == nil {
continue
}
if utils.Contains(loadbalancersToDestroy, *items[i].Name) {
_, err := client.DeleteLoadBalancerExecute(ctx, testutil.ProjectId, region, *items[i].Name)
_, err := client.DefaultAPI.DeleteLoadBalancer(ctx, testutil.ProjectId, region, *items[i].Name).Execute()
if err != nil {
return fmt.Errorf("destroying load balancer %s during CheckDestroy: %w", *items[i].Name, err)
}
_, err = wait.DeleteLoadbalancerWaitHandler(ctx, client, testutil.ProjectId, region, *items[i].Name).WaitWithContext(ctx)
_, err = wait.DeleteLoadbalancerWaitHandler(ctx, client.DefaultAPI, testutil.ProjectId, region, *items[i].Name).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("destroying load balancer %s during CheckDestroy: waiting for deletion %w", *items[i].Name, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
albSdk "github.com/stackitcloud/stackit-sdk-go/services/alb"
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"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
)
Expand Down Expand Up @@ -57,9 +58,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(albSdk.AllowedListenerProtocolEnumValues)
roleOptions := sdkUtils.EnumSliceToStringSlice(albSdk.AllowedNetworkRoleEnumValues)
errorOptions := sdkUtils.EnumSliceToStringSlice(albSdk.AllowedLoadBalancerErrorTypesEnumValues)
protocolOptions := sdkUtils.EnumSliceToStringSlice(legacyAlb.AllowedListenerProtocolEnumValues)
roleOptions := sdkUtils.EnumSliceToStringSlice(legacyAlb.AllowedNetworkRoleEnumValues)
errorOptions := sdkUtils.EnumSliceToStringSlice(legacyAlb.AllowedLoadBalancerErrorTypesEnumValues)

descriptions := map[string]string{
"main": "Application Load Balancer resource schema.",
Expand Down Expand Up @@ -201,7 +202,7 @@ func (r *albDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, re
Description: descriptions["listeners.name"],
Computed: true,
},
"port": schema.Int64Attribute{
"port": schema.Int32Attribute{
Description: descriptions["port"],
Computed: true,
},
Expand Down Expand Up @@ -433,7 +434,7 @@ func (r *albDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, re
Description: descriptions["active_health_check"],
Computed: true,
Attributes: map[string]schema.Attribute{
"healthy_threshold": schema.Int64Attribute{
"healthy_threshold": schema.Int32Attribute{
Description: descriptions["healthy_threshold"],
Computed: true,
},
Expand All @@ -449,7 +450,7 @@ func (r *albDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, re
Description: descriptions["timeout"],
Computed: true,
},
"unhealthy_threshold": schema.Int64Attribute{
"unhealthy_threshold": schema.Int32Attribute{
Description: descriptions["unhealthy_threshold"],
Computed: true,
},
Expand All @@ -474,7 +475,7 @@ func (r *albDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, re
Description: descriptions["target_pools.name"],
Computed: true,
},
"target_port": schema.Int64Attribute{
"target_port": schema.Int32Attribute{
Description: descriptions["target_port"],
Computed: true,
},
Expand Down Expand Up @@ -555,7 +556,7 @@ func (r *albDataSource) Read(ctx context.Context, req datasource.ReadRequest, re
ctx = tflog.SetField(ctx, "name", name)
ctx = tflog.SetField(ctx, "region", region)

albResp, err := r.client.GetLoadBalancer(ctx, projectId, region, name).Execute()
albResp, err := r.client.DefaultAPI.GetLoadBalancer(ctx, projectId, region, name).Execute()
if err != nil {
utils.LogError(
ctx,
Expand Down
Loading
Loading