소스 검색

add clean up for static nodes

abhishek9686 4 주 전
부모
커밋
ea40d54667
1개의 변경된 파일45개의 추가작업 그리고 0개의 파일을 삭제
  1. 45 0
      controllers/ext_client.go

+ 45 - 0
controllers/ext_client.go

@@ -47,6 +47,8 @@ func extClientHandlers(r *mux.Router) {
 	r.HandleFunc("/api/extclients/{network}/{nodeid}", logic.SecurityCheck(false, checkFreeTierLimits(limitChoiceMachines, http.HandlerFunc(createExtClient)))).
 		Methods(http.MethodPost)
 	r.HandleFunc("/api/v1/client_conf/{network}", logic.SecurityCheck(false, http.HandlerFunc(getExtClientHAConf))).Methods(http.MethodGet)
+	r.HandleFunc("/api/v1/extclients", logic.SecurityCheck(true, http.HandlerFunc(deleteOfflineExtClients))).
+		Methods(http.MethodDelete)
 }
 
 func checkIngressExists(nodeID string) bool {
@@ -1077,6 +1079,49 @@ func deleteExtClient(w http.ResponseWriter, r *http.Request) {
 	logic.ReturnSuccessResponse(w, r, params["clientid"]+" deleted.")
 }
 
+// @Summary     Delete an individual remote access client
+// @Router      /api/extclients/{network}/{clientid} [delete]
+// @Tags        Remote Access Client
+// @Security    oauth2
+// @Success     200
+// @Failure     500 {object} models.ErrorResponse
+// @Failure     403 {object} models.ErrorResponse
+func deleteOfflineExtClients(w http.ResponseWriter, r *http.Request) {
+	// Set header
+	network := r.URL.Query().Get("network")
+	if network == "" {
+		logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("network param is missing"), "badrequest"))
+		return
+	}
+	go func() {
+		defaultPolicy, _ := logic.GetDefaultPolicy(models.NetworkID(network), models.DevicePolicy)
+		extclients, _ := logic.GetNetworkExtClients(network)
+		sendPeerUpdate := false
+		for _, extclient := range extclients {
+			staticNode := extclient.ConvertToStaticNode()
+			logic.GetNodeStatus(&staticNode, defaultPolicy.Enabled)
+			if staticNode.Status == models.OfflineSt {
+				err := logic.DeleteExtClientAndCleanup(extclient)
+				if err != nil {
+					continue
+				}
+				sendPeerUpdate = true
+			}
+
+		}
+
+		if sendPeerUpdate {
+			mq.PublishPeerUpdate(true)
+			if servercfg.IsDNSMode() {
+				logic.SetDNS()
+			}
+		}
+
+	}()
+
+	logic.ReturnSuccessResponse(w, r, "intiated cleanup")
+}
+
 // validateCustomExtClient	Validates the extclient object
 func validateCustomExtClient(customExtClient *models.CustomExtClient, checkID bool) error {
 	v := validator.New()