Skip to content

Commit

Permalink
Prevent keepers from triggering an in-place update following import (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed Apr 15, 2024
1 parent 9192f92 commit f8a3f8b
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20230313-114526.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: 'all: Prevent `keepers` from triggering an in-place update following import'
time: 2023-03-13T11:45:26.616828Z
custom:
Issue: "385"
3 changes: 1 addition & 2 deletions internal/provider/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ func (r *idResource) ImportState(ctx context.Context, req resource.ImportStateRe

state.ID = types.StringValue(id)
state.ByteLength = types.Int64Value(int64(len(bytes)))
// Using types.MapValueMust to ensure map is known.
state.Keepers = types.MapValueMust(types.StringType, nil)
state.Keepers = types.MapNull(types.StringType)
state.B64Std = types.StringValue(prefix + b64Std)
state.B64URL = types.StringValue(prefix + id)
state.Hex = types.StringValue(prefix + hexStr)
Expand Down
23 changes: 23 additions & 0 deletions internal/provider/resource_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ func TestAccResourceID_ImportWithPrefix(t *testing.T) {
})
}

func TestAccResourceID_ImportWithoutKeepersProducesNoPlannedChanges(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV5ProviderFactories: protoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_id" "foo" {
byte_length = 4
}`,
ResourceName: "random_id.foo",
ImportStateId: "p-9hUg",
ImportState: true,
ImportStatePersist: true,
},
{
Config: `resource "random_id" "foo" {
byte_length = 4
}`,
PlanOnly: true,
},
},
})
}

func TestAccResourceID_UpgradeFromVersion3_3_2(t *testing.T) {
resource.Test(t, resource.TestCase{
Steps: []resource.TestStep{
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/resource_integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ func (r *integerResource) ImportState(ctx context.Context, req resource.ImportSt
var state integerModelV0

state.ID = types.StringValue(parts[0])
// Using types.MapValueMust to ensure map is known.
state.Keepers = types.MapValueMust(types.StringType, nil)
state.Keepers = types.MapNull(types.StringType)
state.Result = types.Int64Value(result)
state.Min = types.Int64Value(min)
state.Max = types.Int64Value(max)
Expand Down
27 changes: 27 additions & 0 deletions internal/provider/resource_integer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,33 @@ func TestAccResourceInteger(t *testing.T) {
})
}

func TestAccResourceInteger_ImportWithoutKeepersProducesNoPlannedChanges(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV5ProviderFactories: protoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_integer" "integer_1" {
min = 1
max = 3
seed = "12345"
}`,
ResourceName: "random_integer.integer_1",
ImportStateId: "3,1,3,12345",
ImportState: true,
ImportStatePersist: true,
},
{
Config: `resource "random_integer" "integer_1" {
min = 1
max = 3
seed = "12345"
}`,
PlanOnly: true,
},
},
})
}

func TestAccResourceInteger_ChangeSeed(t *testing.T) {
t.Parallel()
resource.UnitTest(t, resource.TestCase{
Expand Down
23 changes: 23 additions & 0 deletions internal/provider/resource_password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,29 @@ func TestAccResourcePassword_OverrideSpecial_FromVersion3_4_2(t *testing.T) {
})
}

func TestAccResourcePassword_ImportWithoutKeepersProducesNoPlannedChanges(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV5ProviderFactories: protoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_password" "test" {
length = 12
}`,
ResourceName: "random_password.test",
ImportStateId: "Z=:cbrJE?Ltg",
ImportState: true,
ImportStatePersist: true,
},
{
Config: `resource "random_password" "test" {
length = 12
}`,
PlanOnly: true,
},
},
})
}

// TestAccResourcePassword_Import_FromVersion3_1_3 verifies behaviour when resource has been imported and stores
// null for length, lower, number, special, upper, min_lower, min_numeric, min_special, min_upper attributes in state.
// v3.1.3 was selected as this is the last provider version using schema version 0.
Expand Down
23 changes: 23 additions & 0 deletions internal/provider/resource_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ func TestAccResourceString_Import(t *testing.T) {
})
}

func TestAccResourceString_ImportWithoutKeepersProducesNoPlannedChanges(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV5ProviderFactories: protoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_string" "basic" {
length = 12
}`,
ResourceName: "random_string.basic",
ImportStateId: "Z=:cbrJE?Ltg",
ImportState: true,
ImportStatePersist: true,
},
{
Config: `resource "random_string" "basic" {
length = 12
}`,
PlanOnly: true,
},
},
})
}

func TestAccResourceString_Keepers_Keep_EmptyMap(t *testing.T) {
var id1, id2 string

Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (r *uuidResource) ImportState(ctx context.Context, req resource.ImportState

state.ID = types.StringValue(result)
state.Result = types.StringValue(result)
state.Keepers = types.MapValueMust(types.StringType, nil)
state.Keepers = types.MapNull(types.StringType)

diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
Expand Down
21 changes: 21 additions & 0 deletions internal/provider/resource_uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ func TestAccResourceUUID(t *testing.T) {
})
}

func TestAccResourceUUID_ImportWithoutKeepersProducesNoPlannedChanges(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
ProtoV5ProviderFactories: protoV5ProviderFactories(),
Steps: []resource.TestStep{
{
Config: `resource "random_uuid" "basic" {
}`,
ResourceName: "random_uuid.basic",
ImportStateId: "6b0f8e7c-3ea6-4523-88a2-5a70419ee954",
ImportState: true,
ImportStatePersist: true,
},
{
Config: `resource "random_uuid" "basic" {
}`,
PlanOnly: true,
},
},
})
}

func TestAccResourceUUID_Keepers_Keep_EmptyMap(t *testing.T) {
var id1, id2 string

Expand Down

0 comments on commit f8a3f8b

Please sign in to comment.