Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ngrokapi): Refactor ngrokapi package to better support mocking #578

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/controller/ingress/domain_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ import (

"github.com/go-logr/logr"
"github.com/ngrok/ngrok-api-go/v7"
"github.com/ngrok/ngrok-api-go/v7/reserved_domains"
ingressv1alpha1 "github.com/ngrok/ngrok-operator/api/ingress/v1alpha1"
"github.com/ngrok/ngrok-operator/internal/controller"
"github.com/ngrok/ngrok-operator/internal/ngrokapi"
)

// DomainReconciler reconciles a Domain object
Expand All @@ -50,7 +50,7 @@ type DomainReconciler struct {
Log logr.Logger
Scheme *runtime.Scheme
Recorder record.EventRecorder
DomainsClient *reserved_domains.Client
DomainsClient ngrokapi.DomainClient

controller *controller.BaseController[*ingressv1alpha1.Domain]
}
Expand Down
5 changes: 2 additions & 3 deletions internal/controller/ingress/httpsedge_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

"github.com/go-logr/logr"
"github.com/ngrok/ngrok-api-go/v7"
"github.com/ngrok/ngrok-api-go/v7/backends/tunnel_group"
ingressv1alpha1 "github.com/ngrok/ngrok-operator/api/ingress/v1alpha1"
"github.com/ngrok/ngrok-operator/internal/controller"
ierr "github.com/ngrok/ngrok-operator/internal/errors"
Expand Down Expand Up @@ -508,11 +507,11 @@

// Tunnel Group Backend planner
type tunnelGroupBackendReconciler struct {
client *tunnel_group.Client
client ngrokapi.TunnelGroupBackendsClient
backends []*ngrok.TunnelGroupBackend
}

