From af90825f136f2864a07779c765d06f46b6748396 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sat, 26 Aug 2023 18:32:44 +0000 Subject: [PATCH] zones: Machine.SetGateway() Signed-off-by: Alejandro Mery --- pkg/zones/machine.go | 18 ++++++++++++++++++ pkg/zones/machine_rings.go | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/pkg/zones/machine.go b/pkg/zones/machine.go index 0a49ad3..1dd64a2 100644 --- a/pkg/zones/machine.go +++ b/pkg/zones/machine.go @@ -43,6 +43,24 @@ func (m *Machine) IsGateway() bool { return ok } +// SetGateway enables/disables a Machine ring0 integration +func (m *Machine) SetGateway(enabled bool) error { + ri, found := m.getRingInfo(0) + switch { + case !found && !enabled: + return nil + case !found: + var err error + + if ri, err = m.createRingInfo(0, false); err != nil { + return err + } + } + + ri.Enabled = enabled + return m.SyncWireguardConfig(0) +} + // Zone indicates the [Zone] this machine belongs to func (m *Machine) Zone() int { return m.zone.ID diff --git a/pkg/zones/machine_rings.go b/pkg/zones/machine_rings.go index 62c00f9..9e774e4 100644 --- a/pkg/zones/machine_rings.go +++ b/pkg/zones/machine_rings.go @@ -228,3 +228,23 @@ func (m *Machine) RemoveWireguardConfig(ring int) error { return err } + +func (m *Machine) createRingInfo(ring int, enabled bool) (*RingInfo, error) { + keys, err := wireguard.NewKeyPair() + if err != nil { + return nil, err + } + + ri := &RingInfo{ + Ring: ring, + Enabled: enabled, + Keys: keys, + } + + err = m.applyRingInfo(ring, ri) + if err != nil { + return nil, err + } + + return ri, nil +}