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
5 changes: 4 additions & 1 deletion stackit/internal/services/sfs/resourcepool/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ func (r *resourcePoolResource) Schema(_ context.Context, _ resource.SchemaReques
Description: "Name of the snapshot policy.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifierUtils.UseStateForUnknownIf(stringplanmodifierUtils.StringUnchanged(path.Root("snapshot_policy").AtName("id")), "sets `UseStateForUnknown` only if `id` has not changed"),
stringplanmodifierUtils.UseStateForUnknownIf(
stringplanmodifierUtils.UnchangedPaths(path.MatchRoot("snapshot_policy").AtName("id")),
"sets `UseStateForUnknown` only if `id` has not changed",
),
},
},
},
Expand Down
10 changes: 8 additions & 2 deletions stackit/internal/services/ske/cluster/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,10 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Description: "Full Kubernetes version used. For example, if 1.22 was set in `kubernetes_version_min`, this value may result to 1.22.15. " + SKEUpdateDoc,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifierUtils.UseStateForUnknownIf(stringplanmodifierUtils.StringUnchanged(path.Root("kubernetes_version_min")), "sets `UseStateForUnknown` only if `kubernetes_min_version` has not changed"),
stringplanmodifierUtils.UseStateForUnknownIf(
stringplanmodifierUtils.UnchangedPaths(path.MatchRoot("kubernetes_version_min")),
"sets `UseStateForUnknown` only if `kubernetes_min_version` has not changed",
),
},
},
"egress_address_ranges": schema.ListAttribute{
Expand Down Expand Up @@ -580,7 +583,10 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re
Description: "Full OS image version used. For example, if 3815.2 was set in `os_version_min`, this value may result to 3815.2.2. " + SKEUpdateDoc,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifierUtils.UseStateForUnknownIf(skeUtils.HasOsVersionMinChanged, "sets `UseStateForUnknown` only if `os_version_min` has not changed"), //nolint:staticcheck // temporary fix for issue with StringUnchanged
stringplanmodifierUtils.UseStateForUnknownIf(
stringplanmodifierUtils.UnchangedPaths(path.MatchRelative().AtParent().AtName("os_version_min")),
"sets `UseStateForUnknown` only if `os_version_min` has not changed",
),
},
},
"volume_type": schema.StringAttribute{
Expand Down
28 changes: 28 additions & 0 deletions stackit/internal/services/ske/ske_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil"
)

const ephemeralKubeconfigAddress = "ephemeral.stackit_ske_kubeconfig.ephemeral_kubeconfig"

