Skip to content

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 2 commits into from
Jul 23, 2024
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/petoju/terraform-provider-mysql/v3
module github.com/zph/terraform-provider-mysql/v3

require (
cloud.google.com/go/cloudsqlconn v1.11.0
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
"github.com/petoju/terraform-provider-mysql/v3/mysql"
"github.com/zph/terraform-provider-mysql/v3/mysql"
)

func main() {
Expand Down
20 changes: 12 additions & 8 deletions mysql/resource_ti_resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"errors"
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -172,12 +173,14 @@ func ReadResourceGroup(ctx context.Context, d *schema.ResourceData, meta interfa
return diag.Errorf("error during get resource group (%s): %s", d.Id(), err)
}

if err != nil {
// If we're not able to find the resource group, assume that there's terraform
// diff and allow terraform to recreate it instead of throwing an error.
if rg == nil {
d.SetId("")
return diag.Errorf(`error converting burstable value from tidb %e`, err)
return nil
}

setResourceGroupOnResourceData(rg, d)
setResourceGroupOnResourceData(*rg, d)
return nil
}

Expand All @@ -188,7 +191,7 @@ func DeleteResourceGroup(ctx context.Context, d *schema.ResourceData, meta inter
if err != nil {
return diag.FromErr(err)
}
// TODO: check for users assigned as safety? and assert zero?
Copy link
Collaborator Author

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.


deleteQuery := fmt.Sprintf("DROP RESOURCE GROUP IF EXISTS %s", name)
_, err = db.Exec(deleteQuery)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
Expand All @@ -199,7 +202,7 @@ func DeleteResourceGroup(ctx context.Context, d *schema.ResourceData, meta inter
return nil
}

func getResourceGroupFromDB(db *sql.DB, name string) (ResourceGroup, error) {
func getResourceGroupFromDB(db *sql.DB, name string) (*ResourceGroup, error) {
rg := ResourceGroup{Name: name}

/*
Expand All @@ -216,12 +219,13 @@ func getResourceGroupFromDB(db *sql.DB, name string) (ResourceGroup, error) {

err := db.QueryRow(query, name).Scan(&rg.Name, &rg.ResourceUnits, &rg.Priority, &rg.Burstable, &rg.QueryLimit)
if errors.Is(err, sql.ErrNoRows) {
return ResourceGroup{}, fmt.Errorf("resource group doesn't exist (%s): %s", name, err)
log.Printf("[DEBUG] resource group doesn't exist (%s): %s", name, err)
return nil, nil
} else if err != nil {
return ResourceGroup{}, fmt.Errorf("error during get resource group (%s): %s", name, err)
return nil, fmt.Errorf("error during get resource group (%s): %s", name, err)
}

return rg, nil
return &rg, nil
}

func NewResourceGroupFromResourceData(d *schema.ResourceData) ResourceGroup {
Expand Down
22 changes: 18 additions & 4 deletions mysql/resource_ti_resource_group_user_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
}
Expand All @@ -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) {
Expand All @@ -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)
}
Expand Down