节点分组删除

pull/1/head
Doflatango 2017-02-20 14:53:36 +08:00 committed by miraclesu
parent f9dbc16d0f
commit 53b293e967
2 changed files with 67 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import (
v3 "github.com/coreos/etcd/clientv3"
"github.com/gorilla/mux"
"fmt"
"sunteng/commons/log"
"sunteng/cronsun/conf"
"sunteng/cronsun/models"
@ -88,12 +89,64 @@ func (n *Node) GetGroupByGroupId(w http.ResponseWriter, r *http.Request) {
func (n *Node) DeleteGroup(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
_, err := models.DeleteGroupById(vars["id"])
groupId := strings.TrimSpace(vars["id"])
if len(groupId) == 0 {
outJSONError(w, http.StatusBadRequest, "empty node ground id.")
return
}
_, err := models.DeleteGroupById(groupId)
if err != nil {
outJSONError(w, http.StatusInternalServerError, err.Error())
return
}
gresp, err := models.DefalutClient.Get(conf.Config.Cmd, v3.WithPrefix())
if err != nil {
errstr := fmt.Sprintf("failed to fetch jobs from etcd after deleted node group[%s]: %s", groupId, err.Error())
log.Error(errstr)
outJSONError(w, http.StatusInternalServerError, errstr)
return
}
// update rule's node group
for i := range gresp.Kvs {
job := models.Job{}
err = json.Unmarshal(gresp.Kvs[i].Value, &job)
key := string(gresp.Kvs[i].Key)
if err != nil {
log.Errorf("failed to unmarshal job[%s]: %s", key, err.Error())
continue
}
update := false
for j := range job.Rules {
var ngs []string
for _, gid := range job.Rules[j].GroupIDs {
if gid != groupId {
ngs = append(ngs, gid)
}
}
if len(ngs) != len(job.Rules[j].GroupIDs) {
job.Rules[j].GroupIDs = ngs
update = true
}
}
if update {
v, err := json.Marshal(&job)
if err != nil {
log.Errorf("failed to marshal job[%s]: %s", key, err.Error())
continue
}
_, err = models.DefalutClient.PutWithModRev(key, string(v), gresp.Kvs[i].ModRevision)
if err != nil {
log.Errorf("failed to update job[%s]: %s", key, err.Error())
continue
}
}
}
outJSONWithCode(w, http.StatusNoContent, nil)
}

View File

@ -14,6 +14,9 @@
<div class="field">
<button class="fluid blue ui button" type="button" v-on:click="submit"><i class="upload icon"></i> 保存分组</button>
</div>
<div class="field">
<button class="fluid red ui button" type="button" v-on:click="remove"><i class="remove icon"></i> 删除分组</button>
</div>
</form>
</template>
@ -80,6 +83,16 @@ export default {
.onfailed((resp)=>{console.log(resp)})
.onend(()=>{vm.loading=false})
.do();
},
remove(){
if (!confirm('确定删除该分组 ' + this.group.name + '?')) return;
var vm = this;
this.$rest.DELETE('node/group/'+this.group.id)
.onsucceed(204, ()=>{vm.$router.push('/node/group')})
.onfailed((resp)=>{console.log(resp)})
.onend(()=>{vm.loading=false})
.do();
}
},