@@ -57,7 +57,8 @@ func (s *OciConfigService) saveCompartmentCache(ctx context.Context, cfgID uint,
|
||||
rows := make([]model.CompartmentCache, 0, len(comps))
|
||||
for _, c := range comps {
|
||||
rows = append(rows, model.CompartmentCache{
|
||||
OciConfigID: cfgID, OCID: c.ID, Name: c.Name, State: c.LifecycleState,
|
||||
OciConfigID: cfgID, OCID: c.ID, Name: c.Name,
|
||||
ParentOCID: c.ParentID, State: c.LifecycleState,
|
||||
})
|
||||
}
|
||||
return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
@@ -191,16 +192,29 @@ func (s *OciConfigService) CachedCompartments(ctx context.Context, id uint) ([]o
|
||||
Order("name").Find(&rows).Error; err != nil {
|
||||
return nil, fmt.Errorf("load compartment cache: %w", err)
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
if len(rows) == 0 || legacyCompartmentCache(rows) {
|
||||
return s.refreshCompartmentCache(ctx, cfg)
|
||||
}
|
||||
comps := make([]oci.Compartment, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
comps = append(comps, oci.Compartment{ID: r.OCID, Name: r.Name, LifecycleState: r.State})
|
||||
comps = append(comps, oci.Compartment{
|
||||
ID: r.OCID, Name: r.Name, ParentID: r.ParentOCID, LifecycleState: r.State,
|
||||
})
|
||||
}
|
||||
return comps, nil
|
||||
}
|
||||
|
||||
// legacyCompartmentCache 识别未存 parentId 的旧格式缓存:每个 compartment
|
||||
// 必有父(至少是租户根),整组 parent 全空即旧数据,需实时刷新一次自愈。
|
||||
func legacyCompartmentCache(rows []model.CompartmentCache) bool {
|
||||
for _, r := range rows {
|
||||
if r.ParentOCID != "" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return len(rows) > 0
|
||||
}
|
||||
|
||||
// refreshCompartmentCache 实时拉取 compartment 并覆盖缓存。
|
||||
func (s *OciConfigService) refreshCompartmentCache(ctx context.Context, cfg *model.OciConfig) ([]oci.Compartment, error) {
|
||||
cred, err := s.credentialsOf(cfg)
|
||||
|
||||
Reference in New Issue
Block a user