nfs.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/log"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/compute/options"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. )
  27. type SNfsStorageDriver struct {
  28. SBaseStorageDriver
  29. }
  30. func init() {
  31. driver := SNfsStorageDriver{}
  32. models.RegisterStorageDriver(&driver)
  33. }
  34. func (self *SNfsStorageDriver) GetStorageType() string {
  35. return api.STORAGE_NFS
  36. }
  37. func (self *SNfsStorageDriver) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, input *api.StorageCreateInput) error {
  38. input.StorageConf = jsonutils.NewDict()
  39. if len(input.NfsHost) == 0 {
  40. return httperrors.NewMissingParameterError("nfs_host")
  41. }
  42. if len(input.NfsSharedDir) == 0 {
  43. return httperrors.NewMissingParameterError("nfs_shared_dir")
  44. }
  45. input.StorageConf.Update(jsonutils.Marshal(map[string]string{
  46. "nfs_host": input.NfsHost,
  47. "nfs_shared_dir": input.NfsSharedDir,
  48. }))
  49. return nil
  50. }
  51. func (self *SNfsStorageDriver) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, storage *models.SStorage, data jsonutils.JSONObject) {
  52. sc := &models.SStoragecache{}
  53. sc.Path = options.Options.DefaultImageCacheDir
  54. sc.ExternalId = storage.Id
  55. sc.Name = "nfs-" + storage.Name + time.Now().Format("2006-01-02 15:04:05")
  56. if err := models.StoragecacheManager.TableSpec().Insert(ctx, sc); err != nil {
  57. log.Errorf("insert storagecache for storage %s error: %v", storage.Name, err)
  58. return
  59. }
  60. _, err := db.Update(storage, func() error {
  61. storage.StoragecacheId = sc.Id
  62. storage.Status = api.STORAGE_ONLINE
  63. return nil
  64. })
  65. if err != nil {
  66. log.Errorf("update storagecache info for storage %s error: %v", storage.Name, err)
  67. }
  68. }