-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth2.go
187 lines (168 loc) · 4.93 KB
/
oauth2.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
184
185
186
187
package types
import (
"context"
"errors"
"fmt"
"github.com/rainest/decklib/pkg/crud"
"github.com/rainest/decklib/pkg/state"
"github.com/rainest/decklib/pkg/utils"
"github.com/kong/go-kong/kong"
)
// oauth2CredCRUD implements crud.Actions interface.
type oauth2CredCRUD struct {
client *kong.Client
}
func oauth2CredFromStruct(arg crud.Event) *state.Oauth2Credential {
oauth2Cred, ok := arg.Obj.(*state.Oauth2Credential)
if !ok {
panic("unexpected type, expected *state.OAuth2")
}
return oauth2Cred
}
// Create creates a Route in Kong.
// The arg should be of type crud.Event, containing the oauth2Cred to be created,
// else the function will panic.
// It returns a the created *state.Route.
func (s *oauth2CredCRUD) Create(ctx context.Context, arg ...crud.Arg) (crud.Arg, error) {
event := crud.EventFromArg(arg[0])
oauth2Cred := oauth2CredFromStruct(event)
cid := ""
if !utils.Empty(oauth2Cred.Consumer.Username) {
cid = *oauth2Cred.Consumer.Username
}
if !utils.Empty(oauth2Cred.Consumer.ID) {
cid = *oauth2Cred.Consumer.ID
}
createdOauth2Cred, err := s.client.Oauth2Credentials.Create(ctx, &cid,
&oauth2Cred.Oauth2Credential)
if err != nil {
return nil, err
}
return &state.Oauth2Credential{Oauth2Credential: *createdOauth2Cred}, nil
}
// Delete deletes a Route in Kong.
// The arg should be of type crud.Event, containing the oauth2Cred to be deleted,
// else the function will panic.
// It returns a the deleted *state.Route.
func (s *oauth2CredCRUD) Delete(ctx context.Context, arg ...crud.Arg) (crud.Arg, error) {
event := crud.EventFromArg(arg[0])
oauth2Cred := oauth2CredFromStruct(event)
cid := ""
if !utils.Empty(oauth2Cred.Consumer.Username) {
cid = *oauth2Cred.Consumer.Username
}
if !utils.Empty(oauth2Cred.Consumer.ID) {
cid = *oauth2Cred.Consumer.ID
}
err := s.client.Oauth2Credentials.Delete(ctx, &cid, oauth2Cred.ID)
if err != nil {
return nil, err
}
return oauth2Cred, nil
}
// Update updates a Route in Kong.
// The arg should be of type crud.Event, containing the oauth2Cred to be updated,
// else the function will panic.
// It returns a the updated *state.Route.
func (s *oauth2CredCRUD) Update(ctx context.Context, arg ...crud.Arg) (crud.Arg, error) {
event := crud.EventFromArg(arg[0])
oauth2Cred := oauth2CredFromStruct(event)
cid := ""
if !utils.Empty(oauth2Cred.Consumer.Username) {
cid = *oauth2Cred.Consumer.Username
}
if !utils.Empty(oauth2Cred.Consumer.ID) {
cid = *oauth2Cred.Consumer.ID
}
updatedOauth2Cred, err := s.client.Oauth2Credentials.Create(ctx, &cid,
&oauth2Cred.Oauth2Credential)
if err != nil {
return nil, err
}
return &state.Oauth2Credential{Oauth2Credential: *updatedOauth2Cred}, nil
}
type oauth2CredDiffer struct {
kind crud.Kind
currentState, targetState *state.KongState
}
func (d *oauth2CredDiffer) Deletes(handler func(crud.Event) error) error {
currentOauth2Creds, err := d.currentState.Oauth2Creds.GetAll()
if err != nil {
return fmt.Errorf("error fetching oauth2-cred from state: %w", err)
}
for _, oauth2Cred := range currentOauth2Creds {
n, err := d.deleteOauth2Cred(oauth2Cred)
if err != nil {
return err
}
if n != nil {
err = handler(*n)
if err != nil {
return err
}
}
}
return nil
}
func (d *oauth2CredDiffer) deleteOauth2Cred(oauth2Cred *state.Oauth2Credential) (
*crud.Event, error,
) {
_, err := d.targetState.Oauth2Creds.Get(*oauth2Cred.ID)
if errors.Is(err, state.ErrNotFound) {
return &crud.Event{
Op: crud.Delete,
Kind: d.kind,
Obj: oauth2Cred,
}, nil
}
if err != nil {
return nil, fmt.Errorf("looking up oauth2-cred %q: %w", *oauth2Cred.Name, err)
}
return nil, nil
}
func (d *oauth2CredDiffer) CreateAndUpdates(handler func(crud.Event) error) error {
targetOauth2Creds, err := d.targetState.Oauth2Creds.GetAll()
if err != nil {
return fmt.Errorf("error fetching oauth2-creds from state: %w", err)
}
for _, oauth2Cred := range targetOauth2Creds {
n, err := d.createUpdateOauth2Cred(oauth2Cred)
if err != nil {
return err
}
if n != nil {
err = handler(*n)
if err != nil {
return err
}
}
}
return nil
}
func (d *oauth2CredDiffer) createUpdateOauth2Cred(oauth2Cred *state.Oauth2Credential) (*crud.Event, error) {
oauth2Cred = &state.Oauth2Credential{Oauth2Credential: *oauth2Cred.DeepCopy()}
currentOauth2Cred, err := d.currentState.Oauth2Creds.Get(*oauth2Cred.ID)
if errors.Is(err, state.ErrNotFound) {
// oauth2Cred not present, create it
return &crud.Event{
Op: crud.Create,
Kind: d.kind,
Obj: oauth2Cred,
}, nil
}
if err != nil {
return nil, fmt.Errorf("error looking up oauth2-cred %q: %w",
*oauth2Cred.Name, err)
}
currentOauth2Cred = &state.Oauth2Credential{Oauth2Credential: *currentOauth2Cred.DeepCopy()}
// found, check if update needed
if !currentOauth2Cred.EqualWithOpts(oauth2Cred, false, true, false) {
return &crud.Event{
Op: crud.Update,
Kind: d.kind,
Obj: oauth2Cred,
OldObj: currentOauth2Cred,
}, nil
}
return nil, nil
}