Skip to content

Commit 8eb4960

Browse files
sendmalwhereMajkl578
authored andcommitted
Improvements to resource_gitlab_user Import (#193)
This change makes three improvements to the gitlab_user resource, resolving Issue #193. Change #1: The `password` attribute for gitlab_user has been changed from required to optional. There is no endpoint on the Gitlab `user` API for `password` (see: https://docs.gitlab.com/ee/api/users.html), so it is impossible for us to ever read this attribute to set state. The field should therefore only be necessary when we are performing a Create or Update action on a gitlab_user. Change #2: The resource's State Importer originally used `ImportStatePassthrough`, which essentially ensured that `terraform import` could only read the fields defined in the default Read method (in this case, `username`, `name`, `can_create_group`, and `projects_limit`, all of which are called in `resourceGitlabUserSetToState`, inside `resourceGitlabUserRead`). I have replaced the `ImportStatePassthrough` with an anonymous function that reuses `resourceGitlabUserSetToState` and additionally sets `email`, `is_admin`, and `is_external`. I did not modify the fields set in `resourceGitlabUserSetToState` because that function is intended to provide only a basic level of detail about a user (i.e., all the data we'd be able to pull for a user if we didn't have a token with Admin privileges). These fields can be easily extended in the future if need be. Change #3: The test for the gitlab_user resource has been updated to include the mandatory ImportState and ImportStateVerify elements in the final stage, per the Terraform `import` implementation documentation at https://www.terraform.io/docs/extend/resources/import.html. The `password` and `skip_confirmation` attributes are ignored because it is not possible to read them from the API (they are not part of the Read schema: https://docs.gitlab.com/ee/api/users.html)
1 parent 08bba6a commit 8eb4960

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)