Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add group api for UI module #344 #373

Merged
merged 6 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions pkg/admin/router/db_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package router
dongzl marked this conversation as resolved.
Show resolved Hide resolved

import (
"context"
"net/http"
)

import (
"github.com/arana-db/arana/pkg/admin"
"github.com/arana-db/arana/pkg/boot"
dongzl marked this conversation as resolved.
Show resolved Hide resolved
)

import (
"github.com/gin-gonic/gin"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 3rd imports should before arana imports, pls reformat


func init() {
admin.Register(func(router gin.IRoutes) {
router.POST("/tenants/:tenant/groups", CreateGroup)
router.GET("/tenants/:tenant/groups", ListGroups)
router.GET("/tenants/:tenant/groups/:group", GetGroup)
router.PUT("/tenants/:tenant/groups/:group", UpdateGroup)
router.DELETE("/tenants/:tenant/groups/:group", RemoveGroup)
})
}

func CreateGroup(c *gin.Context) {
service := admin.GetService(c)
tenantName := c.Param("tenant")
var group *boot.GroupBody
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be boot.GroupBody.

if err := c.ShouldBindJSON(&group); err == nil {
err := service.UpsertGroup(context.Background(), tenantName, "", "", group)
if err != nil {
_ = c.Error(err)
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里有问题吧,当出错的时候,返回值是啥呢?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不会的,我昨天试了下,当error 的时候,返回值是空的

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package main

import (
	"errors"
	"github.com/gin-gonic/gin"
)

func main() {
	g := gin.Default()
	g.GET("/11", func(context *gin.Context) {
		context.Error(errors.New("111"))
	})
	g.Run()
}

结果:
image
image

}
c.JSON(http.StatusOK, nil)
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
}

func ListGroups(c *gin.Context) {
service := admin.GetService(c)
tenantName := c.Param("tenant")
groups, err := service.ListGroups(context.Background(), tenantName)
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, groups)
}

func GetGroup(c *gin.Context) {
service := admin.GetService(c)
tenant := c.Param("tenant")
group := c.Param("group")
data, err := service.GetGroup(context.Background(), tenant, "", group)
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, data)
}

func UpdateGroup(c *gin.Context) {
service := admin.GetService(c)
tenant := c.Param("tenant")
group := c.Param("group")
var groupBody *boot.GroupBody
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

if err := c.ShouldBindJSON(&groupBody); err == nil {
err := service.UpsertGroup(context.Background(), tenant, "", group, groupBody)
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, nil)
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
}

func RemoveGroup(c *gin.Context) {
service := admin.GetService(c)
tenant, group := c.Param("tenant"), c.Param("group")

err := service.RemoveGroup(context.Background(), tenant, "", group)
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusOK, nil)
}
9 changes: 9 additions & 0 deletions pkg/boot/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ func (fp *discovery) GetDataSourceCluster(ctx context.Context, cluster string) (
return dataSourceCluster, nil
}

func (fp *discovery) GetGroup(ctx context.Context, tenant, cluster, group string) (*config.Group, error) {
exist, ok := fp.loadGroup(cluster, group)
if !ok {
return nil, nil
}

return exist, nil
}

func (fp *discovery) GetCluster(ctx context.Context, tenant, cluster string) (*Cluster, error) {
exist, ok := fp.loadCluster(cluster)
if !ok {
Expand Down
3 changes: 3 additions & 0 deletions pkg/boot/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ type ConfigProvider interface {
// GetDataSourceCluster returns the dataSourceCluster object
GetDataSourceCluster(ctx context.Context, cluster string) (*config.DataSourceCluster, error)

// GetGroup returns the cluster info
GetGroup(ctx context.Context, tenant, cluster, group string) (*config.Group, error)

// GetCluster returns the cluster info
GetCluster(ctx context.Context, tenant, cluster string) (*Cluster, error)

Expand Down