Skip to content

Commit

Permalink
add plugin SDK interface and mock
Browse files Browse the repository at this point in the history
  • Loading branch information
randmonkey committed Sep 5, 2024
1 parent 8bbceb0 commit 0b5df8e
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 10 deletions.
1 change: 1 addition & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ packages:
interfaces:
ControlPlaneSDK:
ServicesSDK:
PluginSDK:
6 changes: 3 additions & 3 deletions controller/konnect/ops/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Create[
case *configurationv1beta1.KongConsumerGroup:
return e, createConsumerGroup(ctx, sdk.ConsumerGroups, ent)
case *configurationv1alpha1.KongPluginBinding:
return e, createPlugin(ctx, cl, sdk, ent)
return e, createPlugin(ctx, cl, sdk.Plugins, ent)

// ---------------------------------------------------------------------
// TODO: add other Konnect types
Expand Down Expand Up @@ -102,7 +102,7 @@ func Delete[
case *configurationv1beta1.KongConsumerGroup:
return deleteConsumerGroup(ctx, sdk.ConsumerGroups, ent)
case *configurationv1alpha1.KongPluginBinding:
return deletePlugin(ctx, sdk, ent)
return deletePlugin(ctx, sdk.Plugins, ent)

// ---------------------------------------------------------------------
// TODO: add other Konnect types
Expand Down Expand Up @@ -165,7 +165,7 @@ func Update[
case *configurationv1beta1.KongConsumerGroup:
return ctrl.Result{}, updateConsumerGroup(ctx, sdk.ConsumerGroups, cl, ent)
case *configurationv1alpha1.KongPluginBinding:
return ctrl.Result{}, updatePlugin(ctx, sdk, cl, ent)
return ctrl.Result{}, updatePlugin(ctx, sdk.Plugins, cl, ent)

// ---------------------------------------------------------------------
// TODO: add other Konnect types
Expand Down
13 changes: 6 additions & 7 deletions controller/konnect/ops/ops_kongpluginbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"

sdkkonnectgo "github.com/Kong/sdk-konnect-go"
sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
sdkkonnecterrs "github.com/Kong/sdk-konnect-go/models/sdkerrors"
Expand All @@ -32,7 +31,7 @@ import (
func createPlugin(
ctx context.Context,
cl client.Client,
sdk *sdkkonnectgo.SDK,
sdk PluginSDK,
pluginBinding *configurationv1alpha1.KongPluginBinding,
) error {
controlPlaneID := pluginBinding.GetControlPlaneID()
Expand All @@ -44,7 +43,7 @@ func createPlugin(
return err
}

resp, err := sdk.Plugins.CreatePlugin(ctx,
resp, err := sdk.CreatePlugin(ctx,
controlPlaneID,
*pluginInput,
)
Expand Down Expand Up @@ -87,7 +86,7 @@ func createPlugin(
// if the operation fails.
func updatePlugin(
ctx context.Context,
sdk *sdkkonnectgo.SDK,
sdk PluginSDK,
cl client.Client,
pb *configurationv1alpha1.KongPluginBinding,
) error {
Expand Down Expand Up @@ -121,7 +120,7 @@ func updatePlugin(
return err
}

resp, err := sdk.Plugins.UpsertPlugin(ctx,
resp, err := sdk.UpsertPlugin(ctx,
sdkkonnectops.UpsertPluginRequest{
ControlPlaneID: controlPlaneID,
PluginID: pb.GetKonnectID(),
Expand Down Expand Up @@ -167,11 +166,11 @@ func updatePlugin(
// It returns an error if the operation fails.
func deletePlugin(
ctx context.Context,
sdk *sdkkonnectgo.SDK,
sdk PluginSDK,
pb *configurationv1alpha1.KongPluginBinding,
) error {
id := pb.GetKonnectID()
_, err := sdk.Plugins.DeletePlugin(ctx, pb.GetControlPlaneID(), id)
_, err := sdk.DeletePlugin(ctx, pb.GetControlPlaneID(), id)
if errWrapped := wrapErrIfKonnectOpFailed[configurationv1alpha1.KongPluginBinding](err, DeleteOp, pb); errWrapped != nil {
// plugin delete operation returns an SDKError instead of a NotFoundError.
var sdkError *sdkkonnecterrs.SDKError
Expand Down
14 changes: 14 additions & 0 deletions controller/konnect/ops/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ops

import (
"context"

sdkkonnectcomp "github.com/Kong/sdk-konnect-go/models/components"
sdkkonnectops "github.com/Kong/sdk-konnect-go/models/operations"
)

type PluginSDK interface {
CreatePlugin(ctx context.Context, controlPlaneID string, plugin sdkkonnectcomp.PluginInput, opts ...sdkkonnectops.Option) (*sdkkonnectops.CreatePluginResponse, error)
UpsertPlugin(ctx context.Context, request sdkkonnectops.UpsertPluginRequest, opts ...sdkkonnectops.Option) (*sdkkonnectops.UpsertPluginResponse, error)
DeletePlugin(ctx context.Context, controlPlaneID string, pluginID string, opts ...sdkkonnectops.Option) (*sdkkonnectops.DeletePluginResponse, error)
}
264 changes: 264 additions & 0 deletions controller/konnect/ops/plugin_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0b5df8e

Please sign in to comment.