-
Notifications
You must be signed in to change notification settings - Fork 1
add postgresql_script resource #18
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
Changes from 1 commit
a35a285
48ac715
aafff3b
255d84e
2e843f4
e1a4937
3f3e83f
fc353f8
faa3740
0ece4b4
b46675b
0d873a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package postgresql | ||
|
||
import ( | ||
"crypto/sha1" | ||
"encoding/hex" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
const ( | ||
scriptCommandsAttr = "commands" | ||
scriptTriesAttr = "tries" | ||
scriptTimeoutAttr = "timeout" | ||
scriptShasumAttr = "shasum" | ||
) | ||
|
||
func resourcePostgreSQLScript() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: PGResourceFunc(resourcePostgreSQLScriptCreateOrUpdate), | ||
Read: PGResourceFunc(resourcePostgreSQLScriptRead), | ||
Update: PGResourceFunc(resourcePostgreSQLScriptCreateOrUpdate), | ||
Delete: PGResourceFunc(resourcePostgreSQLScriptDelete), | ||
|
||
Schema: map[string]*schema.Schema{ | ||
scriptCommandsAttr: { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Description: "List of SQL commands to execute", | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
scriptTriesAttr: { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Default: 1, | ||
Description: "Number of tries for a failing command", | ||
}, | ||
scriptTimeoutAttr: { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Default: 1, | ||
Description: "Number of seconds between two tries for a command", | ||
antoineverin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}, | ||
scriptShasumAttr: { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Shasum of commands", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourcePostgreSQLScriptCreateOrUpdate(db *DBConnection, d *schema.ResourceData) error { | ||
commands := d.Get(scriptCommandsAttr).([]any) | ||
tries := d.Get(scriptTriesAttr).(int) | ||
timeout := d.Get(scriptTimeoutAttr).(int) | ||
|
||
sum := shasumCommands(commands) | ||
|
||
if err := executeCommands(db, commands, tries, timeout); err != nil { | ||
return err | ||
} | ||
|
||
d.Set(scriptShasumAttr, sum) | ||
d.SetId(sum) | ||
return nil | ||
} | ||
|
||
func resourcePostgreSQLScriptRead(db *DBConnection, d *schema.ResourceData) error { | ||
commands := d.Get(scriptCommandsAttr).([]any) | ||
newSum := shasumCommands(commands) | ||
d.Set(scriptShasumAttr, newSum) | ||
|
||
return nil | ||
} | ||
|
||
func resourcePostgreSQLScriptDelete(db *DBConnection, d *schema.ResourceData) error { | ||
return nil | ||
} | ||
|
||
func executeCommands(db *DBConnection, commands []any, tries int, timeout int) error { | ||
for _, command := range commands { | ||
for i := 1; ; i++ { | ||
|
||
_, err := db.Query(command.(string)) | ||
bpaquet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if err != nil { | ||
if i >= tries { | ||
return err | ||
} | ||
time.Sleep(time.Duration(timeout) * time.Second) | ||
continue | ||
} | ||
break | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func shasumCommands(commands []any) string { | ||
sha := sha1.New() | ||
for _, command := range commands { | ||
sha.Write([]byte(command.(string))) | ||
} | ||
return hex.EncodeToString(sha.Sum(nil)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package postgresql | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccPostgresqlScript_basic(t *testing.T) { | ||
config := ` | ||
resource "postgresql_script" "test" { | ||
commands = [ | ||
"SELECT 1;" | ||
] | ||
tries = 2 | ||
timeout = 4 | ||
} | ||
` | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 1;"), | ||
resource.TestCheckResourceAttr("postgresql_script.test", "tries", "2"), | ||
resource.TestCheckResourceAttr("postgresql_script.test", "timeout", "4"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccPostgresqlScript_multiple(t *testing.T) { | ||
config := ` | ||
resource "postgresql_script" "test" { | ||
commands = [ | ||
"SELECT 1;", | ||
"SELECT 2;", | ||
"SELECT 3;" | ||
] | ||
tries = 2 | ||
timeout = 4 | ||
} | ||
` | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 1;"), | ||
resource.TestCheckResourceAttr("postgresql_script.test", "commands.1", "SELECT 2;"), | ||
resource.TestCheckResourceAttr("postgresql_script.test", "commands.2", "SELECT 3;"), | ||
resource.TestCheckResourceAttr("postgresql_script.test", "tries", "2"), | ||
resource.TestCheckResourceAttr("postgresql_script.test", "timeout", "4"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccPostgresqlScript_default(t *testing.T) { | ||
config := ` | ||
resource "postgresql_script" "test" { | ||
commands = [ | ||
"SELECT 1;" | ||
] | ||
} | ||
` | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 1;"), | ||
resource.TestCheckResourceAttr("postgresql_script.test", "tries", "1"), | ||
resource.TestCheckResourceAttr("postgresql_script.test", "timeout", "1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccPostgresqlScript_reapply(t *testing.T) { | ||
config := ` | ||
resource "postgresql_script" "test" { | ||
commands = [ | ||
"SELECT 1;" | ||
] | ||
} | ||
` | ||
|
||
configChange := ` | ||
resource "postgresql_script" "test" { | ||
commands = [ | ||
"SELECT 2;" | ||
] | ||
} | ||
` | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 1;"), | ||
), | ||
}, | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 1;"), | ||
), | ||
}, | ||
{ | ||
Config: configChange, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("postgresql_script.test", "commands.0", "SELECT 2;"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccPostgresqlScript_fail(t *testing.T) { | ||
config := ` | ||
resource "postgresql_script" "invalid" { | ||
commands = [ | ||
"SLC FROM nowhere;" | ||
] | ||
tries = 2 | ||
timeout = 2 | ||
} | ||
` | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
ExpectError: regexp.MustCompile("syntax error"), | ||
}, | ||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,46 @@ | ||||||
--- | ||||||
bpaquet marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
layout: "postgresql" | ||||||
page_title: "PostgreSQL: postgresql_script" | ||||||
sidebar_current: "docs-postgresql-resource-postgresql_script" | ||||||
description: |- | ||||||
Execute a SQL scipt | ||||||
bpaquet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
--- | ||||||
|
||||||
# postgresql\_script | ||||||
|
||||||
The ``postgresql_script`` execute a script given as parameter. | ||||||
|
The ``postgresql_script`` execute a script given as parameter. | |
The ``postgresql_script`` execute a script given as parameter. This script will be executed each time it changes. |
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.
that's not really a timeout. More a backoff ?
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.
+1, can you rename?