@@ -2,7 +2,7 @@ package iam
22
33import (
44 "context"
5- "fmt "
5+ "errors "
66
77 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
88 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -13,6 +13,8 @@ import (
1313 "github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
1414)
1515
16+ var descParameterIgnoredForGuest = " (this parameter is ignored in case of guest users)"
17+
1618func ResourceUser () * schema.Resource {
1719 return & schema.Resource {
1820 CreateContext : resourceIamUserCreate ,
@@ -40,44 +42,44 @@ func ResourceUser() *schema.Resource {
4042 "send_password_email" : {
4143 Type : schema .TypeBool ,
4244 Optional : true ,
43- Description : "Whether or not to send an email containing the member's password" ,
45+ Description : "Whether or not to send an email containing the member's password" + descParameterIgnoredForGuest ,
4446 },
4547 "send_welcome_email" : {
4648 Type : schema .TypeBool ,
4749 Optional : true ,
48- Description : "Whether or not to send a welcome email that includes onboarding information" ,
50+ Description : "Whether or not to send a welcome email that includes onboarding information" + descParameterIgnoredForGuest ,
4951 },
5052 "username" : {
5153 Type : schema .TypeString ,
5254 Optional : true ,
5355 Computed : true ,
54- Description : "The member's username" ,
56+ Description : "The member's username" + descParameterIgnoredForGuest ,
5557 },
5658 "password" : {
5759 Type : schema .TypeString ,
5860 Optional : true ,
59- Description : "The member's password for first access" ,
61+ Description : "The member's password for first access" + descParameterIgnoredForGuest ,
6062 },
6163 "first_name" : {
6264 Type : schema .TypeString ,
6365 Optional : true ,
64- Description : "The member's first name" ,
66+ Description : "The member's first name" + descParameterIgnoredForGuest ,
6567 },
6668 "last_name" : {
6769 Type : schema .TypeString ,
6870 Optional : true ,
69- Description : "The member's last name" ,
71+ Description : "The member's last name" + descParameterIgnoredForGuest ,
7072 },
7173 "phone_number" : {
7274 Type : schema .TypeString ,
7375 Optional : true ,
74- Description : "The member's phone number" ,
76+ Description : "The member's phone number" + descParameterIgnoredForGuest ,
7577 },
7678 "locale" : {
7779 Type : schema .TypeString ,
7880 Optional : true ,
7981 Computed : true ,
80- Description : "The member's locale" ,
82+ Description : "The member's locale" + descParameterIgnoredForGuest ,
8183 },
8284 // Computed data
8385 "created_at" : {
@@ -161,6 +163,7 @@ func resourceIamUserCreate(ctx context.Context, d *schema.ResourceData, m interf
161163 api := NewAPI (m )
162164
163165 var user * iam.User
166+
164167 var err error
165168
166169 if d .Get ("username" ).(string ) != "" {
@@ -186,7 +189,6 @@ func resourceIamUserRead(ctx context.Context, d *schema.ResourceData, m interfac
186189 user , err := api .GetUser (& iam.GetUserRequest {
187190 UserID : d .Id (),
188191 }, scw .WithContext (ctx ))
189-
190192 if err != nil {
191193 if httperrors .Is404 (err ) {
192194 d .SetId ("" )
@@ -241,23 +243,28 @@ func resourceIamUserUpdate(ctx context.Context, d *schema.ResourceData, m interf
241243 return diag .FromErr (err )
242244 }
243245 }
246+
244247 if d .HasChange ("email" ) {
245- return diag .FromErr (fmt . Errorf ("the email of a guest user cannot be updated, you need to create a new user" ))
248+ return diag .FromErr (errors . New ("the email of a guest user cannot be updated, you need to create a new user" ))
246249 }
247250 } else {
248- if d .HasChanges ("tags" , "email" , "first_name" , "last_name" , "phone_number" , "locale" ) {
249- _ , err = api .UpdateUser (& iam.UpdateUserRequest {
250- UserID : user .ID ,
251- Tags : types .ExpandUpdatedStringsPtr (d .Get ("tags" )),
252- Email : scw .StringPtr (d .Get ("email" ).(string )),
253- FirstName : scw .StringPtr (d .Get ("first_name" ).(string )),
254- LastName : scw .StringPtr (d .Get ("last_name" ).(string )),
255- PhoneNumber : scw .StringPtr (d .Get ("phone_number" ).(string )),
256- Locale : scw .StringPtr (d .Get ("locale" ).(string )),
257- }, scw .WithContext (ctx ))
258- if err != nil {
259- return diag .FromErr (err )
260- }
251+ req := & iam.UpdateUserRequest {UserID : user .ID }
252+
253+ if d .HasChanges ("tags" , "email" , "first_name" ) {
254+ req .Tags = types .ExpandUpdatedStringsPtr (d .Get ("tags" ))
255+ req .Email = scw .StringPtr (d .Get ("email" ).(string ))
256+ req .FirstName = scw .StringPtr (d .Get ("first_name" ).(string ))
257+ }
258+
259+ if d .HasChanges ("last_name" , "phone_number" , "locale" ) {
260+ req .LastName = scw .StringPtr (d .Get ("last_name" ).(string ))
261+ req .PhoneNumber = scw .StringPtr (d .Get ("phone_number" ).(string ))
262+ req .Locale = scw .StringPtr (d .Get ("locale" ).(string ))
263+ }
264+
265+ _ , err = api .UpdateUser (req , scw .WithContext (ctx ))
266+ if err != nil {
267+ return diag .FromErr (err )
261268 }
262269 // The update of the 'username' field is made through a different endpoint and payload.
263270 if d .HasChange ("username" ) {
@@ -270,6 +277,7 @@ func resourceIamUserUpdate(ctx context.Context, d *schema.ResourceData, m interf
270277 }
271278 }
272279 }
280+
273281 return resourceIamUserRead (ctx , d , m )
274282}
275283
0 commit comments