snapshot_policy_resources.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. "fmt"
  17. "yunion.io/x/sqlchemy"
  18. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  19. )
  20. type SSnapshotPolicyResourceManager struct {
  21. db.SResourceBaseManager
  22. }
  23. var SnapshotPolicyResourceManager *SSnapshotPolicyResourceManager
  24. func init() {
  25. SnapshotPolicyResourceManager = &SSnapshotPolicyResourceManager{
  26. SResourceBaseManager: db.NewResourceBaseManager(
  27. SSnapshotPolicyResource{},
  28. "snapshot_policy_resources_tbl",
  29. "snapshot_policy_resource",
  30. "snapshot_policy_resources",
  31. ),
  32. }
  33. SnapshotPolicyResourceManager.SetVirtualObject(SnapshotPolicyResourceManager)
  34. }
  35. type SSnapshotPolicyResource struct {
  36. db.SResourceBase
  37. SnapshotpolicyId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
  38. ResourceId string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
  39. ResourceType string `width:"36" charset:"ascii" nullable:"false" list:"user" create:"required" index:"true"`
  40. }
  41. func (man *SSnapshotPolicyResourceManager) RemoveByResource(id, typ string) error {
  42. _, err := sqlchemy.GetDB().Exec(
  43. fmt.Sprintf(
  44. "delete from %s where resource_id = ? and resource_type = ?",
  45. man.TableSpec().Name(),
  46. ), id, typ,
  47. )
  48. return err
  49. }
  50. func (self *SSnapshotPolicyResource) GetServer() (*SGuest, error) {
  51. guest, err := GuestManager.FetchById(self.ResourceId)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return guest.(*SGuest), nil
  56. }
  57. func (self *SSnapshotPolicyResource) GetDisk() (*SDisk, error) {
  58. disk, err := DiskManager.FetchById(self.ResourceId)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return disk.(*SDisk), nil
  63. }
  64. func (self *SSnapshotPolicyResource) GetSnapshotPolicy() (*SSnapshotPolicy, error) {
  65. policy, err := SnapshotPolicyManager.FetchById(self.SnapshotpolicyId)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return policy.(*SSnapshotPolicy), nil
  70. }
  71. func (man *SSnapshotPolicyResourceManager) RemoveBySnapshotpolicy(id string) error {
  72. _, err := sqlchemy.GetDB().Exec(
  73. fmt.Sprintf(
  74. "delete from %s where snapshotpolicy_id = ?",
  75. man.TableSpec().Name(),
  76. ), id,
  77. )
  78. return err
  79. }
  80. // GetBindingCount returns the number of snapshot policies bound to the given resource.
  81. func (man *SSnapshotPolicyResourceManager) GetBindingCount(resourceId, resourceType string) (int, error) {
  82. return man.Query().Equals("resource_id", resourceId).Equals("resource_type", resourceType).CountWithError()
  83. }