Skip to content

Commit

Permalink
etcd
Browse files Browse the repository at this point in the history
  • Loading branch information
qishu321 committed Aug 18, 2023
1 parent 7096139 commit 2c85da0
Show file tree
Hide file tree
Showing 13 changed files with 788 additions and 80 deletions.
1 change: 0 additions & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 115 additions & 0 deletions api/etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package api

import (
"cmdb-ops-flow/models"
"cmdb-ops-flow/service"
"cmdb-ops-flow/utils/msg"
"cmdb-ops-flow/utils/result"
"context"
"github.com/gin-gonic/gin"
clientv3 "go.etcd.io/etcd/client/v3"
"net/http"
)

func AddEtcd(c *gin.Context) {
var data models.EtcdGroup
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := models.CheckEtcdGroup(data.EtcdGroupname)
if code != msg.SUCCSE {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, "etcd name不能重复", msg.GetErrMsg(msg.ERROR)))
return
}

list, err := service.AddEtcd(data)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))

}
func EditEtcd(c *gin.Context) {
var data models.EtcdGroup
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, err := service.EditEtcd(data)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))
}
func GetEtcd(c *gin.Context) {
//var data models.ScriptManager
var data struct {
ID int `json:"id"`
}

if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(400, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, err := service.GetEtcdList(data.ID)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))

}

func DelEtcd(c *gin.Context) {

var data models.EtcdGroup
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := models.DelEtcdGroup(data.EtcdGroupid)

c.JSON(http.StatusOK, (&result.Result{}).Ok(code, msg.SUCCSE, msg.GetErrMsg(code)))

}

func EtcdGetall(c *gin.Context) {

var data models.EtcdGroup

if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(400, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}

// 初始化 ETCD 客户端
etcdClient, err := service.Etcdinit(data)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
defer etcdClient.Close() // 在使用完毕后关闭 ETCD 客户端连接

// 获取 ETCD 中的数据
resp, err := etcdClient.Get(context.Background(), "", clientv3.WithPrefix())
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}

var list []map[string]string
for _, kv := range resp.Kvs {
list = append(list, map[string]string{
"key": string(kv.Key),
"value": string(kv.Value),
})
}

c.JSON(http.StatusOK, (&result.Result{}).Ok(200, list, msg.GetErrMsg(200)))
}
65 changes: 65 additions & 0 deletions api/etcd_bak.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package api

import (
"cmdb-ops-flow/models"
"cmdb-ops-flow/service"
"cmdb-ops-flow/utils/msg"
"cmdb-ops-flow/utils/result"
"github.com/gin-gonic/gin"
"net/http"
)

func AddEtcdbak(c *gin.Context) {
var data models.EtcdBak
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := models.CheckEtcdBak(data.Etcdbakname)
if code != msg.SUCCSE {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, "etcd name不能重复", msg.GetErrMsg(msg.ERROR)))
return
}

list, err := service.AddEtcdbak(data)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))

}

func GetEtcdbak(c *gin.Context) {
//var data models.ScriptManager
var data struct {
ID int `json:"id"`
}

if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(400, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
list, err := service.GetEtcdbakList(data.ID)
if err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := msg.SUCCSE
c.JSON(http.StatusOK, (&result.Result{}).Ok(code, list, msg.GetErrMsg(code)))

}

func DelEtcdbak(c *gin.Context) {

var data models.EtcdBak
if err := c.ShouldBindJSON(&data); err != nil {
c.JSON(http.StatusOK, (&result.Result{}).Error(msg.ERROR, err.Error(), msg.GetErrMsg(msg.ERROR)))
return
}
code := models.DelEtcdBak(data.Etcdbakid)

c.JSON(http.StatusOK, (&result.Result{}).Ok(code, msg.SUCCSE, msg.GetErrMsg(code)))

}
15 changes: 12 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@ require (
github.com/bwmarrin/snowflake v0.3.0
github.com/gin-gonic/gin v1.9.1
github.com/go-ini/ini v1.67.0
github.com/go-redis/redis/v8 v8.11.5
github.com/gorilla/websocket v1.5.0
github.com/jinzhu/gorm v1.9.16
go.etcd.io/etcd/client/v3 v3.5.9
golang.org/x/crypto v0.9.0
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
Expand All @@ -35,10 +37,17 @@ require (
github.com/stretchr/testify v1.8.4 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
go.etcd.io/etcd/api/v3 v3.5.9 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.9 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.41.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 2c85da0

Please sign in to comment.