-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(managed): Add generic interfaces for external types
Add a wrapper for typed external clients that take over the conversion from resource.Managed to the actual Go struct that is always necessary in every controller. It does not reduce complexity as a whole but allows controllers to save some LoCs and focus on the important things. Keep the previous types as type aliases to avoid any breaking changes. Signed-off-by: Maximilian Blatt <[email protected]>
- Loading branch information
Showing
3 changed files
with
204 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package managed | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/crossplane/crossplane-runtime/pkg/errors" | ||
"github.com/crossplane/crossplane-runtime/pkg/resource" | ||
) | ||
|
||
const errFmtUnexpectedObjectType = "unexpected object type %T" | ||
|
||
// typedExternalConnectDisconnecterWrapper wraps a TypedExternalConnecter to a | ||
// common ExternalConnector. | ||
type typedExternalConnectDisconnecterWrapper[managed resource.Managed] struct { | ||
c TypedExternalConnectDisconnecter[managed] | ||
} | ||
|
||
func (c *typedExternalConnectDisconnecterWrapper[managed]) Connect(ctx context.Context, mg resource.Managed) (ExternalClient, error) { | ||
cr, ok := mg.(managed) | ||
if !ok { | ||
return nil, errors.Errorf(errFmtUnexpectedObjectType, mg) | ||
} | ||
external, err := c.c.Connect(ctx, cr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &typedExternalClientWrapper[managed]{c: external}, nil | ||
} | ||
|
||
func (c *typedExternalConnectDisconnecterWrapper[managed]) Disconnect(ctx context.Context) error { | ||
return c.c.Disconnect(ctx) | ||
} | ||
|
||
// typedExternalClientWrapper wraps a TypedExternalClient to a common | ||
// ExternalClient. | ||
type typedExternalClientWrapper[managed resource.Managed] struct { | ||
c TypedExternalClient[managed] | ||
} | ||
|
||
func (c *typedExternalClientWrapper[managed]) Observe(ctx context.Context, mg resource.Managed) (ExternalObservation, error) { | ||
cr, ok := mg.(managed) | ||
if !ok { | ||
return ExternalObservation{}, errors.Errorf(errFmtUnexpectedObjectType, mg) | ||
} | ||
return c.c.Observe(ctx, cr) | ||
} | ||
|
||
func (c *typedExternalClientWrapper[managed]) Create(ctx context.Context, mg resource.Managed) (ExternalCreation, error) { | ||
cr, ok := mg.(managed) | ||
if !ok { | ||
return ExternalCreation{}, errors.Errorf(errFmtUnexpectedObjectType, mg) | ||
} | ||
return c.c.Create(ctx, cr) | ||
} | ||
func (c *typedExternalClientWrapper[managed]) Update(ctx context.Context, mg resource.Managed) (ExternalUpdate, error) { | ||
cr, ok := mg.(managed) | ||
if !ok { | ||
return ExternalUpdate{}, errors.Errorf(errFmtUnexpectedObjectType, mg) | ||
} | ||
return c.c.Update(ctx, cr) | ||
} | ||
|
||
func (c *typedExternalClientWrapper[managed]) Delete(ctx context.Context, mg resource.Managed) (ExternalDelete, error) { | ||
cr, ok := mg.(managed) | ||
if !ok { | ||
return ExternalDelete{}, errors.Errorf(errFmtUnexpectedObjectType, mg) | ||
} | ||
return c.c.Delete(ctx, cr) | ||
} | ||
|
||
func (c *typedExternalClientWrapper[managed]) Disconnect(ctx context.Context) error { | ||
return c.c.Disconnect(ctx) | ||
} |