forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_check.go
150 lines (122 loc) · 3.4 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
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
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: []boshtbl.Header{
boshtbl.NewHeader("#"),
boshtbl.NewHeader("Type"),
boshtbl.NewHeader("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 if len(opts.Resolutions) > 0 {
answers, err = c.applyResolutions(opts.Resolutions, 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 (_ CloudCheckCmd) applyResolutions(resolutionsToApply []string, probs []boshdir.Problem) ([]boshdir.ProblemAnswer, error) {
var answers []boshdir.ProblemAnswer
for _, prob := range probs {
if problemAnswer, found := findProblemAnswer(resolutionsToApply, prob); found {
answers = append(answers, problemAnswer)
} else {
answers = append(answers, boshdir.ProblemAnswer{
ProblemID: prob.ID,
Resolution: boshdir.ProblemResolutionSkip,
})
}
}
if len(answers) <= 0 {
return nil, bosherr.Error("Unable to apply resolution to any problem")
}
return answers, nil
}
func findProblemAnswer(resolutionsToApply []string, prob boshdir.Problem) (boshdir.ProblemAnswer, bool) {
for _, resolution := range resolutionsToApply {
for _, res := range prob.Resolutions {
if resolution == *res.Name {
return boshdir.ProblemAnswer{
ProblemID: prob.ID,
Resolution: res,
}, true
}
}
}
return boshdir.ProblemAnswer{}, false
}
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
}