-
Notifications
You must be signed in to change notification settings - Fork 162
/
manager.go
124 lines (102 loc) · 2.95 KB
/
manager.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
package disk
import (
"fmt"
bicloud "github.com/cloudfoundry/bosh-cli/cloud"
biconfig "github.com/cloudfoundry/bosh-cli/config"
bideplmanifest "github.com/cloudfoundry/bosh-cli/deployment/manifest"
biui "github.com/cloudfoundry/bosh-cli/ui"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
)
type Manager interface {
FindCurrent() ([]Disk, error)
Create(bideplmanifest.DiskPool, string) (Disk, error)
FindUnused() ([]Disk, error)
DeleteUnused(biui.Stage) error
}
func NewManager(
cloud bicloud.Cloud,
diskRepo biconfig.DiskRepo,
logger boshlog.Logger,
) Manager {
return &manager{
cloud: cloud,
diskRepo: diskRepo,
logger: logger,
logTag: "diskManager",
}
}
type manager struct {
cloud bicloud.Cloud
diskRepo biconfig.DiskRepo
logger boshlog.Logger
logTag string
}
func (m *manager) FindCurrent() ([]Disk, error) {
disks := []Disk{}
diskRecord, found, err := m.diskRepo.FindCurrent()
if err != nil {
return disks, bosherr.WrapError(err, "Reading disk record")
}
if found {
disk := NewDisk(diskRecord, m.cloud, m.diskRepo)
disks = append(disks, disk)
}
return disks, nil
}
func (m *manager) Create(diskPool bideplmanifest.DiskPool, vmCID string) (Disk, error) {
diskCloudProperties := diskPool.CloudProperties
m.logger.Debug(m.logTag, "Creating disk")
cid, err := m.cloud.CreateDisk(diskPool.DiskSize, diskCloudProperties, vmCID)
if err != nil {
return nil,
bosherr.WrapErrorf(err,
"Creating disk with size %d, cloudProperties %#v, instanceID %s",
diskPool.DiskSize, diskCloudProperties, vmCID,
)
}
diskRecord, err := m.diskRepo.Save(cid, diskPool.DiskSize, diskCloudProperties)
if err != nil {
return nil, bosherr.WrapError(err, "Saving deployment disk record")
}
disk := NewDisk(diskRecord, m.cloud, m.diskRepo)
return disk, nil
}
func (m *manager) FindUnused() ([]Disk, error) {
disks := []Disk{}
diskRecords, err := m.diskRepo.All()
if err != nil {
return disks, bosherr.WrapError(err, "Getting all disk records")
}
currentDiskRecord, found, err := m.diskRepo.FindCurrent()
if err != nil {
return disks, bosherr.WrapError(err, "Finding current disk record")
}
for _, diskRecord := range diskRecords {
if !found || diskRecord.ID != currentDiskRecord.ID {
disks = append(disks, NewDisk(diskRecord, m.cloud, m.diskRepo))
}
}
return disks, nil
}
func (m *manager) DeleteUnused(eventLoggerStage biui.Stage) error {
disks, err := m.FindUnused()
if err != nil {
return bosherr.WrapError(err, "Finding unused disks")
}
for _, disk := range disks {
stepName := fmt.Sprintf("Deleting unused disk '%s'", disk.CID())
err = eventLoggerStage.Perform(stepName, func() error {
err := disk.Delete()
cloudErr, ok := err.(bicloud.Error)
if ok && cloudErr.Type() == bicloud.DiskNotFoundError {
return biui.NewSkipStageError(cloudErr, "Disk Not Found")
}
return err
})
if err != nil {
return err
}
}
return nil
}