Skip to content

Commit fc50f37

Browse files
MonitobCodelax
authored andcommitted
feat: add user tags option
1 parent e1cce41 commit fc50f37

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

internal/services/iam/user.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func ResourceUser() *schema.Resource {
1616
return &schema.Resource{
1717
CreateContext: resourceIamUserCreate,
1818
ReadContext: resourceIamUserRead,
19+
UpdateContext: resourceIamUserUpdate,
1920
DeleteContext: resourceIamUserDelete,
2021
Importer: &schema.ResourceImporter{
2122
StateContext: schema.ImportStatePassthroughContext,
@@ -28,6 +29,14 @@ func ResourceUser() *schema.Resource {
2829
ForceNew: true,
2930
Description: "The description of the iam user",
3031
},
32+
"tags": {
33+
Type: schema.TypeList,
34+
Elem: &schema.Schema{
35+
Type: schema.TypeString,
36+
},
37+
Optional: true,
38+
Description: "The tags associated with the user",
39+
},
3140
"created_at": {
3241
Type: schema.TypeString,
3342
Computed: true,
@@ -79,6 +88,7 @@ func resourceIamUserCreate(ctx context.Context, d *schema.ResourceData, m interf
7988
user, err := api.CreateUser(&iam.CreateUserRequest{
8089
OrganizationID: d.Get("organization_id").(string),
8190
Email: &email,
91+
Tags: types.ExpandStrings(d.Get("tags")),
8292
}, scw.WithContext(ctx))
8393
if err != nil {
8494
return diag.FromErr(err)
@@ -107,6 +117,7 @@ func resourceIamUserRead(ctx context.Context, d *schema.ResourceData, m interfac
107117
_ = d.Set("updated_at", types.FlattenTime(user.UpdatedAt))
108118
_ = d.Set("organization_id", user.OrganizationID)
109119
_ = d.Set("deletable", user.Deletable)
120+
_ = d.Set("tags", types.FlattenSliceString(group.Tags))
110121
_ = d.Set("last_login_at", types.FlattenTime(user.LastLoginAt))
111122
_ = d.Set("type", user.Type)
112123
_ = d.Set("status", user.Status)
@@ -115,6 +126,29 @@ func resourceIamUserRead(ctx context.Context, d *schema.ResourceData, m interfac
115126
return nil
116127
}
117128

129+
func resourceIamUserUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
130+
api := NewAPI(m)
131+
132+
user, err := api.GetUser(&iam.GetUserRequest{
133+
UserID: d.Id(),
134+
}, scw.WithContext(ctx))
135+
if err != nil {
136+
return diag.FromErr(err)
137+
}
138+
139+
if d.HasChanges("tags") {
140+
_, err = api.UpdateUser(&iam.UpdateUserRequest{
141+
UserID: user.ID,
142+
Tags: types.ExpandUpdatedStringsPtr(d.Get("tags")),
143+
}, scw.WithContext(ctx))
144+
if err != nil {
145+
return diag.FromErr(err)
146+
}
147+
}
148+
149+
return resourceIamUserRead(ctx, d, m)
150+
}
151+
118152
func resourceIamUserDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
119153
api := NewAPI(m)
120154

internal/services/iam/user_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ func TestAccUser_Basic(t *testing.T) {
2323
Config: `
2424
resource "scaleway_iam_user" "user_basic" {
2525
26+
tags = ["tf_tests", "tests"]
2627
}
2728
`,
2829
Check: resource.ComposeTestCheckFunc(
2930
testAccCheckIamUserExists(tt, "scaleway_iam_user.user_basic"),
3031
acctest.CheckResourceAttrUUID("scaleway_iam_user.user_basic", "id"),
3132
resource.TestCheckResourceAttr("scaleway_iam_user.user_basic", "email", "[email protected]"),
33+
resource.TestCheckResourceAttr("scaleway_iam_user.user_basic", "tags.#", "2"),
34+
resource.TestCheckResourceAttr("scaleway_iam_user.user_basic", "tags.0", "tf_tests"),
35+
resource.TestCheckResourceAttr("scaleway_iam_user.user_basic", "tags.1", "tests"),
3236
),
3337
},
3438
},

0 commit comments

Comments
 (0)