specs.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/util/printutils"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  21. "yunion.io/x/onecloud/pkg/mcclient/options"
  22. )
  23. func init() {
  24. type ListOptions struct {
  25. options.BaseListOptions
  26. Model string `help:"Specified model specs" choices:"hosts|isolated_devices|guests"`
  27. HostType string `help:"Host type filter" choices:"baremetal|hypervisor|esxi|kubelet|hyperv"`
  28. Gpu bool `help:"Only show gpu devices"`
  29. Zone string `help:"Filter by zone id or name"`
  30. Occupied bool `help:"show occupid host" json:"-"`
  31. }
  32. R(&ListOptions{}, "spec", "List all kinds of model specs", func(s *mcclient.ClientSession, args *ListOptions) error {
  33. var params *jsonutils.JSONDict
  34. {
  35. var err error
  36. params, err = args.BaseListOptions.Params()
  37. if err != nil {
  38. return err
  39. }
  40. }
  41. model := ""
  42. if len(args.Model) != 0 {
  43. model = args.Model
  44. }
  45. if len(args.HostType) > 0 {
  46. params.Add(jsonutils.NewString(args.HostType), "host_type")
  47. }
  48. if args.Gpu {
  49. params.Add(jsonutils.JSONTrue, "gpu")
  50. }
  51. if args.Zone != "" {
  52. params.Add(jsonutils.NewString(args.Zone), "zone")
  53. }
  54. if args.Occupied {
  55. params.Add(jsonutils.JSONFalse, "is_empty")
  56. }
  57. result, err := modules.Specs.GetModelSpecs(s, model, params)
  58. if err != nil {
  59. return err
  60. }
  61. printObject(result)
  62. return nil
  63. })
  64. type HostsQueryOptions struct {
  65. options.BaseListOptions
  66. HostType string `help:"Host type filter" choices:"baremetal|hypervisor|esxi|kubelet|hyperv"`
  67. Ncpu int64 `help:"#CPU count of host" metavar:"<CPU_COUNT>"`
  68. MemSize int64 `help:"Memory MB size"`
  69. DiskSpec []string `help:"Disk spec string, like 'Linux_adapter0_HDD_111Gx4'"`
  70. Nic int64 `help:"#Nics count of host" metavar:"<NIC_COUNT>"`
  71. GpuModel []string `help:"GPU model, like 'GeForce GTX 1050 Ti'"`
  72. Occupied bool `help:"Show occupid host" json:"-"`
  73. Manufacture string `help:"Manufacture of host"`
  74. Model string `help:"Model of host"`
  75. }
  76. R(&HostsQueryOptions{}, "spec-hosts-list", "List hosts according by specs", func(s *mcclient.ClientSession, args *HostsQueryOptions) error {
  77. newHostSpecKeys := func() []string {
  78. keys := []string{}
  79. if args.Ncpu > 0 {
  80. keys = append(keys, fmt.Sprintf("cpu:%d", args.Ncpu))
  81. }
  82. if args.MemSize > 0 {
  83. keys = append(keys, fmt.Sprintf("mem:%dM", args.MemSize))
  84. }
  85. if args.Nic > 0 {
  86. keys = append(keys, fmt.Sprintf("nic:%d", args.Nic))
  87. }
  88. if args.Manufacture != "" {
  89. keys = append(keys, fmt.Sprintf("manufacture:%s", args.Manufacture))
  90. }
  91. if args.Model != "" {
  92. keys = append(keys, fmt.Sprintf("model:%s", args.Model))
  93. }
  94. for _, gm := range args.GpuModel {
  95. keys = append(keys, fmt.Sprintf("gpu_model:%s", gm))
  96. }
  97. for _, ds := range args.DiskSpec {
  98. keys = append(keys, fmt.Sprintf("disk:%s", ds))
  99. }
  100. return keys
  101. }
  102. var params *jsonutils.JSONDict
  103. {
  104. var err error
  105. params, err = args.BaseListOptions.Params()
  106. if err != nil {
  107. return err
  108. }
  109. }
  110. if len(args.HostType) > 0 {
  111. params.Add(jsonutils.NewString(args.HostType), "host_type")
  112. }
  113. if args.Occupied {
  114. params.Add(jsonutils.JSONFalse, "is_empty")
  115. }
  116. specKeys := newHostSpecKeys()
  117. resp, err := modules.Specs.SpecsQueryModelObjects(s, "hosts", specKeys, params)
  118. if err != nil {
  119. return err
  120. }
  121. hosts, err := resp.GetArray()
  122. if err != nil {
  123. return err
  124. }
  125. data := &printutils.ListResult{Data: hosts, Total: len(hosts)}
  126. printList(data, []string{"ID", "Name", "MEM", "CPU", "Storage_Info"})
  127. return nil
  128. })
  129. type IsoDevQueryOptions struct {
  130. options.BaseListOptions
  131. Model string `help:"Device model name"`
  132. Vendor string `help:"Device vendor name"`
  133. }
  134. R(&IsoDevQueryOptions{}, "spec-isolated-devices-list", "List isolated devices according by specs", func(s *mcclient.ClientSession, args *IsoDevQueryOptions) error {
  135. newSpecKeys := func() []string {
  136. keys := []string{}
  137. if len(args.Vendor) != 0 {
  138. keys = append(keys, fmt.Sprintf("vendor:%s", args.Vendor))
  139. }
  140. if len(args.Model) != 0 {
  141. keys = append(keys, fmt.Sprintf("model:%s", args.Model))
  142. }
  143. return keys
  144. }
  145. var params *jsonutils.JSONDict
  146. {
  147. var err error
  148. params, err = args.BaseListOptions.Params()
  149. if err != nil {
  150. return err
  151. }
  152. }
  153. resp, err := modules.Specs.SpecsQueryModelObjects(s, "isolated_devices", newSpecKeys(), params)
  154. if err != nil {
  155. return err
  156. }
  157. devs, err := resp.GetArray()
  158. if err != nil {
  159. return err
  160. }
  161. data := &printutils.ListResult{Data: devs, Total: len(devs)}
  162. printList(data, []string{"ID", "Addr", "Dev_Type", "Model", "Vendor_Device_Id"})
  163. return nil
  164. })
  165. }