var (
minTestName = "acc-min" + acctest.RandStringFromCharSet(3, acctest.CharSetAlpha)
maxTestName = "acc-max" + acctest.RandStringFromCharSet(3, acctest.CharSetAlpha)
Expand Down Expand Up @@ -138,6 +140,11 @@ func TestAccSKEMin(t *testing.T) {
{
Config: testutil.NewConfigBuilder().Experiments(testutil.ExperimentSKE).BuildProviderConfig() + "\n" + resourceMin,
ConfigVariables: testConfigVarsMin,
ConfigPlanChecks: resource.ConfigPlanChecks{
PostApplyPreRefresh: []plancheck.PlanCheck{testutil.ExpectOnlyEphemeralOpen(ephemeralKubeconfigAddress)},
PostApplyPostRefresh: []plancheck.PlanCheck{testutil.ExpectOnlyEphemeralOpen(ephemeralKubeconfigAddress)},
},
ExpectNonEmptyPlan: testutil.UsingOpenTofu(),
Check: resource.ComposeAggregateTestCheckFunc(
// cluster data
resource.TestCheckResourceAttr("stackit_ske_cluster.cluster", "project_id", testutil.ConvertConfigVariable(testConfigVarsMin["project_id"])),
Expand Down Expand Up @@ -189,6 +196,11 @@ func TestAccSKEMin(t *testing.T) {
{
Config: testutil.NewConfigBuilder().Experiments(testutil.ExperimentSKE).BuildProviderConfig() + "\n" + resourceMin,
ConfigVariables: testConfigVarsMin,
ConfigPlanChecks: resource.ConfigPlanChecks{
PostApplyPreRefresh: []plancheck.PlanCheck{testutil.ExpectOnlyEphemeralOpen(ephemeralKubeconfigAddress)},
PostApplyPostRefresh: []plancheck.PlanCheck{testutil.ExpectOnlyEphemeralOpen(ephemeralKubeconfigAddress)},
},
ExpectNonEmptyPlan: testutil.UsingOpenTofu(),
Check: resource.ComposeAggregateTestCheckFunc(

// cluster data
Expand Down Expand Up @@ -247,7 +259,10 @@ func TestAccSKEMin(t *testing.T) {
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("stackit_ske_cluster.cluster", plancheck.ResourceActionUpdate),
},
PostApplyPreRefresh: []plancheck.PlanCheck{testutil.ExpectOnlyEphemeralOpen(ephemeralKubeconfigAddress)},
PostApplyPostRefresh: []plancheck.PlanCheck{testutil.ExpectOnlyEphemeralOpen(ephemeralKubeconfigAddress)},
},
ExpectNonEmptyPlan: testutil.UsingOpenTofu(),
Check: resource.ComposeAggregateTestCheckFunc(
// cluster data
resource.TestCheckResourceAttr("stackit_ske_cluster.cluster", "project_id", testutil.ConvertConfigVariable(configVarsMinUpdated()["project_id"])),
Expand Down Expand Up @@ -299,6 +314,11 @@ func TestAccSKEMax(t *testing.T) {
{
Config: testutil.NewConfigBuilder().Experiments(testutil.ExperimentSKE).BuildProviderConfig() + "\n" + resourceMax,
ConfigVariables: testConfigVarsMax,
ConfigPlanChecks: resource.ConfigPlanChecks{
PostApplyPreRefresh: []plancheck.PlanCheck{testutil.ExpectOnlyEphemeralOpen(ephemeralKubeconfigAddress)},
PostApplyPostRefresh: []plancheck.PlanCheck{testutil.ExpectOnlyEphemeralOpen(ephemeralKubeconfigAddress)},
},
ExpectNonEmptyPlan: testutil.UsingOpenTofu(),
Check: resource.ComposeAggregateTestCheckFunc(
// cluster data
resource.TestCheckResourceAttr("stackit_ske_cluster.cluster", "project_id", testutil.ConvertConfigVariable(testConfigVarsMax["project_id"])),
Expand Down Expand Up @@ -389,6 +409,11 @@ func TestAccSKEMax(t *testing.T) {
{
Config: testutil.NewConfigBuilder().Experiments(testutil.ExperimentSKE).BuildProviderConfig() + "\n" + resourceMax,
ConfigVariables: testConfigVarsMax,
ConfigPlanChecks: resource.ConfigPlanChecks{
PostApplyPreRefresh: []plancheck.PlanCheck{testutil.ExpectOnlyEphemeralOpen(ephemeralKubeconfigAddress)},
PostApplyPostRefresh: []plancheck.PlanCheck{testutil.ExpectOnlyEphemeralOpen(ephemeralKubeconfigAddress)},
},
ExpectNonEmptyPlan: testutil.UsingOpenTofu(),
Check: resource.ComposeAggregateTestCheckFunc(

// cluster data
Expand Down Expand Up @@ -486,7 +511,10 @@ func TestAccSKEMax(t *testing.T) {
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("stackit_ske_cluster.cluster", plancheck.ResourceActionUpdate),
},
PostApplyPreRefresh: []plancheck.PlanCheck{testutil.ExpectOnlyEphemeralOpen(ephemeralKubeconfigAddress)},
PostApplyPostRefresh: []plancheck.PlanCheck{testutil.ExpectOnlyEphemeralOpen(ephemeralKubeconfigAddress)},
},
ExpectNonEmptyPlan: testutil.UsingOpenTofu(),
Check: resource.ComposeAggregateTestCheckFunc(
// cluster data
resource.TestCheckResourceAttr("stackit_ske_cluster.cluster", "project_id", testutil.ConvertConfigVariable(configVarsMaxUpdated()["project_id"])),
Expand Down
27 changes: 0 additions & 27 deletions stackit/internal/services/ske/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import (
"fmt"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/core/config"
ske "github.com/stackitcloud/stackit-sdk-go/services/ske/v2api"

"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils/planmodifiers/stringplanmodifier"
)

func ConfigureClient(ctx context.Context, providerData *core.ProviderData, diags *diag.Diagnostics) *ske.APIClient {
Expand Down Expand Up @@ -45,27 +42,3 @@ func IsEmptyExtension(extension *ske.Extension) bool {
}
return false
}

// Deprecated: HasOsVersionMinChanged
func HasOsVersionMinChanged(ctx context.Context, request planmodifier.StringRequest, response *stringplanmodifier.UseStateForUnknownFuncResponse) { // nolint:gocritic // function signature required by Terraform
dependencyPath := request.Path.ParentPath().AtName("os_version_min")

var minVersionPlan types.String
diags := request.Plan.GetAttribute(ctx, dependencyPath, &minVersionPlan)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

var minVersionState types.String
diags = request.State.GetAttribute(ctx, dependencyPath, &minVersionState)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

if minVersionState == minVersionPlan {
response.UseStateForUnknown = true
return
}
}
45 changes: 45 additions & 0 deletions stackit/internal/testutil/plancheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package testutil

import (
"context"
"fmt"
"slices"

"github.com/hashicorp/terraform-plugin-testing/plancheck"
)

// ExpectOnlyEphemeralOpen returns a plan check that allows no-op actions and
// OpenTofu "open" actions for the given ephemeral resource addresses.
func ExpectOnlyEphemeralOpen(configAddresses ...string) plancheck.PlanCheck {
return expectOnlyEphemeralOpen{configAddresses: configAddresses}
}

type expectOnlyEphemeralOpen struct {
configAddresses []string
}

func (c expectOnlyEphemeralOpen) CheckPlan(_ context.Context, req plancheck.CheckPlanRequest, resp *plancheck.CheckPlanResponse) {
for _, resourceChange := range req.Plan.ResourceChanges {
if resourceChange.Change == nil || resourceChange.Change.Actions.NoOp() {
continue
}

actions := resourceChange.Change.Actions
isSpecificResource := slices.Contains(c.configAddresses, resourceChange.Address)
if isSpecificResource &&
string(resourceChange.Mode) == "ephemeral" &&
len(actions) == 1 && string(actions[0]) == "open" {
continue
}

resp.Error = fmt.Errorf("unexpected planned action(s) %v for %s", actions, resourceChange.Address)
return
}

for name, outputChange := range req.Plan.OutputChanges {
if outputChange != nil && !outputChange.Actions.NoOp() {
resp.Error = fmt.Errorf("unexpected planned action(s) %v for output %s", outputChange.Actions, name)
return
}
}
}
5 changes: 5 additions & 0 deletions stackit/internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ const (
credentialsFilePath = ".stackit/credentials.json" //nolint:gosec // linter false positive
)

// UsingOpenTofu reports whether acceptance tests are running with the OpenTofu CLI.
func UsingOpenTofu() bool {
return strings.HasSuffix(os.Getenv("TF_ACC_TERRAFORM_PATH"), "tofu")
}

var (
// TestAccProtoV6ProviderFactories is used to instantiate a provider during
// acceptance testing. The factory function will be invoked for every Terraform
Expand Down
12 changes: 12 additions & 0 deletions stackit/internal/testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ func newTestState(resourceName string, attributes map[string]string) *terraform.
}
}

func TestUsingOpenTofu(t *testing.T) {
t.Setenv("TF_ACC_TERRAFORM_PATH", "/usr/local/bin/tofu")
if !UsingOpenTofu() {
t.Error("UsingOpenTofu() = false, want true")
}

t.Setenv("TF_ACC_TERRAFORM_PATH", "/usr/local/bin/terraform")
if UsingOpenTofu() {
t.Error("UsingOpenTofu() = true, want false")
}
}

func TestConvertConfigVariable(t *testing.T) {
tests := []struct {
name string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)

type UseStateForUnknownFuncResponse struct {
Expand Down Expand Up @@ -71,30 +70,6 @@ func (m useStateForUnknownIf) PlanModifyString(ctx context.Context, req planmodi
}
}

// StringUnchanged sets UseStateForUnkown to true if the attribute's planned value matches the current state
func StringUnchanged(attributePath path.Path) UseStateForUnknownIfFunc { // nolint:gocritic // function signature required by Terraform
return func(ctx context.Context, request planmodifier.StringRequest, response *UseStateForUnknownFuncResponse) {
var attributePlan types.String
diags := request.Plan.GetAttribute(ctx, attributePath, &attributePlan)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

var attributeState types.String
diags = request.State.GetAttribute(ctx, attributePath, &attributeState)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}

if attributeState == attributePlan {
response.UseStateForUnknown = true
return
}
}
}

// UnchangedPaths sets UseStateForUnknown to true if all values matched by paths are equal in Plan & State
func UnchangedPaths(paths ...path.Expression) UseStateForUnknownIfFunc {
return func(ctx context.Context, req planmodifier.StringRequest, resp *UseStateForUnknownFuncResponse) {
Expand Down
Loading