diff --git a/plugin/api/gateway/instance.go b/plugin/api/gateway/instance.go index 7a1f8e04..e07ccca2 100644 --- a/plugin/api/gateway/instance.go +++ b/plugin/api/gateway/instance.go @@ -14,6 +14,7 @@ import ( elastic2 "infini.sh/framework/core/elastic" "infini.sh/framework/core/orm" "infini.sh/framework/core/proxy" + "infini.sh/framework/core/task" "infini.sh/framework/core/util" "infini.sh/framework/modules/elastic" "net/http" @@ -146,6 +147,53 @@ func (h *GatewayAPI) deleteInstance(w http.ResponseWriter, req *http.Request, ps return } + //check reference + query := util.MapStr{ + "size": 1, + "query": util.MapStr{ + "bool": util.MapStr{ + "must": []util.MapStr{ + { + "term": util.MapStr{ + "metadata.labels.permit_nodes.id": util.MapStr{ + "value": id, + }, + }, + }, + { + "terms": util.MapStr{ + "metadata.type": []string{"cluster_migration", "cluster_comparison"}, + }, + }, + }, + "must_not": []util.MapStr{ + { + "terms": util.MapStr{ + "status": []string{task.StatusError, task.StatusComplete}, + }, + }, + }, + }, + }, + } + q := &orm.Query{ + RawQuery: util.MustToJSONBytes(query), + } + err, result := orm.Search(task.Task{}, q) + if err != nil { + h.WriteError(w, err.Error(), http.StatusInternalServerError) + log.Error(err) + return + } + if len(result.Result) > 0 { + var taskId interface{} + if m, ok := result.Result[0].(map[string]interface{}); ok { + taskId = m["id"] + } + h.WriteError(w, fmt.Sprintf("failed to delete gateway instance [%s] since it is used by task [%v]", id, taskId), http.StatusInternalServerError) + return + } + err = orm.Delete(nil, &obj) if err != nil { h.WriteError(w, err.Error(), http.StatusInternalServerError) diff --git a/plugin/task_manager/cluster_comparison/orm.go b/plugin/task_manager/cluster_comparison/orm.go index 3137bf4e..f3d37c57 100644 --- a/plugin/task_manager/cluster_comparison/orm.go +++ b/plugin/task_manager/cluster_comparison/orm.go @@ -104,6 +104,7 @@ func buildTask(config *migration_model.ClusterComparisonTaskConfig, creator *rba "target_cluster_id": config.Cluster.Target.Id, "source_total_docs": sourceTotalDocs, "target_total_docs": targetTotalDocs, + "permit_nodes": config.Settings.Execution.Nodes.Permit, }, }, Cancellable: true, diff --git a/plugin/task_manager/cluster_migration/orm.go b/plugin/task_manager/cluster_migration/orm.go index 728f60df..29b7464d 100644 --- a/plugin/task_manager/cluster_migration/orm.go +++ b/plugin/task_manager/cluster_migration/orm.go @@ -101,6 +101,7 @@ func buildTask(config *migration_model.ClusterMigrationTaskConfig, creator *rbac "source_cluster_id": config.Cluster.Source.Id, "target_cluster_id": config.Cluster.Target.Id, "source_total_docs": totalDocs, + "permit_nodes": config.Settings.Execution.Nodes.Permit, }, }, Cancellable: true,