Files
jpictl/pkg/zones/machine.go
T
amery 61d1ea85ee zones: SetGateway [WIP]
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-23 20:28:33 +00:00

92 lines
1.6 KiB
Go

package zones
import (
"net/netip"
"strconv"
"strings"
"sync"
)
// A Machine is a machine on a Zone
type Machine struct {
mu sync.Mutex
zone *Zone
id int
Name string `toml:"name"`
PublicAddresses []netip.Addr `toml:"public,omitempty"`
RingAddresses []*RingInfo `toml:"rings,omitempty"`
}
func (m *Machine) String() string {
return m.Name
}
// ID return the index within the [Zone] associated to this [Machine]
func (m *Machine) ID() int {
m.mu.Lock()
defer m.mu.Unlock()
if m.id == 0 {
zoneName := m.zone.Name
s := m.Name[len(zoneName)+1:]
id, err := strconv.ParseInt(s, 10, 8)
if err != nil {
panic(err)
}
m.id = int(id)
}
return m.id
}
// FullName returns the Name of the machine including domain name
func (m *Machine) FullName() string {
if domain := m.zone.zones.domain; domain != "" {
var s = []string{
m.Name,
domain,
}
return strings.Join(s, ".")
}
return m.Name
}
// IsGateway tells if the Machine is a ring0 gateway
func (m *Machine) IsGateway() bool {
m.mu.Lock()
defer m.mu.Unlock()
if ri, found := m.getRingInfo(0); found {
return ri.Enabled
}
return false
}
// SetGateway enables/disables a Machine ring0 integration
func (m *Machine) SetGateway(enabled bool) error {
m.mu.Lock()
defer m.mu.Unlock()
ri, found := m.getRingInfo(0)
switch {
case !found && !enabled:
return nil
case !found:
return m.createRingInfo(0, true)
default:
ri.Enabled = enabled
return m.writeRingInfo(ri)
}
}
func (m *Machine) getPeerByName(name string) (*Machine, bool) {
return m.zone.zones.GetMachineByName(name)
}