From b5fcfb5d166f51287946f1b82b8ea3ee115c9621 Mon Sep 17 00:00:00 2001 From: Qu Xuan Date: Thu, 20 May 2021 20:39:27 +0800 Subject: [PATCH] fix(region): add project and domain info for project mapping rules --- cmd/climc/shell/compute/project_mappings.go | 2 + pkg/apis/compute/project_mappings.go | 31 ++++-- pkg/compute/models/project_mappings.go | 107 +++++++------------- 3 files changed, 61 insertions(+), 79 deletions(-) diff --git a/cmd/climc/shell/compute/project_mappings.go b/cmd/climc/shell/compute/project_mappings.go index c9962ad1c9..d9fb2d3046 100644 --- a/cmd/climc/shell/compute/project_mappings.go +++ b/cmd/climc/shell/compute/project_mappings.go @@ -28,4 +28,6 @@ func init() { cmd.Delete(&options.BaseIdOptions{}) cmd.Show(&options.BaseIdOptions{}) cmd.Create(&compute.ProjectMappingCreateOption{}) + cmd.Perform("enable", &options.BaseIdOptions{}) + cmd.Perform("disable", &options.BaseIdOptions{}) } diff --git a/pkg/apis/compute/project_mappings.go b/pkg/apis/compute/project_mappings.go index 88e2ab718c..70c92a6054 100644 --- a/pkg/apis/compute/project_mappings.go +++ b/pkg/apis/compute/project_mappings.go @@ -34,23 +34,29 @@ const ( ) type STag struct { - Key string - Value string + Key string `json:"key"` + Value string `json:"value"` } type ProjectMappingRuleInfo struct { // 标签列表, 不可为空 - Tags []STag + Tags []STag `json:"tags"` // 条件表达式 // enmu: and, or // default: and - Condition string + Condition string `json:"condition"` // 是否自动根据标签值创建项目, 仅标签列表中有且仅有一个没有value的key时支持 - AutoCreateProject bool + AutoCreateProject bool `json:"auto_create_project"` // 符合条件时,资源放置的项目id, 此参数和auto_create_project互斥 - ProjectId string + ProjectId string `json:"project_id"` + // 只读信息 // swagger:ignore - DomainId string + Project string `json:"project"` + // swagger:ignore + DomainId string `json:"domain_id"` + // 只读信息 + // swagger:ignore + Domain string `json:"domain"` } type MappingRules []ProjectMappingRuleInfo @@ -135,15 +141,18 @@ type ProjectMappingRuleInfoDetails struct { Domain string } +type SProjectMappingAccount struct { + Id string `json:"id"` + Name string `json:"name"` +} + type ProjectMappingDetails struct { apis.EnabledStatusInfrasResourceBaseDetails Rules []ProjectMappingRuleInfoDetails - Accounts []struct { - Id string - Name string - } + // 所绑定的云账号列表 + Accounts []SProjectMappingAccount `json:"accounts"` } type ProjectMappingCreateInput struct { diff --git a/pkg/compute/models/project_mappings.go b/pkg/compute/models/project_mappings.go index 6d06249223..bac6322180 100644 --- a/pkg/compute/models/project_mappings.go +++ b/pkg/compute/models/project_mappings.go @@ -105,6 +105,8 @@ func (manager *SProjectMappingManager) ValidateCreateData( return input, err } input.Rules[i].DomainId = tenant.DomainId + input.Rules[i].Domain = tenant.Domain + input.Rules[i].Project = tenant.Name } } input.SetEnabled() @@ -132,97 +134,38 @@ func (manager *SProjectMappingManager) FetchCustomizeColumns( rows := make([]api.ProjectMappingDetails, len(objs)) stdRows := manager.SEnabledStatusInfrasResourceBaseManager.FetchCustomizeColumns(ctx, userCred, query, objs, fields, isList) mpIds := make([]string, len(objs)) - projIds := []string{} - domainIds := []string{} for i := range rows { rows[i] = api.ProjectMappingDetails{ EnabledStatusInfrasResourceBaseDetails: stdRows[i], } mp := objs[i].(*SProjectMapping) mpIds[i] = mp.Id - if mp.Rules != nil { - for _, r := range *mp.Rules { - if len(r.ProjectId) > 0 { - projIds = append(projIds, r.ProjectId) - domainIds = append(domainIds, r.DomainId) - } - } - } - } - q := db.DefaultDomainQuery("domain", "domain_id").In("domain_id", domainIds) - domains := []struct { - DomainId string - Domain string - }{} - err := q.All(&domainIds) - if err != nil { - return rows - } - domainMaps := map[string]string{} - for _, domain := range domains { - domainMaps[domain.DomainId] = domain.Domain - } - q = db.DefaultProjectQuery("id", "name").In("id", projIds) - projects := []struct { - Id string - Name string - }{} - err = q.All(&projects) - if err != nil { - return rows - } - projectMaps := map[string]string{} - for _, proj := range projects { - projectMaps[proj.Id] = proj.Name } accounts := []struct { Id string Name string ProjectMappingId string }{} - q = CloudaccountManager.Query().In("project_mapping_id", mpIds) - err = q.All(&accounts) + q := CloudaccountManager.Query().In("project_mapping_id", mpIds) + err := q.All(&accounts) if err != nil { return rows } - accountMapping := map[string][]struct { - Id string - Name string - }{} + accountMapping := map[string][]api.SProjectMappingAccount{} for i := range accounts { _, ok := accountMapping[accounts[i].ProjectMappingId] if !ok { - accountMapping[accounts[i].ProjectMappingId] = []struct { - Id string - Name string - }{} + accountMapping[accounts[i].ProjectMappingId] = []api.SProjectMappingAccount{} } - accountMapping[accounts[i].ProjectMappingId] = append(accountMapping[accounts[i].ProjectMappingId], - struct { - Id string - Name string - }{ - Id: accounts[i].Id, - Name: accounts[i].Name, - }) + account := api.SProjectMappingAccount{ + Id: accounts[i].Id, + Name: accounts[i].Name, + } + accountMapping[accounts[i].ProjectMappingId] = append(accountMapping[accounts[i].ProjectMappingId], account) } for i := range rows { - mp := objs[i].(*SProjectMapping) rows[i].Accounts, _ = accountMapping[mpIds[i]] - if mp.Rules != nil { - rows[i].Rules = []api.ProjectMappingRuleInfoDetails{} - for j := range *mp.Rules { - rules := *mp.Rules - rule := api.ProjectMappingRuleInfoDetails{ - ProjectMappingRuleInfo: rules[j], - } - rule.Tenant, _ = projectMaps[rules[j].ProjectId] - rule.Project = rule.Tenant - rule.Domain, _ = domainMaps[rules[j].DomainId] - rows[i].Rules = append(rows[i].Rules, rule) - } - } } return rows } @@ -275,6 +218,8 @@ func (self *SProjectMapping) ValidateUpdateData(ctx context.Context, userCred mc return input, err } input.Rules[i].DomainId = tenant.DomainId + input.Rules[i].Domain = tenant.Domain + input.Rules[i].Project = tenant.Name } } input.EnabledStatusInfrasResourceBaseUpdateInput, err = self.SEnabledStatusInfrasResourceBase.ValidateUpdateData(ctx, userCred, query, input.EnabledStatusInfrasResourceBaseUpdateInput) @@ -316,3 +261,29 @@ func (self *SProjectMapping) refreshMapping() error { projectRuleMapping[self.Id] = self return nil } + +func (self *SProjectMapping) AllowPerformEnable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformEnableInput) bool { + return db.IsDomainAllowPerform(userCred, self, "enable") +} + +// 启用资源映射 +func (self *SProjectMapping) PerformEnable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformEnableInput) (jsonutils.JSONObject, error) { + _, err := self.SEnabledStatusInfrasResourceBase.PerformEnable(ctx, userCred, query, input) + if err != nil { + return nil, err + } + return nil, self.refreshMapping() +} + +func (self *SProjectMapping) AllowPerformDisable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformDisableInput) bool { + return db.IsDomainAllowPerform(userCred, self, "disable") +} + +// 禁用资源映射 +func (self *SProjectMapping) PerformDisable(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformDisableInput) (jsonutils.JSONObject, error) { + _, err := self.SEnabledStatusInfrasResourceBase.PerformDisable(ctx, userCred, query, input) + if err != nil { + return nil, err + } + return nil, self.refreshMapping() +}