summaryrefslogtreecommitdiff
path: root/source/static.go
blob: d2645d63c006ae6ba7c73bcc593a0dc41f4f14e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package source

import (
	"fmt"
	"log/slog"
	"net/netip"
)

// a simple source that returns the configured address.
// useful when a record needs to have one specific value,
// that is known in advance, and for debugging.
type staticSource struct {
	log  *slog.Logger
	addr netip.Addr
}

var _ Source = (*staticSource)(nil)

func newStatic(log *slog.Logger, cfg Config) (*staticSource, error) {
	log = log.With("source", "static")
	addr, err := parse(cfg.Value, cfg.Family)
	if err != nil {
		log.Error(
			"static source can't be created from value",
			slog.String("family", string(cfg.Family)),
			slog.String("value", cfg.Value),
		)
		return nil, fmt.Errorf("can't create static source from value: %w", err)
	}

	return &staticSource{log: log, addr: addr}, nil
}

func (s *staticSource) Supports(f Family) bool {
	return (s.addr.Is4() && f == IPv4) || (s.addr.Is6() && f == IPv6)
}

func (s *staticSource) Lookup(Family) (netip.Addr, error) {
	s.log.Info(
		"looked up address from static source",
		slog.String("addr", s.addr.String()),
	)
	return s.addr, nil
}