forked from cloudfoundry/bosh-io-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstemcells_controller.go
165 lines (128 loc) · 4.17 KB
/
stemcells_controller.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package controllers
import (
"net/http"
bosherr "github.com/cloudfoundry/bosh-agent/errors"
boshlog "github.com/cloudfoundry/bosh-agent/logger"
semiver "github.com/cppforlife/go-semi-semantic/version"
mart "github.com/go-martini/martini"
martrend "github.com/martini-contrib/render"
bhstemsrepo "github.com/cppforlife/bosh-hub/stemcell/stemsrepo"
bhstemui "github.com/cppforlife/bosh-hub/ui/stemcell"
)
type StemcellsController struct {
stemcellsRepo bhstemsrepo.StemcellsRepository
indexTmpl string
errorTmpl string
logTag string
logger boshlog.Logger
}
func NewStemcellsController(
stemcellsRepo bhstemsrepo.StemcellsRepository,
logger boshlog.Logger,
) StemcellsController {
return StemcellsController{
stemcellsRepo: stemcellsRepo,
indexTmpl: "stemcells/index",
errorTmpl: "error",
logTag: "StemcellsController",
logger: logger,
}
}
type stemcellsControllerIndexPage struct {
DistroGroups bhstemui.DistroGroups
Filter bhstemui.StemcellFilter
}
func (c StemcellsController) Index(req *http.Request, r martrend.Render, params mart.Params) {
filter := bhstemui.StemcellFilter{
Name: params["_1"],
IncludeAll: c.isParamTrue(req.URL.Query().Get("all")),
IncludeDeprecatedDistros: true,
}
stemcells, err := c.stemcellsRepo.FindAll(filter.Name)
if err != nil {
r.HTML(500, c.errorTmpl, err)
return
}
// Show either groups of stemcells by OS or for a specific stemcell name
distroGroups := bhstemui.NewDistroGroups(stemcells, filter)
r.HTML(200, c.indexTmpl, stemcellsControllerIndexPage{distroGroups, filter})
}
// Show uses '_1' param as stemcell name and 'v' param as release version
func (c StemcellsController) Download(req *http.Request, r martrend.Render, params mart.Params) {
relSource := params["_1"]
if len(relSource) == 0 {
err := bosherr.New("Param 'source' must be non-empty")
r.HTML(400, c.errorTmpl, err)
return
}
stemcells, err := c.stemcellsRepo.FindAll(relSource)
if err != nil {
r.HTML(500, c.errorTmpl, err)
return
}
filter := bhstemui.StemcellFilter{Name: relSource, IncludeAll: true}
// List of versions for the specific stemcell name
// todo really filtering below should be part of the repo instead of ui
uniqVerStems := bhstemui.NewUniqueVersionStemcells(stemcells, filter)
sortedStemcells := uniqVerStems.ForAPI()
relVersion := req.URL.Query().Get("v")
preferLight := true
lightParam := req.URL.Query().Get("light")
if lightParam != "" {
preferLight = c.isParamTrue(lightParam)
}
// todo remove china stemcell once consolidated into light stemcells
chinaParam := req.URL.Query().Get("china")
mustBeForChina := c.isParamTrue(chinaParam)
if relVersion == "" {
if len(sortedStemcells) == 0 {
err := bosherr.New("Latest stemcell is not found")
r.HTML(404, c.errorTmpl, err)
return
}
url, err := sortedStemcells[0].ActualDownloadURL(preferLight, mustBeForChina)
if err != nil {
r.HTML(404, c.errorTmpl, err)
return
}
r.Redirect(url)
return
}
ver, err := semiver.NewVersionFromString(relVersion)
if err != nil {
err = bosherr.New("Version '%s' is not valid: %s", relVersion, err)
r.HTML(400, c.errorTmpl, err)
return
}
for _, stemcell := range sortedStemcells {
if stemcell.Version.IsEq(ver) {
url, err := stemcell.ActualDownloadURL(preferLight, mustBeForChina)
if err != nil {
r.HTML(404, c.errorTmpl, err)
return
}
r.Redirect(url)
return
}
}
err = bosherr.New("Stemcell version '%s' is not found", relVersion)
r.HTML(404, c.errorTmpl, err)
}
func (c StemcellsController) isParamTrue(s string) bool {
return s == "1" || s == "true" || s == "t"
}
func (c StemcellsController) APIV1Index(req *http.Request, r martrend.Render, params mart.Params) {
filter := bhstemui.StemcellFilter{Name: params["_1"]}
if len(filter.Name) == 0 {
r.JSON(400, map[string]string{"error": "Param 'name' must be non-empty"})
return
}
stemcells, err := c.stemcellsRepo.FindAll(filter.Name)
if err != nil {
r.JSON(500, map[string]string{"error": err.Error()})
return
}
// Show list of latest versions for the specific stemcell name
uniqVerStems := bhstemui.NewUniqueVersionStemcells(stemcells, filter)
r.JSON(200, uniqVerStems.ForAPI())
}