-
Notifications
You must be signed in to change notification settings - Fork 134
/
status_checker.go
71 lines (63 loc) · 2.02 KB
/
status_checker.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
package fastly
import (
"context"
"fmt"
"time"
gofastly "github.com/fastly/go-fastly/v3/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
const (
WAFStatusCheckDelay = 5 * time.Second
WAFStatusCheckMinTimeout = 5 * time.Second
)
type WAFDeploymentStatusCheck func(wafID string, version int) (*gofastly.WAFVersion, error)
type WAFDeploymentChecker struct {
Timeout time.Duration
Delay time.Duration
MinTimeout time.Duration
Check WAFDeploymentStatusCheck
}
func DefaultWAFDeploymentChecker(conn *gofastly.Client) func(wafID string, version int) (*gofastly.WAFVersion, error) {
checkDeploymentStatus := func(wafID string, version int) (*gofastly.WAFVersion, error) {
resp, err := conn.GetWAFVersion(&gofastly.GetWAFVersionInput{
WAFID: wafID,
WAFVersionNumber: version,
})
if err != nil {
return nil, err
}
return resp, nil
}
return checkDeploymentStatus
}
func (c *WAFDeploymentChecker) waitForDeployment(ctx context.Context, wafID string, latestVersion *gofastly.WAFVersion) error {
createStateConf := &resource.StateChangeConf{
Pending: []string{
gofastly.WAFVersionDeploymentStatusPending,
gofastly.WAFVersionDeploymentStatusInProgress,
},
Target: []string{
gofastly.WAFVersionDeploymentStatusCompleted,
},
Refresh: func() (interface{}, string, error) {
res, err := c.Check(wafID, latestVersion.Number)
if err != nil {
return nil, "", err
}
if res.LastDeploymentStatus == gofastly.WAFVersionDeploymentStatusFailed {
return res, res.LastDeploymentStatus, fmt.Errorf("WAF deployment failed. Error message: %v", res.Error)
}
return res, res.LastDeploymentStatus, nil
},
Timeout: c.Timeout,
Delay: c.Delay,
MinTimeout: c.MinTimeout,
ContinuousTargetOccurence: 5,
NotFoundChecks: 1,
}
_, err := createStateConf.WaitForStateContext(ctx)
if err != nil {
return fmt.Errorf("Error waiting for WAF Version (%s) to be updated: %v", wafID, err)
}
return nil
}