clvm.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 storagedrivers
  15. import (
  16. "context"
  17. "strings"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. api "yunion.io/x/onecloud/pkg/apis/compute"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. )
  27. type SCLVMStorageDriver struct {
  28. SBaseStorageDriver
  29. }
  30. func init() {
  31. driver := SCLVMStorageDriver{}
  32. models.RegisterStorageDriver(&driver)
  33. }
  34. func (s *SCLVMStorageDriver) GetStorageType() string {
  35. return api.STORAGE_CLVM
  36. }
  37. func (s *SCLVMStorageDriver) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *api.StorageCreateInput) error {
  38. input.CLVMVgName = strings.TrimSpace(input.CLVMVgName)
  39. if len(input.CLVMVgName) == 0 {
  40. return httperrors.NewMissingParameterError("clvm_vg_name")
  41. }
  42. storages := []models.SStorage{}
  43. q := models.StorageManager.Query().Equals("storage_type", api.STORAGE_CLVM)
  44. err := db.FetchModelObjects(models.StorageManager, q, &storages)
  45. if err != nil {
  46. return httperrors.NewGeneralError(err)
  47. }
  48. for i := 0; i < len(storages); i++ {
  49. vgName, _ := storages[i].StorageConf.GetString("clvm_vg_name")
  50. if input.CLVMVgName == vgName {
  51. return httperrors.NewDuplicateResourceError("This CLVM Storage[%s/%s] has already exist", storages[i].Name, input.CLVMVgName)
  52. }
  53. }
  54. input.StorageConf = jsonutils.NewDict()
  55. input.StorageConf.Set("clvm_vg_name", jsonutils.NewString(input.CLVMVgName))
  56. return nil
  57. }
  58. func (self *SCLVMStorageDriver) ValidateSnapshotDelete(ctx context.Context, snapshot *models.SSnapshot) error {
  59. return nil
  60. }
  61. func (s *SCLVMStorageDriver) ValidateCreateSnapshotData(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, input *api.SnapshotCreateInput) error {
  62. return nil
  63. }
  64. func (s *SCLVMStorageDriver) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, storage *models.SStorage, data jsonutils.JSONObject) {
  65. sc := &models.SStoragecache{}
  66. sc.ExternalId = storage.Id
  67. sc.Name = "clvm-" + storage.Name + time.Now().Format("2006-01-02 15:04:05")
  68. if err := models.StoragecacheManager.TableSpec().Insert(ctx, sc); err != nil {
  69. log.Errorf("insert storagecache for storage %s error: %v", storage.Name, err)
  70. return
  71. }
  72. _, err := db.Update(storage, func() error {
  73. storage.StoragecacheId = sc.Id
  74. storage.Status = api.STORAGE_ONLINE
  75. return nil
  76. })
  77. if err != nil {
  78. log.Errorf("update storagecache info for storage %s error: %v", storage.Name, err)
  79. }
  80. }