11package headscale
22
33import (
4- "fmt"
54 "log"
65 "time"
76
87 "github.com/jinzhu/gorm"
98 "tailscale.com/tailcfg"
109)
1110
11+ const errorNamespaceExists = Error ("Namespace already exists" )
12+ const errorNamespaceNotFound = Error ("Namespace not found" )
13+ const errorNamespaceNotEmpty = Error ("Namespace not empty" )
14+
1215// Namespace is the way Headscale implements the concept of users in Tailscale
1316//
1417// At the end of the day, users in Tailscale are some kind of 'bubbles' or namespaces
@@ -30,7 +33,7 @@ func (h *Headscale) CreateNamespace(name string) (*Namespace, error) {
3033
3134 n := Namespace {}
3235 if err := db .Where ("name = ?" , name ).First (& n ).Error ; err == nil {
33- return nil , fmt . Errorf ( "Namespace already exists" )
36+ return nil , errorNamespaceExists
3437 }
3538 n .Name = name
3639 if err := db .Create (& n ).Error ; err != nil {
@@ -40,6 +43,37 @@ func (h *Headscale) CreateNamespace(name string) (*Namespace, error) {
4043 return & n , nil
4144}
4245
46+ // DestroyNamespace destroys a Namespace. Returns error if the Namespace does
47+ // not exist or if there are machines associated with it.
48+ func (h * Headscale ) DestroyNamespace (name string ) error {
49+ db , err := h .db ()
50+ if err != nil {
51+ log .Printf ("Cannot open DB: %s" , err )
52+ return err
53+ }
54+ defer db .Close ()
55+
56+ n , err := h .GetNamespace (name )
57+ if err != nil {
58+ return errorNamespaceNotFound
59+ }
60+
61+ m , err := h .ListMachinesInNamespace (name )
62+ if err != nil {
63+ return err
64+ }
65+ if len (* m ) > 0 {
66+ return errorNamespaceNotEmpty
67+ }
68+
69+ err = db .Unscoped ().Delete (& n ).Error
70+ if err != nil {
71+ return err
72+ }
73+
74+ return nil
75+ }
76+
4377// GetNamespace fetches a namespace by name
4478func (h * Headscale ) GetNamespace (name string ) (* Namespace , error ) {
4579 db , err := h .db ()
@@ -51,7 +85,7 @@ func (h *Headscale) GetNamespace(name string) (*Namespace, error) {
5185
5286 n := Namespace {}
5387 if db .First (& n , "name = ?" , name ).RecordNotFound () {
54- return nil , fmt . Errorf ( "Namespace not found" )
88+ return nil , errorNamespaceNotFound
5589 }
5690 return & n , nil
5791}
0 commit comments