|
@@ -6,12 +6,9 @@ import (
|
|
"fmt"
|
|
"fmt"
|
|
"io"
|
|
"io"
|
|
"math/rand"
|
|
"math/rand"
|
|
-
|
|
|
|
- // "fmt"
|
|
|
|
"net/http"
|
|
"net/http"
|
|
"strconv"
|
|
"strconv"
|
|
"time"
|
|
"time"
|
|
-
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/gorilla/mux"
|
|
"github.com/gravitl/netmaker/database"
|
|
"github.com/gravitl/netmaker/database"
|
|
"github.com/gravitl/netmaker/functions"
|
|
"github.com/gravitl/netmaker/functions"
|
|
@@ -31,43 +28,6 @@ func extClientHandlers(r *mux.Router) {
|
|
r.HandleFunc("/api/extclients/{network}/{macaddress}", securityCheck(false, http.HandlerFunc(createExtClient))).Methods("POST")
|
|
r.HandleFunc("/api/extclients/{network}/{macaddress}", securityCheck(false, http.HandlerFunc(createExtClient))).Methods("POST")
|
|
}
|
|
}
|
|
|
|
|
|
-// TODO: Implement Validation
|
|
|
|
-func ValidateExtClientCreate(networkName string, extclient models.ExtClient) error {
|
|
|
|
- // v := validator.New()
|
|
|
|
- // _ = v.RegisterValidation("macaddress_unique", func(fl validator.FieldLevel) bool {
|
|
|
|
- // var isFieldUnique bool = functions.IsFieldUnique(networkName, "macaddress", extclient.MacAddress)
|
|
|
|
- // return isFieldUnique
|
|
|
|
- // })
|
|
|
|
- // _ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
|
|
|
|
- // _, err := extclient.GetNetwork()
|
|
|
|
- // return err == nil
|
|
|
|
- // })
|
|
|
|
- // err := v.Struct(extclient)
|
|
|
|
-
|
|
|
|
- // if err != nil {
|
|
|
|
- // for _, e := range err.(validator.ValidationErrors) {
|
|
|
|
- // fmt.Println(e)
|
|
|
|
- // }
|
|
|
|
- // }
|
|
|
|
- return nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// TODO: Implement Validation
|
|
|
|
-func ValidateExtClientUpdate(networkName string, extclient models.ExtClient) error {
|
|
|
|
- // v := validator.New()
|
|
|
|
- // _ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
|
|
|
|
- // _, err := extclient.GetNetwork()
|
|
|
|
- // return err == nil
|
|
|
|
- // })
|
|
|
|
- // err := v.Struct(extclient)
|
|
|
|
- // if err != nil {
|
|
|
|
- // for _, e := range err.(validator.ValidationErrors) {
|
|
|
|
- // fmt.Println(e)
|
|
|
|
- // }
|
|
|
|
- // }
|
|
|
|
- return nil
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func checkIngressExists(network string, macaddress string) bool {
|
|
func checkIngressExists(network string, macaddress string) bool {
|
|
node, err := functions.GetNodeByMacAddress(network, macaddress)
|
|
node, err := functions.GetNodeByMacAddress(network, macaddress)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -178,14 +138,14 @@ func getExtClientConf(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
gwnode, err := functions.GetNodeByMacAddress(client.Network, client.IngressGatewayID)
|
|
gwnode, err := functions.GetNodeByMacAddress(client.Network, client.IngressGatewayID)
|
|
if err != nil {
|
|
if err != nil {
|
|
- fmt.Println("Could not retrieve Ingress Gateway Node " + client.IngressGatewayID)
|
|
|
|
|
|
+ functions.PrintUserLog(r.Header.Get("user"),"Could not retrieve Ingress Gateway Node " + client.IngressGatewayID,1)
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
network, err := functions.GetParentNetwork(client.Network)
|
|
network, err := functions.GetParentNetwork(client.Network)
|
|
if err != nil {
|
|
if err != nil {
|
|
- fmt.Println("Could not retrieve Ingress Gateway Network " + client.Network)
|
|
|
|
|
|
+ functions.PrintUserLog(r.Header.Get("user"),"Could not retrieve Ingress Gateway Network " + client.Network,1)
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -238,7 +198,7 @@ Endpoint = %s
|
|
}
|
|
}
|
|
return
|
|
return
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ functions.PrintUserLog(r.Header.Get("user"),"retrieved ext client config",2)
|
|
w.WriteHeader(http.StatusOK)
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(client)
|
|
json.NewEncoder(w).Encode(client)
|
|
}
|
|
}
|
|
@@ -263,9 +223,7 @@ func CreateExtClient(extclient models.ExtClient) error {
|
|
}
|
|
}
|
|
|
|
|
|
if extclient.ClientID == "" {
|
|
if extclient.ClientID == "" {
|
|
- clientid := StringWithCharset(7, charset)
|
|
|
|
- clientname := "client-" + clientid
|
|
|
|
- extclient.ClientID = clientname
|
|
|
|
|
|
+ extclient.ClientID = models.GenerateNodeName()
|
|
}
|
|
}
|
|
|
|
|
|
extclient.LastModified = time.Now().Unix()
|
|
extclient.LastModified = time.Now().Unix()
|
|
@@ -284,10 +242,10 @@ func CreateExtClient(extclient models.ExtClient) error {
|
|
err = SetNetworkNodesLastModified(extclient.Network)
|
|
err = SetNetworkNodesLastModified(extclient.Network)
|
|
return err
|
|
return err
|
|
}
|
|
}
|
|
-
|
|
|
|
-//This one's a doozy
|
|
|
|
-//To create a extclient
|
|
|
|
-//Must have valid key and be unique
|
|
|
|
|
|
+/**
|
|
|
|
+ * To create a extclient
|
|
|
|
+ * Must have valid key and be unique
|
|
|
|
+ */
|
|
func createExtClient(w http.ResponseWriter, r *http.Request) {
|
|
func createExtClient(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
@@ -295,9 +253,6 @@ func createExtClient(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
networkName := params["network"]
|
|
networkName := params["network"]
|
|
macaddress := params["macaddress"]
|
|
macaddress := params["macaddress"]
|
|
- //Check if network exists first
|
|
|
|
- //TODO: This is inefficient. Let's find a better way.
|
|
|
|
- //Just a few rows down we grab the network anyway
|
|
|
|
ingressExists := checkIngressExists(networkName, macaddress)
|
|
ingressExists := checkIngressExists(networkName, macaddress)
|
|
if !ingressExists {
|
|
if !ingressExists {
|
|
returnErrorResponse(w, r, formatError(errors.New("ingress does not exist"), "internal"))
|
|
returnErrorResponse(w, r, formatError(errors.New("ingress does not exist"), "internal"))
|
|
@@ -319,11 +274,6 @@ func createExtClient(w http.ResponseWriter, r *http.Request) {
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- err = ValidateExtClientCreate(params["network"], extclient)
|
|
|
|
- if err != nil {
|
|
|
|
- returnErrorResponse(w, r, formatError(err, "badrequest"))
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
err = CreateExtClient(extclient)
|
|
err = CreateExtClient(extclient)
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -340,14 +290,8 @@ func updateExtClient(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var newExtClient models.ExtClient
|
|
var newExtClient models.ExtClient
|
|
var oldExtClient models.ExtClient
|
|
var oldExtClient models.ExtClient
|
|
- // we decode our body request params
|
|
|
|
_ = json.NewDecoder(r.Body).Decode(&newExtClient)
|
|
_ = json.NewDecoder(r.Body).Decode(&newExtClient)
|
|
- // TODO: Validation for update.
|
|
|
|
- // err := ValidateExtClientUpdate(params["network"], params["clientid"], newExtClient)
|
|
|
|
- // if err != nil {
|
|
|
|
- // returnErrorResponse(w, r, formatError(err, "badrequest"))
|
|
|
|
- // return
|
|
|
|
- // }
|
|
|
|
|
|
+
|
|
key, err := functions.GetRecordKey(params["clientid"], params["network"])
|
|
key, err := functions.GetRecordKey(params["clientid"], params["network"])
|
|
if err != nil {
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
@@ -362,7 +306,6 @@ func updateExtClient(w http.ResponseWriter, r *http.Request) {
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
return
|
|
return
|
|
}
|
|
}
|
|
-
|
|
|
|
newclient, err := UpdateExtClient(newExtClient.ClientID, params["network"], oldExtClient)
|
|
newclient, err := UpdateExtClient(newExtClient.ClientID, params["network"], oldExtClient)
|
|
if err != nil {
|
|
if err != nil {
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
@@ -404,7 +347,7 @@ func DeleteGatewayExtClients(gatewayID string, networkName string) error {
|
|
for _, extClient := range currentExtClients {
|
|
for _, extClient := range currentExtClients {
|
|
if extClient.IngressGatewayID == gatewayID {
|
|
if extClient.IngressGatewayID == gatewayID {
|
|
if err = DeleteExtClient(networkName, extClient.ClientID); err != nil {
|
|
if err = DeleteExtClient(networkName, extClient.ClientID); err != nil {
|
|
- functions.PrintUserLog("netmaker", "failed to remove ext client "+extClient.ClientID, 2)
|
|
|
|
|
|
+ functions.PrintUserLog(models.NODE_SERVER_NAME, "failed to remove ext client "+extClient.ClientID, 2)
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|