vcenters.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/onecloud/pkg/mcclient"
  18. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. func init() {
  22. type VCenterListOptions struct {
  23. options.BaseListOptions
  24. }
  25. R(&VCenterListOptions{}, "vcenter-list", "List VMWare vcenters", func(s *mcclient.ClientSession, args *VCenterListOptions) error {
  26. var params *jsonutils.JSONDict
  27. {
  28. var err error
  29. params, err = args.BaseListOptions.Params()
  30. if err != nil {
  31. return err
  32. }
  33. }
  34. result, err := modules.VCenters.List(s, params)
  35. if err != nil {
  36. return err
  37. }
  38. printList(result, modules.VCenters.GetColumns(s))
  39. return nil
  40. })
  41. type VCenterCreateOptions struct {
  42. NAME string `help:"Name of vcenter"`
  43. HOST string `help:"Hostname or IP of vcenter server"`
  44. Port int64 `help:"Port number" default:"443"`
  45. USER string `help:"User account name"`
  46. PASSWD string `help:"Password"`
  47. Desc string `help:"Description" metavar:"DESCRIPTION"`
  48. }
  49. R(&VCenterCreateOptions{}, "vcenter-create", "Create a vcenter", func(s *mcclient.ClientSession, args *VCenterCreateOptions) error {
  50. params := jsonutils.NewDict()
  51. params.Add(jsonutils.NewString(args.NAME), "name")
  52. params.Add(jsonutils.NewString(args.HOST), "hostname")
  53. params.Add(jsonutils.NewString(args.USER), "account")
  54. params.Add(jsonutils.NewString(args.PASSWD), "password")
  55. if args.Port > 0 {
  56. params.Add(jsonutils.NewInt(args.Port), "port")
  57. }
  58. vc, err := modules.VCenters.Create(s, params)
  59. if err != nil {
  60. return err
  61. }
  62. printObject(vc)
  63. return nil
  64. })
  65. type VCenterDetailOptions struct {
  66. ID string `help:"ID or name of vcenter"`
  67. }
  68. R(&VCenterDetailOptions{}, "vcenter-show", "Show details of a vcenter", func(s *mcclient.ClientSession, args *VCenterDetailOptions) error {
  69. vc, err := modules.VCenters.Get(s, args.ID, nil)
  70. if err != nil {
  71. return err
  72. }
  73. printObject(vc)
  74. return nil
  75. })
  76. R(&VCenterDetailOptions{}, "vcenter-delete", "Delete a vcenter", func(s *mcclient.ClientSession, args *VCenterDetailOptions) error {
  77. vc, err := modules.VCenters.Delete(s, args.ID, nil)
  78. if err != nil {
  79. return err
  80. }
  81. printObject(vc)
  82. return nil
  83. })
  84. type VCenteSyncOptions struct {
  85. ID string `help:"Sync vcenter ID or name"`
  86. SyncHost string `help:"Also full sync the host information"`
  87. SyncAllHosts bool `help:"Sync all hosts"`
  88. Force bool `help:"Force sync, disregard status"`
  89. }
  90. R(&VCenteSyncOptions{}, "vcenter-sync", "Sync a vcenter", func(s *mcclient.ClientSession, args *VCenteSyncOptions) error {
  91. params := jsonutils.NewDict()
  92. if args.SyncAllHosts {
  93. params.Add(jsonutils.JSONTrue, "sync_host")
  94. } else if len(args.SyncHost) > 0 {
  95. params.Add(jsonutils.NewString(args.SyncHost), "sync_host_ip")
  96. }
  97. if args.Force {
  98. params.Add(jsonutils.JSONTrue, "force")
  99. }
  100. vc, err := modules.VCenters.PerformAction(s, args.ID, "sync", params)
  101. if err != nil {
  102. return err
  103. }
  104. printObject(vc)
  105. return nil
  106. })
  107. type VCenterUpdateCredentialOptions struct {
  108. ID string `help:"ID or name of vcenter"`
  109. User string `help:"New Account"`
  110. Password string `help:"New password"`
  111. }
  112. R(&VCenterUpdateCredentialOptions{}, "vcenter-update-credential", "Update account and password information of a vcenter", func(s *mcclient.ClientSession, args *VCenterUpdateCredentialOptions) error {
  113. params := jsonutils.NewDict()
  114. if len(args.User) > 0 {
  115. params.Add(jsonutils.NewString(args.User), "account")
  116. }
  117. if len(args.Password) > 0 {
  118. params.Add(jsonutils.NewString(args.Password), "password")
  119. }
  120. if params.Size() == 0 {
  121. return InvalidUpdateError()
  122. }
  123. vc, err := modules.VCenters.PerformAction(s, args.ID, "update-credential", params)
  124. if err != nil {
  125. return err
  126. }
  127. printObject(vc)
  128. return nil
  129. })
  130. }