-
Notifications
You must be signed in to change notification settings - Fork 7
/
resourcegroup_router.go
183 lines (158 loc) · 5.74 KB
/
resourcegroup_router.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
180
181
182
183
package router
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/grafana/grafana-app-sdk/plugin"
"github.com/grafana/grafana-app-sdk/resource"
)
// Store is the interface for a CRD storage component.
type Store interface {
Add(ctx context.Context, obj resource.Object) (resource.Object, error)
Get(ctx context.Context, kind string, identifier resource.Identifier) (resource.Object, error)
List(ctx context.Context, kind, namespace string, filters ...string) (resource.ListObject, error)
Update(ctx context.Context, obj resource.Object) (resource.Object, error)
Delete(ctx context.Context, kind string, identifier resource.Identifier) error
}
// ResourceGroupRouter is a Router which exposes generic CRUD routes for every resource contained in a given group.
type ResourceGroupRouter struct {
*JSONRouter
resourceGroup resource.KindCollection
namespace string
store Store
}
// NewResourceGroupRouter returns a new ResourceGroupRouter,
// exposing CRUD routes to manipulate resources in given group.
func NewResourceGroupRouter(
resourceGroup resource.KindCollection,
namespace string,
clientGenerator resource.ClientGenerator,
) (*ResourceGroupRouter, error) {
store := resource.NewStore(clientGenerator, resourceGroup)
return NewResourceGroupRouterWithStore(resourceGroup, namespace, store)
}
// NewResourceGroupRouterWithStore returns a new ResourceGroupRouter with pre-configured Store.
func NewResourceGroupRouterWithStore(
resourceGroup resource.KindCollection,
namespace string,
store Store,
) (*ResourceGroupRouter, error) {
router := &ResourceGroupRouter{
JSONRouter: NewJSONRouter(),
resourceGroup: resourceGroup,
store: store,
namespace: namespace,
}
for _, schema := range router.resourceGroup.Kinds() {
// TODO: all possible versions for each kind should be handled, address this with SchemaGroup in codegen?
baseRoute := fmt.Sprintf(
"%s/%s/%s",
schema.Group(),
schema.Version(),
schema.Plural(),
)
router.HandleWithCode(
baseRoute, router.createResource(schema), http.StatusAccepted, http.MethodPost,
)
router.Handle(
baseRoute, router.listResources(schema), http.MethodGet,
)
router.Handle(
fmt.Sprintf("%s/{name}", baseRoute), router.getResource(schema), http.MethodGet,
)
router.HandleWithCode(
fmt.Sprintf("%s/{name}", baseRoute), router.updateResource(schema), http.StatusAccepted, http.MethodPut,
)
router.Handle(
fmt.Sprintf("%s/{name}", baseRoute), router.deleteResource(schema), http.MethodDelete,
)
}
return router, nil
}
func (router *ResourceGroupRouter) createResource(cr resource.Schema) JSONHandlerFunc {
return func(ctx context.Context, request JSONRequest) (JSONResponse, error) {
toBeInserted := cr.ZeroValue()
// TODO: use Unmarshal() method for version stuff
if err := json.NewDecoder(request.Body).Decode(toBeInserted); err != nil {
return nil, plugin.WrapError(http.StatusBadRequest, err)
}
// The only bit of static metadata the user can specify here is the name
toBeInserted.SetStaticMetadata(resource.StaticMetadata{
Name: toBeInserted.GetName(),
Namespace: router.namespace,
Group: cr.Group(),
Version: cr.Version(),
Kind: cr.Kind(),
})
addedResource, err := router.store.Add(ctx, toBeInserted)
if err != nil {
return nil, plugin.WrapError(http.StatusInternalServerError, err)
}
return addedResource, nil
}
}
func (router *ResourceGroupRouter) listResources(cr resource.Schema) JSONHandlerFunc {
return func(ctx context.Context, request JSONRequest) (JSONResponse, error) {
resources, err := router.store.List(ctx, cr.Kind(), router.namespace) // TODO: support labels
if err != nil {
return nil, plugin.WrapError(http.StatusInternalServerError, err)
}
return resources, nil
}
}
func (router *ResourceGroupRouter) getResource(cr resource.Schema) JSONHandlerFunc {
return func(ctx context.Context, request JSONRequest) (JSONResponse, error) {
name, ok := request.Vars.Get("name")
if !ok {
return nil, plugin.NewError(http.StatusBadRequest, "must provide resource name")
}
obj, err := router.store.Get(ctx, cr.Kind(), resource.Identifier{
Namespace: router.namespace,
Name: name,
})
if err != nil {
return nil, plugin.WrapError(http.StatusInternalServerError, err)
}
return obj, nil
}
}
func (router *ResourceGroupRouter) updateResource(cr resource.Schema) JSONHandlerFunc {
return func(ctx context.Context, request JSONRequest) (JSONResponse, error) {
if _, ok := request.Vars.Get("name"); !ok {
return nil, plugin.NewError(http.StatusBadRequest, "must provide resource name")
}
updatedResource := cr.ZeroValue()
if err := json.NewDecoder(request.Body).Decode(updatedResource); err != nil {
return nil, plugin.WrapError(http.StatusBadRequest, err)
}
// The only bit of static metadata the user can specify here is the name
updatedResource.SetStaticMetadata(resource.StaticMetadata{
Name: updatedResource.GetName(),
Namespace: router.namespace,
Group: cr.Group(),
Version: cr.Version(),
Kind: cr.Kind(),
})
updated, err := router.store.Update(ctx, updatedResource)
if err != nil {
return nil, plugin.WrapError(http.StatusInternalServerError, err)
}
return updated, nil
}
}
func (router *ResourceGroupRouter) deleteResource(cr resource.Schema) JSONHandlerFunc {
return func(ctx context.Context, request JSONRequest) (JSONResponse, error) {
name, ok := request.Vars.Get("name")
if !ok {
return nil, plugin.NewError(http.StatusBadRequest, "must provide resource name")
}
if err := router.store.Delete(ctx, cr.Kind(), resource.Identifier{
Namespace: router.namespace,
Name: name,
}); err != nil {
return nil, plugin.WrapError(http.StatusInternalServerError, err)
}
return nil, nil
}
}