/
detach_disk.go
57 lines (47 loc) · 1.47 KB
/
detach_disk.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
package action
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
"bosh-softlayer-cpi/api"
"bosh-softlayer-cpi/softlayer/virtual_guest_service"
"bosh-softlayer-cpi/registry"
)
type DetachDisk struct {
vmService instance.Service
registryClient registry.Client
}
func NewDetachDisk(
vmService instance.Service,
registryClient registry.Client,
) DetachDisk {
return DetachDisk{
vmService: vmService,
registryClient: registryClient,
}
}
func (dd DetachDisk) Run(vmCID VMCID, diskCID DiskCID) (interface{}, error) {
// Detach the disk
if err := dd.vmService.DetachDisk(vmCID.Int(), diskCID.Int()); err != nil {
if _, ok := err.(api.CloudError); ok {
return nil, err
}
return nil, bosherr.WrapErrorf(err, "Detaching disk '%s' from vm '%s", diskCID, vmCID)
}
exist, err := dd.registryClient.IsExist(vmCID.String())
if err != nil {
return nil, bosherr.WrapErrorf(err, "Checking if instance '%s' exists", vmCID)
}
if !exist {
return nil, nil
}
// Read VM agent settings
oldAgentSettings, err := dd.registryClient.Fetch(vmCID.String())
if err != nil {
return nil, bosherr.WrapErrorf(err, "Detaching disk '%s' from vm '%s'", diskCID, vmCID)
}
// Update VM agent settings
newAgentSettings := oldAgentSettings.DetachPersistentDisk(diskCID.String())
if err = dd.registryClient.Update(vmCID.String(), newAgentSettings); err != nil {
return nil, bosherr.WrapErrorf(err, "Detaching disk '%s' from vm '%s'", diskCID, vmCID)
}
return nil, nil
}