/
subsys.go
136 lines (110 loc) · 3.66 KB
/
subsys.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
package inventory
// GeneralSubsys implements the inventory sub-system. It is instantiated using
// the New* methods of specific subsystems like collins, boltdb and so on
type GeneralSubsys struct {
client SubsysClient
assets map[string]*Asset
}
// NewGeneralSubsys returns a instance of GeneralSubsys initialized with a subsystem client
func NewGeneralSubsys(client SubsysClient) *GeneralSubsys {
return &GeneralSubsys{
client: client,
assets: make(map[string]*Asset),
}
}
// RestoreAsset makes the subsystem update asset info
func (ci *GeneralSubsys) RestoreAsset(name string, asset *Asset) error {
if _, ok := ci.assets[name]; ok {
return errAssetExists(name)
}
ci.assets[name] = asset
return nil
}
//AddAsset adds an asset to collins in 'Discovered' status
func (ci *GeneralSubsys) AddAsset(name string) error {
if _, ok := ci.assets[name]; ok {
return errAssetExists(name)
}
host, err := NewAsset(ci.client, name)
if err != nil {
return err
}
ci.assets[name] = host
return nil
}
//SetAssetDiscovered sets an asset state to discovered
func (ci *GeneralSubsys) SetAssetDiscovered(name string) error {
if _, ok := ci.assets[name]; !ok {
return errAssetNotExists(name)
}
status, _ := ci.assets[name].GetStatus()
return ci.assets[name].SetStatus(status, Discovered)
}
//SetAssetDisappeared sets an asset state to disappeared
func (ci *GeneralSubsys) SetAssetDisappeared(name string) error {
if _, ok := ci.assets[name]; !ok {
return errAssetNotExists(name)
}
status, _ := ci.assets[name].GetStatus()
return ci.assets[name].SetStatus(status, Disappeared)
}
//SetAssetProvisioning sets an asset state to provisioning
func (ci *GeneralSubsys) SetAssetProvisioning(name string) error {
if _, ok := ci.assets[name]; !ok {
return errAssetNotExists(name)
}
_, state := ci.assets[name].GetStatus()
return ci.assets[name].SetStatus(Provisioning, state)
}
//SetAssetCommissioned sets an asset status to unallocated
func (ci *GeneralSubsys) SetAssetCommissioned(name string) error {
if _, ok := ci.assets[name]; !ok {
return errAssetNotExists(name)
}
_, state := ci.assets[name].GetStatus()
// collins equivalent of commissioned status in allocated
return ci.assets[name].SetStatus(Allocated, state)
}
//SetAssetCancelled sets an asset state to cancelled
func (ci *GeneralSubsys) SetAssetCancelled(name string) error {
if _, ok := ci.assets[name]; !ok {
return errAssetNotExists(name)
}
_, state := ci.assets[name].GetStatus()
return ci.assets[name].SetStatus(Cancelled, state)
}
//SetAssetDecommissioned sets an asset status to decommissioned
func (ci *GeneralSubsys) SetAssetDecommissioned(name string) error {
if _, ok := ci.assets[name]; !ok {
return errAssetNotExists(name)
}
_, state := ci.assets[name].GetStatus()
return ci.assets[name].SetStatus(Decommissioned, state)
}
//SetAssetInMaintenance sets an asset state to decommissioned
func (ci *GeneralSubsys) SetAssetInMaintenance(name string) error {
if _, ok := ci.assets[name]; !ok {
return errAssetNotExists(name)
}
_, state := ci.assets[name].GetStatus()
return ci.assets[name].SetStatus(Maintenance, state)
}
//SetAssetUnallocated sets an asset status to unallocated
func (ci *GeneralSubsys) SetAssetUnallocated(name string) error {
if _, ok := ci.assets[name]; !ok {
return errAssetNotExists(name)
}
_, state := ci.assets[name].GetStatus()
return ci.assets[name].SetStatus(Unallocated, state)
}
//GetAsset finds and returns the asset in inventory
func (ci *GeneralSubsys) GetAsset(name string) SubsysAsset {
if a, ok := ci.assets[name]; ok {
return a
}
return nil
}
//GetAllAssets returns all the assets in inventory
func (ci *GeneralSubsys) GetAllAssets() SubsysAssets {
return ci.assets
}