hoststorages.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 compute
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/pkg/util/printutils"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  20. "yunion.io/x/onecloud/pkg/mcclient/options"
  21. )
  22. func init() {
  23. type HostStorageListOptions struct {
  24. options.BaseListOptions
  25. Host string `help:"ID or Name of Host"`
  26. Storage string `help:"ID or Name of Storage"`
  27. }
  28. R(&HostStorageListOptions{}, "host-storage-list", "List host storage pairs", func(s *mcclient.ClientSession, args *HostStorageListOptions) error {
  29. var params *jsonutils.JSONDict
  30. {
  31. var err error
  32. params, err = args.BaseListOptions.Params()
  33. if err != nil {
  34. return err
  35. }
  36. }
  37. var result *printutils.ListResult
  38. var err error
  39. // if len(args.Storage) > 0 {
  40. // params.Add(jsonutils.NewString(args.Storage), "storage")
  41. // }
  42. if len(args.Host) > 0 {
  43. result, err = modules.Hoststorages.ListDescendent(s, args.Host, params)
  44. } else if len(args.Storage) > 0 {
  45. result, err = modules.Hoststorages.ListDescendent2(s, args.Storage, params)
  46. } else {
  47. result, err = modules.Hoststorages.List(s, params)
  48. }
  49. if err != nil {
  50. return err
  51. }
  52. printList(result, modules.Hoststorages.GetColumns(s))
  53. return nil
  54. })
  55. type HostStorageDetailOptions struct {
  56. HOST string `help:"ID or Name of Host"`
  57. STORAGE string `help:"ID or Name of Storage"`
  58. }
  59. R(&HostStorageDetailOptions{}, "host-storage-show", "Show host storage details", func(s *mcclient.ClientSession, args *HostStorageDetailOptions) error {
  60. result, err := modules.Hoststorages.Get(s, args.HOST, args.STORAGE, nil)
  61. if err != nil {
  62. return err
  63. }
  64. printObject(result)
  65. return nil
  66. })
  67. R(&HostStorageDetailOptions{}, "host-storage-detach", "Detach a storage from a host", func(s *mcclient.ClientSession, args *HostStorageDetailOptions) error {
  68. result, err := modules.Hoststorages.Detach(s, args.HOST, args.STORAGE, nil)
  69. if err != nil {
  70. return err
  71. }
  72. printObject(result)
  73. return nil
  74. })
  75. type HostStorageAttachOptions struct {
  76. HOST string `help:"ID or Name of Host"`
  77. STORAGE string `help:"ID or Name of Storage"`
  78. MountPoint string `help:"MountPoint of Storage"`
  79. }
  80. R(&HostStorageAttachOptions{}, "host-storage-attach", "Attach a storage to a host", func(s *mcclient.ClientSession, args *HostStorageAttachOptions) error {
  81. params := jsonutils.NewDict()
  82. if args.MountPoint != "" {
  83. params.Add(jsonutils.NewString(args.MountPoint), "mount_point")
  84. }
  85. result, err := modules.Hoststorages.Attach(s, args.HOST, args.STORAGE, params)
  86. if err != nil {
  87. return err
  88. }
  89. printObject(result)
  90. return nil
  91. })
  92. }