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
@@ -19,10 +21,10 @@ const (
19
21
20
22
func resourcePostgreSQLScript () * schema.Resource {
21
23
return & schema.Resource {
22
- Create : PGResourceFunc (resourcePostgreSQLScriptCreateOrUpdate ),
23
- Read : PGResourceFunc (resourcePostgreSQLScriptRead ),
24
- Update : PGResourceFunc (resourcePostgreSQLScriptCreateOrUpdate ),
25
- Delete : PGResourceFunc (resourcePostgreSQLScriptDelete ),
24
+ CreateContext : PGResourceContextFunc (resourcePostgreSQLScriptCreateOrUpdate ),
25
+ Read : PGResourceFunc (resourcePostgreSQLScriptRead ),
26
+ UpdateContext : PGResourceContextFunc (resourcePostgreSQLScriptCreateOrUpdate ),
27
+ Delete : PGResourceFunc (resourcePostgreSQLScriptDelete ),
26
28
27
29
Schema : map [string ]* schema.Schema {
28
30
scriptCommandsAttr : {
@@ -54,19 +56,27 @@ func resourcePostgreSQLScript() *schema.Resource {
54
56
}
55
57
}
56
58
57
- func resourcePostgreSQLScriptCreateOrUpdate (db * DBConnection , d * schema.ResourceData ) error {
59
+ func resourcePostgreSQLScriptCreateOrUpdate (ctx context. Context , db * DBConnection , d * schema.ResourceData ) diag. Diagnostics {
58
60
commands , err := toStringArray (d .Get (scriptCommandsAttr ).([]any ))
59
61
tries := d .Get (scriptTriesAttr ).(int )
60
62
backoffDelay := d .Get (scriptBackoffDelayAttr ).(int )
61
63
62
64
if err != nil {
63
- return err
65
+ return diag.Diagnostics {diag.Diagnostic {
66
+ Severity : diag .Error ,
67
+ Summary : "Commands input is not valid" ,
68
+ Detail : err .Error (),
69
+ }}
64
70
}
65
71
66
72
sum := shasumCommands (commands )
67
73
68
- if err := executeCommands (db , commands , tries , backoffDelay ); err != nil {
69
- return err
74
+ if err := executeCommands (ctx , db , commands , tries , backoffDelay ); err != nil {
75
+ return diag.Diagnostics {diag.Diagnostic {
76
+ Severity : diag .Error ,
77
+ Summary : "Commands execution failed" ,
78
+ Detail : err .Error (),
79
+ }}
70
80
}
71
81
72
82
d .Set (scriptShasumAttr , sum )
@@ -89,9 +99,9 @@ func resourcePostgreSQLScriptDelete(db *DBConnection, d *schema.ResourceData) er
89
99
return nil
90
100
}
91
101
92
- func executeCommands (db * DBConnection , commands []string , tries int , backoffDelay int ) error {
102
+ func executeCommands (ctx context. Context , db * DBConnection , commands []string , tries int , backoffDelay int ) error {
93
103
for try := 1 ; ; try ++ {
94
- err := executeBatch (db , commands )
104
+ err := executeBatch (ctx , db , commands )
95
105
if err == nil {
96
106
return nil
97
107
} else {
@@ -103,10 +113,10 @@ func executeCommands(db *DBConnection, commands []string, tries int, backoffDela
103
113
}
104
114
}
105
115
106
- func executeBatch (db * DBConnection , commands []string ) error {
116
+ func executeBatch (ctx context. Context , db * DBConnection , commands []string ) error {
107
117
for _ , command := range commands {
108
118
log .Printf ("[DEBUG] Executing %s" , command )
109
- _ , err := db .Exec ( command )
119
+ _ , err := db .ExecContext ( ctx , command )
110
120
log .Printf ("[DEBUG] Result %s: %v" , command , err )
111
121
if err != nil {
112
122
log .Println ("[DEBUG] Error catched:" , err )
0 commit comments