snapshotpolicydisks.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. )
  22. type SSnapshotPolicyDiskManager struct {
  23. db.SVirtualJointResourceBaseManager
  24. SDiskResourceBaseManager
  25. }
  26. func (m *SSnapshotPolicyDiskManager) GetMasterFieldName() string {
  27. return "disk_id"
  28. }
  29. func (m *SSnapshotPolicyDiskManager) GetSlaveFieldName() string {
  30. return "snapshotpolicy_id"
  31. }
  32. var SnapshotPolicyDiskManager *SSnapshotPolicyDiskManager
  33. func init() {
  34. db.InitManager(func() {
  35. SnapshotPolicyDiskManager = &SSnapshotPolicyDiskManager{
  36. SVirtualJointResourceBaseManager: db.NewVirtualJointResourceBaseManager(
  37. SSnapshotPolicyDisk{},
  38. "snapshot_policy_disks_tbl",
  39. "snapshot_policy_disk",
  40. "snapshot_policy_disks",
  41. DiskManager,
  42. SnapshotPolicyManager,
  43. ),
  44. }
  45. SnapshotPolicyDiskManager.SetVirtualObject(SnapshotPolicyDiskManager)
  46. })
  47. }
  48. type SSnapshotPolicyDisk struct {
  49. db.SVirtualJointResourceBase
  50. SnapshotpolicyId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
  51. SDiskResourceBase `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
  52. }
  53. func (manager *SSnapshotPolicyDiskManager) InitializeData() error {
  54. disks := DiskManager.Query("id").SubQuery()
  55. policies := SnapshotPolicyManager.Query("id").SubQuery()
  56. q := manager.Query().In("disk_id", disks).In("snapshotpolicy_id", policies)
  57. pds := []SSnapshotPolicyDisk{}
  58. err := db.FetchModelObjects(manager, q, &pds)
  59. if err != nil {
  60. return err
  61. }
  62. for i := range pds {
  63. pd := &pds[i]
  64. migrateData := &SSnapshotPolicyResource{
  65. SnapshotpolicyId: pd.SnapshotpolicyId,
  66. ResourceId: pd.DiskId,
  67. ResourceType: api.SNAPSHOT_POLICY_TYPE_DISK,
  68. }
  69. migrateData.SetModelManager(SnapshotPolicyResourceManager, migrateData)
  70. cnt, err := SnapshotPolicyResourceManager.Query().
  71. Equals("resource_type", api.SNAPSHOT_POLICY_TYPE_DISK).
  72. Equals("resource_id", pd.DiskId).
  73. Equals("snapshotpolicy_id", pd.SnapshotpolicyId).CountWithError()
  74. if err != nil {
  75. return errors.Wrapf(err, "Count")
  76. }
  77. if cnt == 0 {
  78. err = SnapshotPolicyResourceManager.TableSpec().Insert(context.Background(), migrateData)
  79. if err != nil {
  80. return errors.Wrapf(err, "Insert %s", migrateData.Keyword())
  81. }
  82. }
  83. err = db.Purge(manager, "row_id", []string{fmt.Sprintf("%d", pd.RowId)}, true)
  84. if err != nil {
  85. return errors.Wrapf(err, "Purge %d", pd.RowId)
  86. }
  87. }
  88. return nil
  89. }