Skip to content

Commit

Permalink
feat: add group api for UI module #344 (#373)
Browse files Browse the repository at this point in the history
* add: missing interfaces in proto files and discovery files

* feat: group api in file db_groups.go (#344)

* style: add license header and add tool: imports-formatter

* style: fix import
  • Loading branch information
jettcc authored Aug 31, 2022
1 parent de677cb commit 1113978
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
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

import (
"context"
"net/http"
)

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

import (
"github.com/arana-db/arana/pkg/admin"
"github.com/arana-db/arana/pkg/boot"
)

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
if err := c.ShouldBindJSON(&group); err == nil {
err := service.UpsertGroup(context.Background(), tenantName, "", "", group)
if err != nil {
_ = c.Error(err)
return
}
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
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

0 comments on commit 1113978

Please sign in to comment.