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
|
package solver
import (
"github.com/kong/deck/crud"
"github.com/kong/deck/diff"
"github.com/kong/deck/state"
"github.com/kong/deck/utils"
"github.com/kong/go-kong/kong"
)
// aclGroupCRUD implements crud.Actions interface.
type aclGroupCRUD struct {
client *kong.Client
}
func aclGroupFromStuct(arg diff.Event) *state.ACLGroup {
aclGroup, ok := arg.Obj.(*state.ACLGroup)
if !ok {
panic("unexpected type, expected *state.Route")
}
return aclGroup
}
// Create creates a Route in Kong.
// The arg should be of type diff.Event, containing the aclGroup to be created,
// else the function will panic.
// It returns a the created *state.Route.
func (s *aclGroupCRUD) Create(arg ...crud.Arg) (crud.Arg, error) {
event := eventFromArg(arg[0])
aclGroup := aclGroupFromStuct(event)
cid := ""
if !utils.Empty(aclGroup.Consumer.Username) {
cid = *aclGroup.Consumer.Username
}
if !utils.Empty(aclGroup.Consumer.ID) {
cid = *aclGroup.Consumer.ID
}
createdACLGroup, err := s.client.ACLs.Create(nil, &cid,
&aclGroup.ACLGroup)
if err != nil {
return nil, err
}
return &state.ACLGroup{ACLGroup: *createdACLGroup}, nil
}
// Delete deletes a Route in Kong.
// The arg should be of type diff.Event, containing the aclGroup to be deleted,
// else the function will panic.
// It returns a the deleted *state.Route.
func (s *aclGroupCRUD) Delete(arg ...crud.Arg) (crud.Arg, error) {
event := eventFromArg(arg[0])
aclGroup := aclGroupFromStuct(event)
cid := ""
if !utils.Empty(aclGroup.Consumer.Username) {
cid = *aclGroup.Consumer.Username
}
if !utils.Empty(aclGroup.Consumer.ID) {
cid = *aclGroup.Consumer.ID
}
err := s.client.ACLs.Delete(nil, &cid, aclGroup.ID)
if err != nil {
return nil, err
}
return aclGroup, nil
}
// Update updates a Route in Kong.
// The arg should be of type diff.Event, containing the aclGroup to be updated,
// else the function will panic.
// It returns a the updated *state.Route.
func (s *aclGroupCRUD) Update(arg ...crud.Arg) (crud.Arg, error) {
event := eventFromArg(arg[0])
aclGroup := aclGroupFromStuct(event)
cid := ""
if !utils.Empty(aclGroup.Consumer.Username) {
cid = *aclGroup.Consumer.Username
}
if !utils.Empty(aclGroup.Consumer.ID) {
cid = *aclGroup.Consumer.ID
}
updatedACLGroup, err := s.client.ACLs.Create(nil, &cid, &aclGroup.ACLGroup)
if err != nil {
return nil, err
}
return &state.ACLGroup{ACLGroup: *updatedACLGroup}, nil
}
|