/
testhelper.go
109 lines (87 loc) · 3.25 KB
/
testhelper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package testhelper
import (
"context"
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lightsail"
tflightsail "github.com/deyoungtech/terraform-provider-awslightsail/internal/lightsail"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
// OperationStatusNotStarted is a OperationStatus enum value
OperationStatusNotStarted = "NotStarted"
// OperationStatusStarted is a OperationStatus enum value
OperationStatusStarted = "Started"
// OperationStatusFailed is a OperationStatus enum value
OperationStatusFailed = "Failed"
// OperationStatusCompleted is a OperationStatus enum value
OperationStatusCompleted = "Completed"
// OperationStatusSucceeded is a OperationStatus enum value
OperationStatusSucceeded = "Succeeded"
// OperationTimeout is the Timout Value for Operations
OperationTimeout = 20 * time.Minute
// OperationDelay is the Delay Value for Operations
OperationDelay = 5 * time.Second
// OperationMinTimeout is the MinTimout Value for Operations
OperationMinTimeout = 3 * time.Second
)
// initialized once, so it can be shared by each acceptance test
var provider = tflightsail.Provider()
// GetProvider returns the lightsail provider
func GetProvider() *schema.Provider {
return provider
}
func GetProviders() map[string]*schema.Provider {
return map[string]*schema.Provider{
"awslightsail": GetProvider(),
}
}
// DefaultEmailAddress is the default email address to set as a
// resource or data source parameter for acceptance tests.
const DefaultEmailAddress = "no-reply@deyoung.dev"
// initialized once, so it can be shared by each acceptance test
// waitLightsailOperation waits for an Operation to return Succeeded or Compleated
func WaitLightsailOperation(conn *lightsail.Client, oid *string) error {
stateConf := &resource.StateChangeConf{
Pending: []string{OperationStatusStarted},
Target: []string{OperationStatusCompleted, OperationStatusSucceeded},
Refresh: statusLightsailOperation(conn, oid),
Timeout: OperationTimeout,
Delay: OperationDelay,
MinTimeout: OperationMinTimeout,
}
outputRaw, err := stateConf.WaitForState()
if _, ok := outputRaw.(*lightsail.GetOperationOutput); ok {
return err
}
return err
}
func statusLightsailOperation(conn *lightsail.Client, oid *string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
input := &lightsail.GetOperationInput{
OperationId: oid,
}
oidValue := aws.ToString(oid)
log.Printf("[DEBUG] Checking if Lightsail Operation (%s) is Completed", oidValue)
output, err := conn.GetOperation(context.TODO(), input)
if err != nil {
return output, "FAILED", err
}
if output.Operation == nil {
return nil, "Failed", fmt.Errorf("Error retrieving Operation info for operation (%s)", oidValue)
}
log.Printf("[DEBUG] Lightsail Operation (%s) is currently %q", oidValue, output.Operation.Status)
return output, string(output.Operation.Status), nil
}
}
// func testAccPreCheck(t *testing.T) {
// conn := provider.Meta(*lightsail.Client)
// input := &lightsail.GetInstancesInput{}
// _, err := conn.GetInstances(input)
// if err != nil {
// t.Fatalf("unexpected PreCheck error: %s", err)
// }
// }