instance_snapshot_joint.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. )
  21. func init() {
  22. db.InitManager(func() {
  23. InstanceSnapshotJointManager = &SInstanceSnapshotJointManager{
  24. SVirtualJointResourceBaseManager: db.NewVirtualJointResourceBaseManager(
  25. SInstanceSnapshotJoint{},
  26. "instancesnapshotjoints_tbl",
  27. "instancesnapshotjoint",
  28. "instancesnapshotjoints",
  29. InstanceSnapshotManager,
  30. SnapshotManager,
  31. ),
  32. }
  33. InstanceSnapshotJointManager.SetVirtualObject(InstanceSnapshotJointManager)
  34. })
  35. }
  36. // +onecloud:swagger-gen-ignore
  37. type SInstanceSnapshotJoint struct {
  38. db.SVirtualJointResourceBase
  39. InstanceSnapshotId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
  40. SnapshotId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
  41. DiskIndex int8 `nullable:"false" default:"0" list:"user" create:"required"`
  42. }
  43. // +onecloud:swagger-gen-ignore
  44. type SInstanceSnapshotJointManager struct {
  45. db.SVirtualJointResourceBaseManager
  46. }
  47. func (manager *SInstanceSnapshotJointManager) GetMasterFieldName() string {
  48. return "instance_snapshot_id"
  49. }
  50. func (manager *SInstanceSnapshotJointManager) GetSlaveFieldName() string {
  51. return "snapshot_id"
  52. }
  53. var InstanceSnapshotJointManager *SInstanceSnapshotJointManager
  54. func (manager *SInstanceSnapshotJointManager) CreateJoint(ctx context.Context, instanceSnapshotId, snapshotId string, diskIndex int8) error {
  55. instanceSnapshotJoint := &SInstanceSnapshotJoint{}
  56. instanceSnapshotJoint.SetModelManager(manager, instanceSnapshotJoint)
  57. instanceSnapshotJoint.InstanceSnapshotId = instanceSnapshotId
  58. instanceSnapshotJoint.SnapshotId = snapshotId
  59. instanceSnapshotJoint.DiskIndex = diskIndex
  60. return manager.TableSpec().Insert(ctx, instanceSnapshotJoint)
  61. }
  62. func (manager *SInstanceSnapshotJointManager) IsSubSnapshot(snapshotId string) (bool, error) {
  63. count, err := manager.Query().Equals("snapshot_id", snapshotId).CountWithError()
  64. if err != nil {
  65. return false, err
  66. }
  67. return count > 0, nil
  68. }
  69. func (self *SInstanceSnapshotJoint) Detach(ctx context.Context, userCred mcclient.TokenCredential) error {
  70. return db.DetachJoint(ctx, userCred, self)
  71. }
  72. func (self *SInstanceSnapshotJoint) GetSnapshotDisk() (*SDisk, error) {
  73. sp, err := SnapshotManager.FetchById(self.SnapshotId)
  74. if err != nil {
  75. return nil, errors.Wrapf(err, "Get snapshot by %q", self.SnapshotId)
  76. }
  77. return sp.(*SSnapshot).GetDisk()
  78. }