add host endpoints to cli

This commit is contained in:
Anish Mukherjee 2023-01-04 16:00:19 +05:30
parent 75dc77a6ea
commit 8b5eb0cbd4
7 changed files with 167 additions and 0 deletions

20
cli/cmd/host/delete.go Normal file
View file

@ -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)
}

20
cli/cmd/host/list.go Normal file
View file

@ -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)
}

38
cli/cmd/host/root.go Normal file
View file

@ -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")
}

32
cli/cmd/host/update.go Normal file
View file

@ -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)
}

View file

@ -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)
}

View file

@ -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())
}

33
cli/functions/host.go Normal file
View file

@ -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,
})
}