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

chore: Add tests for Mikrotik API client code #91

Merged
merged 15 commits into from
Oct 26, 2024
Merged
Prev Previous commit
Next Next commit
Fetch system info as part of the provider initialization instead of c…
…lient initialization
mircea-pavel-anton committed Oct 16, 2024
commit 48c00db58fc664700c98560cb4ff60ee989b72d3
7 changes: 0 additions & 7 deletions internal/mikrotik/client.go
Original file line number Diff line number Diff line change
@@ -77,13 +77,6 @@ func NewMikrotikClient(config *Config) (*MikrotikApiClient, error) {
},
}

info, err := client.GetSystemInfo()
if err != nil {
log.Errorf("failed to connect to the MikroTik RouterOS API Endpoint: %v", err)
return nil, err
}

log.Infof("connected to board %s running RouterOS version %s (%s)", info.BoardName, info.Version, info.ArchitectureName)
return client, nil
}

17 changes: 14 additions & 3 deletions internal/mikrotik/provider.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import (
"context"
"fmt"

log "github.com/sirupsen/logrus"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/plan"
"sigs.k8s.io/external-dns/provider"
@@ -17,15 +18,25 @@ type MikrotikProvider struct {
domainFilter endpoint.DomainFilter
}

// NewMikrotikProvider initializes a new DNSProvider.
// NewMikrotikProvider initializes a new DNSProvider, of the Mikrotik variety
func NewMikrotikProvider(domainFilter endpoint.DomainFilter, config *Config) (provider.Provider, error) {
c, err := NewMikrotikClient(config)
// Create the Mikrotik API Client
client, err := NewMikrotikClient(config)
if err != nil {
return nil, fmt.Errorf("failed to create the MikroTik client: %w", err)
}

// Ensure the Client can connect to the API by fetching system info
info, err := client.GetSystemInfo()
if err != nil {
log.Errorf("failed to connect to the MikroTik RouterOS API Endpoint: %v", err)
return nil, err
}
log.Infof("connected to board %s running RouterOS version %s (%s)", info.BoardName, info.Version, info.ArchitectureName)

// If the client connects properly, create the DNS Provider
p := &MikrotikProvider{
client: c,
client: client,
domainFilter: domainFilter,
}