Skip to content

Commit f201ea2

Browse files
authored
Merge pull request #261 from ilpianista/bugfix/user-attributes-state
Fix gitlab_user resource attribute incorrect mapping and allow to change user email without recreating the resource
2 parents f60dc34 + 3ab5042 commit f201ea2

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

gitlab/resource_gitlab_user.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ func resourceGitlabUser() *schema.Resource {
4848
"email": {
4949
Type: schema.TypeString,
5050
Required: true,
51-
ForceNew: true,
5251
},
5352
"name": {
5453
Type: schema.TypeString,
@@ -92,6 +91,10 @@ func resourceGitlabUserSetToState(d *schema.ResourceData, user *gitlab.User) {
9291
d.Set("name", user.Name)
9392
d.Set("can_create_group", user.CanCreateGroup)
9493
d.Set("projects_limit", user.ProjectsLimit)
94+
d.Set("email", user.Email)
95+
d.Set("is_admin", user.IsAdmin)
96+
d.Set("is_external", user.External)
97+
d.Set("skip_confirmation", !user.ConfirmedAt.IsZero())
9598
}
9699

97100
func resourceGitlabUserCreate(d *schema.ResourceData, meta interface{}) error {
@@ -117,8 +120,6 @@ func resourceGitlabUserCreate(d *schema.ResourceData, meta interface{}) error {
117120
}
118121

119122
d.SetId(fmt.Sprintf("%d", user.ID))
120-
d.Set("is_admin", user.IsAdmin)
121-
d.Set("is_external", user.External)
122123

123124
return resourceGitlabUserRead(d, meta)
124125
}
@@ -151,6 +152,11 @@ func resourceGitlabUserUpdate(d *schema.ResourceData, meta interface{}) error {
151152
options.Username = gitlab.String(d.Get("username").(string))
152153
}
153154

155+
if d.HasChange("email") {
156+
options.Email = gitlab.String(d.Get("email").(string))
157+
options.SkipReconfirmation = gitlab.Bool(true)
158+
}
159+
154160
if d.HasChange("is_admin") {
155161
options.Admin = gitlab.Bool(d.Get("is_admin").(bool))
156162
}
@@ -164,7 +170,7 @@ func resourceGitlabUserUpdate(d *schema.ResourceData, meta interface{}) error {
164170
}
165171

166172
if d.HasChange("is_external") {
167-
options.Admin = gitlab.Bool(d.Get("is_external").(bool))
173+
options.External = gitlab.Bool(d.Get("is_external").(bool))
168174
}
169175

170176
log.Printf("[DEBUG] update gitlab user %s", d.Id())

gitlab/resource_gitlab_user_test.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestAccGitlabUser_basic(t *testing.T) {
2626
Check: resource.ComposeTestCheckFunc(
2727
testAccCheckGitlabUserExists("gitlab_user.foo", &user),
2828
testAccCheckGitlabUserAttributes(&user, &testAccGitlabUserExpectedAttributes{
29-
Email: "listest%[email protected]",
29+
Email: fmt.Sprintf("listest%[email protected]", rInt),
3030
Password: fmt.Sprintf("test%dtt", rInt),
3131
Username: fmt.Sprintf("listest%d", rInt),
3232
Name: fmt.Sprintf("foo %d", rInt),
@@ -38,21 +38,21 @@ func TestAccGitlabUser_basic(t *testing.T) {
3838
}),
3939
),
4040
},
41-
// Update the user to change the name
41+
// Update the user to change the name, email, projects_limit and more
4242
{
4343
Config: testAccGitlabUserUpdateConfig(rInt),
4444
Check: resource.ComposeTestCheckFunc(
4545
testAccCheckGitlabUserExists("gitlab_user.foo", &user),
4646
testAccCheckGitlabUserAttributes(&user, &testAccGitlabUserExpectedAttributes{
47-
Email: "listest%d@ssss.com",
47+
Email: fmt.Sprintf("listest%d@tttt.com", rInt),
4848
Password: fmt.Sprintf("test%dtt", rInt),
4949
Username: fmt.Sprintf("listest%d", rInt),
5050
Name: fmt.Sprintf("bar %d", rInt),
5151
ProjectsLimit: 10,
5252
Admin: true,
5353
CanCreateGroup: true,
5454
SkipConfirmation: false,
55-
External: true,
55+
External: false,
5656
}),
5757
),
5858
},
@@ -62,7 +62,7 @@ func TestAccGitlabUser_basic(t *testing.T) {
6262
Check: resource.ComposeTestCheckFunc(
6363
testAccCheckGitlabUserExists("gitlab_user.foo", &user),
6464
testAccCheckGitlabUserAttributes(&user, &testAccGitlabUserExpectedAttributes{
65-
Email: "listest%[email protected]",
65+
Email: fmt.Sprintf("listest%[email protected]", rInt),
6666
Password: fmt.Sprintf("test%dtt", rInt),
6767
Username: fmt.Sprintf("listest%d", rInt),
6868
Name: fmt.Sprintf("foo %d", rInt),
@@ -151,6 +151,26 @@ func testAccCheckGitlabUserAttributes(user *gitlab.User, want *testAccGitlabUser
151151
return fmt.Errorf("got username %q; want %q", user.Username, want.Username)
152152
}
153153

154+
if user.Email != want.Email {
155+
return fmt.Errorf("got email %q; want %q", user.Email, want.Email)
156+
}
157+
158+
if user.CanCreateGroup != want.CanCreateGroup {
159+
return fmt.Errorf("got can_create_group %t; want %t", user.CanCreateGroup, want.CanCreateGroup)
160+
}
161+
162+
if user.External != want.External {
163+
return fmt.Errorf("got is_external %t; want %t", user.External, want.External)
164+
}
165+
166+
if user.IsAdmin != want.Admin {
167+
return fmt.Errorf("got is_admin %t; want %t", user.IsAdmin, want.Admin)
168+
}
169+
170+
if user.ProjectsLimit != want.ProjectsLimit {
171+
return fmt.Errorf("got projects_limit %d; want %d", user.ProjectsLimit, want.ProjectsLimit)
172+
}
173+
154174
return nil
155175
}
156176
}
@@ -200,11 +220,11 @@ resource "gitlab_user" "foo" {
200220
name = "bar %d"
201221
username = "listest%d"
202222
password = "test%dtt"
203-
email = "listest%d@ssss.com"
223+
email = "listest%d@tttt.com"
204224
is_admin = true
205225
projects_limit = 10
206226
can_create_group = true
207-
is_external = true
227+
is_external = false
208228
}
209229
`, rInt, rInt, rInt, rInt)
210230
}

0 commit comments

Comments
 (0)