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

attempt at fixing an idempotence on roleparam resource #12

Closed
wants to merge 4 commits into from
Closed
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
21 changes: 8 additions & 13 deletions postgresql/resource_postgresql_alter_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,13 @@ 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()
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)
Expand All @@ -148,16 +143,16 @@ 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 {
d.Set("parameter_key", parameterKey)
if strings.ToLower(parameterKey) == alterParameterKey {
d.Set("parameter_key", strings.ToLower(parameterKey))
d.Set("parameter_value", parameterValue)
}
}

return nil
}

Expand Down Expand Up @@ -206,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)}, "_")
}
Loading