forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment.go
102 lines (85 loc) · 2.48 KB
/
deployment.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
package deployment
import (
"fmt"
"time"
bicloud "github.com/cloudfoundry/bosh-cli/cloud"
bidisk "github.com/cloudfoundry/bosh-cli/deployment/disk"
biinstance "github.com/cloudfoundry/bosh-cli/deployment/instance"
bistemcell "github.com/cloudfoundry/bosh-cli/stemcell"
biui "github.com/cloudfoundry/bosh-cli/ui"
)
type Deployment interface {
Delete(biui.Stage) error
}
type deployment struct {
instances []biinstance.Instance
disks []bidisk.Disk
stemcells []bistemcell.CloudStemcell
pingTimeout time.Duration
pingDelay time.Duration
}
func NewDeployment(
instances []biinstance.Instance,
disks []bidisk.Disk,
stemcells []bistemcell.CloudStemcell,
pingTimeout time.Duration,
pingDelay time.Duration,
) Deployment {
return &deployment{
instances: instances,
disks: disks,
stemcells: stemcells,
pingTimeout: pingTimeout,
pingDelay: pingDelay,
}
}
func (d *deployment) Delete(deleteStage biui.Stage) error {
// le sigh... consuming from an array sucks without generics
for len(d.instances) > 0 {
lastIdx := len(d.instances) - 1
instance := d.instances[lastIdx]
if err := instance.Delete(d.pingTimeout, d.pingDelay, deleteStage); err != nil {
return err
}
d.instances = d.instances[:lastIdx]
}
for len(d.disks) > 0 {
lastIdx := len(d.disks) - 1
disk := d.disks[lastIdx]
if err := d.deleteDisk(deleteStage, disk); err != nil {
return err
}
d.disks = d.disks[:lastIdx]
}
for len(d.stemcells) > 0 {
lastIdx := len(d.stemcells) - 1
stemcell := d.stemcells[lastIdx]
if err := d.deleteStemcell(deleteStage, stemcell); err != nil {
return err
}
d.stemcells = d.stemcells[:lastIdx]
}
return nil
}
func (d *deployment) deleteDisk(deleteStage biui.Stage, disk bidisk.Disk) error {
stepName := fmt.Sprintf("Deleting disk '%s'", disk.CID())
return deleteStage.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
})
}
func (d *deployment) deleteStemcell(deleteStage biui.Stage, stemcell bistemcell.CloudStemcell) error {
stepName := fmt.Sprintf("Deleting stemcell '%s'", stemcell.CID())
return deleteStage.Perform(stepName, func() error {
err := stemcell.Delete()
cloudErr, ok := err.(bicloud.Error)
if ok && cloudErr.Type() == bicloud.StemcellNotFoundError {
return biui.NewSkipStageError(cloudErr, "Stemcell not found")
}
return err
})
}