-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
instance_type.go
79 lines (61 loc) · 1.92 KB
/
instance_type.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
package instancetype
import (
"fmt"
"github.com/lxc/incus/v6/shared/api"
)
// Type indicates the type of instance.
type Type int
const (
// Any represents any type of instance.
Any = Type(-1)
// Container represents a container instance type.
Container = Type(0)
// VM represents a virtual-machine instance type.
VM = Type(1)
)
// New validates the supplied string against the allowed types of instance and returns the internal
// representation of that type. If empty string is supplied then the type returned is TypeContainer.
// If an invalid name is supplied an error will be returned.
func New(name string) (Type, error) {
// If "container" or "" is supplied, return type as Container.
if api.InstanceType(name) == api.InstanceTypeContainer || name == "" {
return Container, nil
}
// If "virtual-machine" is supplied, return type as VM.
if api.InstanceType(name) == api.InstanceTypeVM {
return VM, nil
}
return -1, fmt.Errorf("Invalid instance type")
}
// String converts the internal representation of instance type to a string used in API requests.
// Returns empty string if value is not a valid instance type.
func (instanceType Type) String() string {
if instanceType == Container {
return string(api.InstanceTypeContainer)
}
if instanceType == VM {
return string(api.InstanceTypeVM)
}
return ""
}
// ToAPI converts the internal representation of instance type to an api.InstanceType.
func (instanceType Type) ToAPI() api.InstanceType {
if instanceType == Container {
return api.InstanceTypeContainer
}
if instanceType == VM {
return api.InstanceTypeVM
}
if instanceType == Any {
return api.InstanceTypeAny
}
return api.InstanceType("")
}
// Filter returns a valid filter field compatible with cluster.InstanceFilter.
// 'Any' represents any possible instance type, and so it is omitted.
func (instanceType Type) Filter() *Type {
if instanceType == Any {
return nil
}
return &instanceType
}