/
ref_start.go
116 lines (96 loc) · 2.45 KB
/
ref_start.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
package handlers
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/replicatedhq/kurl/testgrid/tgapi/pkg/logger"
"github.com/replicatedhq/kurl/testgrid/tgapi/pkg/testinstance"
"github.com/replicatedhq/kurl/testgrid/tgapi/pkg/testrun"
"go.uber.org/zap"
)
type StartRefRequest struct {
Overwrite bool `json:"overwrite"`
Instances []PlannedInstance `json:"instances"`
}
type StartRefResponse struct {
Success bool `json:"success"`
}
type PlannedInstance struct {
ID string
KurlYAML string
KurlURL string
UpgradeYAML string
UpgradeURL string
SupportbundleYAML string
PostInstallScript string
PostUpgradeScript string
OperatingSystemName string
OperatingSystemVersion string
OperatingSystemImage string
OperatingSystemPreInit string
IsUnsupported bool
}
func StartRef(w http.ResponseWriter, r *http.Request) {
startRefRequest := StartRefRequest{}
if err := json.NewDecoder(r.Body).Decode(&startRefRequest); err != nil {
logger.Error(err)
JSON(w, 500, StartRefResponse{})
return
}
refID := mux.Vars(r)["refId"]
logger.Debug("refStart",
zap.String("ref", refID))
existingTestRun, err := testrun.TryGet(refID)
if err != nil {
logger.Error(err)
JSON(w, 500, StartRefResponse{})
return
}
if existingTestRun != nil {
if !startRefRequest.Overwrite {
JSON(w, 419, StartRefResponse{})
return
}
if err := testrun.Delete(refID); err != nil {
logger.Error(err)
JSON(w, 500, StartRefResponse{})
return
}
}
if err := testrun.Create(refID); err != nil {
logger.Error(err)
JSON(w, 500, StartRefResponse{})
return
}
for _, plannedInstance := range startRefRequest.Instances {
err := testinstance.Create(
plannedInstance.ID,
refID,
plannedInstance.KurlYAML,
plannedInstance.KurlURL,
plannedInstance.UpgradeYAML,
plannedInstance.UpgradeURL,
plannedInstance.SupportbundleYAML,
plannedInstance.PostInstallScript,
plannedInstance.PostUpgradeScript,
plannedInstance.OperatingSystemName,
plannedInstance.OperatingSystemVersion,
plannedInstance.OperatingSystemImage,
plannedInstance.OperatingSystemPreInit,
)
if err != nil {
logger.Error(err)
JSON(w, 500, StartRefResponse{})
return
}
if plannedInstance.IsUnsupported {
err := testinstance.SetInstanceUnsupported(plannedInstance.ID)
if err != nil {
logger.Error(err)
JSON(w, 500, StartRefResponse{})
return
}
}
}
JSON(w, 200, StartRefResponse{Success: true})
}