forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_check.go
105 lines (83 loc) · 2.24 KB
/
cloud_check.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
package cmd
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshdir "github.com/cloudfoundry/bosh-cli/director"
boshui "github.com/cloudfoundry/bosh-cli/ui"
boshtbl "github.com/cloudfoundry/bosh-cli/ui/table"
)
type CloudCheckCmd struct {
deployment boshdir.Deployment
ui boshui.UI
}
func NewCloudCheckCmd(deployment boshdir.Deployment, ui boshui.UI) CloudCheckCmd {
return CloudCheckCmd{deployment: deployment, ui: ui}
}
func (c CloudCheckCmd) Run(opts CloudCheckOpts) error {
probs, err := c.deployment.ScanForProblems()
if err != nil {
return err
}
table := boshtbl.Table{
Content: "problems",
Header: []string{"#", "Type", "Description"},
SortBy: []boshtbl.ColumnSort{{Column: 0, Asc: true}},
}
for _, p := range probs {
table.Rows = append(table.Rows, []boshtbl.Value{
boshtbl.NewValueInt(p.ID),
boshtbl.NewValueString(p.Type),
boshtbl.NewValueString(p.Description),
})
}
c.ui.PrintTable(table)
if len(probs) == 0 {
return nil
} else if opts.Report {
return bosherr.Errorf("%d problem(s) found", len(probs))
}
var answers []boshdir.ProblemAnswer
if opts.Auto {
answers, err = c.defaultResolutions(probs)
if err != nil {
return err
}
} else {
answers, err = c.askForResolutions(probs)
if err != nil {
return err
}
}
err = c.ui.AskForConfirmation()
if err != nil {
return err
}
return c.deployment.ResolveProblems(answers)
}
func (c CloudCheckCmd) askForResolutions(probs []boshdir.Problem) ([]boshdir.ProblemAnswer, error) {
var answers []boshdir.ProblemAnswer
for _, prob := range probs {
var opts []string
for _, res := range prob.Resolutions {
opts = append(opts, res.Plan)
}
chosenIndex, err := c.ui.AskForChoice(prob.Description, opts)
if err != nil {
return nil, err
}
answers = append(answers, boshdir.ProblemAnswer{
ProblemID: prob.ID,
Resolution: prob.Resolutions[chosenIndex],
})
}
return answers, nil
}
func (c CloudCheckCmd) defaultResolutions(probs []boshdir.Problem) ([]boshdir.ProblemAnswer, error) {
var answers []boshdir.ProblemAnswer
for _, prob := range probs {
answers = append(answers, boshdir.ProblemAnswer{
ProblemID: prob.ID,
Resolution: boshdir.ProblemResolutionDefault,
})
}
return answers, nil
}