Skip to content

Commit 1cbab81

Browse files
authored
Merge pull request gitlabhq#340 from Majkl578/users-improvements
Improvements to resource_gitlab_user import
2 parents 08bba6a + 8eb4960 commit 1cbab81

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

gitlab/resource_gitlab_user.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,23 @@ func resourceGitlabUser() *schema.Resource {
1616
Update: resourceGitlabUserUpdate,
1717
Delete: resourceGitlabUserDelete,
1818
Importer: &schema.ResourceImporter{
19-
State: schema.ImportStatePassthrough,
19+
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
20+
client := meta.(*gitlab.Client)
21+
log.Printf("[DEBUG] read gitlab user %s", d.Id())
22+
23+
id, _ := strconv.Atoi(d.Id())
24+
25+
user, _, err := client.Users.GetUser(id)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
resourceGitlabUserSetToState(d, user)
31+
d.Set("email", user.Email)
32+
d.Set("is_admin", user.IsAdmin)
33+
d.Set("is_external", user.External)
34+
return []*schema.ResourceData{d}, nil
35+
},
2036
},
2137

2238
Schema: map[string]*schema.Schema{
@@ -26,7 +42,7 @@ func resourceGitlabUser() *schema.Resource {
2642
},
2743
"password": {
2844
Type: schema.TypeString,
29-
Required: true,
45+
Optional: true,
3046
Sensitive: true,
3147
},
3248
"email": {
@@ -109,7 +125,7 @@ func resourceGitlabUserCreate(d *schema.ResourceData, meta interface{}) error {
109125

110126
func resourceGitlabUserRead(d *schema.ResourceData, meta interface{}) error {
111127
client := meta.(*gitlab.Client)
112-
log.Printf("[DEBUG] read gitlab user %s", d.Id())
128+
log.Printf("[DEBUG] import -- read gitlab user %s", d.Id())
113129

114130
id, _ := strconv.Atoi(d.Id())
115131

gitlab/resource_gitlab_user_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ func TestAccGitlabUser_basic(t *testing.T) {
7474
}),
7575
),
7676
},
77+
{
78+
ResourceName: "gitlab_user.foo",
79+
ImportState: true,
80+
ImportStateVerify: true,
81+
ImportStateVerifyIgnore: []string{
82+
"password",
83+
"skip_confirmation",
84+
},
85+
},
7786
},
7887
})
7988
}

0 commit comments

Comments
 (0)