Skip to content

Commit 4813553

Browse files
committed
feat: allow email updates in user resources
1 parent 375a205 commit 4813553

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

internal/provider/user_resource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ func (r *UserResource) Schema(ctx context.Context, req resource.SchemaRequest, r
8181
"email": schema.StringAttribute{
8282
MarkdownDescription: "Email address of the user.",
8383
Required: true,
84+
PlanModifiers: []planmodifier.String{
85+
stringplanmodifier.RequiresReplace(),
86+
},
8487
},
8588
"roles": schema.SetAttribute{
8689
MarkdownDescription: "Roles assigned to the user. Valid roles are 'owner', 'template-admin', 'user-admin', and 'auditor'.",

internal/provider/user_resource_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/coder/terraform-provider-coderd/integration"
1212
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
13+
"github.com/hashicorp/terraform-plugin-testing/terraform"
1314
"github.com/stretchr/testify/require"
1415
)
1516

@@ -35,6 +36,10 @@ func TestAccUserResource(t *testing.T) {
3536
cfg2.Username = PtrTo("exampleNew")
3637
cfg2.Name = PtrTo("Example User New")
3738

39+
cfg3 := cfg1
40+
cfg3.Email = PtrTo("[email protected]")
41+
42+
var userId string
3843
resource.Test(t, resource.TestCase{
3944
PreCheck: func() { testAccPreCheck(t) },
4045
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
@@ -68,6 +73,15 @@ func TestAccUserResource(t *testing.T) {
6873
Check: resource.ComposeAggregateTestCheckFunc(
6974
resource.TestCheckResourceAttr("coderd_user.test", "username", "exampleNew"),
7075
resource.TestCheckResourceAttr("coderd_user.test", "name", "Example User New"),
76+
testAccIdChanged("coderd_user.test", &userId),
77+
),
78+
},
79+
// Replace testing
80+
{
81+
Config: cfg3.String(t),
82+
Check: resource.ComposeAggregateTestCheckFunc(
83+
resource.TestCheckResourceAttr("coderd_user.test", "email", "[email protected]"),
84+
testAccIdChanged("coderd_user.test", &userId),
7185
),
7286
},
7387
// Delete testing automatically occurs in TestCase
@@ -151,3 +165,24 @@ resource "coderd_user" "test" {
151165

152166
return buf.String()
153167
}
168+
169+
// Check if the id has changed since the last time this check function was run.
170+
func testAccIdChanged(resourceName string, id *string) resource.TestCheckFunc {
171+
return func(s *terraform.State) error {
172+
rs, ok := s.RootModule().Resources[resourceName]
173+
if !ok {
174+
return fmt.Errorf("Resource %s not found", resourceName)
175+
}
176+
if rs.Primary.ID == "" {
177+
return fmt.Errorf("No ID is set")
178+
}
179+
if *id == "" {
180+
*id = rs.Primary.ID
181+
return nil
182+
}
183+
if rs.Primary.ID == *id {
184+
return fmt.Errorf("ID did not change from %s", rs.Primary.ID)
185+
}
186+
return nil
187+
}
188+
}

0 commit comments

Comments
 (0)