forked from convox/rack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresource.go
65 lines (49 loc) · 1.35 KB
/
resource.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
package models
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/convox/rack/api/cache"
)
type Resource struct {
Id string
Name string
Reason string
Status string
Type string
Time time.Time
}
type Resources map[string]Resource
func ListResources(app string) (Resources, error) {
if resources, ok := cache.Get("ListResources", app).(Resources); ok {
return resources, nil
}
stackName := shortNameToStackName(app)
res, err := CloudFormation().DescribeStackResources(&cloudformation.DescribeStackResourcesInput{
StackName: aws.String(stackName),
})
if app != stackName && awsError(err) == "ValidationError" {
res, err = CloudFormation().DescribeStackResources(&cloudformation.DescribeStackResourcesInput{
StackName: aws.String(app),
})
}
if err != nil {
return nil, err
}
resources := make(Resources, len(res.StackResources))
for _, r := range res.StackResources {
resources[*r.LogicalResourceId] = Resource{
Id: cs(r.PhysicalResourceId, ""),
Name: cs(r.LogicalResourceId, ""),
Reason: cs(r.ResourceStatusReason, ""),
Status: cs(r.ResourceStatus, ""),
Type: cs(r.ResourceType, ""),
Time: ct(r.Timestamp),
}
}
err = cache.Set("ListResources", app, resources, 15*time.Second)
if err != nil {
return nil, err
}
return resources, nil
}