diff --git a/cli/cmd/host/delete.go b/cli/cmd/host/delete.go new file mode 100644 index 00000000..4da8acb0 --- /dev/null +++ b/cli/cmd/host/delete.go @@ -0,0 +1,20 @@ +package host + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var hostDeleteCmd = &cobra.Command{ + Use: "delete HostID", + Args: cobra.ExactArgs(1), + Short: "Delete a host", + Long: `Delete a host`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.DeleteHost(args[0])) + }, +} + +func init() { + rootCmd.AddCommand(hostDeleteCmd) +} diff --git a/cli/cmd/host/list.go b/cli/cmd/host/list.go new file mode 100644 index 00000000..fbee28e9 --- /dev/null +++ b/cli/cmd/host/list.go @@ -0,0 +1,20 @@ +package host + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var hostListCmd = &cobra.Command{ + Use: "list", + Args: cobra.NoArgs, + Short: "List all hosts", + Long: `List all hosts`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.GetHosts()) + }, +} + +func init() { + rootCmd.AddCommand(hostListCmd) +} diff --git a/cli/cmd/host/root.go b/cli/cmd/host/root.go new file mode 100644 index 00000000..c17d600b --- /dev/null +++ b/cli/cmd/host/root.go @@ -0,0 +1,38 @@ +package host + +import ( + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "host", + Short: "Manage hosts", + Long: `Manage hosts`, + // Run: func(cmd *cobra.Command, args []string) { }, +} + +// GetRoot returns the root subcommand +func GetRoot() *cobra.Command { + return rootCmd +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + // Cobra also supports local flags, which will only run + // when this action is called directly. + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cli/cmd/host/update.go b/cli/cmd/host/update.go new file mode 100644 index 00000000..d7b7c0bc --- /dev/null +++ b/cli/cmd/host/update.go @@ -0,0 +1,32 @@ +package host + +import ( + "encoding/json" + "log" + "os" + + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var hostUpdateCmd = &cobra.Command{ + Use: "update HostID /path/to/host_definition.json", + Args: cobra.ExactArgs(2), + Short: "Update a host", + Long: `Update a host`, + Run: func(cmd *cobra.Command, args []string) { + apiHost := &models.ApiHost{} + content, err := os.ReadFile(args[1]) + if err != nil { + log.Fatal("Error when opening file: ", err) + } + if err := json.Unmarshal(content, apiHost); err != nil { + log.Fatal(err) + } + functions.PrettyPrint(functions.UpdateHost(args[0], apiHost)) + }, +} + +func init() { + rootCmd.AddCommand(hostUpdateCmd) +} diff --git a/cli/cmd/host/update_networks.go b/cli/cmd/host/update_networks.go new file mode 100644 index 00000000..afe3f747 --- /dev/null +++ b/cli/cmd/host/update_networks.go @@ -0,0 +1,22 @@ +package host + +import ( + "strings" + + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var hostUpdateNetworksCmd = &cobra.Command{ + Use: "update_network HostID Networks(comma separated list)", + Args: cobra.ExactArgs(2), + Short: "Update a host's networks", + Long: `Update a host's networks`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.UpdateHostNetworks(args[0], strings.Split(args[1], ","))) + }, +} + +func init() { + rootCmd.AddCommand(hostUpdateNetworksCmd) +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 89e35962..ce9979c7 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -7,6 +7,7 @@ import ( "github.com/gravitl/netmaker/cli/cmd/context" "github.com/gravitl/netmaker/cli/cmd/dns" "github.com/gravitl/netmaker/cli/cmd/ext_client" + "github.com/gravitl/netmaker/cli/cmd/host" "github.com/gravitl/netmaker/cli/cmd/keys" "github.com/gravitl/netmaker/cli/cmd/metrics" "github.com/gravitl/netmaker/cli/cmd/network" @@ -66,4 +67,5 @@ func init() { rootCmd.AddCommand(usergroup.GetRoot()) rootCmd.AddCommand(metrics.GetRoot()) rootCmd.AddCommand(network_user.GetRoot()) + rootCmd.AddCommand(host.GetRoot()) } diff --git a/cli/functions/host.go b/cli/functions/host.go new file mode 100644 index 00000000..3ff0ed08 --- /dev/null +++ b/cli/functions/host.go @@ -0,0 +1,33 @@ +package functions + +import ( + "net/http" + + "github.com/gravitl/netmaker/models" +) + +type hostNetworksUpdatePayload struct { + Networks []string `json:"networks"` +} + +// GetHosts - fetch all host entries +func GetHosts() *[]models.ApiHost { + return request[[]models.ApiHost](http.MethodGet, "/api/hosts", nil) +} + +// DeleteHost - delete a host +func DeleteHost(hostID string) *models.ApiHost { + return request[models.ApiHost](http.MethodDelete, "/api/hosts/"+hostID, nil) +} + +// UpdateHost - update a host +func UpdateHost(hostID string, body *models.ApiHost) *models.ApiHost { + return request[models.ApiHost](http.MethodPut, "/api/hosts/"+hostID, body) +} + +// UpdateHostNetworks - update a host's networks +func UpdateHostNetworks(hostID string, networks []string) *hostNetworksUpdatePayload { + return request[hostNetworksUpdatePayload](http.MethodPut, "/api/hosts/"+hostID+"/networks", &hostNetworksUpdatePayload{ + Networks: networks, + }) +}