Skip to content

Commit a1bc7bc

Browse files
authored
fix(region): add cloud_env filter for cachedimages (#23190)
1 parent 9603dd9 commit a1bc7bc

File tree

6 files changed

+47
-14
lines changed

6 files changed

+47
-14
lines changed

pkg/apis/compute/cachedimage.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,7 @@ type CachedimageListInput struct {
9999

100100
// valid cachedimage
101101
Valid bool `json:"valid"`
102+
103+
// enum: [public, private]
104+
CloudEnv string `json:"cloud_env"`
102105
}

pkg/cloudcommon/db/standalone_anon.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ func GetTagValueCountMap(
10171017
}
10181018
objSubQ = objSubQ.AppendField(sumFieldQ)
10191019

1020-
objSubQ.DebugQuery2("GetTagValueCountMap objSubQ")
1020+
// objSubQ.DebugQuery2("GetTagValueCountMap objSubQ")
10211021

10221022
q := objSubQ.SubQuery().Query()
10231023
q = q.AppendField(sqlchemy.SUM(tagValueCountKey, q.Field("_sub_count_")))
@@ -1040,7 +1040,7 @@ func GetTagValueCountMap(
10401040
}
10411041
q = q.GroupBy(groupBy...)
10421042

1043-
q.DebugQuery2("GetTagValueCountMap")
1043+
// q.DebugQuery2("GetTagValueCountMap")
10441044

10451045
valueMap, err := q.AllStringMap()
10461046
if err != nil {

pkg/cloudcommon/db/taskman/tasks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ func (manager *STaskManager) migrateObjectInfo() error {
14041404
q = q.Filter(sqlchemy.IsNull(taskObj.Field("task_id")))
14051405
q = q.Asc("created_at")
14061406

1407-
q.DebugQuery2("migrateObjectInfo")
1407+
// q.DebugQuery2("migrateObjectInfo")
14081408

14091409
rows, err := q.Rows()
14101410
if err != nil {

pkg/compute/models/cachedimages.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,26 @@ func (manager *SCachedimageManager) ListItemFilter(
912912
if len(query.CloudproviderId) > 0 {
913913
storagesQ = storagesQ.In("manager_id", query.CloudproviderId)
914914
}
915+
if len(query.CloudEnv) > 0 {
916+
switch query.CloudEnv {
917+
case api.CLOUD_ENV_PUBLIC_CLOUD:
918+
pubQ := CloudproviderManager.GetPublicProviderIdsQuery()
919+
storagesQ = storagesQ.In("manager_id", pubQ)
920+
case api.CLOUD_ENV_PRIVATE_CLOUD:
921+
privQ := CloudproviderManager.GetPrivateProviderIdsQuery()
922+
storagesQ = storagesQ.In("manager_id", privQ)
923+
case api.CLOUD_ENV_ON_PREMISE:
924+
storagesQ = storagesQ.IsNullOrEmpty("manager_id")
925+
case api.CLOUD_ENV_PRIVATE_ON_PREMISE:
926+
privQ := CloudproviderManager.GetPrivateProviderIdsQuery()
927+
storagesQ = storagesQ.Filter(
928+
sqlchemy.OR(
929+
sqlchemy.In(storagesQ.Field("manager_id"), privQ),
930+
sqlchemy.IsNullOrEmpty(storagesQ.Field("manager_id")),
931+
),
932+
)
933+
}
934+
}
915935
if len(query.HostSchedtagId) > 0 {
916936
hostschedtags := HostschedtagManager.Query("host_id").Equals("schedtag_id", query.HostSchedtagId)
917937
hoststorages := HoststorageManager.Query("storage_id").In("host_id", hostschedtags.Distinct().SubQuery())

pkg/compute/models/cloudimages.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323
"yunion.io/x/pkg/gotypes"
2424

2525
"yunion.io/x/onecloud/pkg/cloudcommon/db"
26+
"yunion.io/x/onecloud/pkg/compute/options"
2627
"yunion.io/x/onecloud/pkg/mcclient"
28+
"yunion.io/x/onecloud/pkg/mcclient/auth"
2729
"yunion.io/x/onecloud/pkg/util/yunionmeta"
2830
)
2931

@@ -153,6 +155,7 @@ func (self *SCloudimage) syncWithImage(ctx context.Context, userCred mcclient.To
153155

154156
skuUrl := region.getMetaUrl(meta.ImageBase, image.GetGlobalId())
155157

158+
s := auth.GetAdminSession(ctx, options.Options.Region)
156159
obj, err := db.FetchByExternalId(CachedimageManager, image.GetGlobalId())
157160
if err != nil {
158161
if errors.Cause(err) != sql.ErrNoRows {
@@ -168,26 +171,31 @@ func (self *SCloudimage) syncWithImage(ctx context.Context, userCred mcclient.To
168171
}
169172

170173
cachedImage.IsPublic = true
171-
cachedImage.ProjectId = "system"
174+
cachedImage.ProjectId = s.GetProjectId()
172175
err = CachedimageManager.TableSpec().Insert(ctx, cachedImage)
173176
if err != nil {
174177
return errors.Wrapf(err, "Insert cachedimage")
175178
}
176179
return nil
177180
}
178181
cachedImage := obj.(*SCachedimage)
179-
if gotypes.IsNil(cachedImage.Info) {
180-
err = meta.Get(skuUrl, &image)
181-
if err != nil {
182-
return errors.Wrapf(err, "Get")
183-
}
184-
_, err := db.Update(cachedImage, func() error {
182+
_, err = db.Update(cachedImage, func() error {
183+
if gotypes.IsNil(cachedImage.Info) {
184+
err = meta.Get(skuUrl, &image)
185+
if err != nil {
186+
return errors.Wrapf(err, "Get")
187+
}
185188
cachedImage.Info = image.Info
186189
cachedImage.Size = image.Size
187190
cachedImage.UEFI = image.UEFI
188-
return nil
189-
})
190-
return err
191+
}
192+
if cachedImage.ProjectId == "system" {
193+
cachedImage.ProjectId = s.GetProjectId()
194+
}
195+
return nil
196+
})
197+
if err != nil {
198+
return errors.Wrapf(err, "Update cachedimage")
191199
}
192200
return nil
193201
}

pkg/compute/models/cloudregions.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"yunion.io/x/onecloud/pkg/compute/options"
3838
"yunion.io/x/onecloud/pkg/httperrors"
3939
"yunion.io/x/onecloud/pkg/mcclient"
40+
"yunion.io/x/onecloud/pkg/mcclient/auth"
4041
"yunion.io/x/onecloud/pkg/util/stringutils2"
4142
"yunion.io/x/onecloud/pkg/util/yunionmeta"
4243
)
@@ -1264,7 +1265,8 @@ func (self *SCloudregion) newCloudimage(ctx context.Context, userCred mcclient.T
12641265
}
12651266

12661267
image.IsPublic = true
1267-
image.ProjectId = "system"
1268+
s := auth.GetAdminSession(ctx, options.Options.Region)
1269+
image.ProjectId = s.GetProjectId()
12681270
err = CachedimageManager.TableSpec().Insert(ctx, image)
12691271
if err != nil {
12701272
return errors.Wrapf(err, "Insert cachedimage")

0 commit comments

Comments
 (0)