deviceclass.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Copyright 2020 The Rook Authors. All rights reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "encoding/json"
  16. "github.com/pkg/errors"
  17. "github.com/rook/rook/pkg/clusterd"
  18. )
  19. // GetDeviceClasses gets the available device classes.
  20. func GetDeviceClasses(context *clusterd.Context, clusterInfo *ClusterInfo) ([]string, error) {
  21. args := []string{"osd", "crush", "class", "ls"}
  22. cmd := NewCephCommand(context, clusterInfo, args)
  23. buf, err := cmd.Run()
  24. if err != nil {
  25. return nil, errors.Wrapf(err, "failed to get deviceclasses. %s", string(buf))
  26. }
  27. var deviceclass []string
  28. if err := json.Unmarshal(buf, &deviceclass); err != nil {
  29. return nil, errors.Wrapf(err, "failed to unmarshal osd crush class list response %q", string(buf))
  30. }
  31. return deviceclass, nil
  32. }
  33. // GetDeviceClassOSDs gets the OSDs associated with a device class.
  34. func GetDeviceClassOSDs(context *clusterd.Context, clusterInfo *ClusterInfo, deviceClass string) ([]int, error) {
  35. args := []string{"osd", "crush", "class", "ls-osd", deviceClass}
  36. cmd := NewCephCommand(context, clusterInfo, args)
  37. buf, err := cmd.Run()
  38. if err != nil {
  39. return nil, errors.Wrapf(err, "failed to get device class osd. %s", string(buf))
  40. }
  41. var osds []int
  42. if err := json.Unmarshal(buf, &osds); err != nil {
  43. return nil, errors.Wrap(err, "failed to unmarshal device class osd response")
  44. }
  45. return osds, nil
  46. }