forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystem.go
96 lines (71 loc) · 2.23 KB
/
system.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
package models
import (
"fmt"
"os"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
)
var DescribeStacksCache = map[string]DescribeStacksResult{}
var DescribeStacksCacheTTL = 5 * time.Second
var DescribeStacksMutex = &sync.Mutex{}
type DescribeStacksResult struct {
Name string
Output *cloudformation.DescribeStacksOutput
RequestTime time.Time
}
func DescribeStacks() (*cloudformation.DescribeStacksOutput, error) {
return doDescribeStack(cloudformation.DescribeStacksInput{})
}
func DescribeStack(name string) (*cloudformation.DescribeStacksOutput, error) {
return doDescribeStack(cloudformation.DescribeStacksInput{
StackName: aws.String(name),
})
}
func UpdateStack(req *cloudformation.UpdateStackInput) (*cloudformation.UpdateStackOutput, error) {
if req.StackName != nil {
name := *req.StackName
fmt.Printf("fn=UpdateStack at=delete name=%q\n", name)
delete(DescribeStacksCache, name)
}
return CloudFormation().UpdateStack(req)
}
func doDescribeStack(input cloudformation.DescribeStacksInput) (*cloudformation.DescribeStacksOutput, error) {
log := Logger.At("doDescribeStack").Start()
DescribeStacksMutex.Lock()
defer DescribeStacksMutex.Unlock()
name := "<blank>"
if input.StackName != nil {
name = *input.StackName
}
s := DescribeStacksCache[name]
// if last request was before the TTL, or if running in the test environment, make a request
if s.RequestTime.Before(time.Now().Add(-DescribeStacksCacheTTL)) || os.Getenv("PROVIDER") == "test" {
log.Logf("name=%q age=%s status=miss", name, time.Since(s.RequestTime))
var err error
var res *cloudformation.DescribeStacksOutput
var stacks []*cloudformation.Stack
for {
res, err = CloudFormation().DescribeStacks(&input)
if err != nil {
log.Namespace("name=%q", name).Error(err)
return nil, err
}
stacks = append(stacks, res.Stacks...)
if res.NextToken == nil {
break
}
input.NextToken = res.NextToken
}
res.Stacks = stacks
DescribeStacksCache[name] = DescribeStacksResult{
Name: name,
Output: res,
RequestTime: time.Now(),
}
return res, err
}
log.Logf("name=%q age=%s status=hit", name, time.Since(s.RequestTime))
return s.Output, nil
}