diff --git a/cli/cmd/acl/root.go b/cli/cmd/acl/root.go index 59715754..300a907e 100644 --- a/cli/cmd/acl/root.go +++ b/cli/cmd/acl/root.go @@ -14,6 +14,7 @@ var rootCmd = &cobra.Command{ // Run: func(cmd *cobra.Command, args []string) { }, } +// GetRoot returns the root subcommand func GetRoot() *cobra.Command { return rootCmd } diff --git a/cli/cmd/context/delete.go b/cli/cmd/context/delete.go index ec6df905..56979a95 100644 --- a/cli/cmd/context/delete.go +++ b/cli/cmd/context/delete.go @@ -5,7 +5,6 @@ import ( "github.com/spf13/cobra" ) -// contextDeleteCmd deletes a contex var contextDeleteCmd = &cobra.Command{ Use: "delete [NAME]", Args: cobra.ExactArgs(1), diff --git a/cli/cmd/context/list.go b/cli/cmd/context/list.go index e6dc7ad1..c90a255b 100644 --- a/cli/cmd/context/list.go +++ b/cli/cmd/context/list.go @@ -5,7 +5,6 @@ import ( "github.com/spf13/cobra" ) -// contextListCmd lists all contexts var contextListCmd = &cobra.Command{ Use: "list", Args: cobra.NoArgs, diff --git a/cli/cmd/context/root.go b/cli/cmd/context/root.go index 43498aed..1b74417d 100644 --- a/cli/cmd/context/root.go +++ b/cli/cmd/context/root.go @@ -14,6 +14,7 @@ var rootCmd = &cobra.Command{ // Run: func(cmd *cobra.Command, args []string) { }, } +// GetRoot returns the root subcommand func GetRoot() *cobra.Command { return rootCmd } diff --git a/cli/cmd/context/set.go b/cli/cmd/context/set.go index 1871ac98..1914cea0 100644 --- a/cli/cmd/context/set.go +++ b/cli/cmd/context/set.go @@ -14,7 +14,6 @@ var ( masterKey string ) -// contextSetCmd creates/updates a context var contextSetCmd = &cobra.Command{ Use: "set [NAME]", Args: cobra.ExactArgs(1), @@ -41,6 +40,5 @@ func init() { contextSetCmd.Flags().StringVar(&password, "password", "", "Password") contextSetCmd.MarkFlagsRequiredTogether("username", "password") contextSetCmd.Flags().StringVar(&masterKey, "master_key", "", "Master Key") - rootCmd.AddCommand(contextSetCmd) } diff --git a/cli/cmd/context/use.go b/cli/cmd/context/use.go index bdb688f9..f1e6d4ff 100644 --- a/cli/cmd/context/use.go +++ b/cli/cmd/context/use.go @@ -5,7 +5,6 @@ import ( "github.com/spf13/cobra" ) -// contextUseCmd sets the current context var contextUseCmd = &cobra.Command{ Use: "use [NAME]", Args: cobra.ExactArgs(1), diff --git a/cli/cmd/dns/root.go b/cli/cmd/dns/root.go index 219437b7..8defc2f3 100644 --- a/cli/cmd/dns/root.go +++ b/cli/cmd/dns/root.go @@ -14,6 +14,7 @@ var rootCmd = &cobra.Command{ // Run: func(cmd *cobra.Command, args []string) { }, } +// GetRoot returns the root subcommand func GetRoot() *cobra.Command { return rootCmd } diff --git a/cli/cmd/ext_client/root.go b/cli/cmd/ext_client/root.go index d55f2de5..d539b661 100644 --- a/cli/cmd/ext_client/root.go +++ b/cli/cmd/ext_client/root.go @@ -14,6 +14,7 @@ var rootCmd = &cobra.Command{ // Run: func(cmd *cobra.Command, args []string) { }, } +// GetRoot returns the root subcommand func GetRoot() *cobra.Command { return rootCmd } diff --git a/cli/cmd/keys/root.go b/cli/cmd/keys/root.go index add2610d..3cddc9d0 100644 --- a/cli/cmd/keys/root.go +++ b/cli/cmd/keys/root.go @@ -14,6 +14,7 @@ var rootCmd = &cobra.Command{ // Run: func(cmd *cobra.Command, args []string) { }, } +// GetRoot returns the root subcommand func GetRoot() *cobra.Command { return rootCmd } diff --git a/cli/cmd/network/root.go b/cli/cmd/network/root.go index b3b6e5df..6ddb8d8c 100644 --- a/cli/cmd/network/root.go +++ b/cli/cmd/network/root.go @@ -16,6 +16,7 @@ var rootCmd = &cobra.Command{ // Run: func(cmd *cobra.Command, args []string) { }, } +// GetRoot returns the root subcommand func GetRoot() *cobra.Command { return rootCmd } diff --git a/cli/cmd/node/root.go b/cli/cmd/node/root.go index 85d0711f..548ae6c4 100644 --- a/cli/cmd/node/root.go +++ b/cli/cmd/node/root.go @@ -14,6 +14,7 @@ var rootCmd = &cobra.Command{ // Run: func(cmd *cobra.Command, args []string) { }, } +// GetRoot returns the root subcommand func GetRoot() *cobra.Command { return rootCmd } diff --git a/cli/cmd/server/root.go b/cli/cmd/server/root.go index cbff46e0..1e14f46e 100644 --- a/cli/cmd/server/root.go +++ b/cli/cmd/server/root.go @@ -14,6 +14,7 @@ var rootCmd = &cobra.Command{ // Run: func(cmd *cobra.Command, args []string) { }, } +// GetRoot returns the root subcommand func GetRoot() *cobra.Command { return rootCmd } diff --git a/cli/cmd/user/root.go b/cli/cmd/user/root.go index 1e86ed85..416eaff0 100644 --- a/cli/cmd/user/root.go +++ b/cli/cmd/user/root.go @@ -14,6 +14,7 @@ var rootCmd = &cobra.Command{ // Run: func(cmd *cobra.Command, args []string) { }, } +// GetRoot returns the root subcommand func GetRoot() *cobra.Command { return rootCmd } diff --git a/cli/config/config.go b/cli/config/config.go index b0f22d27..ba492dda 100644 --- a/cli/config/config.go +++ b/cli/config/config.go @@ -9,6 +9,7 @@ import ( "gopkg.in/yaml.v3" ) +// Context maintains configuration for interaction with Netmaker API type Context struct { Endpoint string `yaml:"endpoint"` Username string `yaml:"username,omitempty"` @@ -56,18 +57,7 @@ func loadConfig() { } } -func GetCurrentContext() (ret Context) { - for _, ctx := range contextMap { - if ctx.Current { - ret = ctx - return - } - } - log.Fatalf("No current context set, do so via `netmaker context use `") - return -} - -func SaveContext() { +func saveContext() { bodyBytes, err := yaml.Marshal(&contextMap) if err != nil { log.Fatalf("Error marshalling into YAML %s", err) @@ -84,6 +74,19 @@ func SaveContext() { } } +// GetCurrentContext - returns current set context +func GetCurrentContext() (ret Context) { + for _, ctx := range contextMap { + if ctx.Current { + ret = ctx + return + } + } + log.Fatalf("No current context set, do so via `netmaker context use `") + return +} + +// SetCurrentContext - sets a given context as current context func SetCurrentContext(ctxName string) { if _, ok := contextMap[ctxName]; !ok { log.Fatalf("No such context %s", ctxName) @@ -92,26 +95,29 @@ func SetCurrentContext(ctxName string) { ctx.Current = key == ctxName contextMap[key] = ctx } - SaveContext() + saveContext() } +// SetContext - updates an existing context or creates a new one func SetContext(ctxName string, ctx Context) { if oldCtx, ok := contextMap[ctxName]; ok && oldCtx.Current { ctx.Current = true } contextMap[ctxName] = ctx - SaveContext() + saveContext() } +// DeleteContext - deletes a context func DeleteContext(ctxName string) { if _, ok := contextMap[ctxName]; ok { delete(contextMap, ctxName) - SaveContext() + saveContext() } else { log.Fatalf("No such context %s", ctxName) } } +// ListAll - lists all contexts func ListAll() { for key, ctx := range contextMap { fmt.Print("\n", key, " -> ", ctx.Endpoint) diff --git a/cli/functions/acl.go b/cli/functions/acl.go index 86e7fa90..f86fe5b8 100644 --- a/cli/functions/acl.go +++ b/cli/functions/acl.go @@ -7,10 +7,12 @@ import ( "github.com/gravitl/netmaker/logic/acls" ) +// GetACL - fetch all ACLs associated with a network func GetACL(networkName string) *acls.ACLContainer { return request[acls.ACLContainer](http.MethodGet, fmt.Sprintf("/api/networks/%s/acls", networkName), nil) } +// UpdateACL - update an ACL func UpdateACL(networkName string, payload *acls.ACLContainer) *acls.ACLContainer { return request[acls.ACLContainer](http.MethodPut, fmt.Sprintf("/api/networks/%s/acls", networkName), payload) } diff --git a/cli/functions/dns.go b/cli/functions/dns.go index fa297159..9d148770 100644 --- a/cli/functions/dns.go +++ b/cli/functions/dns.go @@ -7,30 +7,37 @@ import ( "github.com/gravitl/netmaker/models" ) +// GetDNS - fetch all DNS entries func GetDNS() *[]models.DNSEntry { return request[[]models.DNSEntry](http.MethodGet, "/api/dns", nil) } +// GetNodeDNS - fetch all Node DNS entires func GetNodeDNS(networkName string) *[]models.DNSEntry { return request[[]models.DNSEntry](http.MethodGet, fmt.Sprintf("/api/dns/adm/%s/nodes", networkName), nil) } +// GetCustomDNS - fetch user defined DNS entriees func GetCustomDNS(networkName string) *[]models.DNSEntry { return request[[]models.DNSEntry](http.MethodGet, fmt.Sprintf("/api/dns/adm/%s/custom", networkName), nil) } +// GetNetworkDNS - fetch DNS entries associated with a network func GetNetworkDNS(networkName string) *[]models.DNSEntry { return request[[]models.DNSEntry](http.MethodGet, "/api/dns/adm/"+networkName, nil) } +// CreateDNS - create a DNS entry func CreateDNS(networkName string, payload *models.DNSEntry) *models.DNSEntry { return request[models.DNSEntry](http.MethodPost, "/api/dns/"+networkName, payload) } +// PushDNS - push a DNS entry to CoreDNS func PushDNS() *string { return request[string](http.MethodPost, "/api/dns/adm/pushdns", nil) } +// DeleteDNS - delete a DNS entry func DeleteDNS(networkName, domainName string) *string { return request[string](http.MethodDelete, fmt.Sprintf("/api/dns/%s/%s", networkName, domainName), nil) } diff --git a/cli/functions/ext_client.go b/cli/functions/ext_client.go index e9000861..c95a4ac1 100644 --- a/cli/functions/ext_client.go +++ b/cli/functions/ext_client.go @@ -10,18 +10,22 @@ import ( "github.com/gravitl/netmaker/models" ) +// GetAllExtClients - fetch all external clients func GetAllExtClients() *[]models.ExtClient { return request[[]models.ExtClient](http.MethodGet, "/api/extclients", nil) } +// GetNetworkExtClients - fetch external clients associated with a network func GetNetworkExtClients(networkName string) *[]models.ExtClient { return request[[]models.ExtClient](http.MethodGet, "/api/extclients/"+networkName, nil) } +// GetExtClient - fetch a single external client func GetExtClient(networkName, clientID string) *models.ExtClient { return request[models.ExtClient](http.MethodGet, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), nil) } +// GetExtClientConfig - fetch a wireguard config of an external client func GetExtClientConfig(networkName, clientID string) string { ctx := config.GetCurrentContext() req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/extclients/%s/%s/file", ctx.Endpoint, networkName, clientID), nil) @@ -44,6 +48,7 @@ func GetExtClientConfig(networkName, clientID string) string { return string(bodyBytes) } +// CreateExtClient - create an external client func CreateExtClient(networkName, nodeID, extClientID string) { if extClientID != "" { request[any](http.MethodPost, fmt.Sprintf("/api/extclients/%s/%s", networkName, nodeID), &models.CustomExtClient{ @@ -54,10 +59,12 @@ func CreateExtClient(networkName, nodeID, extClientID string) { } } +// DeleteExtClient - delete an external client func DeleteExtClient(networkName, clientID string) *models.SuccessResponse { return request[models.SuccessResponse](http.MethodDelete, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), nil) } +// UpdateExtClient - update an external client func UpdateExtClient(networkName, clientID string, payload *models.ExtClient) *models.ExtClient { return request[models.ExtClient](http.MethodPut, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), payload) } diff --git a/cli/functions/node.go b/cli/functions/node.go index ef7bcf85..cd7a1779 100644 --- a/cli/functions/node.go +++ b/cli/functions/node.go @@ -7,6 +7,7 @@ import ( "github.com/gravitl/netmaker/models" ) +// GetNodes - fetch all nodes func GetNodes(networkName ...string) *[]models.Node { if len(networkName) == 1 { return request[[]models.Node](http.MethodGet, "/api/nodes/"+networkName[0], nil) @@ -15,18 +16,22 @@ func GetNodes(networkName ...string) *[]models.Node { } } +// GetNodeByID - fetch a single node by ID func GetNodeByID(networkName, nodeID string) *models.NodeGet { return request[models.NodeGet](http.MethodGet, fmt.Sprintf("/api/nodes/%s/%s", networkName, nodeID), nil) } +// UpdateNode - update a single node func UpdateNode(networkName, nodeID string, node *models.Node) *models.Node { return request[models.Node](http.MethodPut, fmt.Sprintf("/api/nodes/%s/%s", networkName, nodeID), node) } +// DeleteNode - delete a node func DeleteNode(networkName, nodeID string) *models.SuccessResponse { return request[models.SuccessResponse](http.MethodDelete, fmt.Sprintf("/api/nodes/%s/%s", networkName, nodeID), nil) } +// CreateRelay - turn a node into a relay func CreateRelay(networkName, nodeID string, relayAddresses []string) *models.Node { return request[models.Node](http.MethodPost, fmt.Sprintf("/api/nodes/%s/%s/createrelay", networkName, nodeID), &models.RelayRequest{ NetID: networkName, @@ -35,28 +40,34 @@ func CreateRelay(networkName, nodeID string, relayAddresses []string) *models.No }) } +// DeleteRelay - remove relay role from a node func DeleteRelay(networkName, nodeID string) *models.Node { return request[models.Node](http.MethodDelete, fmt.Sprintf("/api/nodes/%s/%s/deleterelay", networkName, nodeID), nil) } +// CreateEgress - turn a node into an egress func CreateEgress(networkName, nodeID string, payload *models.EgressGatewayRequest) *models.Node { return request[models.Node](http.MethodPost, fmt.Sprintf("/api/nodes/%s/%s/creategateway", networkName, nodeID), payload) } +// DeleteEgress - remove egress role from a node func DeleteEgress(networkName, nodeID string) *models.Node { return request[models.Node](http.MethodDelete, fmt.Sprintf("/api/nodes/%s/%s/deletegateway", networkName, nodeID), nil) } +// CreateIngress - turn a node into an ingress func CreateIngress(networkName, nodeID string, failover bool) *models.Node { return request[models.Node](http.MethodPost, fmt.Sprintf("/api/nodes/%s/%s/createingress", networkName, nodeID), &struct { Failover bool `json:"failover"` }{Failover: failover}) } +// DeleteIngress - remove ingress role from a node func DeleteIngress(networkName, nodeID string) *models.Node { return request[models.Node](http.MethodDelete, fmt.Sprintf("/api/nodes/%s/%s/deleteingress", networkName, nodeID), nil) } +// UncordonNode - uncordon a node func UncordonNode(networkName, nodeID string) *string { return request[string](http.MethodPost, fmt.Sprintf("/api/nodes/%s/%s/approve", networkName, nodeID), nil) } diff --git a/cli/functions/server.go b/cli/functions/server.go index 8b89612c..4570eebf 100644 --- a/cli/functions/server.go +++ b/cli/functions/server.go @@ -10,6 +10,7 @@ import ( "github.com/gravitl/netmaker/models" ) +// GetLogs - fetch Netmaker server logs func GetLogs() string { ctx := config.GetCurrentContext() req, err := http.NewRequest(http.MethodGet, ctx.Endpoint+"/api/logs", nil) @@ -32,14 +33,17 @@ func GetLogs() string { return string(bodyBytes) } +// GetServerInfo - fetch minimal server info func GetServerInfo() *models.ServerConfig { return request[models.ServerConfig](http.MethodGet, "/api/server/getserverinfo", nil) } +// GetServerConfig - fetch entire server config including secrets func GetServerConfig() *cfg.ServerConfig { return request[cfg.ServerConfig](http.MethodGet, "/api/server/getconfig", nil) } +// GetServerHealth - fetch server current health status func GetServerHealth() string { ctx := config.GetCurrentContext() req, err := http.NewRequest(http.MethodGet, ctx.Endpoint+"/api/server/health", nil) diff --git a/cli/functions/user.go b/cli/functions/user.go index 466caad5..b286bc4e 100644 --- a/cli/functions/user.go +++ b/cli/functions/user.go @@ -6,26 +6,32 @@ import ( "github.com/gravitl/netmaker/models" ) +// HasAdmin - check if server has an admin user func HasAdmin() *bool { return request[bool](http.MethodGet, "/api/users/adm/hasadmin", nil) } +// CreateUser - create a user func CreateUser(payload *models.User) *models.User { return request[models.User](http.MethodPost, "/api/users/"+payload.UserName, payload) } +// UpdateUser - update a user func UpdateUser(payload *models.User) *models.User { return request[models.User](http.MethodPut, "/api/users/networks/"+payload.UserName, payload) } +// DeleteUser - delete a user func DeleteUser(username string) *string { return request[string](http.MethodDelete, "/api/users/"+username, nil) } +// GetUser - fetch a single user func GetUser(username string) *models.User { return request[models.User](http.MethodGet, "/api/users/"+username, nil) } +// ListUsers - fetch all users func ListUsers() *[]models.ReturnUser { return request[[]models.ReturnUser](http.MethodGet, "/api/users", nil) }