distinctfield.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 misc
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  20. )
  21. func init() {
  22. type DistinctFieldOption struct {
  23. MODULE string `help:"module name"`
  24. FIELD string `help:"distinct field name to query"`
  25. Extra bool `help:"query extra distinct field"`
  26. Scope string `help:"resource scope" choices:"system|domain|project"`
  27. }
  28. R(&DistinctFieldOption{}, "distinct-field", "Query values of a distinct field for a module", func(s *mcclient.ClientSession, args *DistinctFieldOption) error {
  29. mod, err := modulebase.GetModule(s, args.MODULE)
  30. if err != nil || mod == nil {
  31. if err != nil {
  32. return fmt.Errorf("module %s not found %s", args.MODULE, err)
  33. }
  34. return fmt.Errorf("No module %s found", args.MODULE)
  35. }
  36. params := jsonutils.NewDict()
  37. if args.Extra {
  38. params.Add(jsonutils.NewString(args.FIELD), "extra_field")
  39. } else {
  40. params.Add(jsonutils.NewString(args.FIELD), "field")
  41. }
  42. if len(args.Scope) > 0 {
  43. params.Add(jsonutils.NewString(args.Scope), "scope")
  44. }
  45. result, err := mod.Get(s, "distinct-field", params)
  46. if err != nil {
  47. return err
  48. }
  49. fmt.Println(result)
  50. return nil
  51. })
  52. type DistinctFieldsOption struct {
  53. MODULE string `help:"module name"`
  54. Field []string `help:"distinct field name to query"`
  55. ExtraField []string `help:"distinct field name to query"`
  56. ExtraResource string
  57. Scope string `help:"resource scope" choices:"system|domain|project"`
  58. }
  59. R(&DistinctFieldsOption{}, "distinct-fields", "Query values of a distinct fields for a module", func(s *mcclient.ClientSession, args *DistinctFieldsOption) error {
  60. mod, err := modulebase.GetModule(s, args.MODULE)
  61. if err != nil || mod == nil {
  62. if err != nil {
  63. return fmt.Errorf("module %s not found %s", args.MODULE, err)
  64. }
  65. return fmt.Errorf("No module %s found", args.MODULE)
  66. }
  67. params := jsonutils.Marshal(args)
  68. result, err := mod.Get(s, "distinct-fields", params)
  69. if err != nil {
  70. return err
  71. }
  72. fmt.Println(result)
  73. return nil
  74. })
  75. }