Skip to content

Commit

Permalink
feat(admin): add table open api (arana-db#516)
Browse files Browse the repository at this point in the history
Co-authored-by: Jianhui Dong <[email protected]>
  • Loading branch information
Mulavar and Jianhui Dong authored Nov 17, 2022
1 parent b9a4258 commit d7fbaad
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 29 deletions.
114 changes: 94 additions & 20 deletions pkg/admin/admin.api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,6 @@ paths:
schema:
$ref: '#/components/schemas/Groups'

/tenants/{tenantName}/clusters/{clusterName}/groups/{groupName}:
get:
operationId: getGroup
summary: Get a DB group
responses:
'200':
description: Single DB group
content:
application/json:
schema:
$ref: '#/components/schemas/Group'

post:
operationId: createGroup
summary: Create a DB group
Expand All @@ -164,6 +152,18 @@ paths:
'200':
description: OK

/tenants/{tenantName}/clusters/{clusterName}/groups/{groupName}:
get:
operationId: getGroup
summary: Get a DB group
responses:
'200':
description: Single DB group
content:
application/json:
schema:
$ref: '#/components/schemas/Group'

put:
operationId: putGroup
summary: Update a DB group
Expand Down Expand Up @@ -195,6 +195,18 @@ paths:
schema:
$ref: '#/components/schemas/Clusters'

post:
operationId: createCluster
summary: Create a cluster
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Cluster'
responses:
'200':
description: OK

/tenants/{tenantName}/clusters/{clusterName}:
get:
operationId: getCluster
Expand All @@ -207,9 +219,9 @@ paths:
schema:
$ref: '#/components/schemas/Cluster'

post:
operationId: createCluster
summary: Create a cluster
put:
operationId: putCluster
summary: Update a cluster
requestBody:
content:
application/json:
Expand All @@ -219,21 +231,64 @@ paths:
'200':
description: OK

delete:
operationId: deleteCluster
summary: Delete a cluster
responses:
'204':
description: NONE

/tenants/{tenantName}/clusters/{clusterName}/tables:
get:
operationId: listTables
summary: List all tables
responses:
'200':
description: All Tables
content:
application/json:
schema:
$ref: '#/components/schemas/Tables'

post:
operationId: createTable
summary: Create a table
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Table'
responses:
'200':
description: OK

/tenants/{tenantName}/clusters/{clusterName}/tables/{tableName}:
get:
operationId: getTable
summary: Get a table
responses:
'200':
description: Single Table
content:
application/json:
schema:
$ref: '#/components/schemas/Table'

put:
operationId: putCluster
summary: Update a cluster
operationId: upsertTable
summary: Upsert a table
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Cluster'
$ref: '#/components/schemas/Table'
responses:
'200':
description: OK

delete:
operationId: deleteCluster
summary: Delete a cluster
operationId: deleteTable
summary: Delete a table
responses:
'204':
description: NONE
Expand Down Expand Up @@ -330,6 +385,25 @@ components:
items:
$ref: '#/components/schemas/Group'

Table:
type: object
required:
- sequence
- db_rules
- tbl_rules
- topology
- shadow_topology
- attributes
properties:
# TODO
example:
# TODO

Tables:
type: array
items:
$ref: '#/components/schemas/Table'

Cluster:
type: object
properties:
Expand Down
10 changes: 6 additions & 4 deletions pkg/admin/router/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
func init() {
admin.Register(func(router admin.Router) {
router.GET("/tenants/:tenant/clusters", ListClusters)
router.POST("/tenants/:tenant/clusters/:cluster", CreateCluster)
router.POST("/tenants/:tenant/clusters", CreateCluster)
router.GET("/tenants/:tenant/clusters/:cluster", GetCluster)
router.PUT("/tenants/:tenant/clusters/:cluster", UpdateCluster)
router.DELETE("/tenants/:tenant/clusters/:cluster", RemoveCluster)
Expand Down Expand Up @@ -79,14 +79,16 @@ func CreateCluster(c *gin.Context) error {
var (
service = admin.GetService(c)
tenant = c.Param("tenant")
cluster = c.Param("cluster")
clusterBody *boot.ClusterBody
clusterBody struct {
Name string `json:"name"`
boot.ClusterBody
}
)
if err := c.ShouldBindJSON(&clusterBody); err != nil {
return exception.Wrap(exception.CodeInvalidParams, err)
}

err := service.UpsertCluster(context.Background(), tenant, cluster, clusterBody)
err := service.UpsertCluster(context.Background(), tenant, clusterBody.Name, &clusterBody.ClusterBody)
if err != nil {
return err
}
Expand Down
14 changes: 10 additions & 4 deletions pkg/admin/router/db_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (

func init() {
admin.Register(func(router admin.Router) {
router.POST("/tenants/:tenant/clusters/:cluster/groups/:group", CreateGroup)
router.GET("/tenants/:tenant/groups", ListGroups)
router.POST("/tenants/:tenant/clusters/:cluster/groups", CreateGroup)
router.GET("/tenants/:tenant/clusters/:cluster/groups/:group", GetGroup)
router.PUT("/tenants/:tenant/clusters/:cluster/groups/:group", UpdateGroup)
router.DELETE("/tenants/:tenant/clusters/:cluster/groups/:group", RemoveGroup)
Expand All @@ -45,13 +45,19 @@ func init() {

func CreateGroup(c *gin.Context) error {
service := admin.GetService(c)
tenant, cluster, group := c.Param("tenant"), c.Param("cluster"), c.Param("group")
var groupBody *boot.GroupBody
var (
tenant = c.Param("tenant")
cluster = c.Param("cluster")
groupBody struct {
Name string `json:"name"`
boot.GroupBody
}
)
if err := c.ShouldBindJSON(&groupBody); err != nil {
return exception.Wrap(exception.CodeInvalidParams, err)
}

err := service.UpsertGroup(context.Background(), tenant, cluster, group, groupBody)
err := service.UpsertGroup(context.Background(), tenant, cluster, groupBody.Name, &groupBody.GroupBody)
if err != nil {
return err
}
Expand Down
130 changes: 130 additions & 0 deletions pkg/admin/router/tables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* 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/admin/exception"
"github.com/arana-db/arana/pkg/boot"
"github.com/arana-db/arana/pkg/proto/rule"
)

func init() {
admin.Register(func(router admin.Router) {
router.GET("/tenants/:tenant/clusters/:cluster/tables", ListTables)
router.POST("/tenants/:tenant/clusters/:cluster/tables", CreateTable)
router.GET("/tenants/:tenant/clusters/:cluster/tables/:table", GetTable)
router.PUT("/tenants/:tenant/clusters/:cluster/tables/:table", UpsertTable)
router.DELETE("/tenants/:tenant/clusters/:cluster/tables/:table", RemoveTable)
})
}

func ListTables(c *gin.Context) error {
service := admin.GetService(c)
tenant, cluster := c.Param("tenant"), c.Param("cluster")

tables, err := service.ListTables(context.Background(), tenant, cluster)
if err != nil {
return err
}
c.JSON(http.StatusOK, tables)
return nil
}

func CreateTable(c *gin.Context) error {
service := admin.GetService(c)
var (
tenant = c.Param("tenant")
cluster = c.Param("cluster")
tableBody struct {
Name string `json:"name"`
boot.TableBody
}
)

if err := c.ShouldBindJSON(&tableBody); err != nil {
return exception.Wrap(exception.CodeInvalidParams, err)
}

err := service.UpsertTable(context.Background(), tenant, cluster, tableBody.Name, &tableBody.TableBody)
if err != nil {
return err
}
c.JSON(http.StatusOK, nil)
return nil
}

func GetTable(c *gin.Context) error {
service := admin.GetService(c)
tenant, cluster, table := c.Param("tenant"), c.Param("cluster"), c.Param("table")

data, err := service.GetTable(context.Background(), tenant, cluster, table)
if err != nil {
return err
}
if data == nil {
return exception.New(exception.CodeNotFound, "no such table `%s`", table)
}

c.JSON(http.StatusOK, convertTableVO(data))
return nil
}

func UpsertTable(c *gin.Context) error {
service := admin.GetService(c)
tenant, cluster, table := c.Param("tenant"), c.Param("cluster"), c.Param("table")
var tableBody *boot.TableBody
if err := c.ShouldBindJSON(&tableBody); err != nil {
return exception.Wrap(exception.CodeInvalidParams, err)
}

err := service.UpsertTable(context.Background(), tenant, cluster, table, tableBody)
if err != nil {
return err
}
c.JSON(http.StatusOK, nil)
return nil
}

func RemoveTable(c *gin.Context) error {
service := admin.GetService(c)
tenant, cluster, table := c.Param("tenant"), c.Param("cluster"), c.Param("table")

err := service.RemoveTable(context.Background(), tenant, cluster, table)
if err != nil {
return err
}
c.JSON(http.StatusOK, nil)
return nil
}

func convertTableVO(table *rule.VTable) gin.H {
// TODO convert vTable to VO
return gin.H{
"name": table.Name(),
}
}
2 changes: 1 addition & 1 deletion pkg/boot/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (fp *discovery) UpsertTable(ctx context.Context, tenant, cluster, table str

var (
rule = tenantCfg.ShardingRule
newTables = make([]*config.Table, 0, len(rule.Tables))
newTables = make([]*config.Table, len(rule.Tables))
exist = false
)
_ = reflect.Copy(reflect.ValueOf(newTables), reflect.ValueOf(rule.Tables))
Expand Down

0 comments on commit d7fbaad

Please sign in to comment.