machine.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 k8s
  15. import (
  16. "github.com/pkg/errors"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/util/fileutils"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/cmdline"
  20. "yunion.io/x/onecloud/pkg/mcclient/options"
  21. )
  22. type MachineListOptions struct {
  23. options.BaseListOptions
  24. Cluster string `help:"Filter by cluster"`
  25. }
  26. func (o MachineListOptions) Params() (jsonutils.JSONObject, error) {
  27. return options.ListStructToParams(&o)
  28. }
  29. type MachineCreateOptions struct {
  30. CLUSTER string `help:"Cluster id"`
  31. ROLE string `help:"Machine role" choices:"node|controlplane"`
  32. Type string `help:"Resource type" choices:"vm|baremetal" json:"resource_type"`
  33. Instance string `help:"VM or host instance id" json:"resource_id"`
  34. Name string `help:"Name of node"`
  35. Disk string `help:"VM root disk size, e.g. 100G"`
  36. Net string `help:"VM network config"`
  37. Cpu int `help:"VM cpu count"`
  38. Memory string `help:"VM memory size, e.g. 1G"`
  39. Hypervisor string `help:"VM hypervisor"`
  40. Sku string `help:"VM sku (instance type), e.g. 'ecs.c6.large'"`
  41. }
  42. func (o MachineCreateOptions) Params() (jsonutils.JSONObject, error) {
  43. params := jsonutils.NewDict()
  44. if o.Name != "" {
  45. params.Add(jsonutils.NewString(o.Name), "name")
  46. }
  47. params.Add(jsonutils.NewString(o.CLUSTER), "cluster")
  48. if o.ROLE != "" {
  49. params.Add(jsonutils.NewString(o.ROLE), "role")
  50. }
  51. if o.Instance != "" {
  52. params.Add(jsonutils.NewString(o.Instance), "resource_id")
  53. }
  54. if o.Type != "" {
  55. params.Add(jsonutils.NewString(o.Type), "resource_type")
  56. }
  57. if o.Type != "vm" {
  58. return params, nil
  59. }
  60. vmConfig := jsonutils.NewDict()
  61. if len(o.Disk) != 0 {
  62. diskConf, err := cmdline.ParseDiskConfig(o.Disk, 0)
  63. if err != nil {
  64. return nil, errors.Wrapf(err, "Parse disk %s", o.Disk)
  65. }
  66. vmConfig.Add(jsonutils.NewArray(diskConf.JSON(diskConf)), "disks")
  67. }
  68. if len(o.Net) != 0 {
  69. netConf, err := cmdline.ParseNetworkConfig(o.Net, 0)
  70. if err != nil {
  71. return nil, errors.Wrapf(err, "Parse network %s", o.Net)
  72. }
  73. vmConfig.Add(jsonutils.NewArray(netConf.JSON(netConf)), "nets")
  74. }
  75. if len(o.Memory) != 0 {
  76. memSize, err := fileutils.GetSizeMb(o.Memory, 'M', 1024)
  77. if err != nil {
  78. return nil, errors.Wrapf(err, "Parse memory %s", o.Memory)
  79. }
  80. vmConfig.Add(jsonutils.NewInt(int64(memSize)), "vmem_size")
  81. }
  82. if o.Cpu > 0 {
  83. vmConfig.Add(jsonutils.NewInt(int64(o.Cpu)), "vcpu_count")
  84. }
  85. vmConfig.Add(jsonutils.NewString(o.Hypervisor), "hypervisor")
  86. vmConfig.Add(jsonutils.NewString(o.Sku), "instance_type")
  87. params.Add(vmConfig, "config", "vm")
  88. return params, nil
  89. }
  90. type MachineListNetworkAddressOptions struct {
  91. IdentOptions
  92. }
  93. type MachineAttachNetworkAddressOptions struct {
  94. IdentOptions
  95. IPAddr string `help:"preferred ip address"`
  96. }
  97. func (o MachineAttachNetworkAddressOptions) Params() (jsonutils.JSONObject, error) {
  98. params := jsonutils.NewDict()
  99. if o.IPAddr != "" {
  100. params.Add(jsonutils.NewString(o.IPAddr), "ip_addr")
  101. }
  102. return params, nil
  103. }