mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-07 01:36:23 +08:00
fixing update methods
This commit is contained in:
@@ -177,12 +177,6 @@ func UpdateNode(nodechange models.Node, node models.Node) (models.Node, error) {
|
|||||||
if nodechange.MacAddress != "" {
|
if nodechange.MacAddress != "" {
|
||||||
node.MacAddress = nodechange.MacAddress
|
node.MacAddress = nodechange.MacAddress
|
||||||
}
|
}
|
||||||
if nodechange.IsGateway != nil {
|
|
||||||
node.IsGateway = nodechange.IsGateway
|
|
||||||
}
|
|
||||||
if nodechange.GatewayRange != "" {
|
|
||||||
node.GatewayRange = nodechange.GatewayRange
|
|
||||||
}
|
|
||||||
if nodechange.PublicKey != "" {
|
if nodechange.PublicKey != "" {
|
||||||
node.PublicKey = nodechange.PublicKey
|
node.PublicKey = nodechange.PublicKey
|
||||||
node.KeyUpdateTimeStamp = time.Now().Unix()
|
node.KeyUpdateTimeStamp = time.Now().Unix()
|
||||||
@@ -216,8 +210,6 @@ func UpdateNode(nodechange models.Node, node models.Node) (models.Node, error) {
|
|||||||
{"persistentkeepalive", node.PersistentKeepalive},
|
{"persistentkeepalive", node.PersistentKeepalive},
|
||||||
{"saveconfig", node.SaveConfig},
|
{"saveconfig", node.SaveConfig},
|
||||||
{"accesskey", node.AccessKey},
|
{"accesskey", node.AccessKey},
|
||||||
{"isgateway", node.IsGateway},
|
|
||||||
{"gatewayrange", node.GatewayRange},
|
|
||||||
{"interface", node.Interface},
|
{"interface", node.Interface},
|
||||||
{"lastmodified", node.LastModified},
|
{"lastmodified", node.LastModified},
|
||||||
}},
|
}},
|
||||||
|
@@ -589,8 +589,7 @@ func createGateway(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
var nodechange models.Node
|
var nodechange models.Node
|
||||||
|
|
||||||
isgateway := true
|
nodechange.IsGateway = true
|
||||||
nodechange.IsGateway = &isgateway
|
|
||||||
nodechange.GatewayRange = gateway.RangeString
|
nodechange.GatewayRange = gateway.RangeString
|
||||||
if gateway.PostUp == "" {
|
if gateway.PostUp == "" {
|
||||||
nodechange.PostUp = "iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o " + gateway.Interface + " -j MASQUERADE"
|
nodechange.PostUp = "iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o " + gateway.Interface + " -j MASQUERADE"
|
||||||
@@ -603,20 +602,42 @@ func createGateway(w http.ResponseWriter, r *http.Request) {
|
|||||||
nodechange.PostDown = gateway.PostDown
|
nodechange.PostDown = gateway.PostDown
|
||||||
}
|
}
|
||||||
|
|
||||||
node, err = UpdateNode(nodechange, node)
|
collection := mongoconn.Client.Database("netmaker").Collection("nodes")
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
|
||||||
|
// Create filter
|
||||||
|
filter := bson.M{"macaddress": params["macaddress"], "network": params["network"]}
|
||||||
|
|
||||||
|
nodechange.SetLastModified()
|
||||||
|
|
||||||
|
// prepare update model.
|
||||||
|
update := bson.D{
|
||||||
|
{"$set", bson.D{
|
||||||
|
{"postup", nodechange.PostUp},
|
||||||
|
{"preup", nodechange.PostDown},
|
||||||
|
{"isgateway", nodechange.IsGateway},
|
||||||
|
{"gatewayrange", nodechange.GatewayRange},
|
||||||
|
{"lastmodified", nodechange.LastModified},
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
var nodeupdate models.Node
|
||||||
|
|
||||||
|
err = collection.FindOneAndUpdate(ctx, filter, update).Decode(&nodeupdate)
|
||||||
|
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnErrorResponse(w,r,formatError(err, "internal"))
|
returnErrorResponse(w,r,formatError(err, "internal"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = AlertNetwork(params["networkname"])
|
err = SetNetworkNodesLastModified(params["network"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnErrorResponse(w,r,formatError(err, "internal"))
|
returnErrorResponse(w,r,formatError(err, "internal"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(node)
|
json.NewEncoder(w).Encode(node)
|
||||||
}
|
}
|
||||||
@@ -647,19 +668,42 @@ func deleteGateway(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
var nodechange models.Node
|
var nodechange models.Node
|
||||||
|
|
||||||
isgateway := false
|
nodechange.IsGateway = false
|
||||||
nodechange.IsGateway = &isgateway
|
|
||||||
nodechange.GatewayRange = ""
|
nodechange.GatewayRange = ""
|
||||||
nodechange.PostUp = ""
|
nodechange.PostUp = ""
|
||||||
nodechange.PostDown = ""
|
nodechange.PostDown = ""
|
||||||
|
|
||||||
node, err = UpdateNode(nodechange, node)
|
collection := mongoconn.Client.Database("netmaker").Collection("nodes")
|
||||||
if err != nil {
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
|
|
||||||
|
// Create filter
|
||||||
|
filter := bson.M{"macaddress": params["macaddress"], "network": params["network"]}
|
||||||
|
|
||||||
|
nodechange.SetLastModified()
|
||||||
|
|
||||||
|
// prepare update model.
|
||||||
|
update := bson.D{
|
||||||
|
{"$set", bson.D{
|
||||||
|
{"postup", nodechange.PostUp},
|
||||||
|
{"preup", nodechange.PostDown},
|
||||||
|
{"isgateway", nodechange.IsGateway},
|
||||||
|
{"gatewayrange", nodechange.GatewayRange},
|
||||||
|
{"lastmodified", nodechange.LastModified},
|
||||||
|
}},
|
||||||
|
}
|
||||||
|
var nodeupdate models.Node
|
||||||
|
|
||||||
|
err = collection.FindOneAndUpdate(ctx, filter, update).Decode(&nodeupdate)
|
||||||
|
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
returnErrorResponse(w,r,formatError(err, "internal"))
|
returnErrorResponse(w,r,formatError(err, "internal"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = AlertNetwork(params["networkname"])
|
err = SetNetworkNodesLastModified(params["networkname"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnErrorResponse(w,r,formatError(err, "internal"))
|
returnErrorResponse(w,r,formatError(err, "internal"))
|
||||||
return
|
return
|
||||||
|
@@ -41,7 +41,7 @@ type Node struct {
|
|||||||
Password string `json:"password" bson:"password" validate:"password_check"`
|
Password string `json:"password" bson:"password" validate:"password_check"`
|
||||||
Network string `json:"network" bson:"network" validate:"network_exists"`
|
Network string `json:"network" bson:"network" validate:"network_exists"`
|
||||||
IsPending bool `json:"ispending" bson:"ispending"`
|
IsPending bool `json:"ispending" bson:"ispending"`
|
||||||
IsGateway *bool `json:"isgateway" bson:"isgateway"`
|
IsGateway bool `json:"isgateway" bson:"isgateway"`
|
||||||
GatewayRange string `json:"gatewayrange" bson:"gatewayrange"`
|
GatewayRange string `json:"gatewayrange" bson:"gatewayrange"`
|
||||||
PostChanges string `json:"postchanges" bson:"postchanges"`
|
PostChanges string `json:"postchanges" bson:"postchanges"`
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user