feat: add project with class metadata

This commit is contained in:
rainzm
2022-02-15 18:22:01 +08:00
parent d502c574b4
commit 42a8eb837b
9 changed files with 175 additions and 111 deletions
+14 -10
View File
@@ -283,7 +283,7 @@ func (model *SStandaloneAnonResourceBase) SetClassMetadataValues(ctx context.Con
return nil
}
func (model *SStandaloneAnonResourceBase) SetClassMetadataAll(ctx context.Context, dictstore map[string]interface{}, userCred mcclient.TokenCredential) error {
func (model *SStandaloneAnonResourceBase) SetClassMetadataAll(ctx context.Context, dictstore map[string]string, userCred mcclient.TokenCredential) error {
afterCheck := make(map[string]interface{}, len(dictstore))
for k, v := range dictstore {
if !strings.HasPrefix(k, CLASS_TAG_PREFIX) {
@@ -308,19 +308,19 @@ func (model *SStandaloneAnonResourceBase) Inherit(ctx context.Context, sonModel
return nil
}
userCred := auth.AdminCredential()
dictstore := make(map[string]interface{}, 0)
for k, v := range metadata {
dictstore[k] = v
}
return sonModel.SetClassMetadataAll(ctx, dictstore, userCred)
return sonModel.SetClassMetadataAll(ctx, metadata, userCred)
}
func (model *SStandaloneAnonResourceBase) IsInSameClass(ctx context.Context, pModel *SStandaloneAnonResourceBase) (bool, error) {
pureTags, err := model.GetAllClassMetadata()
type IClassMetadataOwner interface {
GetAllClassMetadata() (map[string]string, error)
}
func IsInSameClass(ctx context.Context, cmo1, cmo2 IClassMetadataOwner) (bool, error) {
pureTags, err := cmo1.GetAllClassMetadata()
if err != nil {
return false, errors.Wrap(err, "GetAllPureMetadata")
}
pureTagsP, err := pModel.GetAllClassMetadata()
pureTagsP, err := cmo2.GetAllClassMetadata()
if err != nil {
return false, errors.Wrap(err, "GetAllPureMetadata")
}
@@ -335,6 +335,10 @@ func (model *SStandaloneAnonResourceBase) IsInSameClass(ctx context.Context, pMo
return true, nil
}
func (model *SStandaloneAnonResourceBase) IsInSameClass(ctx context.Context, pModel *SStandaloneAnonResourceBase) (bool, error) {
return IsInSameClass(ctx, model, pModel)
}
func (model *SStandaloneAnonResourceBase) SetSysCloudMetadataAll(ctx context.Context, dictstore map[string]interface{}, userCred mcclient.TokenCredential) error {
err := Metadata.SetAll(ctx, model, dictstore, userCred, SYS_CLOUD_TAG_PREFIX)
if err != nil {
@@ -481,7 +485,7 @@ func (model *SStandaloneAnonResourceBase) PerformClassMetadata(ctx context.Conte
// 全量替换资源的所有 class 标签
func (model *SStandaloneAnonResourceBase) PerformSetClassMetadata(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformSetClassMetadataInput) (jsonutils.JSONObject, error) {
dictStore := make(map[string]interface{})
dictStore := make(map[string]string)
for k, v := range input {
if len(k) > 64-len(CLASS_TAG_PREFIX) {
return nil, httperrors.NewInputParameterError("input key too long > %d", 64-len(CLASS_TAG_PREFIX))
+40
View File
@@ -282,6 +282,18 @@ func (model *SVirtualResourceBase) CustomizeCreate(ctx context.Context, userCred
return model.SStatusStandaloneResourceBase.CustomizeCreate(ctx, userCred, ownerId, query, data)
}
func (model *SVirtualResourceBase) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
project, err := model.GetTenantCache(ctx)
if err != nil {
log.Errorf("unable to GetTenantCache: %s", err.Error())
return
}
err = project.Inherit(ctx, &model.SStandaloneAnonResourceBase)
if err != nil {
log.Errorf("unable to inherit class metadata from poject %s: %s", project.GetId(), err.Error())
}
}
func (manager *SVirtualResourceBaseManager) FetchCustomizeColumns(
ctx context.Context,
userCred mcclient.TokenCredential,
@@ -416,6 +428,27 @@ func (model *SVirtualResourceBase) PerformChangeOwner(ctx context.Context, userC
log.Warningf("tenant_id %s not found", model.ProjectId)
formerObj := NewTenant(model.ProjectId, "unknown", model.DomainId, "unknown")
former = &formerObj
} else {
// check fromer's class metadata
cm, err := former.GetAllClassMetadata()
if err != nil {
return nil, errors.Wrap(err, "unable to GetAllClassMetadata")
}
if len(cm) > 0 {
return nil, httperrors.NewForbiddenError("can't change owner for resource in project with class metadata")
}
}
toer, err := TenantCacheManager.FetchTenantById(ctx, ownerId.GetProjectId())
if err != nil {
return nil, errors.Wrapf(err, "unable to get project %s", ownerId.GetProjectId())
}
toCm, err := toer.GetAllClassMetadata()
if err != nil {
return nil, errors.Wrap(err, "unable to GetAllClassMetadata")
}
if model.Keyword() != "image" && len(toCm) > 0 {
return nil, httperrors.NewForbiddenError("can't change resource's owner as that in project with class metadata")
}
// clean shared projects before update project id
@@ -461,6 +494,13 @@ func (model *SVirtualResourceBase) PerformChangeOwner(ctx context.Context, userC
NewDomainId: ownerId.GetProjectDomainId(),
NewDomain: ownerId.GetProjectDomain(),
}
// set class metadata
err = model.SetClassMetadataAll(ctx, toCm, userCred)
if err != nil {
return nil, errors.Wrap(err, "unable to SetClassMetadataAll")
}
logclient.AddActionLogWithContext(ctx, model, logclient.ACT_CHANGE_OWNER, notes, userCred, true)
return nil, nil
}