@@ -2,6 +2,7 @@ package iam
22
33import (
44 "context"
5+ "fmt"
56
67 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
78 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -28,8 +29,7 @@ func ResourceUser() *schema.Resource {
2829 "email" : {
2930 Type : schema .TypeString ,
3031 Required : true ,
31- ForceNew : true ,
32- Description : "The description of the iam user" ,
32+ Description : "The email of the user, which is not editable for guests" ,
3333 },
3434 "tags" : {
3535 Type : schema .TypeList ,
@@ -131,7 +131,7 @@ func ResourceUser() *schema.Resource {
131131
132132func createUserRequestBody (d * schema.ResourceData , isMember bool ) * iam.CreateUserRequest {
133133 if isMember {
134- // Create and return a Member user .
134+ // Create and return a member .
135135 return & iam.CreateUserRequest {
136136 OrganizationID : d .Get ("organization_id" ).(string ),
137137 Tags : types .ExpandStrings (d .Get ("tags" )),
@@ -148,7 +148,7 @@ func createUserRequestBody(d *schema.ResourceData, isMember bool) *iam.CreateUse
148148 },
149149 }
150150 } else {
151- // Create and return a Guest user .
151+ // Create and return a guest .
152152 return & iam.CreateUserRequest {
153153 OrganizationID : d .Get ("organization_id" ).(string ),
154154 Email : scw .StringPtr (d .Get ("email" ).(string )),
@@ -164,10 +164,10 @@ func resourceIamUserCreate(ctx context.Context, d *schema.ResourceData, m interf
164164 var err error
165165
166166 if d .Get ("username" ).(string ) != "" {
167- // 'Member' user
167+ // Create a member.
168168 user , err = api .CreateUser (createUserRequestBody (d , true ), scw .WithContext (ctx ))
169169 } else {
170- // 'Guest' user
170+ // Create a guest.
171171 user , err = api .CreateUser (createUserRequestBody (d , false ), scw .WithContext (ctx ))
172172 }
173173
@@ -231,7 +231,7 @@ func resourceIamUserUpdate(ctx context.Context, d *schema.ResourceData, m interf
231231 }
232232
233233 if user .Type == "guest" {
234- // Users of type ' guest' only support the update of tags.
234+ // Users of type " guest" only support the update of tags. The update of the email is not supported .
235235 if d .HasChanges ("tags" ) {
236236 _ , err = api .UpdateUser (& iam.UpdateUserRequest {
237237 UserID : user .ID ,
@@ -241,18 +241,15 @@ func resourceIamUserUpdate(ctx context.Context, d *schema.ResourceData, m interf
241241 return diag .FromErr (err )
242242 }
243243 }
244+ 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" ))
246+ }
244247 } else {
245- /*
246- * The Schema of this Terraform resource is defined so that 'email' is required and
247- * it's the "ForceNew" field. This means that providing the email of an existing user
248- * causes an update, while providing a new email causes the creation of a new user.
249- * For this reason, even though the IAM API supports it, the email is not considered
250- * an updatable field here.
251- */
252- if d .HasChanges ("tags" , "first_name" , "last_name" , "phone_number" , "locale" ) {
248+ if d .HasChanges ("tags" , "email" , "first_name" , "last_name" , "phone_number" , "locale" ) {
253249 _ , err = api .UpdateUser (& iam.UpdateUserRequest {
254250 UserID : user .ID ,
255251 Tags : types .ExpandUpdatedStringsPtr (d .Get ("tags" )),
252+ Email : scw .StringPtr (d .Get ("email" ).(string )),
256253 FirstName : scw .StringPtr (d .Get ("first_name" ).(string )),
257254 LastName : scw .StringPtr (d .Get ("last_name" ).(string )),
258255 PhoneNumber : scw .StringPtr (d .Get ("phone_number" ).(string )),
0 commit comments