forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk_repo.go
189 lines (155 loc) · 4.57 KB
/
disk_repo.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
package config
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
biproperty "github.com/cloudfoundry/bosh-utils/property"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
)
type DiskRepo interface {
UpdateCurrent(diskID string) error
FindCurrent() (DiskRecord, bool, error)
ClearCurrent() error
Save(cid string, size int, cloudProperties biproperty.Map) (DiskRecord, error)
Find(cid string) (DiskRecord, bool, error)
All() ([]DiskRecord, error)
Delete(DiskRecord) error
}
type diskRepo struct {
deploymentStateService DeploymentStateService
uuidGenerator boshuuid.Generator
}
func NewDiskRepo(deploymentStateService DeploymentStateService, uuidGenerator boshuuid.Generator) DiskRepo {
return diskRepo{
deploymentStateService: deploymentStateService,
uuidGenerator: uuidGenerator,
}
}
func (r diskRepo) Save(cid string, size int, cloudProperties biproperty.Map) (DiskRecord, error) {
config, records, err := r.load()
if err != nil {
return DiskRecord{}, err
}
oldRecord, found := r.find(records, cid)
if found {
return DiskRecord{}, bosherr.Errorf("Failed to save disk cid '%s', existing record found '%#v'", cid, oldRecord)
}
newRecord := DiskRecord{
CID: cid,
Size: size,
CloudProperties: cloudProperties,
}
newRecord.ID, err = r.uuidGenerator.Generate()
if err != nil {
return newRecord, bosherr.WrapError(err, "Generating disk id")
}
records = append(records, newRecord)
config.Disks = records
err = r.deploymentStateService.Save(config)
if err != nil {
return newRecord, bosherr.WrapError(err, "Saving new config")
}
return newRecord, nil
}
func (r diskRepo) FindCurrent() (DiskRecord, bool, error) {
deploymentState, err := r.deploymentStateService.Load()
if err != nil {
return DiskRecord{}, false, bosherr.WrapError(err, "Loading existing config")
}
currentDiskID := deploymentState.CurrentDiskID
if currentDiskID == "" {
return DiskRecord{}, false, nil
}
for _, oldRecord := range deploymentState.Disks {
if oldRecord.ID == currentDiskID {
return oldRecord, true, nil
}
}
return DiskRecord{}, false, nil
}
func (r diskRepo) UpdateCurrent(diskID string) error {
deploymentState, err := r.deploymentStateService.Load()
if err != nil {
return bosherr.WrapError(err, "Loading existing config")
}
found := false
for _, oldRecord := range deploymentState.Disks {
if oldRecord.ID == diskID {
found = true
}
}
if !found {
return bosherr.Errorf("Verifying disk record exists with id '%s'", diskID)
}
deploymentState.CurrentDiskID = diskID
err = r.deploymentStateService.Save(deploymentState)
if err != nil {
return bosherr.WrapError(err, "Saving new config")
}
return nil
}
func (r diskRepo) Find(cid string) (DiskRecord, bool, error) {
_, records, err := r.load()
if err != nil {
return DiskRecord{}, false, err
}
foundRecord, found := r.find(records, cid)
return foundRecord, found, nil
}
func (r diskRepo) All() ([]DiskRecord, error) {
deploymentState, err := r.deploymentStateService.Load()
if err != nil {
return []DiskRecord{}, bosherr.WrapError(err, "Loading existing config")
}
return deploymentState.Disks, nil
}
func (r diskRepo) Delete(diskRecord DiskRecord) error {
config, records, err := r.load()
if err != nil {
return err
}
newRecords := []DiskRecord{}
for _, record := range records {
if record.ID != diskRecord.ID {
newRecords = append(newRecords, record)
}
}
config.Disks = newRecords
if config.CurrentDiskID == diskRecord.ID {
config.CurrentDiskID = ""
}
err = r.deploymentStateService.Save(config)
if err != nil {
return bosherr.WrapError(err, "Saving new config")
}
return nil
}
func (r diskRepo) ClearCurrent() error {
deploymentState, err := r.deploymentStateService.Load()
if err != nil {
return bosherr.WrapError(err, "Loading existing config")
}
deploymentState.CurrentDiskID = ""
err = r.deploymentStateService.Save(deploymentState)
if err != nil {
return bosherr.WrapError(err, "Saving new config")
}
return nil
}
func (r diskRepo) load() (DeploymentState, []DiskRecord, error) {
deploymentState, err := r.deploymentStateService.Load()
if err != nil {
return deploymentState, []DiskRecord{}, bosherr.WrapError(err, "Loading existing config")
}
records := deploymentState.Disks
if records == nil {
return deploymentState, []DiskRecord{}, nil
}
return deploymentState, records, nil
}
func (r diskRepo) find(records []DiskRecord, cid string) (DiskRecord, bool) {
for _, existingRecord := range records {
if existingRecord.CID == cid {
return existingRecord, true
}
}
return DiskRecord{}, false
}