-
Notifications
You must be signed in to change notification settings - Fork 127
/
plugin.go
96 lines (86 loc) · 2.61 KB
/
plugin.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
package kong
import (
"github.com/hbagdi/deck/crud"
"github.com/hbagdi/deck/diff"
"github.com/hbagdi/deck/state"
"github.com/hbagdi/go-kong/kong"
"github.com/pkg/errors"
)
// PluginCRUD implements Actions interface
// from the github.com/kong/crud package for the Plugin entitiy of Kong.
type PluginCRUD struct {
client *kong.Client
}
// NewPluginCRUD creates a new PluginCRUD. Client is required.
func NewPluginCRUD(client *kong.Client) (*PluginCRUD, error) {
if client == nil {
return nil, errors.New("client is required")
}
return &PluginCRUD{
client: client,
}, nil
}
func pluginFromStuct(arg diff.Event) *state.Plugin {
plugin, ok := arg.Obj.(*state.Plugin)
if !ok {
panic("unexpected type, expected *state.Plugin")
}
return plugin
}
// Create creates a Plugin in Kong.
// The arg should be of type diff.Event, containing the plugin to be created,
// else the function will panic.
// It returns a the created *state.Plugin.
func (s *PluginCRUD) Create(arg ...crud.Arg) (crud.Arg, error) {
event := eventFromArg(arg[0])
plugin := pluginFromStuct(event)
if plugin.Service != nil {
plugin.Service = &kong.Service{ID: plugin.Service.ID}
}
if plugin.Route != nil {
plugin.Route = &kong.Route{ID: plugin.Route.ID}
}
if plugin.Consumer != nil {
plugin.Consumer = &kong.Consumer{ID: plugin.Consumer.ID}
}
createdPlugin, err := s.client.Plugins.Create(nil, &plugin.Plugin)
if err != nil {
return nil, err
}
return &state.Plugin{Plugin: *createdPlugin}, nil
}
// Delete deletes a Plugin in Kong.
// The arg should be of type diff.Event, containing the plugin to be deleted,
// else the function will panic.
// It returns a the deleted *state.Plugin.
func (s *PluginCRUD) Delete(arg ...crud.Arg) (crud.Arg, error) {
event := eventFromArg(arg[0])
plugin := pluginFromStuct(event)
err := s.client.Plugins.Delete(nil, plugin.ID)
if err != nil {
return nil, err
}
return plugin, nil
}
// Update updates a Plugin in Kong.
// The arg should be of type diff.Event, containing the plugin to be updated,
// else the function will panic.
// It returns a the updated *state.Plugin.
func (s *PluginCRUD) Update(arg ...crud.Arg) (crud.Arg, error) {
event := eventFromArg(arg[0])
plugin := pluginFromStuct(event)
if plugin.Service != nil {
plugin.Service = &kong.Service{ID: plugin.Service.ID}
}
if plugin.Route != nil {
plugin.Route = &kong.Route{ID: plugin.Route.ID}
}
if plugin.Consumer != nil {
plugin.Consumer = &kong.Consumer{ID: plugin.Consumer.ID}
}
updatedPlugin, err := s.client.Plugins.Create(nil, &plugin.Plugin)
if err != nil {
return nil, err
}
return &state.Plugin{Plugin: *updatedPlugin}, nil
}