Skip to content

Commit e12325d

Browse files
authored
Merge pull request #762 from hallaj/631-adding-the-ability-to-block-users
closes #631, adding the support to block users
2 parents 8e64303 + 8febdec commit e12325d

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

docs/resources/user.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ resource "gitlab_user" "example" {
5151
- **projects_limit** (Number) Integer, defaults to 0. Number of projects user can create.
5252
- **reset_password** (Boolean) Boolean, defaults to false. Send user password reset link.
5353
- **skip_confirmation** (Boolean) Boolean, defaults to true. Whether to skip confirmation.
54+
- **state** (String) String, defaults to 'active'. The state of the user account. Valid values are either 'active' or 'blocked'
5455

5556
## Import
5657

internal/provider/resource_gitlab_user.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1314
gitlab "github.com/xanzy/go-gitlab"
1415
)
1516

@@ -92,6 +93,13 @@ var _ = registerResource("gitlab_user", func() *schema.Resource {
9293
Type: schema.TypeString,
9394
Optional: true,
9495
},
96+
"state": {
97+
Description: "String, defaults to 'active'. The state of the user account. Valid values are either 'active' or 'blocked'",
98+
Type: schema.TypeString,
99+
Optional: true,
100+
Default: "active",
101+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"active", "blocked"}, false)),
102+
},
95103
},
96104
}
97105
})
@@ -105,6 +113,7 @@ func resourceGitlabUserSetToState(d *schema.ResourceData, user *gitlab.User) {
105113
d.Set("is_admin", user.IsAdmin)
106114
d.Set("is_external", user.External)
107115
d.Set("note", user.Note)
116+
d.Set("state", user.State)
108117
}
109118

110119
func resourceGitlabUserCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
@@ -136,6 +145,14 @@ func resourceGitlabUserCreate(ctx context.Context, d *schema.ResourceData, meta
136145

137146
d.SetId(fmt.Sprintf("%d", user.ID))
138147

148+
if d.Get("state") == "blocked" {
149+
err := client.Users.BlockUser(user.ID)
150+
151+
if err != nil {
152+
return diag.FromErr(err)
153+
}
154+
}
155+
139156
return resourceGitlabUserRead(ctx, d, meta)
140157
}
141158

@@ -206,6 +223,22 @@ func resourceGitlabUserUpdate(ctx context.Context, d *schema.ResourceData, meta
206223
return diag.FromErr(err)
207224
}
208225

226+
if d.HasChange("state") {
227+
if d.Get("state") == "active" {
228+
err := client.Users.UnblockUser(id)
229+
230+
if err != nil {
231+
return diag.FromErr(err)
232+
}
233+
} else if d.Get("state") == "blocked" {
234+
err := client.Users.BlockUser(id)
235+
236+
if err != nil {
237+
return diag.FromErr(err)
238+
}
239+
}
240+
}
241+
209242
return resourceGitlabUserRead(ctx, d, meta)
210243
}
211244

