2021-09-02 10:57:26 -04:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import "gorm.io/gorm"
|
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
const (
|
|
|
|
errorSameNamespace = Error("Destination namespace same as origin")
|
|
|
|
errorMachineAlreadyShared = Error("Node already shared to this namespace")
|
|
|
|
errorMachineNotShared = Error("Machine not shared to this namespace")
|
|
|
|
)
|
2021-09-02 10:57:26 -04:00
|
|
|
|
2021-11-13 03:39:04 -05:00
|
|
|
// SharedMachine is a join table to support sharing nodes between namespaces.
|
2021-09-06 08:43:43 -04:00
|
|
|
type SharedMachine struct {
|
2021-09-02 10:57:26 -04:00
|
|
|
gorm.Model
|
|
|
|
MachineID uint64
|
|
|
|
Machine Machine
|
|
|
|
NamespaceID uint
|
|
|
|
Namespace Namespace
|
|
|
|
}
|
|
|
|
|
2021-11-13 03:39:04 -05:00
|
|
|
// AddSharedMachineToNamespace adds a machine as a shared node to a namespace.
|
2021-09-06 08:39:52 -04:00
|
|
|
func (h *Headscale) AddSharedMachineToNamespace(m *Machine, ns *Namespace) error {
|
2021-09-02 10:57:26 -04:00
|
|
|
if m.NamespaceID == ns.ID {
|
|
|
|
return errorSameNamespace
|
|
|
|
}
|
|
|
|
|
2021-10-17 11:29:03 -04:00
|
|
|
sharedMachines := []SharedMachine{}
|
|
|
|
if err := h.db.Where("machine_id = ? AND namespace_id = ?", m.ID, ns.ID).Find(&sharedMachines).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(sharedMachines) > 0 {
|
2021-09-09 18:26:46 -04:00
|
|
|
return errorMachineAlreadyShared
|
2021-09-02 10:57:26 -04:00
|
|
|
}
|
|
|
|
|
2021-10-17 11:29:03 -04:00
|
|
|
sharedMachine := SharedMachine{
|
2021-09-02 10:57:26 -04:00
|
|
|
MachineID: m.ID,
|
|
|
|
Machine: *m,
|
|
|
|
NamespaceID: ns.ID,
|
|
|
|
Namespace: *ns,
|
|
|
|
}
|
2021-09-09 18:26:46 -04:00
|
|
|
h.db.Save(&sharedMachine)
|
2021-09-02 10:57:26 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-10 17:55:03 -04:00
|
|
|
|
2021-11-13 03:39:04 -05:00
|
|
|
// RemoveSharedMachineFromNamespace removes a shared machine from a namespace.
|
2021-10-17 16:29:30 -04:00
|
|
|
func (h *Headscale) RemoveSharedMachineFromNamespace(m *Machine, ns *Namespace) error {
|
|
|
|
if m.NamespaceID == ns.ID {
|
2021-10-24 17:00:51 -04:00
|
|
|
// Can't unshare from primary namespace
|
|
|
|
return errorMachineNotShared
|
2021-10-17 16:29:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
sharedMachine := SharedMachine{}
|
2021-11-13 03:36:45 -05:00
|
|
|
result := h.db.Where("machine_id = ? AND namespace_id = ?", m.ID, ns.ID).
|
|
|
|
Unscoped().
|
|
|
|
Delete(&sharedMachine)
|
2021-10-17 16:29:30 -04:00
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
return errorMachineNotShared
|
|
|
|
}
|
|
|
|
|
|
|
|
err := h.RequestMapUpdates(ns.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-13 03:39:04 -05:00
|
|
|
// RemoveSharedMachineFromAllNamespaces removes a machine as a shared node from all namespaces.
|
2021-10-10 17:55:03 -04:00
|
|
|
func (h *Headscale) RemoveSharedMachineFromAllNamespaces(m *Machine) error {
|
|
|
|
sharedMachine := SharedMachine{}
|
|
|
|
if result := h.db.Where("machine_id = ?", m.ID).Unscoped().Delete(&sharedMachine); result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|