slvm.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 SSLVMStorageDriver struct {
  28. SBaseStorageDriver
  29. }
  30. func init() {
  31. driver := SSLVMStorageDriver{}
  32. models.RegisterStorageDriver(&driver)
  33. }
  34. func (s *SSLVMStorageDriver) GetStorageType() string {
  35. return api.STORAGE_SLVM
  36. }
  37. func (s *SSLVMStorageDriver) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *api.StorageCreateInput) error {
  38. input.SLVMVgName = strings.TrimSpace(input.SLVMVgName)
  39. if len(input.SLVMVgName) == 0 {
  40. return httperrors.NewMissingParameterError("slvm_vg_name")
  41. }
  42. if !input.Lvmlockd {
  43. return httperrors.NewMissingParameterError("lvm_lockd")
  44. }
  45. storages := []models.SStorage{}
  46. q := models.StorageManager.Query().Equals("storage_type", api.STORAGE_SLVM)
  47. err := db.FetchModelObjects(models.StorageManager, q, &storages)
  48. if err != nil {
  49. return httperrors.NewGeneralError(err)
  50. }
  51. for i := 0; i < len(storages); i++ {
  52. vgName, _ := storages[i].StorageConf.GetString("slvm_vg_name")
  53. if input.SLVMVgName == vgName {
  54. return httperrors.NewDuplicateResourceError("This SLVM Storage[%s/%s] has already exist", storages[i].Name, input.SLVMVgName)
  55. }
  56. }
  57. input.StorageConf = jsonutils.NewDict()
  58. input.StorageConf.Set("slvm_vg_name", jsonutils.NewString(input.SLVMVgName))
  59. if input.Lvmlockd {
  60. input.StorageConf.Set("enabled_lvmlockd", jsonutils.NewBool(true))
  61. }
  62. return nil
  63. }
  64. func (self *SSLVMStorageDriver) ValidateSnapshotDelete(ctx context.Context, snapshot *models.SSnapshot) error {
  65. return nil
  66. }
  67. func (s *SSLVMStorageDriver) ValidateCreateSnapshotData(ctx context.Context, userCred mcclient.TokenCredential, disk *models.SDisk, input *api.SnapshotCreateInput) error {
  68. return nil
  69. }
  70. func (s *SSLVMStorageDriver) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, storage *models.SStorage, data jsonutils.JSONObject) {
  71. sc := &models.SStoragecache{}
  72. sc.ExternalId = storage.Id
  73. sc.MasterHost = storage.MasterHost
  74. sc.Name = "slvm-" + storage.Name + time.Now().Format("2006-01-02 15:04:05")
  75. if err := models.StoragecacheManager.TableSpec().Insert(ctx, sc); err != nil {
  76. log.Errorf("insert storagecache for storage %s error: %v", storage.Name, err)
  77. return
  78. }
  79. _, err := db.Update(storage, func() error {
  80. storage.StoragecacheId = sc.Id
  81. storage.Status = api.STORAGE_ONLINE
  82. return nil
  83. })
  84. if err != nil {
  85. log.Errorf("update storagecache info for storage %s error: %v", storage.Name, err)
  86. }
  87. }