1
1
package postgresql
2
2
3
3
import (
4
+ "context"
4
5
"crypto/sha1"
5
6
"encoding/hex"
6
7
"fmt"
7
8
"log"
8
9
"time"
9
10
11
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10
12
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11
13
)
12
14
13
15
const (
14
16
scriptCommandsAttr = "commands"
15
17
scriptTriesAttr = "tries"
16
18
scriptBackoffDelayAttr = "backoff_delay"
19
+ scriptTimeoutAttr = "timeout"
17
20
scriptShasumAttr = "shasum"
18
21
)
19
22
20
23
func resourcePostgreSQLScript () * schema.Resource {
21
24
return & schema.Resource {
22
- Create : PGResourceFunc (resourcePostgreSQLScriptCreateOrUpdate ),
23
- Read : PGResourceFunc (resourcePostgreSQLScriptRead ),
24
- Update : PGResourceFunc (resourcePostgreSQLScriptCreateOrUpdate ),
25
- Delete : PGResourceFunc (resourcePostgreSQLScriptDelete ),
25
+ CreateContext : PGResourceContextFunc (resourcePostgreSQLScriptCreateOrUpdate ),
26
+ Read : PGResourceFunc (resourcePostgreSQLScriptRead ),
27
+ UpdateContext : PGResourceContextFunc (resourcePostgreSQLScriptCreateOrUpdate ),
28
+ Delete : PGResourceFunc (resourcePostgreSQLScriptDelete ),
26
29
27
30
Schema : map [string ]* schema.Schema {
28
31
scriptCommandsAttr : {
@@ -45,6 +48,12 @@ func resourcePostgreSQLScript() *schema.Resource {
45
48
Default : 1 ,
46
49
Description : "Number of seconds between two tries of the batch of commands" ,
47
50
},
51
+ scriptTimeoutAttr : {
52
+ Type : schema .TypeInt ,
53
+ Optional : true ,
54
+ Default : 5 * 60 ,
55
+ Description : "Number of seconds for a batch of command to timeout" ,
56
+ },
48
57
scriptShasumAttr : {
49
58
Type : schema .TypeString ,
50
59
Computed : true ,
@@ -54,19 +63,28 @@ func resourcePostgreSQLScript() *schema.Resource {
54
63
}
55
64
}
56
65
57
- func resourcePostgreSQLScriptCreateOrUpdate (db * DBConnection , d * schema.ResourceData ) error {
66
+ func resourcePostgreSQLScriptCreateOrUpdate (ctx context. Context , db * DBConnection , d * schema.ResourceData ) diag. Diagnostics {
58
67
commands , err := toStringArray (d .Get (scriptCommandsAttr ).([]any ))
59
68
tries := d .Get (scriptTriesAttr ).(int )
60
69
backoffDelay := d .Get (scriptBackoffDelayAttr ).(int )
70
+ timeout := d .Get (scriptTimeoutAttr ).(int )
61
71
62
72
if err != nil {
63
- return err
73
+ return diag.Diagnostics {diag.Diagnostic {
74
+ Severity : diag .Error ,
75
+ Summary : "Commands input is not valid" ,
76
+ Detail : err .Error (),
77
+ }}
64
78
}
65
79
66
80
sum := shasumCommands (commands )
67
81
68
- if err := executeCommands (db , commands , tries , backoffDelay ); err != nil {
69
- return err
82
+ if err := executeCommands (ctx , db , commands , tries , backoffDelay , timeout ); err != nil {
83
+ return diag.Diagnostics {diag.Diagnostic {
84
+ Severity : diag .Error ,
85
+ Summary : "Commands execution failed" ,
86
+ Detail : err .Error (),
87
+ }}
70
88
}
71
89
72
90
d .Set (scriptShasumAttr , sum )
@@ -89,9 +107,9 @@ func resourcePostgreSQLScriptDelete(db *DBConnection, d *schema.ResourceData) er
89
107
return nil
90
108
}
91
109
92
- func executeCommands (db * DBConnection , commands []string , tries int , backoffDelay int ) error {
110
+ func executeCommands (ctx context. Context , db * DBConnection , commands []string , tries int , backoffDelay int , timeout int ) error {
93
111
for try := 1 ; ; try ++ {
94
- err := executeBatch (db , commands )
112
+ err := executeBatch (ctx , db , commands , timeout )
95
113
if err == nil {
96
114
return nil
97
115
} else {
@@ -103,10 +121,12 @@ func executeCommands(db *DBConnection, commands []string, tries int, backoffDela
103
121
}
104
122
}
105
123
106
- func executeBatch (db * DBConnection , commands []string ) error {
124
+ func executeBatch (ctx context.Context , db * DBConnection , commands []string , timeout int ) error {
125
+ timeoutContext , timeoutCancel := context .WithTimeout (ctx , time .Duration (timeout )* time .Second )
126
+ defer timeoutCancel ()
107
127
for _ , command := range commands {
108
128
log .Printf ("[DEBUG] Executing %s" , command )
109
- _ , err := db .Exec ( command )
129
+ _ , err := db .ExecContext ( timeoutContext , command )
110
130
log .Printf ("[DEBUG] Result %s: %v" , command , err )
111
131
if err != nil {
112
132
log .Println ("[DEBUG] Error catched:" , err )
0 commit comments