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(config): add tenant&cluster config support #464

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
153 changes: 143 additions & 10 deletions pkg/boot/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -62,6 +63,7 @@ var (
var (
ErrorNoTenant = errors.New("no tenant")
ErrorNoDataSourceCluster = errors.New("no datasourceCluster")
ErrorNoGroup = errors.New("no group")
)

func getTableRegexp() *regexp.Regexp {
Expand Down Expand Up @@ -102,18 +104,82 @@ func (fp *discovery) UpsertTenant(ctx context.Context, tenant string, body *Tena
}

func (fp *discovery) RemoveTenant(ctx context.Context, tenant string) error {
// TODO implement me
panic("implement me")
if err := fp.tenantOp.RemoveTenant(tenant); err != nil {
return errors.Wrapf(err, "failed to remove tenant '%s'", tenant)
}
return nil
}

func (fp *discovery) UpsertCluster(ctx context.Context, tenant, cluster string, body *ClusterBody) error {
// TODO implement me
panic("implement me")
op, ok := fp.centers[tenant]
if !ok {
return ErrorNoTenant
}

cfg, err := fp.GetTenant(ctx, tenant)
if err != nil {
return err
}

var (
newClusters = make([]*config.DataSourceCluster, 0, len(cfg.DataSourceClusters))
exist = false
)

_ = reflect.Copy(reflect.ValueOf(newClusters), reflect.ValueOf(cfg.DataSourceClusters))
for _, newCluster := range newClusters {
if newCluster.Name == cluster {
exist = true
newCluster.Type = body.Type
newCluster.Parameters = body.Parameters
newCluster.SqlMaxLimit = body.SqlMaxLimit
break
}
}
if !exist {
newClusters = append(newClusters, &config.DataSourceCluster{
Name: cluster,
Type: body.Type,
SqlMaxLimit: body.SqlMaxLimit,
Parameters: body.Parameters,
Groups: nil,
})
}
cfg.DataSourceClusters = newClusters

err = op.Write(ctx, config.ConfigItemClusters, cfg)
if err != nil {
return err
}

return nil
}

func (fp *discovery) RemoveCluster(ctx context.Context, tenant, cluster string) error {
// TODO implement me
panic("implement me")
op, ok := fp.centers[tenant]
if !ok {
return ErrorNoTenant
}

tenantCfg, err := fp.GetTenant(ctx, tenant)
if err != nil {
return err
}

remainedDsClusters := make([]*config.DataSourceCluster, 0, len(tenantCfg.DataSourceClusters)-1)
for _, dsc := range tenantCfg.DataSourceClusters {
if dsc.Name != cluster {
remainedDsClusters = append(remainedDsClusters, dsc)
}
}

tenantCfg.DataSourceClusters = remainedDsClusters
err = op.Write(ctx, config.ConfigItemClusters, tenantCfg)
if err != nil {
return err
}

return nil
}

func (fp *discovery) UpsertNode(ctx context.Context, tenant, node string, body *NodeBody) error {
Expand All @@ -127,13 +193,80 @@ func (fp *discovery) RemoveNode(ctx context.Context, tenant, node string) error
}

func (fp *discovery) UpsertGroup(ctx context.Context, tenant, cluster, group string, body *GroupBody) error {
// TODO implement me
panic("implement me")
op, ok := fp.centers[tenant]
if !ok {
return ErrorNoTenant
}

tenantCfg, err := op.LoadAll(ctx)
if err != nil {
return err
}

clusterCfg, err := fp.loadCluster(tenant, cluster)
if err != nil {
return err
}

var (
newGroups = make([]*config.Group, 0, len(clusterCfg.Groups))
exist = false
)
_ = reflect.Copy(reflect.ValueOf(newGroups), reflect.ValueOf(clusterCfg.Groups))

for _, groupCfg := range newGroups {
if groupCfg.Name == group {
exist = true
groupCfg.Nodes = body.Nodes
break
}
}
if !exist {
newGroup := &config.Group{
Name: group,
Nodes: body.Nodes,
}
newGroups = append(newGroups, newGroup)
}
clusterCfg.Groups = newGroups

err = op.Write(ctx, config.ConfigItemClusters, tenantCfg)
if err != nil {
return err
}
return nil
}

func (fp *discovery) RemoveGroup(ctx context.Context, tenant, cluster, group string) error {
// TODO implement me
panic("implement me")
op, ok := fp.centers[tenant]
if !ok {
return ErrorNoTenant
}

tenantCfg, err := op.LoadAll(context.Background())
if err != nil {
return err
}

clusterCfg, err := fp.loadCluster(tenant, cluster)
if err != nil {
return err
}

remainedGroups := make([]*config.Group, 0, len(clusterCfg.Groups)-1)
for _, it := range clusterCfg.Groups {
if it.Name != group {
remainedGroups = append(remainedGroups, it)
}
}

clusterCfg.Groups = remainedGroups

err = op.Write(ctx, config.ConfigItemClusters, tenantCfg)
if err != nil {
return err
}
return nil
}

func (fp *discovery) BindNode(ctx context.Context, tenant, cluster, group, node string) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/config/config_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func (c *configWriter) Write(ctx context.Context, item ConfigItem, cfg *Tenant)
allKeyMap := c.pathInfo.ConfigKeyMapping
ret := make(map[PathKey]string)

for i := range allKeyMap {
if allKeyMap[i] == string(item) {
ret[i] = allKeyMap[i]
for pathKey := range allKeyMap {
if allKeyMap[pathKey] == string(item) {
ret[pathKey] = allKeyMap[pathKey]
}
}

Expand Down