From a0995f97a38f7ddf373f4841628842d555a7b4f5 Mon Sep 17 00:00:00 2001 From: Ali Chellouche Date: Fri, 5 Jan 2024 17:16:05 +0100 Subject: [PATCH 1/4] attempt at fixing an idempotence on roleparam resource --- postgresql/resource_postgresql_alter_role.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/postgresql/resource_postgresql_alter_role.go b/postgresql/resource_postgresql_alter_role.go index e5434ece..41f0e7ce 100644 --- a/postgresql/resource_postgresql_alter_role.go +++ b/postgresql/resource_postgresql_alter_role.go @@ -128,12 +128,7 @@ func readAlterRole(db QueryAble, d *schema.ResourceData) error { alterRoleID := d.Id() alterParameterKey := d.Get("parameter_key") - values := []interface{}{ - &roleName, - &roleParameters, - } - - err := db.QueryRow(getAlterRoleQuery, d.Get("role_name")).Scan(values...) + err := db.QueryRow(getAlterRoleQuery, d.Get("role_name")).Scan(&roleName, &roleParameters) switch { case err == sql.ErrNoRows: log.Printf("[WARN] PostgreSQL alter role (%q) not found", alterRoleID) @@ -148,8 +143,9 @@ func readAlterRole(db QueryAble, d *schema.ResourceData) error { d.Set("role_name", roleName) d.SetId(generateAlterRoleID(d)) - for _, v := range roleParameters { - parameter := string(v) + parameters_string := strings.TrimPrefix(strings.TrimSuffix(string(roleParameters), '}'), '{') + parameters = strings.Split(parameters_string, ",") + for _, parameter := range parameters { parameterKey := strings.Split(parameter, "=")[0] parameterValue := strings.Split(parameter, "=")[1] if parameterKey == alterParameterKey { @@ -157,7 +153,6 @@ func readAlterRole(db QueryAble, d *schema.ResourceData) error { d.Set("parameter_value", parameterValue) } } - return nil } From 3060075b35839cfdb2673ef503f28dfd46ec0056 Mon Sep 17 00:00:00 2001 From: Ali Chellouche Date: Fri, 5 Jan 2024 17:20:15 +0100 Subject: [PATCH 2/4] use utf --- postgresql/resource_postgresql_alter_role.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/postgresql/resource_postgresql_alter_role.go b/postgresql/resource_postgresql_alter_role.go index 41f0e7ce..ce087a22 100644 --- a/postgresql/resource_postgresql_alter_role.go +++ b/postgresql/resource_postgresql_alter_role.go @@ -143,7 +143,7 @@ func readAlterRole(db QueryAble, d *schema.ResourceData) error { d.Set("role_name", roleName) d.SetId(generateAlterRoleID(d)) - parameters_string := strings.TrimPrefix(strings.TrimSuffix(string(roleParameters), '}'), '{') + parameters_string := strings.TrimPrefix(strings.TrimSuffix(string(roleParameters), "}"), "{") parameters = strings.Split(parameters_string, ",") for _, parameter := range parameters { parameterKey := strings.Split(parameter, "=")[0] From 42d1062716d6f96276b3e2d518ed34861b036c5c Mon Sep 17 00:00:00 2001 From: Ali Chellouche Date: Fri, 5 Jan 2024 17:22:24 +0100 Subject: [PATCH 3/4] use string instead --- postgresql/resource_postgresql_alter_role.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/postgresql/resource_postgresql_alter_role.go b/postgresql/resource_postgresql_alter_role.go index ce087a22..e9c0667c 100644 --- a/postgresql/resource_postgresql_alter_role.go +++ b/postgresql/resource_postgresql_alter_role.go @@ -122,7 +122,7 @@ func resourcePostgreSQLAlterRoleDelete(db *DBConnection, d *schema.ResourceData) func readAlterRole(db QueryAble, d *schema.ResourceData) error { var ( roleName string - roleParameters pq.ByteaArray + roleParameters string ) alterRoleID := d.Id() @@ -144,7 +144,7 @@ func readAlterRole(db QueryAble, d *schema.ResourceData) error { d.SetId(generateAlterRoleID(d)) parameters_string := strings.TrimPrefix(strings.TrimSuffix(string(roleParameters), "}"), "{") - parameters = strings.Split(parameters_string, ",") + parameters := strings.Split(parameters_string, ",") for _, parameter := range parameters { parameterKey := strings.Split(parameter, "=")[0] parameterValue := strings.Split(parameter, "=")[1] From 4271fb79aa3ec096a35f9286823177d2dcc5e946 Mon Sep 17 00:00:00 2001 From: Ali Chellouche Date: Fri, 19 Jan 2024 16:04:36 +0100 Subject: [PATCH 4/4] more fixes --- postgresql/resource_postgresql_alter_role.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/postgresql/resource_postgresql_alter_role.go b/postgresql/resource_postgresql_alter_role.go index e9c0667c..c4de93cd 100644 --- a/postgresql/resource_postgresql_alter_role.go +++ b/postgresql/resource_postgresql_alter_role.go @@ -148,8 +148,8 @@ func readAlterRole(db QueryAble, d *schema.ResourceData) error { for _, parameter := range parameters { parameterKey := strings.Split(parameter, "=")[0] parameterValue := strings.Split(parameter, "=")[1] - if parameterKey == alterParameterKey { - d.Set("parameter_key", parameterKey) + if strings.ToLower(parameterKey) == alterParameterKey { + d.Set("parameter_key", strings.ToLower(parameterKey)) d.Set("parameter_value", parameterValue) } } @@ -201,5 +201,5 @@ func resetAlterRole(txn *sql.Tx, d *schema.ResourceData) error { } func generateAlterRoleID(d *schema.ResourceData) string { - return strings.Join([]string{d.Get("role_name").(string), d.Get("parameter_key").(string), d.Get("parameter_value").(string)}, "_") + return strings.Join([]string{d.Get("role_name").(string), d.Get("parameter_key").(string)}, "_") }