From 02ba1ce6ae0449d908cbce5d5b2b0ce95d2fdc81 Mon Sep 17 00:00:00 2001 From: Anish Mukherjee Date: Tue, 24 Jan 2023 16:00:12 +0530 Subject: [PATCH] move relay logic to hosts --- cli/cmd/host/create_relay.go | 22 ++++++++++++++++++++++ cli/cmd/host/delete_relay.go | 20 ++++++++++++++++++++ cli/cmd/node/create_relay.go | 22 ---------------------- cli/cmd/node/delete_relay.go | 20 -------------------- cli/functions/host.go | 14 ++++++++++++++ cli/functions/node.go | 14 -------------- 6 files changed, 56 insertions(+), 56 deletions(-) create mode 100644 cli/cmd/host/create_relay.go create mode 100644 cli/cmd/host/delete_relay.go delete mode 100644 cli/cmd/node/create_relay.go delete mode 100644 cli/cmd/node/delete_relay.go diff --git a/cli/cmd/host/create_relay.go b/cli/cmd/host/create_relay.go new file mode 100644 index 00000000..2344af72 --- /dev/null +++ b/cli/cmd/host/create_relay.go @@ -0,0 +1,22 @@ +package host + +import ( + "strings" + + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var hostCreateRelayCmd = &cobra.Command{ + Use: "create_relay [HOST ID] [RELAYED HOST IDS (comma separated)]", + Args: cobra.ExactArgs(2), + Short: "Turn a Host into a Relay", + Long: `Turn a Host into a Relay`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.CreateRelay(args[0], strings.Split(args[1], ","))) + }, +} + +func init() { + rootCmd.AddCommand(hostCreateRelayCmd) +} diff --git a/cli/cmd/host/delete_relay.go b/cli/cmd/host/delete_relay.go new file mode 100644 index 00000000..ccf203e6 --- /dev/null +++ b/cli/cmd/host/delete_relay.go @@ -0,0 +1,20 @@ +package host + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var hostDeleteRelayCmd = &cobra.Command{ + Use: "delete_relay [HOST ID]", + Args: cobra.ExactArgs(1), + Short: "Delete Relay role from a host", + Long: `Delete Relay role from a host`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.DeleteRelay(args[0])) + }, +} + +func init() { + rootCmd.AddCommand(hostDeleteRelayCmd) +} diff --git a/cli/cmd/node/create_relay.go b/cli/cmd/node/create_relay.go deleted file mode 100644 index 61e921ca..00000000 --- a/cli/cmd/node/create_relay.go +++ /dev/null @@ -1,22 +0,0 @@ -package node - -import ( - "strings" - - "github.com/gravitl/netmaker/cli/functions" - "github.com/spf13/cobra" -) - -var nodeCreateRelayCmd = &cobra.Command{ - Use: "create_relay [NETWORK NAME] [NODE ID] [RELAY ADDRESSES (comma separated)]", - Args: cobra.ExactArgs(3), - Short: "Turn a Node into a Relay", - Long: `Turn a Node into a Relay`, - Run: func(cmd *cobra.Command, args []string) { - functions.PrettyPrint(functions.CreateRelay(args[0], args[1], strings.Split(args[2], ","))) - }, -} - -func init() { - rootCmd.AddCommand(nodeCreateRelayCmd) -} diff --git a/cli/cmd/node/delete_relay.go b/cli/cmd/node/delete_relay.go deleted file mode 100644 index ccef64f9..00000000 --- a/cli/cmd/node/delete_relay.go +++ /dev/null @@ -1,20 +0,0 @@ -package node - -import ( - "github.com/gravitl/netmaker/cli/functions" - "github.com/spf13/cobra" -) - -var nodeDeleteRelayCmd = &cobra.Command{ - Use: "delete_relay [NETWORK NAME] [NODE ID]", - Args: cobra.ExactArgs(2), - Short: "Delete Relay role from a Node", - Long: `Delete Relay role from a Node`, - Run: func(cmd *cobra.Command, args []string) { - functions.PrettyPrint(functions.DeleteRelay(args[0], args[1])) - }, -} - -func init() { - rootCmd.AddCommand(nodeDeleteRelayCmd) -} diff --git a/cli/functions/host.go b/cli/functions/host.go index 3ff0ed08..bfeabd55 100644 --- a/cli/functions/host.go +++ b/cli/functions/host.go @@ -1,6 +1,7 @@ package functions import ( + "fmt" "net/http" "github.com/gravitl/netmaker/models" @@ -31,3 +32,16 @@ func UpdateHostNetworks(hostID string, networks []string) *hostNetworksUpdatePay Networks: networks, }) } + +// CreateRelay - turn a host into a relay +func CreateRelay(hostID string, relayedHosts []string) *models.ApiHost { + return request[models.ApiHost](http.MethodPost, fmt.Sprintf("/api/hosts/%s/relay", hostID), &models.HostRelayRequest{ + HostID: hostID, + RelayedHosts: relayedHosts, + }) +} + +// DeleteRelay - remove relay role from a host +func DeleteRelay(hostID string) *models.ApiHost { + return request[models.ApiHost](http.MethodDelete, fmt.Sprintf("/api/hosts/%s/relay", hostID), nil) +} diff --git a/cli/functions/node.go b/cli/functions/node.go index 8a5fc793..ef75ce39 100644 --- a/cli/functions/node.go +++ b/cli/functions/node.go @@ -31,20 +31,6 @@ 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.ApiNode { - return request[models.ApiNode](http.MethodPost, fmt.Sprintf("/api/nodes/%s/%s/createrelay", networkName, nodeID), &models.RelayRequest{ - NetID: networkName, - NodeID: nodeID, - RelayAddrs: relayAddresses, - }) -} - -// DeleteRelay - remove relay role from a node -func DeleteRelay(networkName, nodeID string) *models.ApiNode { - return request[models.ApiNode](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.ApiNode { return request[models.ApiNode](http.MethodPost, fmt.Sprintf("/api/nodes/%s/%s/creategateway", networkName, nodeID), payload)