Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename namespaces #172

Merged
merged 2 commits into from
Oct 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions cmd/headscale/cli/namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func init() {
namespaceCmd.AddCommand(createNamespaceCmd)
namespaceCmd.AddCommand(listNamespacesCmd)
namespaceCmd.AddCommand(destroyNamespaceCmd)
namespaceCmd.AddCommand(renameNamespaceCmd)
}

var namespaceCmd = &cobra.Command{
Expand Down Expand Up @@ -107,3 +108,31 @@ var listNamespacesCmd = &cobra.Command{
}
},
}

var renameNamespaceCmd = &cobra.Command{
Use: "rename OLD_NAME NEW_NAME",
Short: "Renames a namespace",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
return fmt.Errorf("Missing parameters")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
o, _ := cmd.Flags().GetString("output")
h, err := getHeadscaleApp()
if err != nil {
log.Fatalf("Error initializing: %s", err)
}
err = h.RenameNamespace(args[0], args[1])
if strings.HasPrefix(o, "json") {
JsonOutput(map[string]string{"Result": "Namespace renamed"}, err, o)
return
}
if err != nil {
fmt.Printf("Error renaming namespace: %s\n", err)
return
}
fmt.Printf("Namespace renamed\n")
},
}
29 changes: 29 additions & 0 deletions namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,35 @@ func (h *Headscale) DestroyNamespace(name string) error {
}

if result := h.db.Unscoped().Delete(&n); result.Error != nil {
return result.Error
}

return nil
}

// RenameNamespace renames a Namespace. Returns error if the Namespace does
// not exist or if another Namespace exists with the new name.
func (h *Headscale) RenameNamespace(oldName, newName string) error {
n, err := h.GetNamespace(oldName)
if err != nil {
return err
}
_, err = h.GetNamespace(newName)
if err == nil {
return errorNamespaceExists
}
if !errors.Is(err, errorNamespaceNotFound) {
return err
}

n.Name = newName

if result := h.db.Save(&n); result.Error != nil {
return result.Error
}

err = h.RequestMapUpdates(n.ID)
if err != nil {
return err
}

Expand Down
29 changes: 29 additions & 0 deletions namespaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,32 @@ func (s *Suite) TestDestroyNamespaceErrors(c *check.C) {
err = h.DestroyNamespace("test")
c.Assert(err, check.Equals, errorNamespaceNotEmpty)
}

func (s *Suite) TestRenameNamespace(c *check.C) {
n, err := h.CreateNamespace("test")
c.Assert(err, check.IsNil)
c.Assert(n.Name, check.Equals, "test")

ns, err := h.ListNamespaces()
c.Assert(err, check.IsNil)
c.Assert(len(*ns), check.Equals, 1)

err = h.RenameNamespace("test", "test_renamed")
c.Assert(err, check.IsNil)

_, err = h.GetNamespace("test")
c.Assert(err, check.Equals, errorNamespaceNotFound)

_, err = h.GetNamespace("test_renamed")
c.Assert(err, check.IsNil)

err = h.RenameNamespace("test_does_not_exit", "test")
c.Assert(err, check.Equals, errorNamespaceNotFound)

n2, err := h.CreateNamespace("test2")
c.Assert(err, check.IsNil)
c.Assert(n2.Name, check.Equals, "test2")

err = h.RenameNamespace("test2", "test_renamed")
c.Assert(err, check.Equals, errorNamespaceExists)
}