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
/
workload.go
134 lines (122 loc) · 2.4 KB
/
workload.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
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 (
WorkloadCollection = "workloads"
WorkloadsRoot = ProviderRoot + "/" + WorkloadCollection
WorkloadRoot = WorkloadsRoot + "/:" + VMParam
)
//
// Virtual Machine handler.
type WorkloadHandler struct {
Handler
}
//
// Add routes to the `gin` router.
func (h *WorkloadHandler) AddRoutes(e *gin.Engine) {
e.GET(WorkloadRoot, h.Get)
}
//
// List resources in a REST collection.
func (h WorkloadHandler) List(ctx *gin.Context) {
}
//
// Get a specific REST resource.
func (h WorkloadHandler) Get(ctx *gin.Context) {
status := h.Prepare(ctx)
if status != http.StatusOK {
ctx.Status(status)
return
}
m := &model.VM{
Base: model.Base{
ID: ctx.Param(VMParam),
},
}
db := h.Collector.DB()
err := db.Get(m)
if errors.Is(err, model.NotFound) {
ctx.Status(http.StatusNotFound)
return
}
defer func() {
if err != nil {
log.Trace(
err,
"url",
ctx.Request.URL)
ctx.Status(http.StatusInternalServerError)
}
}()
if err != nil {
return
}
r := Workload{}
r.With(m)
err = r.Expand(db)
if err != nil {
return
}
r.Link(h.Provider)
content := r
ctx.JSON(http.StatusOK, content)
}
//
// Workload
type Workload struct {
SelfLink string `json:"selfLink"`
VM
Host struct {
Host
Cluster struct {
Cluster
Datacenter *Datacenter `json:"datacenter"`
} `json:"cluster"`
} `json:"host"`
}
func (r *Workload) With(m *model.VM) {
r.VM.With(m)
r.Host.ID = m.Host
}
//
// Build self link (URI).
func (r *Workload) Link(p *api.Provider) {
r.SelfLink = base.Link(
WorkloadRoot,
base.Params{
base.ProviderParam: string(p.UID),
VMParam: r.ID,
})
r.Host.Link(p)
r.Host.Cluster.Link(p)
}
//
// Expand the resource.
func (r *Workload) Expand(db libmodel.DB) (err error) {
host := &model.Host{
Base: model.Base{ID: r.Host.ID},
}
err = db.Get(host)
if err != nil {
return
}
r.Host.Host.With(host)
cluster := &model.Cluster{
Base: model.Base{ID: host.Cluster},
}
err = db.Get(cluster)
if err != nil {
return
}
r.Host.Cluster.Cluster.With(cluster)
return
}