|
5 | 5 | "fmt" |
6 | 6 | "strings" |
7 | 7 |
|
| 8 | + "github.com/hashicorp/go-cty/cty" |
8 | 9 | "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
9 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" |
10 | 11 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
@@ -57,10 +58,20 @@ func ResourcePrivilege() *schema.Resource { |
57 | 58 | }, |
58 | 59 | "permission": { |
59 | 60 | Type: schema.TypeString, |
60 | | - Description: "Privilege", |
| 61 | + Description: "Desired permission (readonly, readwrite, all, custom, none)", |
61 | 62 | ValidateDiagFunc: verify.ValidateEnum[rdb.Permission](), |
62 | 63 | Required: true, |
63 | 64 | }, |
| 65 | + "effective_permission": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Description: "Actual permission currently set in Scaleway. May differ from 'permission' after database schema changes", |
| 68 | + Computed: true, |
| 69 | + }, |
| 70 | + "permission_status": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Description: "Permission synchronization status: 'synced' if effective matches desired, 'drifted' if they differ", |
| 73 | + Computed: true, |
| 74 | + }, |
64 | 75 | // Common |
65 | 76 | "region": regional.Schema(), |
66 | 77 | }, |
@@ -120,6 +131,10 @@ func ResourceRdbPrivilegeCreate(ctx context.Context, d *schema.ResourceData, m a |
120 | 131 |
|
121 | 132 | d.SetId(ResourceRdbUserPrivilegeID(region, locality.ExpandID(instanceID), databaseName, userName)) |
122 | 133 |
|
| 134 | + configuredPermission := d.Get("permission").(string) |
| 135 | + _ = d.Set("effective_permission", configuredPermission) |
| 136 | + _ = d.Set("permission_status", "synced") |
| 137 | + |
123 | 138 | return ResourceRdbPrivilegeRead(ctx, d, m) |
124 | 139 | } |
125 | 140 |
|
@@ -184,13 +199,47 @@ func ResourceRdbPrivilegeRead(ctx context.Context, d *schema.ResourceData, m any |
184 | 199 | } |
185 | 200 |
|
186 | 201 | privilege := res.Privileges[0] |
| 202 | + effectivePermission := string(privilege.Permission) |
| 203 | + configuredPermission := d.Get("permission").(string) |
| 204 | + |
187 | 205 | _ = d.Set("database_name", privilege.DatabaseName) |
188 | 206 | _ = d.Set("user_name", privilege.UserName) |
189 | | - _ = d.Set("permission", privilege.Permission) |
190 | 207 | _ = d.Set("instance_id", regional.NewIDString(region, instanceID)) |
191 | 208 | _ = d.Set("region", region) |
| 209 | + _ = d.Set("permission", privilege.Permission) |
| 210 | + _ = d.Set("effective_permission", effectivePermission) |
| 211 | + |
| 212 | + var diags diag.Diagnostics |
| 213 | + |
| 214 | + if effectivePermission != configuredPermission { |
| 215 | + _ = d.Set("permission_status", "drifted") |
| 216 | + |
| 217 | + diags = append(diags, diag.Diagnostic{ |
| 218 | + Severity: diag.Warning, |
| 219 | + Summary: "Database privilege drift detected", |
| 220 | + Detail: fmt.Sprintf( |
| 221 | + "The privilege for user '%s' on database '%s' has drifted:\n"+ |
| 222 | + " • Configured permission: '%s'\n"+ |
| 223 | + " • Effective permission: '%s'\n\n"+ |
| 224 | + "This usually happens after database schema changes (new tables, views, or sequences created).\n"+ |
| 225 | + "The configured permission was applied to objects existing at the time, but new objects created "+ |
| 226 | + "afterward don't automatically inherit these permissions.\n\n"+ |
| 227 | + "To fix this:\n"+ |
| 228 | + " 1. Run 'terraform apply' to reapply the configured permission to all objects\n"+ |
| 229 | + " 2. Or use PostgreSQL default privileges to automatically grant permissions to future objects\n"+ |
| 230 | + " 3. Or set 'permission = \"%s\"' if you want to keep the current state\n\n"+ |
| 231 | + "See: https://www.scaleway.com/en/docs/managed-databases/postgresql-and-mysql/how-to/manage-users/", |
| 232 | + userName, databaseName, |
| 233 | + configuredPermission, effectivePermission, |
| 234 | + effectivePermission, |
| 235 | + ), |
| 236 | + AttributePath: cty.GetAttrPath("permission"), |
| 237 | + }) |
| 238 | + } else { |
| 239 | + _ = d.Set("permission_status", "synced") |
| 240 | + } |
192 | 241 |
|
193 | | - return nil |
| 242 | + return diags |
194 | 243 | } |
195 | 244 |
|
196 | 245 | func ResourceRdbPrivilegeUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { |
|
0 commit comments