forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_type.go
52 lines (40 loc) · 1.31 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
package instancetype
import (
"fmt"
"github.com/lxc/lxd/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 ""
}