This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
folder.go
178 lines (164 loc) · 3.46 KB
/
folder.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
package vsphere
import (
"errors"
"github.com/gin-gonic/gin"
libmodel "github.com/konveyor/controller/pkg/inventory/model"
api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
model "github.com/konveyor/forklift-controller/pkg/controller/provider/model/vsphere"
"github.com/konveyor/forklift-controller/pkg/controller/provider/web/base"
"net/http"
)
//
// Routes.
const (
FolderParam = "folder"
FolderCollection = "folders"
FoldersRoot = ProviderRoot + "/" + FolderCollection
FolderRoot = FoldersRoot + "/:" + FolderParam
)
//
// Folder handler.
type FolderHandler struct {
Handler
// Selected folder.
folder *model.Folder
}
//
// Add routes to the `gin` router.
func (h *FolderHandler) AddRoutes(e *gin.Engine) {
e.GET(FoldersRoot, h.List)
e.GET(FoldersRoot+"/", h.List)
e.GET(FolderRoot, h.Get)
}
//
// List resources in a REST collection.
// A GET onn the collection that includes the `X-Watch`
// header will negotiate an upgrade of the connection
// to a websocket and push watch events.
func (h FolderHandler) List(ctx *gin.Context) {
status := h.Prepare(ctx)
if status != http.StatusOK {
ctx.Status(status)
return
}
if h.WatchRequest {
h.watch(ctx)
return
}
db := h.Collector.DB()
list := []model.Folder{}
err := db.List(&list, h.ListOptions(ctx))
if err != nil {
log.Trace(
err,
"url",
ctx.Request.URL)
ctx.Status(http.StatusInternalServerError)
return
}
content := []interface{}{}
pb := PathBuilder{DB: db}
for _, m := range list {
r := &Folder{}
r.With(&m)
r.Link(h.Provider)
r.Path = pb.Path(&m)
content = append(content, r.Content(h.Detail))
}
ctx.JSON(http.StatusOK, content)
}
//
// Get a specific REST resource.
func (h FolderHandler) Get(ctx *gin.Context) {
status := h.Prepare(ctx)
if status != http.StatusOK {
ctx.Status(status)
return
}
m := &model.Folder{
Base: model.Base{
ID: ctx.Param(FolderParam),
},
}
db := h.Collector.DB()
err := db.Get(m)
if errors.Is(err, model.NotFound) {
ctx.Status(http.StatusNotFound)
return
}
if err != nil {
log.Trace(
err,
"url",
ctx.Request.URL)
ctx.Status(http.StatusInternalServerError)
return
}
pb := PathBuilder{DB: db}
r := &Folder{}
r.With(m)
r.Link(h.Provider)
r.Path = pb.Path(m)
content := r.Content(model.MaxDetail)
ctx.JSON(http.StatusOK, content)
}
//
// Watch.
func (h *FolderHandler) watch(ctx *gin.Context) {
db := h.Collector.DB()
err := h.Watch(
ctx,
db,
&model.Folder{},
func(in libmodel.Model) (r interface{}) {
pb := PathBuilder{DB: db}
m := in.(*model.Folder)
folder := &Folder{}
folder.With(m)
folder.Link(h.Provider)
folder.Path = pb.Path(m)
r = folder
return
})
if err != nil {
log.Trace(
err,
"url",
ctx.Request.URL)
ctx.Status(http.StatusInternalServerError)
}
}
//
// REST Resource.
type Folder struct {
Resource
Folder string `json:"folder"`
Datacenter string `json:"datacenter"`
Children []model.Ref `json:"children"`
}
//
// Build the resource using the model.
func (r *Folder) With(m *model.Folder) {
r.Resource.With(&m.Base)
r.Folder = m.Folder
r.Datacenter = m.Datacenter
r.Children = m.Children
}
//
// Build self link (URI).
func (r *Folder) Link(p *api.Provider) {
r.SelfLink = base.Link(
FolderRoot,
base.Params{
base.ProviderParam: string(p.UID),
FolderParam: r.ID,
})
}
//
// Content.
func (r *Folder) Content(detail int) interface{} {
if detail == 0 {
return r.Resource
}
return r
}