cephfs.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 volume_mount
  15. import (
  16. "context"
  17. "yunion.io/x/pkg/gotypes"
  18. "yunion.io/x/pkg/util/rbacscope"
  19. "yunion.io/x/onecloud/pkg/apis"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/validators"
  23. "yunion.io/x/onecloud/pkg/compute/models"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. )
  27. func init() {
  28. models.RegisterContainerVolumeMountDriver(newCephFS())
  29. }
  30. type cephFS struct{}
  31. func newCephFS() models.IContainerVolumeMountDriver {
  32. return &cephFS{}
  33. }
  34. func (h cephFS) GetType() apis.ContainerVolumeMountType {
  35. return apis.CONTAINER_VOLUME_MOUNT_TYPE_CEPHF_FS
  36. }
  37. func (h cephFS) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, pod *models.SGuest, vm *apis.ContainerVolumeMount) (*apis.ContainerVolumeMount, error) {
  38. return vm, h.ValidatePodCreateData(ctx, userCred, vm, nil)
  39. }
  40. func (h cephFS) ValidatePodCreateData(ctx context.Context, userCred mcclient.TokenCredential, vm *apis.ContainerVolumeMount, input *api.ServerCreateInput) error {
  41. fs := vm.CephFS
  42. if fs == nil {
  43. return httperrors.NewNotEmptyError("ceph_fs is nil")
  44. }
  45. if len(fs.Id) == 0 {
  46. return httperrors.NewNotEmptyError("cep_fs.id is empty")
  47. }
  48. obj, err := validators.ValidateModel(ctx, userCred, models.FileSystemManager, &fs.Id)
  49. if err != nil {
  50. return err
  51. }
  52. filesystem := obj.(*models.SFileSystem)
  53. fs.Id = filesystem.Id
  54. if filesystem.Status != apis.STATUS_AVAILABLE {
  55. return httperrors.NewInvalidStatusError("invalid cephfs status %s", filesystem.Status)
  56. }
  57. account := filesystem.GetCloudaccount()
  58. if gotypes.IsNil(account) {
  59. return httperrors.NewInputParameterError("invalid cephfs %s", filesystem.Name)
  60. }
  61. if !db.IsAllowUpdate(ctx, rbacscope.ScopeProject, userCred, filesystem) {
  62. vm.ReadOnly = true
  63. }
  64. if account.Provider != api.CLOUD_PROVIDER_CEPHFS {
  65. return httperrors.NewInputParameterError("invalid cephfs type %s", account.Provider)
  66. }
  67. if gotypes.IsNil(account.Options) {
  68. return httperrors.NewInputParameterError("missing mon_host")
  69. }
  70. monHost, _ := account.Options.GetString("mon_host")
  71. if len(monHost) == 0 {
  72. return httperrors.NewInputParameterError("missing mon_host")
  73. }
  74. _, err = account.GetOptionPassword()
  75. return err
  76. }