func newTunnelGroupBackendReconciler(client *tunnel_group.Client) (*tunnelGroupBackendReconciler, error) {
func newTunnelGroupBackendReconciler(client ngrokapi.TunnelGroupBackendsClient) (*tunnelGroupBackendReconciler, error) {

Check warning on line 514 in internal/controller/ingress/httpsedge_controller.go

View check run for this annotation

Codecov / codecov/patch

internal/controller/ingress/httpsedge_controller.go#L514

Added line #L514 was not covered by tests
backends := make([]*ngrok.TunnelGroupBackend, 0)
iter := client.List(&ngrok.Paging{})
for iter.Next(context.Background()) {
Expand Down
7 changes: 3 additions & 4 deletions internal/controller/ingress/ippolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ import (

"github.com/go-logr/logr"
"github.com/ngrok/ngrok-api-go/v7"
"github.com/ngrok/ngrok-api-go/v7/ip_policies"
"github.com/ngrok/ngrok-api-go/v7/ip_policy_rules"
ingressv1alpha1 "github.com/ngrok/ngrok-operator/api/ingress/v1alpha1"
"github.com/ngrok/ngrok-operator/internal/controller"
"github.com/ngrok/ngrok-operator/internal/ngrokapi"
)

const (
Expand All @@ -56,8 +55,8 @@ type IPPolicyReconciler struct {
Scheme *runtime.Scheme
Recorder record.EventRecorder

IPPoliciesClient *ip_policies.Client
IPPolicyRulesClient *ip_policy_rules.Client
IPPoliciesClient ngrokapi.IPPoliciesClient
IPPolicyRulesClient ngrokapi.IPPolicyRulesClient

controller *controller.BaseController[*ingressv1alpha1.IPPolicy]
}
Expand Down
26 changes: 13 additions & 13 deletions internal/ngrokapi/bindingendpoint_aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/stretchr/testify/assert"

v6 "github.com/ngrok/ngrok-api-go/v7"
"github.com/ngrok/ngrok-api-go/v7"
bindingsv1alpha1 "github.com/ngrok/ngrok-operator/api/bindings/v1alpha1"
)

Expand Down Expand Up @@ -52,14 +52,14 @@ func Test_AggregateBindingEndpoints(t *testing.T) {

tests := []struct {
name string
endpoints []v6.Endpoint
endpoints []ngrok.Endpoint
want AggregatedEndpoints
wantErr bool
}{
{"empty", []v6.Endpoint{}, AggregatedEndpoints{}, false},
{"empty", []ngrok.Endpoint{}, AggregatedEndpoints{}, false},
{
name: "single",
endpoints: []v6.Endpoint{
endpoints: []ngrok.Endpoint{
{ID: "ep_123", PublicURL: "https://service1.namespace1"},
},
want: AggregatedEndpoints{
Expand All @@ -76,7 +76,7 @@ func Test_AggregateBindingEndpoints(t *testing.T) {
},
Status: bindingsv1alpha1.BoundEndpointStatus{
Endpoints: []bindingsv1alpha1.BindingEndpoint{
{Ref: v6.Ref{ID: "ep_123"}},
{Ref: ngrok.Ref{ID: "ep_123"}},
},
},
},
Expand All @@ -85,7 +85,7 @@ func Test_AggregateBindingEndpoints(t *testing.T) {
},
{
name: "full",
endpoints: []v6.Endpoint{
endpoints: []ngrok.Endpoint{
{ID: "ep_100", PublicURL: "https://service1.namespace1"},
{ID: "ep_101", PublicURL: "https://service1.namespace1"},
{ID: "ep_102", PublicURL: "https://service1.namespace1"},
Expand All @@ -108,9 +108,9 @@ func Test_AggregateBindingEndpoints(t *testing.T) {
},
Status: bindingsv1alpha1.BoundEndpointStatus{
Endpoints: []bindingsv1alpha1.BindingEndpoint{
{Ref: v6.Ref{ID: "ep_100"}},
{Ref: v6.Ref{ID: "ep_101"}},
{Ref: v6.Ref{ID: "ep_102"}},
{Ref: ngrok.Ref{ID: "ep_100"}},
{Ref: ngrok.Ref{ID: "ep_101"}},
{Ref: ngrok.Ref{ID: "ep_102"}},
},
},
},
Expand All @@ -127,8 +127,8 @@ func Test_AggregateBindingEndpoints(t *testing.T) {
},
Status: bindingsv1alpha1.BoundEndpointStatus{
Endpoints: []bindingsv1alpha1.BindingEndpoint{
{Ref: v6.Ref{ID: "ep_200"}},
{Ref: v6.Ref{ID: "ep_201"}},
{Ref: ngrok.Ref{ID: "ep_200"}},
{Ref: ngrok.Ref{ID: "ep_201"}},
},
},
},
Expand All @@ -145,7 +145,7 @@ func Test_AggregateBindingEndpoints(t *testing.T) {
},
Status: bindingsv1alpha1.BoundEndpointStatus{
Endpoints: []bindingsv1alpha1.BindingEndpoint{
{Ref: v6.Ref{ID: "ep_300"}},
{Ref: ngrok.Ref{ID: "ep_300"}},
},
},
},
Expand All @@ -162,7 +162,7 @@ func Test_AggregateBindingEndpoints(t *testing.T) {
},
Status: bindingsv1alpha1.BoundEndpointStatus{
Endpoints: []bindingsv1alpha1.BindingEndpoint{
{Ref: v6.Ref{ID: "ep_400"}},
{Ref: ngrok.Ref{ID: "ep_400"}},
},
},
},
Expand Down
149 changes: 127 additions & 22 deletions internal/ngrokapi/clientset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ngrokapi

import (
"context"

"github.com/ngrok/ngrok-api-go/v7"
tunnel_group_backends "github.com/ngrok/ngrok-api-go/v7/backends/tunnel_group"
https_edges "github.com/ngrok/ngrok-api-go/v7/edges/https"
Expand All @@ -16,18 +18,18 @@
)

type Clientset interface {
Domains() *reserved_domains.Client
Domains() DomainClient
EdgeModules() EdgeModulesClientset
Endpoints() *endpoints.Client
HTTPSEdges() *https_edges.Client
HTTPSEdgeRoutes() *https_edge_routes.Client
IPPolicies() *ip_policies.Client
IPPolicyRules() *ip_policy_rules.Client
KubernetesOperators() *kubernetes_operators.Client
TCPAddresses() *reserved_addrs.Client
TCPEdges() *tcp_edges.Client
TLSEdges() *tls_edges.Client
TunnelGroupBackends() *tunnel_group_backends.Client
Endpoints() EndpointsClient
HTTPSEdges() HTTPSEdgeClient
HTTPSEdgeRoutes() HTTPSEdgeRoutesClient
IPPolicies() IPPoliciesClient
IPPolicyRules() IPPolicyRulesClient
KubernetesOperators() KubernetesOperatorsClient
TCPAddresses() TCPAddressesClient
TCPEdges() TCPEdgesClient
TLSEdges() TLSEdgesClient
TunnelGroupBackends() TunnelGroupBackendsClient
}

type DefaultClientset struct {
Expand Down Expand Up @@ -63,50 +65,153 @@
}
}

func (c *DefaultClientset) Domains() *reserved_domains.Client {
type Creator[R, T any] interface {
Create(context.Context, R) (T, error)
}

type Reader[T any] interface {
Get(context.Context, string) (T, error)
}

type Updater[R, T any] interface {
Update(context.Context, R) (T, error)
}

type Deletor interface {
Delete(context.Context, string) error
}

type Lister[T any] interface {
List(*ngrok.Paging) ngrok.Iter[T]
}

type DomainClient interface {
Creator[*ngrok.ReservedDomainCreate, *ngrok.ReservedDomain]
Reader[*ngrok.ReservedDomain]
Updater[*ngrok.ReservedDomainUpdate, *ngrok.ReservedDomain]
Deletor
Lister[*ngrok.ReservedDomain]
}

func (c *DefaultClientset) Domains() DomainClient {

Check warning on line 96 in internal/ngrokapi/clientset.go

View check run for this annotation

Codecov / codecov/patch

internal/ngrokapi/clientset.go#L96

Added line #L96 was not covered by tests
return c.domainsClient
}

func (c *DefaultClientset) EdgeModules() EdgeModulesClientset {
return c.edgeModulesClientset
}

func (c *DefaultClientset) Endpoints() *endpoints.Client {
type EndpointsClient interface {
Creator[*ngrok.EndpointCreate, *ngrok.Endpoint]
Reader[*ngrok.Endpoint]
Updater[*ngrok.EndpointUpdate, *ngrok.Endpoint]
Deletor
Lister[*ngrok.Endpoint]
}

func (c *DefaultClientset) Endpoints() EndpointsClient {

Check warning on line 112 in internal/ngrokapi/clientset.go

View check run for this annotation

Codecov / codecov/patch

internal/ngrokapi/clientset.go#L112

Added line #L112 was not covered by tests
return c.endpointsClient
}

func (c *DefaultClientset) HTTPSEdges() *https_edges.Client {
type HTTPSEdgeClient interface {
Creator[*ngrok.HTTPSEdgeCreate, *ngrok.HTTPSEdge]
Reader[*ngrok.HTTPSEdge]
Updater[*ngrok.HTTPSEdgeUpdate, *ngrok.HTTPSEdge]
Deletor
Lister[*ngrok.HTTPSEdge]
}

func (c *DefaultClientset) HTTPSEdges() HTTPSEdgeClient {

Check warning on line 124 in internal/ngrokapi/clientset.go

View check run for this annotation

Codecov / codecov/patch

internal/ngrokapi/clientset.go#L124

Added line #L124 was not covered by tests
return c.httpsEdgesClient
}

func (c *DefaultClientset) HTTPSEdgeRoutes() *https_edge_routes.Client {
type HTTPSEdgeRoutesClient interface {
Creator[*ngrok.HTTPSEdgeRouteCreate, *ngrok.HTTPSEdgeRoute]
Get(context.Context, *ngrok.EdgeRouteItem) (*ngrok.HTTPSEdgeRoute, error)
Updater[*ngrok.HTTPSEdgeRouteUpdate, *ngrok.HTTPSEdgeRoute]
Delete(context.Context, *ngrok.EdgeRouteItem) error
}

func (c *DefaultClientset) HTTPSEdgeRoutes() HTTPSEdgeRoutesClient {

Check warning on line 135 in internal/ngrokapi/clientset.go

View check run for this annotation

Codecov / codecov/patch

internal/ngrokapi/clientset.go#L135

Added line #L135 was not covered by tests
return c.httpsEdgeRoutesClient
}

func (c *DefaultClientset) IPPolicies() *ip_policies.Client {
type IPPoliciesClient interface {
Creator[*ngrok.IPPolicyCreate, *ngrok.IPPolicy]
Reader[*ngrok.IPPolicy]
Updater[*ngrok.IPPolicyUpdate, *ngrok.IPPolicy]
Deletor
}

func (c *DefaultClientset) IPPolicies() IPPoliciesClient {

Check warning on line 146 in internal/ngrokapi/clientset.go

View check run for this annotation

Codecov / codecov/patch

internal/ngrokapi/clientset.go#L146

Added line #L146 was not covered by tests
return c.ipPoliciesClient
}

func (c *DefaultClientset) IPPolicyRules() *ip_policy_rules.Client {
type IPPolicyRulesClient interface {
Creator[*ngrok.IPPolicyRuleCreate, *ngrok.IPPolicyRule]
Deletor
Updater[*ngrok.IPPolicyRuleUpdate, *ngrok.IPPolicyRule]
Lister[*ngrok.IPPolicyRule]
}

func (c *DefaultClientset) IPPolicyRules() IPPolicyRulesClient {

Check warning on line 157 in internal/ngrokapi/clientset.go

View check run for this annotation

Codecov / codecov/patch

internal/ngrokapi/clientset.go#L157

Added line #L157 was not covered by tests
return c.ipPolicyRulesClient
}

func (c *DefaultClientset) KubernetesOperators() *kubernetes_operators.Client {
type KubernetesOperatorsClient interface {
Creator[*ngrok.KubernetesOperatorCreate, *ngrok.KubernetesOperator]
Reader[*ngrok.KubernetesOperator]
Updater[*ngrok.KubernetesOperatorUpdate, *ngrok.KubernetesOperator]
Deletor
Lister[*ngrok.KubernetesOperator]
GetBoundEndpoints(string, *ngrok.Paging) ngrok.Iter[*ngrok.Endpoint]
}

func (c *DefaultClientset) KubernetesOperators() KubernetesOperatorsClient {

Check warning on line 170 in internal/ngrokapi/clientset.go

View check run for this annotation

Codecov / codecov/patch

internal/ngrokapi/clientset.go#L170

Added line #L170 was not covered by tests
return c.kubernetesOperatorsClient
}

func (c *DefaultClientset) TCPAddresses() *reserved_addrs.Client {
type TCPAddressesClient interface {
Creator[*ngrok.ReservedAddrCreate, *ngrok.ReservedAddr]
Updater[*ngrok.ReservedAddrUpdate, *ngrok.ReservedAddr]
Lister[*ngrok.ReservedAddr]
}

func (c *DefaultClientset) TCPAddresses() TCPAddressesClient {

Check warning on line 180 in internal/ngrokapi/clientset.go

View check run for this annotation

Codecov / codecov/patch

internal/ngrokapi/clientset.go#L180

Added line #L180 was not covered by tests
return c.tcpAddrsClient
}

func (c *DefaultClientset) TLSEdges() *tls_edges.Client {
type TLSEdgesClient interface {
Creator[*ngrok.TLSEdgeCreate, *ngrok.TLSEdge]
Reader[*ngrok.TLSEdge]
Updater[*ngrok.TLSEdgeUpdate, *ngrok.TLSEdge]
Deletor
Lister[*ngrok.TLSEdge]
}

func (c *DefaultClientset) TLSEdges() TLSEdgesClient {

Check warning on line 192 in internal/ngrokapi/clientset.go

View check run for this annotation

Codecov / codecov/patch

internal/ngrokapi/clientset.go#L192

Added line #L192 was not covered by tests
return c.tlsEdgesClient
}

func (c *DefaultClientset) TCPEdges() *tcp_edges.Client {
type TCPEdgesClient interface {
Creator[*ngrok.TCPEdgeCreate, *ngrok.TCPEdge]
Reader[*ngrok.TCPEdge]
Updater[*ngrok.TCPEdgeUpdate, *ngrok.TCPEdge]
Deletor
Lister[*ngrok.TCPEdge]
}

func (c *DefaultClientset) TCPEdges() TCPEdgesClient {

Check warning on line 204 in internal/ngrokapi/clientset.go

View check run for this annotation

Codecov / codecov/patch

internal/ngrokapi/clientset.go#L204

Added line #L204 was not covered by tests
return c.tcpEdgesClient
}

func (c *DefaultClientset) TunnelGroupBackends() *tunnel_group_backends.Client {
type TunnelGroupBackendsClient interface {
Creator[*ngrok.TunnelGroupBackendCreate, *ngrok.TunnelGroupBackend]
Reader[*ngrok.TunnelGroupBackend]
Updater[*ngrok.TunnelGroupBackendUpdate, *ngrok.TunnelGroupBackend]
Lister[*ngrok.TunnelGroupBackend]
}

func (c *DefaultClientset) TunnelGroupBackends() TunnelGroupBackendsClient {

Check warning on line 215 in internal/ngrokapi/clientset.go

View check run for this annotation

Codecov / codecov/patch

internal/ngrokapi/clientset.go#L215

Added line #L215 was not covered by tests
return c.tunnelGroupBackendsClient
}
19 changes: 18 additions & 1 deletion internal/ngrokapi/edge_modules_clientset.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
package ngrokapi

import "github.com/ngrok/ngrok-api-go/v7"
import (
"context"

"github.com/ngrok/ngrok-api-go/v7"
)

type EdgeModulesClientset interface {
TCP() TCPEdgeModulesClientset
HTTPS() HTTPSEdgeModulesClientset
TLS() TLSEdgeModulesClientset
}

type edgeModulesClient[R, T any] interface {
Deletor
Replace(context.Context, R) (T, error)
}

type EdgeRouteModulesDeletor interface {
Delete(context.Context, *ngrok.EdgeRouteItem) error
}

type EdgeRouteModulesReplacer[R, T any] interface {
Replace(context.Context, R) (T, error)
}

type defaultEdgeModulesClientset struct {
tcp *defaultTCPEdgeModulesClientset
https *defaultHTTPSEdgeModulesClientset
Expand Down
Loading
Loading