storagedrivers.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/jsonutils"
  18. api "yunion.io/x/onecloud/pkg/apis/compute"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. )
  22. type IStorageDriver interface {
  23. GetStorageType() string
  24. ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *api.StorageCreateInput) error
  25. ValidateUpdateData(ctx context.Context, userCred mcclient.TokenCredential, input api.StorageUpdateInput) (api.StorageUpdateInput, error)
  26. DoStorageUpdateTask(ctx context.Context, userCred mcclient.TokenCredential, storage *SStorage, task taskman.ITask) error
  27. PostCreate(ctx context.Context, userCred mcclient.TokenCredential, storage *SStorage, data jsonutils.JSONObject)
  28. ValidateSnapshotDelete(ctx context.Context, snapshot *SSnapshot) error
  29. ValidateCreateSnapshotData(ctx context.Context, userCred mcclient.TokenCredential, disk *SDisk, input *api.SnapshotCreateInput) error
  30. RequestCreateSnapshot(ctx context.Context, snapshot *SSnapshot, task taskman.ITask) error
  31. RequestDeleteSnapshot(ctx context.Context, snapshot *SSnapshot, task taskman.ITask) error
  32. SnapshotIsOutOfChain(disk *SDisk) bool
  33. OnDiskReset(ctx context.Context, userCred mcclient.TokenCredential, disk *SDisk, snapshot *SSnapshot, data jsonutils.JSONObject) error
  34. }
  35. var storageDrivers map[string]IStorageDriver
  36. func init() {
  37. storageDrivers = make(map[string]IStorageDriver)
  38. }
  39. func RegisterStorageDriver(driver IStorageDriver) {
  40. storageDrivers[driver.GetStorageType()] = driver
  41. }
  42. func GetStorageDriver(storageType string) IStorageDriver {
  43. driver, ok := storageDrivers[storageType]
  44. if ok {
  45. return driver
  46. }
  47. return nil
  48. }