-
Notifications
You must be signed in to change notification settings - Fork 1
/
image.go
123 lines (107 loc) · 2.47 KB
/
image.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package cloudmeta
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/spotmaxtech/gokit"
)
type AWSImageData struct {
data map[string]map[string]map[string]*ec2.Image
}
type AWSImage struct {
key string
AWSImageData
}
func NewAWSImage(key string) *AWSImage {
awsImage := AWSImage{
key: key,
}
return &awsImage
}
func (image *AWSImage) ListImagesByRegion(region string) *map[string]map[string]*ec2.Image {
var values = make(map[string]map[string]*ec2.Image)
for k, v := range image.data {
if k == region {
values = v
return &values
}
}
return &values
}
func (image *AWSImage) ListImagesByRegionAndType(region string, imagetype string) *map[string]*ec2.Image {
var values = make(map[string]*ec2.Image)
for k := range image.data {
if k == region {
for kt, vt := range image.data[k] {
if kt == imagetype {
values = vt
return &values
}
}
}
}
return &values
}
func (image *AWSImage) FetchImage(consul *gokit.Consul) error {
value, err := consul.GetKey(image.key)
if err != nil {
return err
}
var tempData map[string]map[string]map[string]*ec2.Image
if err = json.Unmarshal(value, &tempData); err != nil {
return err
}
image.data = tempData
return err
}
type ALiImageData struct {
data map[string]map[string]map[string]*ImageALi
}
type ALiImage struct {
key string
Region Region
ALiImageData
}
func NewALiImage(key string, region Region) *ALiImage {
aliImage := ALiImage{
key: key,
Region: region,
}
return &aliImage
}
func (image *ALiImage) FetchALiImage(consul *gokit.Consul) error {
image.data = make(map[string]map[string]map[string]*ImageALi)
for _, r := range image.Region.List() {
value, err := consul.GetKey(fmt.Sprintf("%s/%s/image.json", image.key, r.Name))
if err != nil {
fmt.Println(err)
return err
}
var tempData map[string]map[string]*ImageALi
if err = json.Unmarshal(value, &tempData); err != nil {
return err
}
image.data[r.Name] = tempData
}
return nil
}
func (image *ALiImage) ListImageByRegion(region string) *map[string]map[string]*ImageALi {
var values = make(map[string]map[string]*ImageALi)
for k, v := range image.data {
if k == region {
values = v
return &values
}
}
return &values
}
func (image *ALiImage) ListImageByRegionAndOS(region string, os string) *map[string]*ImageALi {
var values = make(map[string]*ImageALi)
for k, v := range image.data[region] {
if k == os {
values = v
return &values
}
}
return &values
}