Skip to content

Commit

Permalink
重命名集群
Browse files Browse the repository at this point in the history
  • Loading branch information
shaohq committed May 1, 2022
1 parent 3b23b54 commit 0876b95
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 3 deletions.
45 changes: 45 additions & 0 deletions server/api/cluster/rename_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cluster

import (
"net/http"
"os"

"github.com/eip-work/kuboard-spray/api/cluster/cluster_common"
"github.com/eip-work/kuboard-spray/common"
"github.com/eip-work/kuboard-spray/constants"
"github.com/gin-gonic/gin"
)

type RenameClusterRequest struct {
GetClusterRequest
NewClusterName string `form:"newName"`
}

func RenameCluster(c *gin.Context) {
var req RenameClusterRequest
c.ShouldBindUri(&req)
c.ShouldBindQuery(&req)

clusterDir := constants.GET_DATA_CLUSTER_DIR()
os.Rename(clusterDir+"/"+req.Cluster, clusterDir+"/"+req.NewClusterName)

inventoryPath := cluster_common.ClusterInventoryYamlPath(req.NewClusterName)
inventory, err := common.ParseYamlFile(inventoryPath)
if err != nil {
common.HandleError(c, http.StatusInternalServerError, "读取 Cluster 信息失败", err)
return
}

common.PopulateKuboardSprayVars(inventory, "cluster", req.NewClusterName)

if err := cluster_common.SaveInventory(req.NewClusterName, inventory); err != nil {
common.HandleError(c, http.StatusInternalServerError, "保存 Cluster 信息失败", err)
return
}

c.JSON(http.StatusOK, common.KuboardSprayResponse{
Code: http.StatusOK,
Message: "success",
})

}
1 change: 1 addition & 0 deletions server/kuboard-spray.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func setupRouter() *gin.Engine {
api.GET("/clusters/:cluster", cluster.GetCluster)
api.PUT("/clusters/:cluster", cluster.ModifyCluster)
api.DELETE("/clusters/:cluster", cluster.DeleteCluster)
api.PATCH("/clusters/:cluster", cluster.RenameCluster)

api.POST("/clusters/:cluster/install_cluster", operation.InstallCluster)
api.POST("/clusters/:cluster/remove_node", operation.RemoveNode)
Expand Down
8 changes: 6 additions & 2 deletions web/src/views/clusters/Clusters.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ en:
clusterList: Clusters List
addCluster: Add Cluster Installation Plan
confirmToDelete: Do you confirm to delete the cluster info in KuboardSpray? Cluster itself will not be impacted.
rename: 重命名
zh:
clusters: 集群管理
clusterList: 集群列表
addCluster: 添加集群安装计划
confirmToDelete: 是否删除此集群在 KuboardSpray 的信息?(该集群本身将继续运行不受影响。)
rename: 重命名
</i18n>

<template>
Expand All @@ -30,8 +32,9 @@ zh:
<div v-else style="display: flex; flex-wrap: wrap;">
<div v-for="(item, index) in clusters" :key="'cluster' + index" class="cluster">
<div class="deleteButton">
<ClustersRename :clusterName="item" @success="refresh"></ClustersRename>
<el-popconfirm :confirm-button-text="$t('msg.ok')" :cancel-button-text="$t('msg.cancel')" icon="el-icon-warning" icon-color="red"
placement="right" :title="$t('confirmToDelete')" @confirm="deleteCluster(item)">
placement="top-start" :title="$t('confirmToDelete')" @confirm="deleteCluster(item)">
<template #reference>
<el-button icon="el-icon-delete" circle type="danger"></el-button>
</template>
Expand All @@ -54,6 +57,7 @@ zh:
<script>
import mixin from '../../mixins/mixin.js'
import CreateCluster from './create/CreateCluster.vue'
import ClustersRename from './create/ClustersRename.vue'
export default {
mixins: [mixin],
Expand All @@ -78,7 +82,7 @@ export default {
},
computed: {
},
components: { CreateCluster },
components: { CreateCluster, ClustersRename },
mounted () {
this.refresh()
},
Expand Down
98 changes: 98 additions & 0 deletions web/src/views/clusters/create/ClustersRename.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<i18n>
en:
conflict: conflict with existing one {name}.
rename: Rename
renameCluster: Rename cluster {name} to
input_newName: Please input new cluster name here.
zh:
conflict: 与已有的重复 {name}
rename: 重命名
renameCluster: 将集群 {name} 重命名为
input_newName: 请输入集群的新名称
</i18n>

<template>
<el-popover trigger="manual" v-model:visible="show" placement="top-start" :title="$t('rename')" width="420px">
<template #reference>
<el-button icon="el-icon-edit" circle @click="show = true"></el-button>
</template>
<el-form :model="form" ref="form">
<div class="app_text_mono" style="text-align: left; line-height: 28px;">
<span>{{ $t('renameCluster', { name: clusterName}) }}</span>
<el-form-item prop="name" :rules="nameRules">
<el-input v-model.trim="form.name" :placeholder="$t('input_newName')"></el-input>
</el-form-item>
</div>
<div style="text-align: right;">
<el-button icon="el-icon-close" type="default" @click="show = false">{{ $t('msg.cancel') }}</el-button>
<el-button icon="el-icon-check" type="primary" @click="renameCluster">{{ $t('msg.ok') }}</el-button>
</div>
</el-form>
</el-popover>
</template>

<script>
export default {
props: {
clusterName: { type: String, required: true }
},
data() {
return {
show: false,
nameRules: [
{
validator: (rule, value, callback) => {
if (value.length < 4) {
return callback('min length ' + 4)
}
if (value.length > 20) {
return callback('max length ' + 20)
}
if (!/^[a-zA-Z][a-zA-Z0-9_]{3,21}$/.test(value)) {
return callback('必须以字母开头,可以包含数字和字母')
}
this.kuboardSprayApi.get(`/clusters/${value}`).then(() => {
callback(this.$t('conflict', {name: value}))
}).catch(e => {
// console.log(e.response)
if (e.response && e.response.data.code === 500) {
callback()
}
})
},
trigger: 'blur',
}
],
form: {
name: undefined,
}
}
},
computed: {
},
components: { },
mounted () {
},
methods: {
renameCluster() {
this.$refs.form.validate(flag => {
if (flag) {
this.kuboardSprayApi.patch(`/clusters/${this.clusterName}?newName=${this.form.name}`).then(resp => {
console.log(resp.data)
this.show = false
this.$message.success(this.$t('msg.save_succeeded'))
this.$emit('success')
}).catch(e => {
console.error(e)
this.$message.error('修改集群名称失败 : ' + e)
})
}
})
}
}
}
</script>

<style scoped lang="scss">
</style>
2 changes: 1 addition & 1 deletion web/src/views/clusters/create/CreateCluster.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ en:
createResource: "Add Resource Package"
name: Name
requiresName: Please input name.
conflict: conflict with existing one.
conflict: conflict with existing one {name}.
goToResourcePage: This is going to open Resource package management page in a new window, do you confirm?
zh:
addCluster: 添加集群安装计划
Expand Down

0 comments on commit 0876b95

Please sign in to comment.