diff --git a/controller/strategy/iml.go b/controller/strategy/iml.go index 94786b07..7f0b3e22 100644 --- a/controller/strategy/iml.go +++ b/controller/strategy/iml.go @@ -25,6 +25,14 @@ type imlStrategyController struct { serviceModule service.IServiceModule `autowired:""` } +func (i *imlStrategyController) Restore(ctx *gin.Context, id string) error { + return i.strategyModule.Restore(ctx, id) +} + +func (i *imlStrategyController) DeleteServiceStrategy(ctx *gin.Context, serviceId string, id string) error { + return i.strategyModule.DeleteServiceStrategy(ctx, serviceId, id) +} + func (i *imlStrategyController) ToPublish(ctx *gin.Context, driver string) ([]*strategy_dto.ToPublishItem, string, string, bool, error) { list, err := i.strategyModule.ToPublish(ctx, driver) if err != nil { diff --git a/controller/strategy/strategy.go b/controller/strategy/strategy.go index b6eac85a..ad0f4fac 100644 --- a/controller/strategy/strategy.go +++ b/controller/strategy/strategy.go @@ -22,6 +22,9 @@ type IStrategyController interface { DisableStrategy(ctx *gin.Context, id string) error DeleteStrategy(ctx *gin.Context, id string) error + DeleteServiceStrategy(ctx *gin.Context, serviceId string, id string) error + + Restore(ctx *gin.Context, id string) error FilterGlobalOptions(ctx *gin.Context) ([]*strategy_dto.FilterOption, error) FilterServiceOptions(ctx *gin.Context) ([]*strategy_dto.FilterOption, error) diff --git a/module/strategy/iml.go b/module/strategy/iml.go index 8c37b631..1c3032b4 100644 --- a/module/strategy/iml.go +++ b/module/strategy/iml.go @@ -35,6 +35,22 @@ type imlStrategyModule struct { transaction store.ITransaction `autowired:""` } +func (i *imlStrategyModule) Restore(ctx context.Context, id string) error { + return i.strategyService.Restore(ctx, id) +} + +func (i *imlStrategyModule) DeleteServiceStrategy(ctx context.Context, serviceId string, id string) error { + _, err := i.strategyService.LatestStrategyCommit(ctx, strategy_dto.ScopeService, serviceId, id) + if err != nil { + // 判断是否已经发布,如果未发布则直接删除 + if !errors.Is(err, gorm.ErrRecordNotFound) { + return err + } + return i.strategyService.Delete(ctx, id) + } + return i.strategyService.SortDelete(ctx, id) +} + func (i *imlStrategyModule) ToPublish(ctx context.Context, driver string) ([]*strategy_dto.ToPublishItem, error) { scope := strategy_dto.ToScope(strategy_dto.ScopeGlobal) list, err := i.strategyService.SearchAll(ctx, "", driver, scope.Int(), "") @@ -46,7 +62,7 @@ func (i *imlStrategyModule) ToPublish(ctx context.Context, driver string) ([]*st if err != nil { return nil, err } - commitMap := utils.SliceToMapO(commits, func(c *commit.Commit[strategy.StrategyCommit]) (string, string) { return c.Key, c.Data.Version }) + commitMap := utils.SliceToMapO(commits, func(c *commit.Commit[strategy.StrategyCommit]) (string, string) { return c.Data.Id, c.Data.Version }) items := make([]*strategy_dto.ToPublishItem, 0, len(list)) for _, l := range list { status := strategy_dto.StrategyStatus(l, commitMap[l.Id]) @@ -73,7 +89,7 @@ func (i *imlStrategyModule) Search(ctx context.Context, keyword string, driver s if err != nil { return nil, 0, err } - commitMap := utils.SliceToMapO(commits, func(c *commit.Commit[strategy.StrategyCommit]) (string, string) { return c.Key, c.Data.Version }) + commitMap := utils.SliceToMapO(commits, func(c *commit.Commit[strategy.StrategyCommit]) (string, string) { return c.Data.Id, c.Data.Version }) items := make([]*strategy_dto.StrategyItem, 0, len(list)) for _, l := range list { fs := make([]*strategy_dto.Filter, 0) @@ -210,6 +226,9 @@ func (i *imlStrategyModule) Publish(ctx context.Context, driver string, scope st } } + if l.IsStop { + continue + } // TODO:同步到网关 err = i.strategyService.CommitStrategy(txCtx, scope, target, l.Id, l) if err != nil { diff --git a/module/strategy/module.go b/module/strategy/module.go index d35ee39a..396622f2 100644 --- a/module/strategy/module.go +++ b/module/strategy/module.go @@ -20,6 +20,9 @@ type IStrategyModule interface { Publish(ctx context.Context, driver string, scope string, target string) error Delete(ctx context.Context, id string) error ToPublish(ctx context.Context, driver string) ([]*strategy_dto.ToPublishItem, error) + Restore(ctx context.Context, id string) error + + DeleteServiceStrategy(ctx context.Context, serviceId string, id string) error } func init() { diff --git a/plugins/core/strategy.go b/plugins/core/strategy.go index 64e10396..2ed88374 100644 --- a/plugins/core/strategy.go +++ b/plugins/core/strategy.go @@ -16,12 +16,15 @@ func (p *plugin) strategyApis() []pm3.Api { pm3.CreateApiWidthDoc(http.MethodPatch, "/api/v1/strategy/global/:driver/enable", []string{"context", "query:strategy"}, nil, p.strategyController.EnableStrategy), pm3.CreateApiWidthDoc(http.MethodPatch, "/api/v1/strategy/global/:driver/disable", []string{"context", "query:strategy"}, nil, p.strategyController.DisableStrategy), pm3.CreateApiWidthDoc(http.MethodPost, "/api/v1/strategy/global/:driver/publish", []string{"context", "rest:driver"}, nil, p.strategyController.PublishGlobalStrategy), + pm3.CreateApiWidthDoc(http.MethodPatch, "/api/v1/strategy/global/:driver/restore", []string{"context", "query:strategy"}, nil, p.strategyController.Restore), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/strategy/service/:driver/list", []string{"context", "query:keyword", "query:service", "rest:driver", "query:page", "query:page_size", "query:order", "query:sort", "query:filters"}, []string{"list", "total"}, p.strategyController.ServiceStrategyList), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/strategy/service/:driver", []string{"context", "query:strategy"}, []string{"strategy"}, p.strategyController.GetStrategy), pm3.CreateApiWidthDoc(http.MethodPost, "/api/v1/strategy/service/:driver", []string{"context", "query:service", "rest:driver", "body"}, nil, p.strategyController.CreateServiceStrategy), pm3.CreateApiWidthDoc(http.MethodPatch, "/api/v1/strategy/service/:driver/enable", []string{"context", "query:strategy"}, nil, p.strategyController.EnableStrategy), pm3.CreateApiWidthDoc(http.MethodPatch, "/api/v1/strategy/service/:driver/disable", []string{"context", "query:strategy"}, nil, p.strategyController.DisableStrategy), + pm3.CreateApiWidthDoc(http.MethodDelete, "/api/v1/strategy/service/:driver", []string{"context", "query:service", "query:strategy"}, nil, p.strategyController.DeleteServiceStrategy), + pm3.CreateApiWidthDoc(http.MethodPatch, "/api/v1/strategy/service/:driver/restore", []string{"context", "query:strategy"}, nil, p.strategyController.Restore), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/strategy/global/filter-options", []string{"context"}, []string{"options"}, p.strategyController.FilterGlobalOptions), pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/strategy/service/filter-options", []string{"context"}, []string{"options"}, p.strategyController.FilterServiceOptions), diff --git a/resources/access/access.yaml b/resources/access/access.yaml index 69a6daee..6b24896f 100644 --- a/resources/access/access.yaml +++ b/resources/access/access.yaml @@ -131,6 +131,16 @@ system: value: 'manager' dependents: - system.settings.log_configuration.view + - name: global strategy + value: "strategy" + children: + - name: view + value: 'view' + guest_allow: true + - name: manager + value: 'manager' + dependents: + - system.settings.strategy.view team: - name: service value: 'service' @@ -197,6 +207,16 @@ team: value: 'manager' dependents: - team.service.subscription.view + - name: service strategy + value: 'strategy' + children: + - name: view + value: 'view' + guest_allow: true + - name: manager + value: 'manager' + dependents: + - team.service.strategy.view - name: consumer value: 'consumer' children: diff --git a/resources/access/role.yaml b/resources/access/role.yaml index 8dcd8e1a..9b9613db 100644 --- a/resources/access/role.yaml +++ b/resources/access/role.yaml @@ -19,6 +19,8 @@ system: - system.settings.role.view - system.settings.ssl_certificate.manager - system.settings.ssl_certificate.view + - system.settings.strategy.view + - system.settings.strategy.manager - system.workspace.application.manager_all - system.workspace.application.view_all - system.workspace.service.manager_all @@ -49,6 +51,8 @@ system: - system.settings.log_configuration.view - system.settings.ssl_certificate.manager - system.settings.ssl_certificate.view + - system.settings.strategy.view + - system.settings.strategy.manager - system.workspace.application.view_all - system.workspace.service.view_all - system.workspace.team.view_all @@ -75,6 +79,8 @@ team: - team.service.release.view - team.service.service_intro.manager - team.service.service_intro.view + - team.service.strategy.view + - team.service.strategy.manager - team.service.subscription.manager - team.service.subscription.view - team.service.upstream.manager @@ -99,6 +105,8 @@ team: - team.service.release.view - team.service.service_intro.manager - team.service.service_intro.view + - team.service.strategy.view + - team.service.strategy.manager - team.service.subscription.manager - team.service.subscription.view - team.service.upstream.manager @@ -120,7 +128,7 @@ team: - team.service.service.manager - team.service.service_intro.manager - team.service.service_intro.view - - team.service.subscription.manager + - team.service.strategy.view - team.service.subscription.view - team.service.upstream.manager - team.service.upstream.view diff --git a/service/strategy/iml.go b/service/strategy/iml.go index 21a433cd..ca72ac61 100644 --- a/service/strategy/iml.go +++ b/service/strategy/iml.go @@ -22,6 +22,11 @@ type imlStrategyService struct { universally.IServiceEdit[Edit] } +func (i *imlStrategyService) Restore(ctx context.Context, id string) error { + _, err := i.store.UpdateWhere(ctx, map[string]interface{}{"uuid": id}, map[string]interface{}{"is_delete": false}) + return err +} + func (i *imlStrategyService) SearchAll(ctx context.Context, keyword string, driver string, scope int, target string) ([]*Strategy, error) { w := make(map[string]interface{}) w["scope"] = scope diff --git a/service/strategy/service.go b/service/strategy/service.go index 7dfe0b91..817b2732 100644 --- a/service/strategy/service.go +++ b/service/strategy/service.go @@ -21,6 +21,8 @@ type IStrategyService interface { SortDelete(ctx context.Context, id string) error Delete(ctx context.Context, id ...string) error + Restore(ctx context.Context, id string) error + CommitStrategy(ctx context.Context, scope string, target string, strategyId string, data *Strategy) error GetStrategyCommit(ctx context.Context, commitId string) (*commit.Commit[StrategyCommit], error) LatestStrategyCommit(ctx context.Context, scope string, target string, strategyId string) (*commit.Commit[StrategyCommit], error)