mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2025-12-04 19:20:25 +08:00
阶段性提交
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package nodes
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type CreateAction struct {
|
||||
@@ -10,31 +12,34 @@ type CreateAction struct {
|
||||
}
|
||||
|
||||
func (this *CreateAction) Init() {
|
||||
this.Nav("", "", "create")
|
||||
this.Nav("", "node", "create")
|
||||
}
|
||||
|
||||
func (this *CreateAction) RunGet(params struct{}) {
|
||||
// 所有集群
|
||||
/**clusters, err := models.SharedNodeClusterDAO.FindAllEnableClusters()
|
||||
resp, err := this.RPC().NodeClusterRPC().FindAllEnabledClusters(this.AdminContext(), &pb.FindAllEnabledNodeClustersRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
}
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
clusterMaps := []maps.Map{}
|
||||
for _, cluster := range clusters {
|
||||
for _, cluster := range resp.Clusters {
|
||||
clusterMaps = append(clusterMaps, maps.Map{
|
||||
"id": cluster.Id,
|
||||
"name": cluster.Name,
|
||||
})
|
||||
}
|
||||
this.Data["clusters"] = clusterMaps**/
|
||||
this.Data["clusters"] = clusterMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *CreateAction) RunPost(params struct {
|
||||
Name string
|
||||
ClusterId int
|
||||
ClusterId int64
|
||||
|
||||
Must *actions.Must
|
||||
}) {
|
||||
@@ -48,4 +53,16 @@ func (this *CreateAction) RunPost(params struct {
|
||||
}
|
||||
|
||||
// TODO 检查SSH授权
|
||||
|
||||
// 保存
|
||||
_, err := this.RPC().NodeRPC().CreateNode(this.AdminContext(), &pb.CreateNodeRequest{
|
||||
Name: params.Name,
|
||||
ClusterId: params.ClusterId,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
|
||||
66
internal/web/actions/default/nodes/grants/create.go
Normal file
66
internal/web/actions/default/nodes/grants/create.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package grants
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/grants/grantutils"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
)
|
||||
|
||||
type CreateAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *CreateAction) Init() {
|
||||
this.Nav("", "grant", "create")
|
||||
}
|
||||
|
||||
func (this *CreateAction) RunGet(params struct{}) {
|
||||
this.Data["methods"] = grantutils.AllGrantMethods()
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *CreateAction) RunPost(params struct {
|
||||
Name string
|
||||
Method string
|
||||
Username string
|
||||
Password string
|
||||
PrivateKey string
|
||||
Description string
|
||||
|
||||
Must *actions.Must
|
||||
}) {
|
||||
params.Must.
|
||||
Field("name", params.Name).
|
||||
Require("请输入名称")
|
||||
|
||||
switch params.Method {
|
||||
case "user":
|
||||
if len(params.Username) == 0 {
|
||||
this.FailField("username", "请输入SSH登录用户名")
|
||||
}
|
||||
case "privateKey":
|
||||
if len(params.PrivateKey) == 0 {
|
||||
this.FailField("privateKey", "请输入RSA私钥")
|
||||
}
|
||||
default:
|
||||
this.Fail("请选择正确的认证方式")
|
||||
}
|
||||
|
||||
_, err := this.RPC().NodeGrantRPC().CreateNodeGrant(this.AdminContext(), &pb.CreateNodeGrantRequest{
|
||||
Name: params.Name,
|
||||
Method: params.Method,
|
||||
Username: params.Username,
|
||||
Password: params.Password,
|
||||
PrivateKey: params.PrivateKey,
|
||||
Description: params.Description,
|
||||
NodeId: 0,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
22
internal/web/actions/default/nodes/grants/delete.go
Normal file
22
internal/web/actions/default/nodes/grants/delete.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package grants
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
)
|
||||
|
||||
type DeleteAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *DeleteAction) RunPost(params struct {
|
||||
GrantId int64
|
||||
}) {
|
||||
_, err := this.RPC().NodeGrantRPC().DisableNodeGrant(this.AdminContext(), &pb.DisableNodeGrantRequest{GrantId: params.GrantId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
47
internal/web/actions/default/nodes/grants/grant.go
Normal file
47
internal/web/actions/default/nodes/grants/grant.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package grants
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/grants/grantutils"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type GrantAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *GrantAction) Init() {
|
||||
this.Nav("", "grant", "index")
|
||||
}
|
||||
|
||||
func (this *GrantAction) RunGet(params struct {
|
||||
GrantId int64
|
||||
}) {
|
||||
grantResp, err := this.RPC().NodeGrantRPC().FindEnabledGrant(this.AdminContext(), &pb.FindEnabledGrantRequest{GrantId: params.GrantId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if grantResp.Grant == nil {
|
||||
this.WriteString("can not find the grant")
|
||||
return
|
||||
}
|
||||
|
||||
// TODO 处理节点专用的认证
|
||||
|
||||
grant := grantResp.Grant
|
||||
this.Data["grant"] = maps.Map{
|
||||
"id": grant.Id,
|
||||
"name": grant.Name,
|
||||
"method": grant.Method,
|
||||
"methodName": grantutils.FindGrantMethodName(grant.Method),
|
||||
"username": grant.Username,
|
||||
"password": grant.Password,
|
||||
"privateKey": grant.PrivateKey,
|
||||
"description": grant.Description,
|
||||
"su": grant.Su,
|
||||
}
|
||||
|
||||
this.Show()
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package grantutils
|
||||
|
||||
import "github.com/iwind/TeaGo/maps"
|
||||
|
||||
// 所有的认证类型
|
||||
func AllGrantMethods() []maps.Map {
|
||||
return []maps.Map{
|
||||
{
|
||||
"name": "用户名+密码",
|
||||
"value": "user",
|
||||
},
|
||||
{
|
||||
"name": "私钥",
|
||||
"value": "privateKey",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// 获得对应的认证类型名称
|
||||
func FindGrantMethodName(method string) string {
|
||||
for _, m := range AllGrantMethods() {
|
||||
if m.GetString("value") == method {
|
||||
return m.GetString("name")
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
49
internal/web/actions/default/nodes/grants/index.go
Normal file
49
internal/web/actions/default/nodes/grants/index.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package grants
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/grants/grantutils"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "grant", "index")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct{}) {
|
||||
countResp, err := this.RPC().NodeGrantRPC().CountAllEnabledNodeGrants(this.AdminContext(), &pb.CountAllEnabledNodeGrantsRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
page := this.NewPage(countResp.Count)
|
||||
this.Data["page"] = page.AsHTML()
|
||||
|
||||
grantsResp, err := this.RPC().NodeGrantRPC().ListEnabledNodeGrants(this.AdminContext(), &pb.ListEnabledNodeGrantsRequest{
|
||||
Offset: page.Offset,
|
||||
Size: page.Size,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
grantMaps := []maps.Map{}
|
||||
for _, grant := range grantsResp.Grants {
|
||||
grantMaps = append(grantMaps, maps.Map{
|
||||
"id": grant.Id,
|
||||
"name": grant.Name,
|
||||
"method": maps.Map{
|
||||
"type": grant.Method,
|
||||
"name": grantutils.FindGrantMethodName(grant.Method),
|
||||
},
|
||||
})
|
||||
}
|
||||
this.Data["grants"] = grantMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
98
internal/web/actions/default/nodes/grants/update.go
Normal file
98
internal/web/actions/default/nodes/grants/update.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package grants
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/grants/grantutils"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type UpdateAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *UpdateAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *UpdateAction) RunGet(params struct {
|
||||
GrantId int64
|
||||
}) {
|
||||
this.Data["methods"] = grantutils.AllGrantMethods()
|
||||
|
||||
grantResp, err := this.RPC().NodeGrantRPC().FindEnabledGrant(this.AdminContext(), &pb.FindEnabledGrantRequest{GrantId: params.GrantId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if grantResp.Grant == nil {
|
||||
this.WriteString("can not find the grant")
|
||||
return
|
||||
}
|
||||
|
||||
// TODO 处理节点专用的认证
|
||||
|
||||
grant := grantResp.Grant
|
||||
this.Data["grant"] = maps.Map{
|
||||
"id": grant.Id,
|
||||
"name": grant.Name,
|
||||
"method": grant.Method,
|
||||
"methodName": grantutils.FindGrantMethodName(grant.Method),
|
||||
"username": grant.Username,
|
||||
"password": grant.Password,
|
||||
"privateKey": grant.PrivateKey,
|
||||
"description": grant.Description,
|
||||
"su": grant.Su,
|
||||
}
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *UpdateAction) RunPost(params struct {
|
||||
GrantId int64
|
||||
Name string
|
||||
Method string
|
||||
Username string
|
||||
Password string
|
||||
PrivateKey string
|
||||
Description string
|
||||
|
||||
Must *actions.Must
|
||||
}) {
|
||||
params.Must.
|
||||
Field("name", params.Name).
|
||||
Require("请输入名称")
|
||||
|
||||
switch params.Method {
|
||||
case "user":
|
||||
if len(params.Username) == 0 {
|
||||
this.FailField("username", "请输入SSH登录用户名")
|
||||
}
|
||||
case "privateKey":
|
||||
if len(params.PrivateKey) == 0 {
|
||||
this.FailField("privateKey", "请输入RSA私钥")
|
||||
}
|
||||
default:
|
||||
this.Fail("请选择正确的认证方式")
|
||||
}
|
||||
|
||||
// TODO 检查grantId是否存在
|
||||
|
||||
_, err := this.RPC().NodeGrantRPC().UpdateNodeGrant(this.AdminContext(), &pb.UpdateNodeGrantRequest{
|
||||
GrantId: params.GrantId,
|
||||
Name: params.Name,
|
||||
Method: params.Method,
|
||||
Username: params.Username,
|
||||
Password: params.Password,
|
||||
PrivateKey: params.PrivateKey,
|
||||
Description: params.Description,
|
||||
NodeId: 0,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
@@ -1,10 +1,20 @@
|
||||
package nodes
|
||||
|
||||
import "github.com/iwind/TeaGo/actions"
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
)
|
||||
|
||||
type Helper struct {
|
||||
}
|
||||
|
||||
func (this *Helper) BeforeAction(action *actions.ActionObject) {
|
||||
action.Data["teaMenu"] = "nodes"
|
||||
|
||||
selectedTabbar, _ := action.Data["mainTab"]
|
||||
|
||||
tabbar := actionutils.NewTabbar()
|
||||
tabbar.Add("节点管理", "", "/nodes", "", selectedTabbar == "node")
|
||||
tabbar.Add("认证管理", "", "/nodes/grants", "", selectedTabbar == "grant")
|
||||
actionutils.SetTabbar(action, tabbar)
|
||||
}
|
||||
|
||||
@@ -1,15 +1,45 @@
|
||||
package nodes
|
||||
|
||||
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "", "index")
|
||||
this.Nav("", "node", "index")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct{}) {
|
||||
countResp, err := this.RPC().NodeRPC().CountAllEnabledNodes(this.AdminContext(), &pb.CountAllEnabledNodesRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
page := this.NewPage(countResp.Count)
|
||||
this.Data["page"] = page.AsHTML()
|
||||
|
||||
nodesResp, err := this.RPC().NodeRPC().ListEnabledNodes(this.AdminContext(), &pb.ListEnabledNodesRequest{
|
||||
Offset: page.Offset,
|
||||
Size: page.Size,
|
||||
})
|
||||
nodeMaps := []maps.Map{}
|
||||
for _, node := range nodesResp.Nodes {
|
||||
nodeMaps = append(nodeMaps, maps.Map{
|
||||
"id": node.Id,
|
||||
"name": node.Name,
|
||||
"cluster": maps.Map{
|
||||
"id": node.Cluster.Id,
|
||||
"name": node.Cluster.Name,
|
||||
},
|
||||
})
|
||||
}
|
||||
this.Data["nodes"] = nodeMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package nodes
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/grants"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
|
||||
"github.com/iwind/TeaGo"
|
||||
)
|
||||
@@ -13,6 +14,13 @@ func init() {
|
||||
Prefix("/nodes").
|
||||
Get("", new(IndexAction)).
|
||||
GetPost("/create", new(CreateAction)).
|
||||
|
||||
// 授权管理
|
||||
Get("/grants", new(grants.IndexAction)).
|
||||
GetPost("/grants/create", new(grants.CreateAction)).
|
||||
GetPost("/grants/update", new(grants.UpdateAction)).
|
||||
Post("/grants/delete", new(grants.DeleteAction)).
|
||||
Get("/grants/grant", new(grants.GrantAction)).
|
||||
EndAll()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user