Skip to content

Commit

Permalink
fix: external project priority (#17811)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioito authored Aug 21, 2023
1 parent 276e124 commit b4dd994
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/climc/shell/compute/external_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func init() {
cmd := shell.NewResourceCmd(&compute.ExternalProjects).WithKeyword("external-project")
cmd.List(&opts.ExternalProjectListOptions{})
cmd.Create(&opts.ExternalProjectCreateOptions{})
cmd.Update(&opts.ExternalProjectUpdateOptions{})
cmd.Show(&options.BaseIdOptions{})
cmd.Perform("change-project", &opts.ExterProjectChagneProjectOptions{})
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ require (
k8s.io/client-go v0.19.3
k8s.io/cluster-bootstrap v0.19.3
moul.io/http2curl/v2 v2.3.0
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230818091626-4820b3349fb8
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230821061444-8327b119ec8b
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32
yunion.io/x/jsonutils v1.0.1-0.20230613121553-0f3b41e2ef19
yunion.io/x/log v1.0.1-0.20230411060016-feb3f46ab361
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1174,8 +1174,8 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230818091626-4820b3349fb8 h1:bF/xadUoM/W2OZeds2vkyklKpddclHdmkx1F2s80k3E=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230818091626-4820b3349fb8/go.mod h1:2sgCN7nRPQL3woLfdgqLDd92vwAHqtlz3KKiHxC5BAw=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230821061444-8327b119ec8b h1:G4lU+QiTb3C5TeZ9nuSujP9lnvyadqj2EdO5CIIhH4w=
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230821061444-8327b119ec8b/go.mod h1:2sgCN7nRPQL3woLfdgqLDd92vwAHqtlz3KKiHxC5BAw=
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32 h1:v7POYkQwo1XzOxBoIoRVr/k0V9Y5JyjpshlIFa9raug=
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws=
yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634=
Expand Down
2 changes: 2 additions & 0 deletions pkg/cloudcommon/db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"yunion.io/x/pkg/util/rbacscope"
"yunion.io/x/sqlchemy"

"yunion.io/x/onecloud/pkg/apis"
"yunion.io/x/onecloud/pkg/appsrv"
"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
"yunion.io/x/onecloud/pkg/mcclient"
Expand Down Expand Up @@ -365,6 +366,7 @@ type IVirtualModel interface {
IsOwner(userCred mcclient.TokenCredential) bool
// IsAdmin(userCred mcclient.TokenCredential) bool

SetProjectSrc(apis.TOwnerSource)
SyncCloudProjectId(userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider)

GetIVirtualModel() IVirtualModel
Expand Down
9 changes: 9 additions & 0 deletions pkg/cloudcommon/db/virtualresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,15 @@ func (model *SVirtualResourceBase) GetShortDesc(ctx context.Context) *jsonutils.
return desc
}

func (model *SVirtualResourceBase) SetProjectSrc(src apis.TOwnerSource) {
if model.ProjectSrc != string(src) {
Update(model, func() error {
model.ProjectSrc = string(apis.OWNER_SOURCE_CLOUD)
return nil
})
}
}

func (model *SVirtualResourceBase) SyncCloudProjectId(userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider) {
if model.ProjectSrc != string(apis.OWNER_SOURCE_LOCAL) && ownerId != nil && len(ownerId.GetProjectId()) > 0 {
diff, _ := Update(model, func() error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/models/cloudproviders.go
Original file line number Diff line number Diff line change
Expand Up @@ -1921,7 +1921,7 @@ func (self *SCloudprovider) GetExternalProjectsByProjectIdOrName(projectId, name
sqlchemy.Equals(q.Field("name"), name),
sqlchemy.Equals(q.Field("tenant_id"), projectId),
),
)
).Desc("priority")
err := db.FetchModelObjects(ExternalProjectManager, q, &projects)
if err != nil {
return nil, errors.Wrap(err, "db.FetchModelObjects")
Expand Down
2 changes: 2 additions & 0 deletions pkg/compute/models/cloudsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"yunion.io/x/pkg/utils"
"yunion.io/x/sqlchemy"

"yunion.io/x/onecloud/pkg/apis"
api "yunion.io/x/onecloud/pkg/apis/compute"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
Expand Down Expand Up @@ -2410,6 +2411,7 @@ func SyncCloudProject(ctx context.Context, userCred mcclient.TokenCredential, mo
return nil, errors.Wrapf(err, "GetCloudaccount")
}
if rm != nil && rm.Enabled.Bool() && rm.IsNeedResourceSync() {
model.SetProjectSrc(apis.OWNER_SOURCE_CLOUD)
extTags, err := extModel.GetTags()
if err != nil {
return nil, errors.Wrapf(err, "extModel.GetTags")
Expand Down
4 changes: 3 additions & 1 deletion pkg/compute/models/external_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ type SExternalProject struct {
db.SVirtualResourceBase
db.SExternalizedResourceBase
SManagedResourceBase

// 优先级,同一个本地项目映射多个云上项目,优先级高的优先选择
// 数值越高,优先级越大
Priority int `default:"0" list:"user" update:"user" list:"user"`
ExternalDomainId string `width:"36" charset:"ascii" nullable:"true" list:"user"`
// 归属云账号ID
CloudaccountId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required"`
Expand Down
9 changes: 9 additions & 0 deletions pkg/mcclient/options/compute/external_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,12 @@ type ExternalProjectCreateOptions struct {
func (opts *ExternalProjectCreateOptions) Params() (jsonutils.JSONObject, error) {
return jsonutils.Marshal(opts), nil
}

type ExternalProjectUpdateOptions struct {
options.BaseUpdateOptions
Priority *int
}

func (opts *ExternalProjectUpdateOptions) Params() (jsonutils.JSONObject, error) {
return jsonutils.Marshal(opts), nil
}
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ sigs.k8s.io/structured-merge-diff/v4/value
# sigs.k8s.io/yaml v1.2.0
## explicit; go 1.12
sigs.k8s.io/yaml
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230818091626-4820b3349fb8
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20230821061444-8327b119ec8b
## explicit; go 1.18
yunion.io/x/cloudmux/pkg/apis
yunion.io/x/cloudmux/pkg/apis/billing
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b4dd994

Please sign in to comment.