-
Notifications
You must be signed in to change notification settings - Fork 0
/
sku.go
366 lines (326 loc) · 11.2 KB
/
sku.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package skewer
import (
"fmt"
"strconv"
"strings"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
"github.com/pkg/errors"
)
// SKU wraps an Azure compute SKU with richer functionality
type SKU compute.ResourceSku
func wrapResourceSKUs(in []compute.ResourceSku) []SKU {
out := make([]SKU, len(in))
for index, value := range in {
out[index] = SKU(value)
}
return out
}
const (
// VirtualMachines is the .
VirtualMachines = "virtualMachines"
// Disks is a convenience constant to filter resource SKUs to only include disks.
Disks = "disks"
)
// Supported models an enum of possible boolean values for resource support in the Azure API.
type Supported string
const (
// CapabilitySupported is an enum value for the string "True" returned when a SKU supports a binary capability.
CapabilitySupported Supported = "True"
// CapabilityUnupported is an enum value for the string "True" returned when a SKU does not support a binary capability.
CapabilityUnupported Supported = "False"
)
const (
// EphemeralOSDisk identifies the capability for ephemeral os support.
EphemeralOSDisk = "EphemeralOSDiskSupported"
// AcceleratedNetworking identifies the capability for accelerated networking support.
AcceleratedNetworking = "AcceleratedNetworkingEnabled"
// VCPUs identifies the capability for the number of vCPUS.
VCPUs = "vCPUs"
// MemoryGB identifies the capability for memory capacity.
MemoryGB = "MemoryGB"
// HyperVGenerations identifies the hyper-v generations this vm sku supports.
HyperVGenerations = "HyperVGenerations"
// EncryptionAtHost identifies the capability for accelerated networking support.
EncryptionAtHost = "EncryptionAtHostSupported"
// UltraSSDAvailable identifies the capability for ultra ssd
// enablement.
UltraSSDAvailable = "UltraSSDAvailable"
// CachedDiskBytes identifies the maximum size of the cach disk for
// a vm.
CachedDiskBytes = "CachedDiskBytes"
)
// ErrCapabilityNotFound will be returned when a capability could not be
// found, even without a value.
type ErrCapabilityNotFound struct {
capability string
}
func (e *ErrCapabilityNotFound) Error() string {
return e.capability + "CapabilityNotFound"
}
// ErrCapabilityValueNil will be returned when a capability was found by
// name but the value was nil.
type ErrCapabilityValueNil struct {
capability string
}
func (e *ErrCapabilityValueNil) Error() string {
return e.capability + "CapabilityValueNil"
}
// ErrCapabilityValueParse will be returned when a capability was found by
// name but the value was nil.
type ErrCapabilityValueParse struct {
capability string
value string
err error
}
func (e *ErrCapabilityValueParse) Error() string {
return fmt.Sprintf("%sCapabilityValueParse: failed to parse string '%s' as int64, error: '%s'", e.capability, e.value, e.err)
}
// VCPU returns the number of vCPUs this SKU supports.
func (s *SKU) VCPU() (int64, error) {
return s.GetCapabilityQuantity(VCPUs)
}
// Memory returns the amount of memory this SKU supports.
func (s *SKU) Memory() (int64, error) {
return s.GetCapabilityQuantity(MemoryGB)
}
func (s *SKU) MaxCachedDiskBytes() (int64, error) {
return s.GetCapabilityQuantity(CachedDiskBytes)
}
func (s *SKU) IsEncryptionAtHostSupported() bool {
return s.HasCapability(EncryptionAtHost)
}
func (s *SKU) IsUltraSSDAvailable() bool {
return s.HasZonalCapability(UltraSSDAvailable)
}
func (s *SKU) IsEphemeralOSDiskSupported() bool {
return s.HasCapability(EphemeralOSDisk)
}
// GetCapabilityQuantity retrieves and parses the value of a numeric
// capability with the provided name. It errors if the capability is not
// found, the value was nil, or the value could not be parsed as an integer.
func (s *SKU) GetCapabilityQuantity(name string) (int64, error) {
if s.Capabilities == nil {
return -1, &ErrCapabilityNotFound{name}
}
for _, capability := range *s.Capabilities {
if capability.Name != nil && *capability.Name == name {
if capability.Value != nil {
intVal, err := strconv.ParseInt(*capability.Value, 10, 64)
if err != nil {
return -1, &ErrCapabilityValueParse{name, *capability.Value, err}
}
return intVal, nil
}
return -1, &ErrCapabilityValueNil{name}
}
}
return -1, &ErrCapabilityNotFound{name}
}
// HasCapability return true for a capability which can be either
// supported or not. Examples include "EphemeralOSDiskSupported",
// "EncryptionAtHostSupported", "AcceleratedNetworkingEnabled", and
// "RdmaEnabled"
func (s *SKU) HasCapability(name string) bool {
if s.Capabilities == nil {
return false
}
for _, capability := range *s.Capabilities {
if capability.Name != nil && strings.EqualFold(*capability.Name, name) {
return capability.Value != nil && strings.EqualFold(*capability.Value, string(CapabilitySupported))
}
}
return false
}
// HasZonalCapability return true for a capability which can be either
// supported or not. Examples include "UltraSSDAvailable".
// This function only checks that zone details suggest support: it will
// return true for a whole location even when only one zone supports the
// feature. Currently, the only real scenario that appears to use
// zoneDetails is UltraSSDAvailable which always lists all regions as
// available.
// TODO(ace): update this function signature/behavior if necessary to
// account for per-zone availability.
func (s *SKU) HasZonalCapability(name string) bool {
if s.LocationInfo == nil {
return false
}
for _, locationInfo := range *s.LocationInfo {
if locationInfo.ZoneDetails == nil {
continue
}
for _, zoneDetails := range *locationInfo.ZoneDetails {
if zoneDetails.Capabilities == nil {
continue
}
for _, capability := range *zoneDetails.Capabilities {
if capability.Name != nil && strings.EqualFold(*capability.Name, name) {
if capability.Value != nil && strings.EqualFold(*capability.Value, string(CapabilitySupported)) {
return true
}
}
}
}
}
return false
}
// HasCapabilityWithSeparator return true for a capability which may be
// exposed as a comma-separated list. We check that the list contains
// the desired substring. An example is "HyperVGenerations" which may be
// "V1,V2"
func (s *SKU) HasCapabilityWithSeparator(name, value string) bool {
if s.Capabilities == nil {
return false
}
for _, capability := range *s.Capabilities {
if capability.Name != nil && strings.EqualFold(*capability.Name, name) {
return capability.Value != nil && strings.Contains(*capability.Value, value)
}
}
return false
}
// HasCapabilityWithCapacity returns true when the provided resource
// exposes a numeric capability and the maximum value exposed by that
// capability exceeds the value requested by the user. Examples include
// "MaxResourceVolumeMB", "OSVhdSizeMB", "vCPUs",
// "MemoryGB","MaxDataDiskCount", "CombinedTempDiskAndCachedIOPS",
// "CombinedTempDiskAndCachedReadBytesPerSecond",
// "CombinedTempDiskAndCachedWriteBytesPerSecond", "UncachedDiskIOPS",
// and "UncachedDiskBytesPerSecond"
func (s *SKU) HasCapabilityWithCapacity(name string, value int64) (bool, error) {
if s.Capabilities == nil {
return false, nil
}
for _, capability := range *s.Capabilities {
if capability.Name != nil && strings.EqualFold(*capability.Name, name) {
if capability.Value != nil {
intVal, err := strconv.ParseInt(*capability.Value, 10, 64)
if err != nil {
return false, errors.Wrapf(err, "failed to parse string '%s' as int64", *capability.Value)
}
if intVal >= value {
return true, nil
}
}
return false, nil
}
}
return false, nil
}
// IsAvailable returns true when the requested location matches one on
// the sku, and there are no total restrictions on the location.
func (s *SKU) IsAvailable(location string) bool {
if s.LocationInfo == nil {
return false
}
for _, locationInfo := range *s.LocationInfo {
if strings.EqualFold(*locationInfo.Location, location) {
if s.Restrictions != nil {
for _, restriction := range *s.Restrictions {
// Can't deploy to any zones in this location. We're done.
if restriction.Type == compute.Location {
return false
}
}
}
return true
}
}
return false
}
// IsRestricted returns true when a location restriction exists for
// this SKU.
func (s *SKU) IsRestricted(location string) bool {
if s.Restrictions == nil {
return false
}
for _, restriction := range *s.Restrictions {
if restriction.Values == nil {
continue
}
for _, candidate := range *restriction.Values {
// Can't deploy in this location. We're done.
if strings.EqualFold(candidate, location) && restriction.Type == compute.Location {
return true
}
}
}
return false
}
// IsResourceType returns true when the wrapped SKU has the provided
// value as its resource type. This may be used to filter using values
// such as "virtualMachines", "disks", "availabilitySets", "snapshots",
// and "hostGroups/hosts".
func (s *SKU) IsResourceType(t string) bool {
return s.ResourceType != nil && strings.EqualFold(*s.ResourceType, t)
}
// GetResourceType returns the name of this resource sku. It normalizes pointers
// to the empty string for comparison purposes. For example,
// "virtualMachines" for a virtual machine.
func (s *SKU) GetResourceType() string {
if s.ResourceType == nil {
return ""
}
return *s.ResourceType
}
// GetName returns the name of this resource sku. It normalizes pointers
// to the empty string for comparison purposes. For example,
// "Standard_D8s_v3" for a virtual machine.
func (s *SKU) GetName() string {
if s.Name == nil {
return ""
}
return *s.Name
}
// GetLocation returns the first found location on this *SKU resource.
// Typically only one should be listed (multiple SKU results will be returned for multiple regions).
// We fallback to locationInfo although this appears to be duplicate info.
func (s *SKU) GetLocation() string {
if s.Locations != nil {
for _, location := range *s.Locations {
return location
}
}
// TODO(ace): probably should remove
if s.LocationInfo != nil {
for _, locationInfo := range *s.LocationInfo {
if locationInfo.Location != nil {
return *locationInfo.Location
}
}
}
return ""
}
// AvailabilityZones returns the list of Availability Zones which have this resource SKU available and unrestricted.
func (s *SKU) AvailabilityZones(location string) map[string]bool {
for _, locationInfo := range *s.LocationInfo {
if strings.EqualFold(*locationInfo.Location, location) {
// Use map for easy deletion and iteration
availableZones := make(map[string]bool)
// add all zones
for _, zone := range *locationInfo.Zones {
availableZones[zone] = true
}
if s.Restrictions != nil {
for _, restriction := range *s.Restrictions {
// Can't deploy to any zones in this location. We're done.
if restriction.Type == compute.Location {
availableZones = nil
break
}
// remove restricted zones
for _, restrictedZone := range *restriction.RestrictionInfo.Zones {
delete(availableZones, restrictedZone)
}
}
}
return availableZones
}
}
return nil
}
// Equal returns true when two skus have the same location, type, and name.
func (s *SKU) Equal(other *SKU) bool {
return strings.EqualFold(s.GetResourceType(), other.GetResourceType()) &&
strings.EqualFold(s.GetName(), other.GetName()) &&
strings.EqualFold(s.GetLocation(), other.GetLocation())
}