mirror of
				https://github.com/juanfont/headscale.git
				synced 2025-10-28 10:51:44 +01:00 
			
		
		
		
	Simplify and streamline namespace functions for new cli/rpc/api
This commit is contained in:
		
							parent
							
								
									77f5f8bd1c
								
							
						
					
					
						commit
						95690e614e
					
				| @ -4,16 +4,21 @@ import ( | |||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"strconv" | ||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
|  | 	v1 "github.com/juanfont/headscale/gen/go/headscale/v1" | ||||||
| 	"github.com/rs/zerolog/log" | 	"github.com/rs/zerolog/log" | ||||||
|  | 	"google.golang.org/protobuf/types/known/timestamppb" | ||||||
| 	"gorm.io/gorm" | 	"gorm.io/gorm" | ||||||
| 	"tailscale.com/tailcfg" | 	"tailscale.com/tailcfg" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| const errorNamespaceExists = Error("Namespace already exists") | const ( | ||||||
| const errorNamespaceNotFound = Error("Namespace not found") | 	errorNamespaceExists   = Error("Namespace already exists") | ||||||
| const errorNamespaceNotEmpty = Error("Namespace not empty") | 	errorNamespaceNotFound = Error("Namespace not found") | ||||||
|  | 	errorNamespaceNotEmpty = Error("Namespace not empty") | ||||||
|  | ) | ||||||
| 
 | 
 | ||||||
| // Namespace is the way Headscale implements the concept of users in Tailscale
 | // Namespace is the way Headscale implements the concept of users in Tailscale
 | ||||||
| //
 | //
 | ||||||
| @ -54,7 +59,7 @@ func (h *Headscale) DestroyNamespace(name string) error { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return err | 		return err | ||||||
| 	} | 	} | ||||||
| 	if len(*m) > 0 { | 	if len(m) > 0 { | ||||||
| 		return errorNamespaceNotEmpty | 		return errorNamespaceNotEmpty | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| @ -104,16 +109,16 @@ func (h *Headscale) GetNamespace(name string) (*Namespace, error) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // ListNamespaces gets all the existing namespaces
 | // ListNamespaces gets all the existing namespaces
 | ||||||
| func (h *Headscale) ListNamespaces() (*[]Namespace, error) { | func (h *Headscale) ListNamespaces() ([]Namespace, error) { | ||||||
| 	namespaces := []Namespace{} | 	namespaces := []Namespace{} | ||||||
| 	if err := h.db.Find(&namespaces).Error; err != nil { | 	if err := h.db.Find(&namespaces).Error; err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	return &namespaces, nil | 	return namespaces, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // ListMachinesInNamespace gets all the nodes in a given namespace
 | // ListMachinesInNamespace gets all the nodes in a given namespace
 | ||||||
| func (h *Headscale) ListMachinesInNamespace(name string) (*[]Machine, error) { | func (h *Headscale) ListMachinesInNamespace(name string) ([]Machine, error) { | ||||||
| 	n, err := h.GetNamespace(name) | 	n, err := h.GetNamespace(name) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| @ -123,11 +128,11 @@ func (h *Headscale) ListMachinesInNamespace(name string) (*[]Machine, error) { | |||||||
| 	if err := h.db.Preload("AuthKey").Preload("AuthKey.Namespace").Preload("Namespace").Where(&Machine{NamespaceID: n.ID}).Find(&machines).Error; err != nil { | 	if err := h.db.Preload("AuthKey").Preload("AuthKey.Namespace").Preload("Namespace").Where(&Machine{NamespaceID: n.ID}).Find(&machines).Error; err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| 	} | 	} | ||||||
| 	return &machines, nil | 	return machines, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // ListSharedMachinesInNamespace returns all the machines that are shared to the specified namespace
 | // ListSharedMachinesInNamespace returns all the machines that are shared to the specified namespace
 | ||||||
| func (h *Headscale) ListSharedMachinesInNamespace(name string) (*[]Machine, error) { | func (h *Headscale) ListSharedMachinesInNamespace(name string) ([]Machine, error) { | ||||||
| 	namespace, err := h.GetNamespace(name) | 	namespace, err := h.GetNamespace(name) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return nil, err | 		return nil, err | ||||||
| @ -145,7 +150,7 @@ func (h *Headscale) ListSharedMachinesInNamespace(name string) (*[]Machine, erro | |||||||
| 		} | 		} | ||||||
| 		machines = append(machines, *machine) | 		machines = append(machines, *machine) | ||||||
| 	} | 	} | ||||||
| 	return &machines, nil | 	return machines, nil | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // SetMachineNamespace assigns a Machine to a namespace
 | // SetMachineNamespace assigns a Machine to a namespace
 | ||||||
| @ -275,3 +280,11 @@ func getMapResponseUserProfiles(m Machine, peers Machines) []tailcfg.UserProfile | |||||||
| 	} | 	} | ||||||
| 	return profiles | 	return profiles | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func (n *Namespace) toProto() *v1.Namespace { | ||||||
|  | 	return &v1.Namespace{ | ||||||
|  | 		Id:        strconv.FormatUint(uint64(n.ID), 10), | ||||||
|  | 		Name:      n.Name, | ||||||
|  | 		CreatedAt: timestamppb.New(n.CreatedAt), | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | |||||||
| @ -12,7 +12,7 @@ func (s *Suite) TestCreateAndDestroyNamespace(c *check.C) { | |||||||
| 
 | 
 | ||||||
| 	ns, err := h.ListNamespaces() | 	ns, err := h.ListNamespaces() | ||||||
| 	c.Assert(err, check.IsNil) | 	c.Assert(err, check.IsNil) | ||||||
| 	c.Assert(len(*ns), check.Equals, 1) | 	c.Assert(len(ns), check.Equals, 1) | ||||||
| 
 | 
 | ||||||
| 	err = h.DestroyNamespace("test") | 	err = h.DestroyNamespace("test") | ||||||
| 	c.Assert(err, check.IsNil) | 	c.Assert(err, check.IsNil) | ||||||
| @ -55,7 +55,7 @@ func (s *Suite) TestRenameNamespace(c *check.C) { | |||||||
| 
 | 
 | ||||||
| 	ns, err := h.ListNamespaces() | 	ns, err := h.ListNamespaces() | ||||||
| 	c.Assert(err, check.IsNil) | 	c.Assert(err, check.IsNil) | ||||||
| 	c.Assert(len(*ns), check.Equals, 1) | 	c.Assert(len(ns), check.Equals, 1) | ||||||
| 
 | 
 | ||||||
| 	err = h.RenameNamespace("test", "test_renamed") | 	err = h.RenameNamespace("test", "test_renamed") | ||||||
| 	c.Assert(err, check.IsNil) | 	c.Assert(err, check.IsNil) | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user