fix updates

This commit is contained in:
Qiu Jian
2020-03-27 01:20:29 +08:00
parent 7072f80442
commit 258db2cb48
105 changed files with 916 additions and 372 deletions
+45
View File
@@ -22,6 +22,7 @@ import (
"yunion.io/x/sqlchemy"
"yunion.io/x/onecloud/pkg/apis"
"yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/util/rbacutils"
@@ -201,3 +202,47 @@ func (model *SInfrasResourceBase) PerformChangeOwner(
}
return model.SDomainLevelResourceBase.PerformChangeOwner(ctx, userCred, query, input)
}
func (model *SInfrasResourceBase) CustomizeCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
model.SSharableBaseResource.CustomizeCreate(ctx, userCred, ownerId, query, data)
return model.SDomainLevelResourceBase.CustomizeCreate(ctx, userCred, ownerId, query, data)
}
func (model *SInfrasResourceBase) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
SharedResourceManager.CleanModelShares(ctx, userCred, model.GetIInfrasModel())
return model.SDomainLevelResourceBase.Delete(ctx, userCred)
}
func (model *SInfrasResourceBase) SyncShareState(ctx context.Context, userCred mcclient.TokenCredential, shareInfo apis.SAccountShareInfo) {
if model.PublicScope != string(apis.OWNER_SOURCE_LOCAL) {
diff, _ := Update(model, func() error {
switch shareInfo.ShareMode {
case compute.CLOUD_ACCOUNT_SHARE_MODE_ACCOUNT_DOMAIN:
model.IsPublic = false
model.PublicScope = string(rbacutils.ScopeNone)
case compute.CLOUD_ACCOUNT_SHARE_MODE_PROVIDER_DOMAIN:
model.IsPublic = false
model.PublicScope = string(rbacutils.ScopeNone)
case compute.CLOUD_ACCOUNT_SHARE_MODE_SYSTEM:
if shareInfo.IsPublic && shareInfo.PublicScope == rbacutils.ScopeSystem {
model.IsPublic = true
model.PublicScope = string(rbacutils.ScopeSystem)
} else if len(shareInfo.SharedDomains) > 0 {
model.IsPublic = true
model.PublicScope = string(rbacutils.ScopeDomain)
SharedResourceManager.shareToTarget(ctx, userCred, model.GetIInfrasModel(), SharedTargetProject, nil)
SharedResourceManager.shareToTarget(ctx, userCred, model.GetIInfrasModel(), SharedTargetDomain, shareInfo.SharedDomains)
} else {
model.IsPublic = false
model.PublicScope = string(rbacutils.ScopeNone)
SharedResourceManager.shareToTarget(ctx, userCred, model.GetIInfrasModel(), SharedTargetProject, nil)
SharedResourceManager.shareToTarget(ctx, userCred, model.GetIInfrasModel(), SharedTargetDomain, nil)
}
}
return nil
})
if len(diff) > 0 {
OpsLog.LogEvent(model, ACT_SYNC_SHARE, diff, userCred)
}
}
}
+2 -2
View File
@@ -284,7 +284,7 @@ type IDomainLevelModelManager interface {
IStandaloneModelManager
GetIDomainLevelModelManager() IDomainLevelModelManager
GetResourceCount() ([]SDomainResourceCount, error)
GetResourceCount() ([]SScopeResourceCount, error)
}
type IDomainLevelModel interface {
@@ -315,7 +315,7 @@ type IVirtualModelManager interface {
IStandaloneModelManager
GetIVirtualModelManager() IVirtualModelManager
GetResourceCount() ([]SProjectResourceCount, error)
GetResourceCount() ([]SScopeResourceCount, error)
}
type IVirtualModel interface {
+1
View File
@@ -178,6 +178,7 @@ const (
ACT_CHANGE_OWNER = "change_owner"
ACT_SYNC_OWNER = "sync_owner"
ACT_SYNC_SHARE = "sync_share"
ACT_RESERVE_IP = "reserve_ip"
ACT_RELEASE_IP = "release_ip"
+17 -16
View File
@@ -43,38 +43,44 @@ func getAllProjectResourceCountsHandler(ctx context.Context, w http.ResponseWrit
appsrv.SendJSON(w, jsonutils.Marshal(cnt))
}
func getAllProjectResourceCounts() (map[string][]SProjectResourceCount, error) {
ret := make(map[string][]SProjectResourceCount)
func getAllProjectResourceCounts() (map[string][]SScopeResourceCount, error) {
ret := make(map[string][]SScopeResourceCount)
for _, manager := range globalTables {
virtman, ok := manager.(IVirtualModelManager)
if ok {
if virtman, ok := manager.(IVirtualModelManager); ok {
resCnt, err := virtman.GetResourceCount()
if err != nil {
return nil, errors.Wrap(err, "getProjectResourceCount")
}
ret[virtman.KeywordPlural()] = resCnt
} else if domainMan, ok := manager.(IDomainLevelModelManager); ok {
resCnt, err := domainMan.GetResourceCount()
if err != nil {
return nil, errors.Wrap(err, "getDomainResourceCount")
}
ret[domainMan.KeywordPlural()] = resCnt
}
}
return ret, nil
}
type SProjectResourceCount struct {
type SScopeResourceCount struct {
TenantId string `json:"tenant_id"`
DomainId string `json:"domain_id"`
ResCount int `json:"res_count"`
}
func (virtman *SVirtualResourceBaseManager) GetResourceCount() ([]SProjectResourceCount, error) {
func (virtman *SVirtualResourceBaseManager) GetResourceCount() ([]SScopeResourceCount, error) {
virts := virtman.GetIVirtualModelManager().Query()
// log.Debugf("GetResourceCount: %s", virtman.keywordPlural)
return CalculateProjectResourceCount(virts)
}
func CalculateProjectResourceCount(query *sqlchemy.SQuery) ([]SProjectResourceCount, error) {
func CalculateProjectResourceCount(query *sqlchemy.SQuery) ([]SScopeResourceCount, error) {
virts := query.SubQuery()
q := virts.Query(virts.Field("tenant_id"), sqlchemy.COUNT("res_count"))
q = q.IsNotEmpty("tenant_id")
q = q.GroupBy(virts.Field("tenant_id"))
cnts := make([]SProjectResourceCount, 0)
cnts := make([]SScopeResourceCount, 0)
err := q.All(&cnts)
if err != nil && err != sql.ErrNoRows {
return nil, errors.Wrap(err, "q.All")
@@ -82,23 +88,18 @@ func CalculateProjectResourceCount(query *sqlchemy.SQuery) ([]SProjectResourceCo
return cnts, nil
}
type SDomainResourceCount struct {
DomainId string `json:"domain_id"`
ResCount int `json:"res_count"`
}
func (domainman *SDomainLevelResourceBaseManager) GetResourceCount() ([]SDomainResourceCount, error) {
func (domainman *SDomainLevelResourceBaseManager) GetResourceCount() ([]SScopeResourceCount, error) {
virts := domainman.GetIDomainLevelModelManager().Query()
// log.Debugf("GetResourceCount: %s", virtman.keywordPlural)
return CalculateDomainResourceCount(virts)
}
func CalculateDomainResourceCount(query *sqlchemy.SQuery) ([]SDomainResourceCount, error) {
func CalculateDomainResourceCount(query *sqlchemy.SQuery) ([]SScopeResourceCount, error) {
virts := query.SubQuery()
q := virts.Query(virts.Field("domain_id"), sqlchemy.COUNT("res_count"))
q = q.IsNotEmpty("domain_id")
q = q.GroupBy(virts.Field("domain_id"))
cnts := make([]SDomainResourceCount, 0)
cnts := make([]SScopeResourceCount, 0)
err := q.All(&cnts)
if err != nil && err != sql.ErrNoRows {
return nil, errors.Wrap(err, "q.All")
+34 -1
View File
@@ -65,18 +65,30 @@ func (manager *SSharableBaseResourceManager) FetchCustomizeColumns(
var resType string
resIds := make([]string, len(rows))
var sharedTarget string
var resScope rbacutils.TRbacScope
for i := range rows {
if model, ok := objs[i].(ISharableBaseModel); ok {
if len(resType) == 0 {
resType = model.Keyword()
}
if len(sharedTarget) == 0 {
sharedTarget = string(model.GetModelManager().ResourceScope())
sharedTarget = string(model.GetPublicScope())
}
if len(resScope) == 0 {
resScope = model.GetModelManager().ResourceScope()
}
resIds[i] = model.GetId()
}
}
if sharedTarget != SharedTargetProject && sharedTarget != SharedTargetDomain {
if resScope == rbacutils.ScopeProject && sharedTarget == string(rbacutils.ScopeNone) {
sharedTarget = SharedTargetProject
} else {
return rows
}
}
q := SharedResourceManager.Query()
q = q.Equals("resource_type", resType)
q = q.Equals("target_type", sharedTarget)
@@ -199,6 +211,9 @@ type SSharableBaseResource struct {
IsPublic bool `default:"false" nullable:"false" list:"user"`
// 默认共享范围
PublicScope string `width:"16" charset:"ascii" nullable:"false" default:"system" list:"user"`
// 共享设置的来源, local: 本地设置, cloud: 从云上同步过来
// example: local
PublicSrc string `width:"10" charset:"ascii" nullable:"true" list:"user" json:"public_src"`
}
type ISharableBaseModel interface {
@@ -260,6 +275,7 @@ func (m SSharableBaseResource) GetPublicScope() rbacutils.TRbacScope {
}
func SharablePerformPublic(model ISharableBaseModel, ctx context.Context, userCred mcclient.TokenCredential, input apis.PerformPublicInput) error {
log.Debugf("%s", jsonutils.Marshal(input))
var err error
resourceScope := model.GetModelManager().ResourceScope()
@@ -268,6 +284,16 @@ func SharablePerformPublic(model ISharableBaseModel, ctx context.Context, userCr
return errors.Wrapf(httperrors.ErrNotSupported, "cannot share %s resource to %s", resourceScope, targetScope)
}
if len(input.SharedProjects) > 0 && len(input.SharedDomains) > 0 {
return errors.Wrap(httperrors.ErrInputParameter, "cannot set shared_projects and shared_domains at the same time")
} else if len(input.SharedProjects) > 0 && targetScope != rbacutils.ScopeProject {
targetScope = rbacutils.ScopeProject
// return errors.Wrapf(httperrors.ErrInputParameter, "scope %s != project when shared_projects specified", targetScope)
} else if len(input.SharedDomains) > 0 && targetScope != rbacutils.ScopeDomain {
targetScope = rbacutils.ScopeDomain
// return errors.Wrapf(httperrors.ErrInputParameter, "scope %s != domain when shared_domains specified", targetScope)
}
shareResult := apis.PerformPublicInput{
Scope: string(targetScope),
}
@@ -380,3 +406,10 @@ func SharableModelIsShared(model ISharableBaseModel) bool {
}
return false
}
func (base *SSharableBaseResource) CustomizeCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
base.IsPublic = false
base.PublicScope = string(rbacutils.ScopeNone)
base.PublicSrc = string(apis.OWNER_SOURCE_LOCAL)
return nil
}
+39
View File
@@ -22,6 +22,7 @@ import (
"yunion.io/x/sqlchemy"
"yunion.io/x/onecloud/pkg/apis"
"yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/util/rbacutils"
@@ -194,3 +195,41 @@ func (model *SSharableVirtualResourceBase) PerformChangeOwner(
}
return model.SVirtualResourceBase.PerformChangeOwner(ctx, userCred, query, input)
}
func (model *SSharableVirtualResourceBase) CustomizeCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) error {
model.SSharableBaseResource.CustomizeCreate(ctx, userCred, ownerId, query, data)
return model.SVirtualResourceBase.CustomizeCreate(ctx, userCred, ownerId, query, data)
}
func (model *SSharableVirtualResourceBase) Delete(ctx context.Context, userCred mcclient.TokenCredential) error {
SharedResourceManager.CleanModelShares(ctx, userCred, model.GetISharableVirtualModel())
return model.SVirtualResourceBase.Delete(ctx, userCred)
}
func (model *SSharableVirtualResourceBase) SyncShareState(ctx context.Context, userCred mcclient.TokenCredential, shareInfo apis.SAccountShareInfo) {
if model.PublicScope != string(apis.OWNER_SOURCE_LOCAL) {
diff, _ := Update(model, func() error {
switch shareInfo.ShareMode {
case compute.CLOUD_ACCOUNT_SHARE_MODE_ACCOUNT_DOMAIN:
model.IsPublic = true
model.PublicScope = string(rbacutils.ScopeDomain)
case compute.CLOUD_ACCOUNT_SHARE_MODE_PROVIDER_DOMAIN:
model.IsPublic = true
model.PublicScope = string(rbacutils.ScopeDomain)
case compute.CLOUD_ACCOUNT_SHARE_MODE_SYSTEM:
model.IsPublic = true
if shareInfo.IsPublic && shareInfo.PublicScope == rbacutils.ScopeSystem {
model.PublicScope = string(rbacutils.ScopeSystem)
} else {
model.PublicScope = string(rbacutils.ScopeDomain)
SharedResourceManager.shareToTarget(ctx, userCred, model.GetISharableVirtualModel(), SharedTargetProject, nil)
SharedResourceManager.shareToTarget(ctx, userCred, model.GetISharableVirtualModel(), SharedTargetDomain, shareInfo.SharedDomains)
}
}
return nil
})
if len(diff) > 0 {
OpsLog.LogEvent(model, ACT_SYNC_SHARE, diff, userCred)
}
}
}
+6 -6
View File
@@ -37,14 +37,14 @@ const (
type SSharedResource struct {
SResourceBase
Id int64 `primary:"true" auto_increment:"true" list:"user"`
Id int64 `primary:"true" auto_increment:"true"`
ResourceType string `width:"32" charset:"ascii" nullable:"false" list:"user" json:"resource_type"`
ResourceId string `width:"128" charset:"ascii" nullable:"false" index:"true" list:"user" json:"resource_id"`
OwnerProjectId string `width:"128" charset:"ascii" nullable:"false" index:"true" list:"user" json:"owner_project_id"`
TargetProjectId string `width:"128" charset:"ascii" nullable:"false" index:"true" list:"user" json:"target_project_id"`
ResourceType string `width:"32" charset:"ascii" nullable:"false" json:"resource_type"`
ResourceId string `width:"128" charset:"ascii" nullable:"false" index:"true" json:"resource_id"`
// OwnerProjectId string `width:"128" charset:"ascii" nullable:"false" index:"true" json:"owner_project_id"`
TargetProjectId string `width:"128" charset:"ascii" nullable:"false" index:"true" json:"target_project_id"`
TargetType string `width:"8" charset:"ascii" default:"project" nullable:"false" list:"user" json:"target_type"`
TargetType string `width:"8" charset:"ascii" default:"project" nullable:"false" json:"target_type"`
}
type SSharedResourceManager struct {
+2 -3
View File
@@ -21,8 +21,6 @@ import (
"runtime/debug"
"time"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
@@ -31,6 +29,7 @@ import (
identityapi "yunion.io/x/onecloud/pkg/apis/identity"
"yunion.io/x/onecloud/pkg/cloudcommon/consts"
"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
"yunion.io/x/onecloud/pkg/httperrors"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/mcclient/auth"
"yunion.io/x/onecloud/pkg/mcclient/modules"
@@ -122,7 +121,7 @@ func (manager *STenantCacheManager) fetchTenant(ctx context.Context, idStr strin
return nil, errors.Wrap(err, "CountWithError")
}
if tcnt > 1 {
return nil, httperrors.ErrDuplicateName
return nil, errors.Wrapf(httperrors.ErrDuplicateName, "duplicate tenant/domain name (%d)", tcnt)
}
tobj, err := NewModelObject(manager)
if err != nil {
+2 -1
View File
@@ -18,6 +18,7 @@ import (
"reflect"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/appsrv"
@@ -84,7 +85,7 @@ func (manager *SOptionManager) doSync(first bool) {
merged := manager.session.Merge(newOpts, manager.serviceType, manager.serviceVersion)
if merged && !reflect.DeepEqual(newOpts, manager.options) {
log.Infof("Service config changed ...")
log.Infof("Service config changed ... %s %s", jsonutils.Marshal(newOpts), jsonutils.Marshal(manager.options))
if manager.onOptionsChange != nil && manager.onOptionsChange(manager.options, newOpts) && !first {
log.Infof("Option changes detected and going to restart the program...")
appsrv.SetExitFlag()