forked from petoju/terraform-provider-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
Handle TF drift more gracefully for ti_resource_group
and ti_resource_group_assignment
#5
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,10 +48,15 @@ func CreateOrUpdateResourceGroupUser(ctx context.Context, d *schema.ResourceData | |
var warnLevel, warnMessage string | ||
var warnCode int = 0 | ||
|
||
_, _, err = readUserFromDB(db, user) | ||
currentUser, _, err := readUserFromDB(db, user) | ||
if err != nil { | ||
d.SetId("") | ||
return diag.Errorf(`must create user first before assigning to resource group | getting user %s | error %s`, user, err) | ||
return diag.Errorf(`error during get user (%s): %s`, user, err) | ||
} | ||
|
||
if currentUser == "" { | ||
d.SetId("") | ||
return diag.Errorf(`must create user first before assigning to resource group | getting user %s | error %s`, currentUser, err) | ||
} | ||
|
||
sql := fmt.Sprintf("ALTER USER `%s` RESOURCE GROUP `%s`", user, resourceGroup) | ||
|
@@ -88,8 +93,15 @@ func ReadResourceGroupUser(ctx context.Context, d *schema.ResourceData, meta int | |
return diag.Errorf(`error getting user %s`, err) | ||
} | ||
|
||
// If the user doesn't exist, instead of erroring, recognize that there's | ||
// terraform drift and attempt to create the assignment again. | ||
if user == "" { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.Set("user", user) | ||
d.Set("resourceGroup", resourceGroup) | ||
d.Set("resource_group", resourceGroup) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was resulting in weird tf diffs - fixing so that we see the correct rg state! |
||
|
||
return nil | ||
} | ||
|
@@ -102,6 +114,7 @@ func DeleteResourceGroupUser(ctx context.Context, d *schema.ResourceData, meta i | |
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
deleteQuery := fmt.Sprintf("ALTER USER `%s` RESOURCE GROUP `default`", user) | ||
_, err = db.Exec(deleteQuery) | ||
if err != nil && !errors.Is(err, sql.ErrNoRows) { | ||
|
@@ -120,7 +133,8 @@ func readUserFromDB(db *sql.DB, name string) (string, string, error) { | |
|
||
err := row.Scan(&user, &resourceGroup) | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return "", "", sql.ErrNoRows | ||
log.Printf("[DEBUG] resource group doesn't exist (%s): %s", name, err) | ||
return "", "", nil | ||
} else if err != nil { | ||
return "", "", fmt.Errorf(`error fetching user %e`, err) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This already happens at the TiDB layer - it doesn't let you delete a resource group if it is still assigned to a user.