/
directimage.go
179 lines (163 loc) · 4.12 KB
/
directimage.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package web
import (
"database/sql"
"net/http"
"github.com/gin-gonic/gin"
migapi "github.com/konveyor/mig-controller/pkg/apis/migration/v1alpha1"
"github.com/konveyor/mig-controller/pkg/controller/discovery/model"
auth "k8s.io/api/authorization/v1"
)
const (
DirectImageParam = "directimagemigration"
DirectImagesRoot = Root + "/directimagemigrations"
DirectImageRoot = DirectImagesRoot + "/:" + DirectImageParam
)
// DirectImage (route) handler.
type DirectImageMigrationHandler struct {
// Base
BaseHandler
// DirectImage referenced in the request.
directImage model.DirectImageMigration
}
// Add DIM routes.
func (h DirectImageMigrationHandler) AddRoutes(r *gin.Engine) {
r.GET(DirectImagesRoot, h.List)
r.GET(DirectImagesRoot+"/", h.List)
r.GET(DirectImageRoot, h.Get)
}
// Prepare to fulfil the request.
// Fetch the referenced dim.
// Perform SAR authorization.
func (h *DirectImageMigrationHandler) Prepare(ctx *gin.Context) int {
status := h.BaseHandler.Prepare(ctx)
if status != http.StatusOK {
return status
}
name := ctx.Param(DirectImageParam)
if name != "" {
h.directImage = model.DirectImageMigration{
CR: model.CR{
Namespace: ctx.Param(NsParam),
Name: ctx.Param(DirectImageParam),
},
}
err := h.directImage.Get(h.container.Db)
if err != nil {
if err != sql.ErrNoRows {
Log.Trace(err)
return http.StatusInternalServerError
} else {
return http.StatusNotFound
}
}
}
status = h.allow(h.getSAR())
if status != http.StatusOK {
return status
}
return http.StatusOK
}
// Build the appropriate SAR object.
// The subject is the DirectImageMigration.
func (h *DirectImageMigrationHandler) getSAR() auth.SelfSubjectAccessReview {
return auth.SelfSubjectAccessReview{
Spec: auth.SelfSubjectAccessReviewSpec{
ResourceAttributes: &auth.ResourceAttributes{
Group: "apps",
Resource: "DirectImageMigration",
Namespace: h.directImage.Namespace,
Name: h.directImage.Name,
Verb: "get",
},
},
}
}
// List all of the dims in the namespace.
func (h DirectImageMigrationHandler) List(ctx *gin.Context) {
status := h.Prepare(ctx)
if status != http.StatusOK {
ctx.Status(status)
return
}
db := h.container.Db
collection := model.DirectImageMigration{}
count, err := collection.Count(db, model.ListOptions{})
if err != nil {
Log.Trace(err)
ctx.Status(http.StatusInternalServerError)
return
}
list, err := collection.List(db, model.ListOptions{})
if err != nil {
Log.Trace(err)
ctx.Status(http.StatusInternalServerError)
return
}
content := DirectImageList{
Count: count,
}
for _, m := range list {
r := DirectImage{}
r.With(m)
r.SelfLink = h.Link(m)
content.Items = append(content.Items, r)
}
ctx.JSON(http.StatusOK, content)
}
// Get a specific dim.
func (h DirectImageMigrationHandler) Get(ctx *gin.Context) {
status := h.Prepare(ctx)
if status != http.StatusOK {
ctx.Status(status)
return
}
err := h.directImage.Get(h.container.Db)
if err != nil {
if err != sql.ErrNoRows {
Log.Trace(err)
ctx.Status(http.StatusInternalServerError)
return
} else {
ctx.Status(http.StatusNotFound)
return
}
}
r := DirectImage{}
r.With(&h.directImage)
r.SelfLink = h.Link(&h.directImage)
content := r
ctx.JSON(http.StatusOK, content)
}
// Build self link.
func (h DirectImageMigrationHandler) Link(m *model.DirectImageMigration) string {
return h.BaseHandler.Link(
DirectImageRoot,
Params{
NsParam: m.Namespace,
DirectImageParam: m.Name,
})
}
// DirectImage REST resource.
type DirectImage struct {
// The k8s namespace.
Namespace string `json:"namespace,omitempty"`
// The k8s name.
Name string `json:"name"`
// Self URI.
SelfLink string `json:"selfLink"`
// Raw k8s object.
Object *migapi.DirectImageMigration `json:"object,omitempty"`
}
// Build the resource.
func (r *DirectImage) With(m *model.DirectImageMigration) {
r.Namespace = m.Namespace
r.Name = m.Name
r.Object = m.DecodeObject()
}
// DirectImage collection REST resource.
type DirectImageList struct {
// Total number in the collection.
Count int64 `json:"count"`
// List of resources.
Items []DirectImage `json:"resources"`
}