Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DXCDT-351: Only send changed fields when updating a user #453

Merged
merged 2 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/resources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ resource "auth0_role" "admin" {
- `blocked` (Boolean) Indicates whether the user is blocked or not.
- `email` (String) Email address of the user.
- `email_verified` (Boolean) Indicates whether the email address has been verified.
- `family_name` (String) Family name of the user.
- `given_name` (String) Given name of the user.
- `name` (String) Name of the user.
- `nickname` (String) Preferred nickname or alias of the user.
- `family_name` (String) Family name of the user. This value can only be updated if the connection is a database connection (using the Auth0 store), a passwordless connection (email or sms) or has disabled 'Sync user profile attributes at each login'. For more information, see: [Configure Identity Provider Connection for User Profile Updates](https://auth0.com/docs/manage-users/user-accounts/user-profiles/configure-connection-sync-with-auth0).
- `given_name` (String) Given name of the user. This value can only be updated if the connection is a database connection (using the Auth0 store), a passwordless connection (email or sms) or has disabled 'Sync user profile attributes at each login'. For more information, see: [Configure Identity Provider Connection for User Profile Updates](https://auth0.com/docs/manage-users/user-accounts/user-profiles/configure-connection-sync-with-auth0).
- `name` (String) Name of the user. This value can only be updated if the connection is a database connection (using the Auth0 store), a passwordless connection (email or sms) or has disabled 'Sync user profile attributes at each login'. For more information, see: [Configure Identity Provider Connection for User Profile Updates](https://auth0.com/docs/manage-users/user-accounts/user-profiles/configure-connection-sync-with-auth0).
- `nickname` (String) Preferred nickname or alias of the user. This value can only be updated if the connection is a database connection (using the Auth0 store), a passwordless connection (email or sms) or has disabled 'Sync user profile attributes at each login'. For more information, see: [Configure Identity Provider Connection for User Profile Updates](https://auth0.com/docs/manage-users/user-accounts/user-profiles/configure-connection-sync-with-auth0).
- `password` (String, Sensitive) Initial password for this user. Required for non-passwordless connections (SMS and email).
- `phone_number` (String) Phone number for the user; follows the E.164 recommendation. Used for SMS connections.
- `phone_verified` (Boolean) Indicates whether the phone number has been verified.
- `picture` (String) Picture of the user.
- `picture` (String) Picture of the user. This value can only be updated if the connection is a database connection (using the Auth0 store), a passwordless connection (email or sms) or has disabled 'Sync user profile attributes at each login'. For more information, see: [Configure Identity Provider Connection for User Profile Updates](https://auth0.com/docs/manage-users/user-accounts/user-profiles/configure-connection-sync-with-auth0).
- `roles` (Set of String) Set of IDs of roles assigned to the user.
- `user_id` (String) ID of the user.
- `user_metadata` (String) Custom fields that store info about the user that does not impact a user's core functionality. Examples include work address, home address, and user preferences.
Expand Down
98 changes: 62 additions & 36 deletions internal/provider/resource_auth0_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,38 @@ func newUser() *schema.Resource {
Description: "Username of the user. Only valid if the connection requires a username.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Name of the user.",
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Name of the user. This value can only be updated if the connection is a " +
"database connection (using the Auth0 store), a passwordless connection (email or sms) or " +
"has disabled 'Sync user profile attributes at each login'. For more information, see: " +
"[Configure Identity Provider Connection for User Profile Updates](https://auth0.com/docs/manage-users/user-accounts/user-profiles/configure-connection-sync-with-auth0).",
},
"family_name": {
Type: schema.TypeString,
Optional: true,
Description: "Family name of the user.",
Type: schema.TypeString,
Optional: true,
Description: "Family name of the user. This value can only be updated if the connection is a " +
"database connection (using the Auth0 store), a passwordless connection (email or sms) or " +
"has disabled 'Sync user profile attributes at each login'. For more information, see: " +
"[Configure Identity Provider Connection for User Profile Updates](https://auth0.com/docs/manage-users/user-accounts/user-profiles/configure-connection-sync-with-auth0).",
},
"given_name": {
Type: schema.TypeString,
Optional: true,
Description: "Given name of the user.",
Type: schema.TypeString,
Optional: true,
Description: "Given name of the user. This value can only be updated if the connection is a " +
"database connection (using the Auth0 store), a passwordless connection (email or sms) or " +
"has disabled 'Sync user profile attributes at each login'. For more information, see: " +
"[Configure Identity Provider Connection for User Profile Updates](https://auth0.com/docs/manage-users/user-accounts/user-profiles/configure-connection-sync-with-auth0).",
},
"nickname": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Preferred nickname or alias of the user.",
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Preferred nickname or alias of the user. This value can only be updated if the connection is a " +
"database connection (using the Auth0 store), a passwordless connection (email or sms) or " +
"has disabled 'Sync user profile attributes at each login'. For more information, see: " +
"[Configure Identity Provider Connection for User Profile Updates](https://auth0.com/docs/manage-users/user-accounts/user-profiles/configure-connection-sync-with-auth0).",
},
"password": {
Type: schema.TypeString,
Expand Down Expand Up @@ -126,10 +138,13 @@ func newUser() *schema.Resource {
Description: "Indicates whether the user is blocked or not.",
},
"picture": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Picture of the user.",
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Picture of the user. This value can only be updated if the connection is a " +
"database connection (using the Auth0 store), a passwordless connection (email or sms) or " +
"has disabled 'Sync user profile attributes at each login'. For more information, see: " +
"[Configure Identity Provider Connection for User Profile Updates](https://auth0.com/docs/manage-users/user-accounts/user-profiles/configure-connection-sync-with-auth0).",
},
"roles": {
Type: schema.TypeSet,
Expand Down Expand Up @@ -259,49 +274,60 @@ func deleteUser(ctx context.Context, d *schema.ResourceData, m interface{}) diag
func expandUser(d *schema.ResourceData) (*management.User, error) {
config := d.GetRawConfig()

user := &management.User{
Connection: value.String(config.GetAttr("connection_name")),
Name: value.String(config.GetAttr("name")),
GivenName: value.String(config.GetAttr("given_name")),
FamilyName: value.String(config.GetAttr("family_name")),
Nickname: value.String(config.GetAttr("nickname")),
Picture: value.String(config.GetAttr("picture")),
Blocked: value.Bool(config.GetAttr("blocked")),
}
user := &management.User{}

if d.IsNewResource() {
user.ID = value.String(config.GetAttr("user_id"))
}
if d.IsNewResource() || d.HasChange("email") {
Copy link
Contributor Author

@sergiught sergiught Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The d.IsNewResource is unnecessary as the d.HasChange will also trigger when this is a new resource needing to be created.

if d.HasChange("email") {
user.Email = value.String(config.GetAttr("email"))
}
if d.IsNewResource() || d.HasChange("username") {
if d.HasChange("username") {
user.Username = value.String(config.GetAttr("username"))
}
if d.IsNewResource() || d.HasChange("password") {
if d.HasChange("password") {
user.Password = value.String(config.GetAttr("password"))
}
if d.IsNewResource() || d.HasChange("phone_number") {
if d.HasChange("phone_number") {
user.PhoneNumber = value.String(config.GetAttr("phone_number"))
}
if d.IsNewResource() || d.HasChange("email_verified") {
if d.HasChange("email_verified") {
user.EmailVerified = value.Bool(config.GetAttr("email_verified"))
}
if d.IsNewResource() || d.HasChange("verify_email") {
if d.HasChange("verify_email") {
user.VerifyEmail = value.Bool(config.GetAttr("verify_email"))
}
if d.IsNewResource() || d.HasChange("phone_verified") {
if d.HasChange("phone_verified") {
user.PhoneVerified = value.Bool(config.GetAttr("phone_verified"))
}

if d.HasChange("given_name") {
user.GivenName = value.String(config.GetAttr("given_name"))
}
if d.HasChange("family_name") {
user.FamilyName = value.String(config.GetAttr("family_name"))
}
if d.HasChange("nickname") {
user.Nickname = value.String(config.GetAttr("nickname"))
}
if d.HasChange("name") {
user.Name = value.String(config.GetAttr("name"))
}
if d.HasChange("picture") {
user.Picture = value.String(config.GetAttr("picture"))
}
if d.HasChange("blocked") {
user.Blocked = value.Bool(config.GetAttr("blocked"))
}
if d.HasChange("connection_name") {
user.Connection = value.String(config.GetAttr("connection_name"))
}
if d.HasChange("user_metadata") {
userMetadata, err := expandMetadata(d, "user")
if err != nil {
return nil, err
}
user.UserMetadata = &userMetadata
}

if d.HasChange("app_metadata") {
appMetadata, err := expandMetadata(d, "app")
if err != nil {
Expand Down