internal/provider/resource_gitlab_user_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,33 @@ func TestAccGitlabUser_basic(t *testing.T) {
3434
Admin: false,
3535
CanCreateGroup: false,
3636
External: false,
37+
State: "active",
38+
}),
39+
),
40+
},
41+
{
42+
ResourceName: "gitlab_user.foo",
43+
ImportState: true,
44+
ImportStateVerify: true,
45+
ImportStateVerifyIgnore: []string{
46+
"password",
47+
"skip_confirmation",
48+
},
49+
},
50+
// Create a user with blocked state
51+
{
52+
Config: testAccGitlabUserConfigBlocked(rInt),
53+
Check: resource.ComposeTestCheckFunc(
54+
testAccCheckGitlabUserExists("gitlab_user.foo", &user),
55+
testAccCheckGitlabUserAttributes(&user, &testAccGitlabUserExpectedAttributes{
56+
Email: fmt.Sprintf("listest%[email protected]", rInt),
57+
Username: fmt.Sprintf("listest%d", rInt),
58+
Name: fmt.Sprintf("foo %d", rInt),
59+
ProjectsLimit: 0,
60+
Admin: false,
61+
CanCreateGroup: false,
62+
External: false,
63+
State: "blocked",
3764
}),
3865
),
3966
},
@@ -60,6 +87,34 @@ func TestAccGitlabUser_basic(t *testing.T) {
6087
CanCreateGroup: true,
6188
External: false,
6289
Note: fmt.Sprintf("note%d", rInt),
90+
State: "active",
91+
}),
92+
),
93+
},
94+
{
95+
ResourceName: "gitlab_user.foo",
96+
ImportState: true,
97+
ImportStateVerify: true,
98+
ImportStateVerifyIgnore: []string{
99+
"password",
100+
"skip_confirmation",
101+
},
102+
},
103+
// Update the user to change the state to blocked
104+
{
105+
Config: testAccGitlabUserUpdateConfigBlocked(rInt),
106+
Check: resource.ComposeTestCheckFunc(
107+
testAccCheckGitlabUserExists("gitlab_user.foo", &user),
108+
testAccCheckGitlabUserAttributes(&user, &testAccGitlabUserExpectedAttributes{
109+
Email: fmt.Sprintf("listest%[email protected]", rInt),
110+
Username: fmt.Sprintf("listest%d", rInt),
111+
Name: fmt.Sprintf("bar %d", rInt),
112+
ProjectsLimit: 10,
113+
Admin: true,
114+
CanCreateGroup: true,
115+
External: false,
116+
Note: fmt.Sprintf("note%d", rInt),
117+
State: "blocked",
63118
}),
64119
),
65120
},
@@ -85,6 +140,7 @@ func TestAccGitlabUser_basic(t *testing.T) {
85140
Admin: false,
86141
CanCreateGroup: false,
87142
External: false,
143+
State: "active",
88144
}),
89145
),
90146
},
@@ -110,6 +166,7 @@ func TestAccGitlabUser_basic(t *testing.T) {
110166
Admin: false,
111167
CanCreateGroup: false,
112168
External: false,
169+
State: "active",
113170
}),
114171
),
115172
},
@@ -135,6 +192,7 @@ func TestAccGitlabUser_basic(t *testing.T) {
135192
Admin: false,
136193
CanCreateGroup: false,
137194
External: false,
195+
State: "active",
138196
}),
139197
),
140198
},
@@ -215,6 +273,7 @@ type testAccGitlabUserExpectedAttributes struct {
215273
CanCreateGroup bool
216274
External bool
217275
Note string
276+
State string
218277
}
219278

220279
func testAccCheckGitlabUserAttributes(user *gitlab.User, want *testAccGitlabUserExpectedAttributes) resource.TestCheckFunc {
@@ -251,6 +310,10 @@ func testAccCheckGitlabUserAttributes(user *gitlab.User, want *testAccGitlabUser
251310
return fmt.Errorf("got projects_limit %d; want %d", user.ProjectsLimit, want.ProjectsLimit)
252311
}
253312

313+
if user.State != want.State {
314+
return fmt.Errorf("got state %q; want %q", user.State, want.State)
315+
}
316+
254317
return nil
255318
}
256319
}
@@ -292,6 +355,22 @@ resource "gitlab_user" "foo" {
292355
`, rInt, rInt, rInt, rInt)
293356
}
294357

358+
func testAccGitlabUserConfigBlocked(rInt int) string {
359+
return fmt.Sprintf(`
360+
resource "gitlab_user" "foo" {
361+
name = "foo %d"
362+
username = "listest%d"
363+
password = "test%dtt"
364+
email = "listest%[email protected]"
365+
is_admin = false
366+
projects_limit = 0
367+
can_create_group = false
368+
is_external = false
369+
state = "blocked"
370+
}
371+
`, rInt, rInt, rInt, rInt)
372+
}
373+
295374
func testAccGitlabUserUpdateConfig(rInt int) string {
296375
return fmt.Sprintf(`
297376
resource "gitlab_user" "foo" {
@@ -308,6 +387,23 @@ resource "gitlab_user" "foo" {
308387
`, rInt, rInt, rInt, rInt, rInt)
309388
}
310389

390+
func testAccGitlabUserUpdateConfigBlocked(rInt int) string {
391+
return fmt.Sprintf(`
392+
resource "gitlab_user" "foo" {
393+
name = "bar %d"
394+
username = "listest%d"
395+
password = "test%dtt"
396+
email = "listest%[email protected]"
397+
is_admin = true
398+
projects_limit = 10
399+
can_create_group = true
400+
is_external = false
401+
note = "note%d"
402+
state = "blocked"
403+
}
404+
`, rInt, rInt, rInt, rInt, rInt)
405+
}
406+
311407
func testAccGitlabUserUpdateConfigNoSkipConfirmation(rInt int) string {
312408
return fmt.Sprintf(`
313409
resource "gitlab_user" "foo" {

0 commit comments

Comments
 (0)