2021-09-02 14:57:26 +00: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-06 12:43:43 +00:00
|
|
|
// SharedMachine is a join table to support sharing nodes between namespaces
|
|
|
|
type SharedMachine struct {
|
2021-09-02 14:57:26 +00:00
|
|
|
gorm.Model
|
|
|
|
MachineID uint64
|
|
|
|
Machine Machine
|
|
|
|
NamespaceID uint
|
|
|
|
Namespace Namespace
|
|
|
|
}
|
|
|
|
|
2021-09-06 12:39:52 +00:00
|
|
|
// AddSharedMachineToNamespace adds a machine as a shared node to a namespace
|
|
|
|
func (h *Headscale) AddSharedMachineToNamespace(m *Machine, ns *Namespace) error {
|
2021-09-02 14:57:26 +00:00
|
|
|
if m.NamespaceID == ns.ID {
|
|
|
|
return errorSameNamespace
|
|
|
|
}
|
|
|
|
|
2021-09-06 12:43:43 +00:00
|
|
|
sn := SharedMachine{}
|
2021-09-09 22:26:46 +00:00
|
|
|
if err := h.db.Where("machine_id = ? AND namespace_id", m.ID, ns.ID).First(&sharedMachine).Error; err == nil {
|
|
|
|
return errorMachineAlreadyShared
|
2021-09-02 14:57:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 22:26:46 +00:00
|
|
|
sharedMachine = SharedMachine{
|
2021-09-02 14:57:26 +00:00
|
|
|
MachineID: m.ID,
|
|
|
|
Machine: *m,
|
|
|
|
NamespaceID: ns.ID,
|
|
|
|
Namespace: *ns,
|
|
|
|
}
|
2021-09-09 22:26:46 +00:00
|
|
|
h.db.Save(&sharedMachine)
|
2021-09-02 14:57:26 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|