-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_server_image.go
209 lines (167 loc) · 5.27 KB
/
resource_server_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package ddcloud
import (
"fmt"
"log"
"regexp"
"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
)
const (
serverImageTypeOS = "os"
serverImageTypeCustomer = "customer"
serverImageTypeAuto = "auto"
)
func isUUID(str string) (bool, error) {
return regexp.Match(`[A-Fa-f0-9]{8}(-[A-Fa-f0-9]{4}){3}-[A-Fa-f0-9]{12}`, []byte(str))
}
func resolveServerImage(imageNameOrID string, imageType string, datacenterID string, apiClient *compute.Client) (resolvedImage compute.Image, err error) {
log.Printf("Resolve server image '%s' (%s) in datacenter '%s'.",
imageNameOrID,
imageType,
datacenterID,
)
isID, err := isUUID(imageNameOrID)
if err != nil {
return
}
if isID {
log.Printf("'%s' appears to be a UUID; treating it as an image Id.", imageNameOrID)
} else {
log.Printf("'%s' does not appear to be a UUID; treating it as an image name.", imageNameOrID)
}
var (
osImage *compute.OSImage
customerImage *compute.CustomerImage
)
switch imageType {
case serverImageTypeOS:
if isID {
osImage, err = lookupOSImageByID(imageNameOrID, apiClient)
if err != nil {
return
}
if osImage != nil {
resolvedImage = osImage
log.Printf("Resolved OS image with Id '%s' in datacenter '%s'.", imageNameOrID, datacenterID)
return
}
err = fmt.Errorf("cannot find an OS image with Id '%s' in datacenter '%s'",
imageNameOrID,
datacenterID,
)
} else {
osImage, err = lookupOSImageByName(imageNameOrID, datacenterID, apiClient)
if err != nil {
return
}
if osImage != nil {
resolvedImage = osImage
log.Printf("Resolved OS image named '%s' in datacenter '%s'.", imageNameOrID, datacenterID)
return
}
err = fmt.Errorf("cannot find an OS image named '%s' in datacenter '%s'",
imageNameOrID,
datacenterID,
)
}
case serverImageTypeCustomer:
if isID {
customerImage, err = lookupCustomerImageByID(imageNameOrID, apiClient)
if err != nil {
return
}
if customerImage != nil {
resolvedImage = customerImage
log.Printf("Resolved customer image with Id '%s' in datacenter '%s'.", imageNameOrID, datacenterID)
return
}
err = fmt.Errorf("cannot find a customer image with Id '%s' in datacenter '%s'",
imageNameOrID,
datacenterID,
)
} else {
customerImage, err = lookupCustomerImageByName(imageNameOrID, datacenterID, apiClient)
if err != nil {
return
}
if customerImage != nil {
resolvedImage = customerImage
log.Printf("Resolved customer image named '%s' in datacenter '%s'.", imageNameOrID, datacenterID)
return
}
err = fmt.Errorf("cannot find a customer image named '%s' in datacenter '%s'",
imageNameOrID,
datacenterID,
)
}
case serverImageTypeAuto:
if isID {
osImage, err = lookupOSImageByID(imageNameOrID, apiClient)
if err != nil {
return
}
if osImage != nil {
resolvedImage = osImage
log.Printf("Resolved OS image with Id '%s' in datacenter '%s'.", imageNameOrID, datacenterID)
return
}
// Fall back to customer image, if required.
customerImage, err = lookupCustomerImageByID(imageNameOrID, apiClient)
if err != nil {
return
}
if customerImage != nil {
resolvedImage = customerImage
log.Printf("Resolved customer image with Id '%s' in datacenter '%s'.", imageNameOrID, datacenterID)
return
}
err = fmt.Errorf("cannot find an OS or customer image with Id '%s' in datacenter '%s'",
imageNameOrID,
datacenterID,
)
} else {
osImage, err = lookupOSImageByName(imageNameOrID, datacenterID, apiClient)
if err != nil {
return
}
if osImage != nil {
resolvedImage = osImage
log.Printf("Resolved OS image named '%s' in datacenter '%s'.", imageNameOrID, datacenterID)
return
}
// Fall back to customer image, if required.
customerImage, err = lookupCustomerImageByName(imageNameOrID, datacenterID, apiClient)
if err != nil {
return
}
if customerImage != nil {
resolvedImage = customerImage
log.Printf("Resolved customer image named '%s' in datacenter '%s'.", imageNameOrID, datacenterID)
return
}
err = fmt.Errorf("cannot find an OS or customer image named '%s' in datacenter '%s'",
imageNameOrID,
datacenterID,
)
}
default:
err = fmt.Errorf("invalid image type '%s'", imageType)
return
}
return
}
func lookupOSImageByID(imageID string, apiClient *compute.Client) (*compute.OSImage, error) {
log.Printf("Looking up OS image '%s' by Id...", imageID)
return apiClient.GetOSImage(imageID)
}
func lookupOSImageByName(imageName string, datacenterID string, apiClient *compute.Client) (*compute.OSImage, error) {
log.Printf("Looking up OS image '%s' by name in datacenter '%s'...", imageName, datacenterID)
return apiClient.FindOSImage(imageName, datacenterID)
}
func lookupCustomerImageByID(imageID string, apiClient *compute.Client) (*compute.CustomerImage, error) {
log.Printf("Looking up customer image '%s' by Id...", imageID)
return apiClient.GetCustomerImage(imageID)
}
func lookupCustomerImageByName(imageName string, datacenterID string, apiClient *compute.Client) (*compute.CustomerImage, error) {
log.Printf("Looking up customer image '%s' by name in datacenter '%s'...", imageName, datacenterID)
return apiClient.FindCustomerImage(imageName, datacenterID)
}