feat: notification provider (#8)
Co-authored-by: Timo Behrendt <t.behrendt@t00n.de> Co-committed-by: Timo Behrendt <t.behrendt@t00n.de>
This commit was merged in pull request #8.
This commit is contained in:
@@ -1,26 +1,31 @@
|
||||
package changeDetector
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"realdnydns/pkg/config"
|
||||
"realdnydns/pkg/dnsProvider"
|
||||
"realdnydns/pkg/externalIpProvider"
|
||||
"realdnydns/pkg/notificationProvider"
|
||||
)
|
||||
|
||||
type ChangeDetector struct {
|
||||
externalIpProvider externalIpProvider.ExternalIpProvider
|
||||
dnsProvider dnsProvider.DNSProvider
|
||||
domains []config.DomainConfig
|
||||
externalIpProvider externalIpProvider.ExternalIpProvider
|
||||
dnsProvider dnsProvider.DNSProvider
|
||||
notificationProvider notificationProvider.NotificationProvider
|
||||
domains []config.DomainConfig
|
||||
}
|
||||
|
||||
func New(
|
||||
externalIpProvider externalIpProvider.ExternalIpProvider,
|
||||
dnsProvider dnsProvider.DNSProvider,
|
||||
notificationProvider notificationProvider.NotificationProvider,
|
||||
domains []config.DomainConfig,
|
||||
) ChangeDetector {
|
||||
return ChangeDetector{
|
||||
externalIpProvider: externalIpProvider,
|
||||
dnsProvider: dnsProvider,
|
||||
domains: domains,
|
||||
externalIpProvider: externalIpProvider,
|
||||
dnsProvider: dnsProvider,
|
||||
notificationProvider: notificationProvider,
|
||||
domains: domains,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +45,14 @@ func (c *ChangeDetector) DetectAndApplyChanges() (int, error) {
|
||||
}
|
||||
|
||||
if currentRecord.IP != externalIp.String() {
|
||||
err = c.notificationProvider.SendNotification(
|
||||
fmt.Sprintf("Update %s.%s", subdomain, domain.TLD),
|
||||
fmt.Sprintf("The IP of %s has changed from %s to %s", domain.TLD, currentRecord.IP, externalIp.String()),
|
||||
)
|
||||
if err != nil {
|
||||
return numberUpdated, err
|
||||
}
|
||||
|
||||
_, err = c.dnsProvider.UpdateRecord(domain.TLD, subdomain, externalIp, currentRecord.TTL, currentRecord.Prio, currentRecord.Disabled)
|
||||
numberUpdated++
|
||||
if err != nil {
|
||||
|
||||
@@ -43,6 +43,14 @@ func (m *MockDNSProviderImpl) UpdateRecord(tld string, subdomain string, ip net.
|
||||
}, nil
|
||||
}
|
||||
|
||||
type MockedNotificationProvider struct{}
|
||||
|
||||
type MockedNotificationProviderImpl struct{}
|
||||
|
||||
func (m *MockedNotificationProviderImpl) SendNotification(title string, message string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestDetectAndApplyChanges(t *testing.T) {
|
||||
t.Run("with changes", testDetectAndApplyChangesWithChanges())
|
||||
t.Run("without changes", testDetectAndApplyChangesWithoutChanges())
|
||||
@@ -55,15 +63,16 @@ func testDetectAndApplyChangesWithChanges() func(t *testing.T) {
|
||||
}, &MockDNSProviderImpl{
|
||||
GetRecordIpResponse: "127.0.0.2",
|
||||
UpdateRecordIpResponse: "127.0.0.1",
|
||||
}, []config.DomainConfig{
|
||||
{
|
||||
TLD: "example.com",
|
||||
Subdomains: []string{
|
||||
"test",
|
||||
"@",
|
||||
}, &MockedNotificationProviderImpl{},
|
||||
[]config.DomainConfig{
|
||||
{
|
||||
TLD: "example.com",
|
||||
Subdomains: []string{
|
||||
"test",
|
||||
"@",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
numberUpdated, err := changeDetector.DetectAndApplyChanges()
|
||||
if err != nil {
|
||||
@@ -83,15 +92,16 @@ func testDetectAndApplyChangesWithoutChanges() func(t *testing.T) {
|
||||
}, &MockDNSProviderImpl{
|
||||
GetRecordIpResponse: "127.0.0.1",
|
||||
UpdateRecordIpResponse: "127.0.0.1",
|
||||
}, []config.DomainConfig{
|
||||
{
|
||||
TLD: "example.com",
|
||||
Subdomains: []string{
|
||||
"test",
|
||||
"@",
|
||||
}, &MockedNotificationProviderImpl{},
|
||||
[]config.DomainConfig{
|
||||
{
|
||||
TLD: "example.com",
|
||||
Subdomains: []string{
|
||||
"test",
|
||||
"@",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
numberUpdated, err := changeDetector.DetectAndApplyChanges()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user