/
admin_openshiftversion_put.go
98 lines (84 loc) · 2.99 KB
/
admin_openshiftversion_put.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
package frontend
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"encoding/json"
"net/http"
"path/filepath"
"github.com/sirupsen/logrus"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/api/admin"
"github.com/Azure/ARO-RP/pkg/frontend/middleware"
)
func (f *frontend) putAdminOpenShiftVersion(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := ctx.Value(middleware.ContextKeyLog).(*logrus.Entry)
r.URL.Path = filepath.Dir(r.URL.Path)
converter := f.apis[admin.APIVersion].OpenShiftVersionConverter
staticValidator := f.apis[admin.APIVersion].OpenShiftVersionStaticValidator
body := r.Context().Value(middleware.ContextKeyBody).([]byte)
if len(body) == 0 || !json.Valid(body) {
api.WriteError(w, http.StatusBadRequest, api.CloudErrorCodeInvalidRequestContent, "", "The request content was invalid and could not be deserialized.")
return
}
var ext *admin.OpenShiftVersion
err := json.Unmarshal(body, &ext)
if err != nil {
api.WriteError(w, http.StatusBadRequest, api.CloudErrorCodeInvalidRequestContent, "", "The request content could not be deserialized: "+err.Error())
return
}
docs, err := f.dbOpenShiftVersions.ListAll(ctx)
if err != nil {
api.WriteError(w, http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", "Internal server error.")
return
}
var versionDoc *api.OpenShiftVersionDocument
if docs != nil {
for _, doc := range docs.OpenShiftVersionDocuments {
if doc.OpenShiftVersion.Properties.Version == ext.Properties.Version {
// prevent disabling of the default installation version
if doc.OpenShiftVersion.Properties.Default && !ext.Properties.Enabled {
api.WriteError(w, http.StatusBadRequest, api.CloudErrorCodeInvalidParameter, "properties.enabled", "You cannot disable the default installation version.")
return
}
versionDoc = doc
break
}
}
}
isCreate := versionDoc == nil
if isCreate {
err = staticValidator.Static(ext, nil)
versionDoc = &api.OpenShiftVersionDocument{
ID: f.dbOpenShiftVersions.NewUUID(),
OpenShiftVersion: &api.OpenShiftVersion{},
}
} else {
err = staticValidator.Static(ext, versionDoc.OpenShiftVersion)
}
if err != nil {
adminReply(log, w, nil, []byte{}, err)
return
}
converter.ToInternal(ext, versionDoc.OpenShiftVersion)
if isCreate {
versionDoc, err = f.dbOpenShiftVersions.Create(ctx, versionDoc)
if err != nil {
api.WriteError(w, http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", "Internal server error.")
return
}
} else {
versionDoc, err = f.dbOpenShiftVersions.Update(ctx, versionDoc)
if err != nil {
api.WriteError(w, http.StatusInternalServerError, api.CloudErrorCodeInternalServerError, "", "Internal server error.")
return
}
}
b, err := json.MarshalIndent(converter.ToExternal(versionDoc.OpenShiftVersion), "", " ")
if err == nil {
if isCreate {
err = statusCodeError(http.StatusCreated)
}
}
adminReply(log, w, nil, b, err)
}