/
efs.go
84 lines (62 loc) · 1.53 KB
/
efs.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
package aws
import (
"github.com/aws/aws-sdk-go/service/efs"
"strings"
)
func GetEFSId(projectId string) string {
fsList := getAllEFS()
tags := make(map[string]string, 2)
tags["ProjectId"] = projectId
tags["Type"] = "persistence"
fsList = filterByTags(fsList, tags)
if len(fsList) < 1 {
panic("No EFS FileSystem found.")
}
return *(fsList[0].FileSystemId)
}
func getAllEFS() []*efs.FileSystemDescription {
input := &efs.DescribeFileSystemsInput{}
result, err := EFSClient.DescribeFileSystems(input)
if err != nil {
panic(err)
}
return result.FileSystems
}
func filterByTags(fsList []*efs.FileSystemDescription, tags map[string]string) []*efs.FileSystemDescription {
result := make([]*efs.FileSystemDescription, 0)
for _, fs := range fsList {
fsTags := getTags(fs)
if matchMaps(tags, fsTags) {
result = append(result, fs)
}
}
return result
}
func getTags(fs *efs.FileSystemDescription) map[string]string {
input := &efs.DescribeTagsInput{FileSystemId:fs.FileSystemId}
result, err := EFSClient.DescribeTags(input)
if err != nil {
panic(err)
}
tags := make(map[string]string, len(result.Tags))
for _, tag := range result.Tags {
key := *tag.Key
value := *tag.Value
tags[key] = value
}
return tags
}
func matchMaps(primary map[string]string, target map[string]string) bool {
if len(primary) > len(target) {
return false
}
for key, value := range primary {
if targetValue, ok := target[key]; ok {
if strings.EqualFold(value, targetValue) {
continue
}
}
return false
}
return true
}