2021-09-02 22:57:26 +08:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import "gorm.io/gorm"
|
|
|
|
|
|
|
|
const errorSameNamespace = Error("Destination namespace same as origin")
|
|
|
|
const errorNodeAlreadyShared = Error("Node already shared to this namespace")
|
|
|
|
|
2021-09-02 23:08:39 +08:00
|
|
|
// SharedNode is a join table to support sharing nodes between namespaces
|
2021-09-02 22:57:26 +08:00
|
|
|
type SharedNode struct {
|
|
|
|
gorm.Model
|
|
|
|
MachineID uint64
|
|
|
|
Machine Machine
|
|
|
|
NamespaceID uint
|
|
|
|
Namespace Namespace
|
|
|
|
}
|
|
|
|
|
2021-09-06 20:39:52 +08:00
|
|
|
// AddSharedMachineToNamespace adds a machine as a shared node to a namespace
|
|
|
|
func (h *Headscale) AddSharedMachineToNamespace(m *Machine, ns *Namespace) error {
|
2021-09-02 22:57:26 +08:00
|
|
|
if m.NamespaceID == ns.ID {
|
|
|
|
return errorSameNamespace
|
|
|
|
}
|
|
|
|
|
|
|
|
sn := SharedNode{}
|
|
|
|
if err := h.db.Where("machine_id = ? AND namespace_id", m.ID, ns.ID).First(&sn).Error; err == nil {
|
|
|
|
return errorNodeAlreadyShared
|
|
|
|
}
|
|
|
|
|
|
|
|
sn = SharedNode{
|
|
|
|
MachineID: m.ID,
|
|
|
|
Machine: *m,
|
|
|
|
NamespaceID: ns.ID,
|
|
|
|
Namespace: *ns,
|
|
|
|
}
|
|
|
|
h.db.Save(&sn)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|