Skip to content

Commit

Permalink
feat(config): add tenant&cluster&group config support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jianhui Dong committed Oct 20, 2022
1 parent 9b07dda commit e85f968
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 13 deletions.
113 changes: 103 additions & 10 deletions pkg/boot/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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 +103,64 @@ 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
}

for _, newCluster := range cfg.DataSourceClusters {
if newCluster.Name == cluster {
newCluster.Type = body.Type
newCluster.Parameters = body.Parameters
newCluster.SqlMaxLimit = body.SqlMaxLimit
}
}

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 +174,59 @@ 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(context.Background())
if err != nil {
return err
}

groupCfg, err := fp.GetGroup(ctx, tenant, cluster, group)
if err != nil {
return err
}

groupCfg.Nodes = body.Nodes
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

0 comments on commit e85f968

Please sign in to comment.