mirror of
https://github.com/TeaOSLab/EdgeAdmin.git
synced 2026-03-23 15:35:37 +08:00
阶段性提交
This commit is contained in:
@@ -1,29 +1,29 @@
|
||||
package actionutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"math"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Page struct {
|
||||
Offset int // 开始位置
|
||||
Size int // 每页显示数量
|
||||
Current int // 当前页码
|
||||
Max int // 最大页码
|
||||
Total int // 总数量
|
||||
Offset int64 // 开始位置
|
||||
Size int64 // 每页显示数量
|
||||
Current int64 // 当前页码
|
||||
Max int64 // 最大页码
|
||||
Total int64 // 总数量
|
||||
|
||||
Path string
|
||||
Query url.Values
|
||||
}
|
||||
|
||||
func NewActionPage(actionPtr actions.ActionWrapper, total int, size int) *Page {
|
||||
func NewActionPage(actionPtr actions.ActionWrapper, total int64, size int64) *Page {
|
||||
action := actionPtr.Object()
|
||||
currentPage := action.ParamInt("page")
|
||||
currentPage := action.ParamInt64("page")
|
||||
|
||||
paramSize := action.ParamInt("pageSize")
|
||||
paramSize := action.ParamInt64("pageSize")
|
||||
if paramSize > 0 {
|
||||
size = paramSize
|
||||
}
|
||||
@@ -52,7 +52,7 @@ func (this *Page) calculate() {
|
||||
}
|
||||
|
||||
this.Offset = this.Size * (this.Current - 1)
|
||||
this.Max = int(math.Ceil(float64(this.Total) / float64(this.Size)))
|
||||
this.Max = int64(math.Ceil(float64(this.Total) / float64(this.Size)))
|
||||
}
|
||||
|
||||
func (this *Page) AsHTML() string {
|
||||
@@ -86,9 +86,9 @@ func (this *Page) AsHTML() string {
|
||||
|
||||
for i := before5; i <= after5; i++ {
|
||||
if i == this.Current {
|
||||
result = append(result, `<a href="`+this.composeURL(i)+`" class="active">`+strconv.Itoa(i)+`</a>`)
|
||||
result = append(result, `<a href="`+this.composeURL(i)+`" class="active">`+fmt.Sprintf("%d", i)+`</a>`)
|
||||
} else {
|
||||
result = append(result, `<a href="`+this.composeURL(i)+`">`+strconv.Itoa(i)+`</a>`)
|
||||
result = append(result, `<a href="`+this.composeURL(i)+`">`+fmt.Sprintf("%d", i)+`</a>`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,30 +132,30 @@ func (this *Page) IsLastPage() bool {
|
||||
return this.Current == this.Max
|
||||
}
|
||||
|
||||
func (this *Page) composeURL(page int) string {
|
||||
this.Query["page"] = []string{strconv.Itoa(page)}
|
||||
func (this *Page) composeURL(page int64) string {
|
||||
this.Query["page"] = []string{fmt.Sprintf("%d", page)}
|
||||
return this.Path + "?" + this.Query.Encode()
|
||||
}
|
||||
|
||||
func (this *Page) min(i, j int) int {
|
||||
func (this *Page) min(i, j int64) int64 {
|
||||
if i < j {
|
||||
return i
|
||||
}
|
||||
return j
|
||||
}
|
||||
|
||||
func (this *Page) max(i, j int) int {
|
||||
func (this *Page) max(i, j int64) int64 {
|
||||
if i < j {
|
||||
return j
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func (this *Page) renderSizeOption(size int) string {
|
||||
o := `<option value="` + strconv.Itoa(size) + `"`
|
||||
func (this *Page) renderSizeOption(size int64) string {
|
||||
o := `<option value="` + fmt.Sprintf("%d", size) + `"`
|
||||
if size == this.Size {
|
||||
o += ` selected="selected"`
|
||||
}
|
||||
o += `>` + strconv.Itoa(size) + `条</option>`
|
||||
o += `>` + fmt.Sprintf("%d", size) + `条</option>`
|
||||
return o
|
||||
}
|
||||
|
||||
@@ -1,19 +1,23 @@
|
||||
package actionutils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/admin"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ParentAction struct {
|
||||
actions.ActionObject
|
||||
|
||||
rpcClient *rpc.RPCClient
|
||||
}
|
||||
|
||||
func (this *ParentAction) ErrorPage(err error) {
|
||||
@@ -39,7 +43,7 @@ func (this *ParentAction) NotFound(name string, itemId int) {
|
||||
this.ErrorPage(errors.New(name + " id: '" + strconv.Itoa(itemId) + "' is not found"))
|
||||
}
|
||||
|
||||
func (this *ParentAction) NewPage(total int, size ...int) *Page {
|
||||
func (this *ParentAction) NewPage(total int64, size ...int64) *Page {
|
||||
if len(size) > 0 {
|
||||
return NewActionPage(this, total, size[0])
|
||||
}
|
||||
@@ -56,8 +60,8 @@ func (this *ParentAction) SecondMenu(menuItem string) {
|
||||
this.Data["secondMenuItem"] = menuItem
|
||||
}
|
||||
|
||||
func (this *ParentAction) AdminId() int {
|
||||
return this.Context.GetInt("adminId")
|
||||
func (this *ParentAction) AdminId() int64 {
|
||||
return int64(this.Context.GetInt("adminId"))
|
||||
}
|
||||
|
||||
func (this *ParentAction) CreateLog(level string, description string, args ...interface{}) {
|
||||
@@ -66,7 +70,7 @@ func (this *ParentAction) CreateLog(level string, description string, args ...in
|
||||
utils.PrintError(err)
|
||||
return
|
||||
}
|
||||
_, err = rpcClient.AdminRPC().CreateLog(rpcClient.Context(this.AdminId()), &admin.CreateLogRequest{
|
||||
_, err = rpcClient.AdminRPC().CreateAdminLog(rpcClient.Context(this.AdminId()), &pb.CreateAdminLogRequest{
|
||||
Level: level,
|
||||
Description: fmt.Sprintf(description, args...),
|
||||
Action: this.Request.URL.Path,
|
||||
@@ -76,3 +80,25 @@ func (this *ParentAction) CreateLog(level string, description string, args ...in
|
||||
utils.PrintError(err)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取RPC
|
||||
func (this *ParentAction) RPC() *rpc.RPCClient {
|
||||
if this.rpcClient != nil {
|
||||
return this.rpcClient
|
||||
}
|
||||
|
||||
// 所有集群
|
||||
rpcClient, err := rpc.SharedRPC()
|
||||
if err != nil {
|
||||
logs.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
this.rpcClient = rpcClient
|
||||
|
||||
return rpcClient
|
||||
}
|
||||
|
||||
// 获取Context
|
||||
func (this *ParentAction) AdminContext() context.Context {
|
||||
return this.rpcClient.Context(this.AdminId())
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/admin"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
|
||||
@@ -77,13 +77,13 @@ func (this *IndexAction) RunPost(params struct {
|
||||
if err != nil {
|
||||
this.Fail("服务器出了点小问题:" + err.Error())
|
||||
}
|
||||
resp, err := rpcClient.AdminRPC().Login(rpcClient.Context(0), &admin.LoginRequest{
|
||||
resp, err := rpcClient.AdminRPC().LoginAdmin(rpcClient.Context(0), &pb.LoginAdminRequest{
|
||||
Username: params.Username,
|
||||
Password: params.Password,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
_, err = rpcClient.AdminRPC().CreateLog(rpcClient.Context(0), &admin.CreateLogRequest{
|
||||
_, err = rpcClient.AdminRPC().CreateAdminLog(rpcClient.Context(0), &pb.CreateAdminLogRequest{
|
||||
Level: oplogs.LevelError,
|
||||
Description: "登录时发生系统错误:" + err.Error(),
|
||||
Action: this.Request.URL.Path,
|
||||
@@ -97,7 +97,7 @@ func (this *IndexAction) RunPost(params struct {
|
||||
}
|
||||
|
||||
if !resp.IsOk {
|
||||
_, err = rpcClient.AdminRPC().CreateLog(rpcClient.Context(0), &admin.CreateLogRequest{
|
||||
_, err = rpcClient.AdminRPC().CreateAdminLog(rpcClient.Context(0), &pb.CreateAdminLogRequest{
|
||||
Level: oplogs.LevelWarn,
|
||||
Description: "登录失败,用户名:" + params.Username,
|
||||
Action: this.Request.URL.Path,
|
||||
@@ -114,7 +114,7 @@ func (this *IndexAction) RunPost(params struct {
|
||||
params.Auth.StoreAdmin(adminId, params.Remember)
|
||||
|
||||
// 记录日志
|
||||
_, err = rpcClient.AdminRPC().CreateLog(rpcClient.Context(0), &admin.CreateLogRequest{
|
||||
_, err = rpcClient.AdminRPC().CreateAdminLog(rpcClient.Context(0), &pb.CreateAdminLogRequest{
|
||||
Level: oplogs.LevelInfo,
|
||||
Description: "成功登录系统,用户名:" + params.Username,
|
||||
Action: this.Request.URL.Path,
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
}
|
||||
|
||||
98
internal/web/actions/default/servers/create.go
Normal file
98
internal/web/actions/default/servers/create.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package servers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configs/nodes"
|
||||
"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 {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *CreateAction) Init() {
|
||||
this.Nav("", "", "create")
|
||||
}
|
||||
|
||||
func (this *CreateAction) RunGet(params struct{}) {
|
||||
// 所有集群
|
||||
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 resp.Clusters {
|
||||
clusterMaps = append(clusterMaps, maps.Map{
|
||||
"id": cluster.Id,
|
||||
"name": cluster.Name,
|
||||
})
|
||||
}
|
||||
this.Data["clusters"] = clusterMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *CreateAction) RunPost(params struct {
|
||||
Name string
|
||||
ClusterId int64
|
||||
|
||||
Must *actions.Must
|
||||
}) {
|
||||
params.Must.
|
||||
Field("name", params.Name).
|
||||
Require("请输入服务名称")
|
||||
|
||||
if params.ClusterId <= 0 {
|
||||
this.Fail("请选择部署的集群")
|
||||
}
|
||||
|
||||
// TODO 验证集群ID
|
||||
|
||||
// 配置
|
||||
serverConfig := &nodes.ServerConfig{}
|
||||
serverConfig.IsOn = true
|
||||
serverConfig.Name = params.Name
|
||||
serverConfigJSON, err := serverConfig.AsJSON()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 包含条件
|
||||
includeNodes := []maps.Map{}
|
||||
includeNodesJSON, err := json.Marshal(includeNodes)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 排除条件
|
||||
excludeNodes := []maps.Map{}
|
||||
excludeNodesJSON, err := json.Marshal(excludeNodes)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = this.RPC().ServerRPC().CreateServer(this.AdminContext(), &pb.CreateServerRequest{
|
||||
UserId: 0,
|
||||
AdminId: this.AdminId(),
|
||||
ClusterId: params.ClusterId,
|
||||
Config: serverConfigJSON,
|
||||
IncludeNodesJSON: includeNodesJSON,
|
||||
ExcludeNodesJSON: excludeNodesJSON,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package servers
|
||||
|
||||
import "github.com/iwind/TeaGo/actions"
|
||||
import (
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
)
|
||||
|
||||
type Helper struct {
|
||||
}
|
||||
|
||||
@@ -1,15 +1,60 @@
|
||||
package servers
|
||||
|
||||
import "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configs/nodes"
|
||||
"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("", "", "")
|
||||
this.Nav("", "", "index")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct{}) {
|
||||
countResp, err := this.RPC().ServerRPC().CountAllEnabledServers(this.AdminContext(), &pb.CountAllEnabledServersRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
count := countResp.Count
|
||||
page := this.NewPage(count)
|
||||
this.Data["page"] = page.AsHTML()
|
||||
|
||||
// 服务列表
|
||||
serversResp, err := this.RPC().ServerRPC().ListEnabledServers(this.AdminContext(), &pb.ListEnabledServersRequest{
|
||||
Offset: page.Offset,
|
||||
Size: page.Size,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
serverMaps := []maps.Map{}
|
||||
for _, server := range serversResp.Servers {
|
||||
// 服务名
|
||||
serverConfig := &nodes.ServerConfig{}
|
||||
err = json.Unmarshal(server.Config, &serverConfig)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
serverMaps = append(serverMaps, maps.Map{
|
||||
"id": server.Id,
|
||||
"name": serverConfig.Name,
|
||||
"cluster": maps.Map{
|
||||
"id": server.Cluster.Id,
|
||||
"name": server.Cluster.Name,
|
||||
},
|
||||
})
|
||||
}
|
||||
this.Data["servers"] = serverMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ func init() {
|
||||
Helper(NewHelper()).
|
||||
Prefix("/servers").
|
||||
Get("", new(IndexAction)).
|
||||
GetPost("/create", new(CreateAction)).
|
||||
EndAll()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,10 +3,9 @@ package helpers
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
|
||||
nodes "github.com/TeaOSLab/EdgeAdmin/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/admin"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/utils"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"net/http"
|
||||
"reflect"
|
||||
)
|
||||
@@ -38,9 +37,10 @@ func (this *UserMustAuth) BeforeAction(actionPtr actions.ActionWrapper, paramNam
|
||||
return false
|
||||
}
|
||||
|
||||
rpcResp, err := rpc.AdminRPC().CheckAdminExists(rpc.Context(0), &admin.CheckAdminExistsRequest{AdminId: int64(adminId)})
|
||||
rpcResp, err := rpc.AdminRPC().CheckAdminExists(rpc.Context(0), &pb.CheckAdminExistsRequest{AdminId: int64(adminId)})
|
||||
if err != nil {
|
||||
logs.Error(err)
|
||||
utils.PrintError(err)
|
||||
actionPtr.Object().WriteString(teaconst.ErrServer)
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ func (this *UserMustAuth) BeforeAction(actionPtr actions.ActionWrapper, paramNam
|
||||
action.Data["teaTitle"] = teaconst.ProductNameZH
|
||||
action.Data["teaName"] = teaconst.ProductNameZH
|
||||
|
||||
resp, err := rpc.AdminRPC().FindAdminFullname(rpc.Context(0), &admin.FindAdminNameRequest{AdminId: int64(this.AdminId)})
|
||||
resp, err := rpc.AdminRPC().FindAdminFullname(rpc.Context(0), &pb.FindAdminNameRequest{AdminId: int64(this.AdminId)})
|
||||
if err != nil {
|
||||
utils.PrintError(err)
|
||||
action.Data["teaUsername"] = ""
|
||||
|
||||
Reference in New Issue
Block a user