servernetworks.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. "strconv"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/printutils"
  21. "yunion.io/x/pkg/util/regutils"
  22. "yunion.io/x/onecloud/cmd/climc/shell"
  23. "yunion.io/x/onecloud/pkg/apis/compute"
  24. "yunion.io/x/onecloud/pkg/cloudcommon/cmdline"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  27. "yunion.io/x/onecloud/pkg/mcclient/options"
  28. compute_options "yunion.io/x/onecloud/pkg/mcclient/options/compute"
  29. )
  30. func init() {
  31. cmd := shell.NewResourceCmd(&modules.ServerNetworkTrafficLogs)
  32. cmd.List(new(compute_options.ServerNetworkTrafficLogListOptions))
  33. type ServerNetworkListOptions struct {
  34. options.BaseListOptions
  35. Server string `help:"ID or Name of Server"`
  36. Mac string `help:"search the MAC address"`
  37. Ip string `help:"search the IP address"`
  38. Network string `help:"Network ID or name"`
  39. }
  40. R(&ServerNetworkListOptions{}, "server-network-list", "List server network pairs", func(s *mcclient.ClientSession, args *ServerNetworkListOptions) error {
  41. var params *jsonutils.JSONDict
  42. {
  43. var err error
  44. params, err = args.BaseListOptions.Params()
  45. if err != nil {
  46. return err
  47. }
  48. }
  49. if len(args.Mac) > 0 {
  50. params.Add(jsonutils.NewString(args.Mac), "mac_addr")
  51. }
  52. if len(args.Ip) > 0 {
  53. params.Add(jsonutils.NewString(args.Ip), "ip_addr")
  54. }
  55. var result *printutils.ListResult
  56. var err error
  57. if len(args.Server) > 0 {
  58. result, err = modules.Servernetworks.ListDescendent(s, args.Server, params)
  59. } else if len(args.Network) > 0 {
  60. result, err = modules.Servernetworks.ListDescendent2(s, args.Network, params)
  61. } else {
  62. result, err = modules.Servernetworks.List(s, params)
  63. }
  64. if err != nil {
  65. return err
  66. }
  67. printList(result, modules.Servernetworks.GetColumns(s))
  68. return nil
  69. })
  70. type ServerNetworkDetailOptions struct {
  71. SERVER string `help:"ID or Name of Server"`
  72. NETWORK string `help:"ID or Name of Network"`
  73. Mac string `help:"Mac of the guest NIC"`
  74. }
  75. R(&ServerNetworkDetailOptions{}, "server-network-show", "Show server network details", func(s *mcclient.ClientSession, args *ServerNetworkDetailOptions) error {
  76. query := jsonutils.NewDict()
  77. if len(args.Mac) > 0 {
  78. query.Add(jsonutils.NewString(args.Mac), "mac")
  79. }
  80. result, err := modules.Servernetworks.Get(s, args.SERVER, args.NETWORK, query)
  81. if err != nil {
  82. return err
  83. }
  84. printObject(result)
  85. return nil
  86. })
  87. type ServerNetworkUpdateOptions struct {
  88. SERVER string `help:"ID or Name of Server"`
  89. NETWORK string `help:"ID or Name of Wire"`
  90. Mac string `help:"Mac of NIC"`
  91. Driver string `help:"Driver model of vNIC" choices:"virtio|e1000|vmxnet3|rtl8139"`
  92. Index int64 `help:"Index of NIC" default:"-1"`
  93. Ifname string `help:"Interface name of vNIC on host"`
  94. Default bool `help:"is default nic?"`
  95. PortMapping []string `help:"Network port mapping, e.g. 'port=80,host_port=8080,protocol=<tcp|udp>,host_port_range=<int>-<int>,remote_ips=x.x.x.x|y.y.y.y'" short-token:"p"`
  96. }
  97. R(&ServerNetworkUpdateOptions{}, "server-network-update", "Update server network settings", func(s *mcclient.ClientSession, args *ServerNetworkUpdateOptions) error {
  98. params := jsonutils.NewDict()
  99. if len(args.Driver) > 0 {
  100. params.Add(jsonutils.NewString(args.Driver), "driver")
  101. }
  102. if args.Index >= 0 {
  103. params.Add(jsonutils.NewInt(args.Index), "index")
  104. }
  105. if len(args.Ifname) > 0 {
  106. params.Add(jsonutils.NewString(args.Ifname), "ifname")
  107. }
  108. if args.Default {
  109. params.Add(jsonutils.JSONTrue, "is_default")
  110. }
  111. if len(args.PortMapping) > 0 {
  112. psm, err := cmdline.ParseNetworkConfigPortMappings(args.PortMapping)
  113. if err != nil {
  114. return errors.Wrap(err, "parse port mapping")
  115. }
  116. ps := psm[0]
  117. params.Add(jsonutils.Marshal(ps), "port_mappings")
  118. }
  119. if params.Size() == 0 {
  120. return InvalidUpdateError()
  121. }
  122. query := jsonutils.NewDict()
  123. if len(args.Mac) > 0 {
  124. query.Add(jsonutils.NewString(args.Mac), "mac")
  125. }
  126. result, err := modules.Servernetworks.Update(s, args.SERVER, args.NETWORK, query, params)
  127. if err != nil {
  128. return err
  129. }
  130. printObject(result)
  131. return nil
  132. })
  133. type ServerNetworkBWOptions struct {
  134. SERVER string `help:"ID or Name of server"`
  135. MACORIP string `help:"IP, Mac, or Index of NIC"`
  136. BW int64 `help:"Bandwidth in Mbps"`
  137. }
  138. R(&ServerNetworkBWOptions{}, "server-change-bandwidth", "Change server network bandwidth in Mbps", func(s *mcclient.ClientSession, args *ServerNetworkBWOptions) error {
  139. params := jsonutils.NewDict()
  140. if regutils.MatchMacAddr(args.MACORIP) {
  141. params.Add(jsonutils.NewString(args.MACORIP), "mac")
  142. } else if regutils.MatchIP4Addr(args.MACORIP) {
  143. params.Add(jsonutils.NewString(args.MACORIP), "ip_addr")
  144. } else if regutils.MatchInteger(args.MACORIP) {
  145. index, err := strconv.ParseInt(args.MACORIP, 10, 64)
  146. if err != nil {
  147. return err
  148. }
  149. params.Add(jsonutils.NewInt(index), "index")
  150. } else {
  151. return fmt.Errorf("Please specify Ip or Mac")
  152. }
  153. params.Add(jsonutils.NewInt(args.BW), "bandwidth")
  154. server, err := modules.Servers.PerformAction(s, args.SERVER, "change-bandwidth", params)
  155. if err != nil {
  156. return err
  157. }
  158. printObject(server)
  159. return nil
  160. })
  161. type ServerAttachNetworkOptions struct {
  162. SERVER string `help:"ID or Name of server"`
  163. DisableSyncConfig bool `help:"Disable sync config"`
  164. NETDESC []string `help:"Network description"`
  165. }
  166. R(&ServerAttachNetworkOptions{}, "server-attach-network", "Attach a server to a virtual network", func(s *mcclient.ClientSession, args *ServerAttachNetworkOptions) error {
  167. input := compute.AttachNetworkInput{}
  168. for i := 0; i < len(args.NETDESC); i++ {
  169. conf, err := cmdline.ParseNetworkConfig(args.NETDESC[i], -1)
  170. if err != nil {
  171. return err
  172. }
  173. input.Nets = append(input.Nets, conf)
  174. }
  175. input.DisableSyncConfig = &args.DisableSyncConfig
  176. params := jsonutils.Marshal(input)
  177. srv, err := modules.Servers.PerformAction(s, args.SERVER, "attachnetwork", params)
  178. if err != nil {
  179. return err
  180. }
  181. printObject(srv)
  182. return nil
  183. })
  184. type ServerDetachNetworkOptions struct {
  185. SERVER string `help:"ID or Name of server"`
  186. MACORIP string `help:"Mac Or IP of NIC"`
  187. Reserve bool `help:"Put the release IP address into reserved address pool"`
  188. Force bool `help:"detach server network by force"`
  189. }
  190. R(&ServerDetachNetworkOptions{}, "server-detach-network", "Detach the virtual network fron a virtual server", func(s *mcclient.ClientSession, args *ServerDetachNetworkOptions) error {
  191. params := jsonutils.NewDict()
  192. // params.Add(jsonutils.NewString(args.NETWORK), "net_id")
  193. if args.Reserve {
  194. params.Add(jsonutils.JSONTrue, "reserve")
  195. }
  196. if args.Force {
  197. params.Add(jsonutils.JSONTrue, "force")
  198. }
  199. if regutils.MatchMacAddr(args.MACORIP) {
  200. params.Add(jsonutils.NewString(args.MACORIP), "mac")
  201. } else if regutils.MatchIP4Addr(args.MACORIP) {
  202. params.Add(jsonutils.NewString(args.MACORIP), "ip_addr")
  203. } else if regutils.MatchInteger(args.MACORIP) {
  204. index, err := strconv.ParseInt(args.MACORIP, 10, 64)
  205. if err != nil {
  206. return err
  207. }
  208. params.Add(jsonutils.NewInt(index), "index")
  209. } else if len(args.MACORIP) > 0 {
  210. params.Add(jsonutils.NewString(args.MACORIP), "net_id")
  211. } else {
  212. return fmt.Errorf("Please specify Ip or Mac")
  213. }
  214. srv, err := modules.Servers.PerformAction(s, args.SERVER, "detachnetwork", params)
  215. if err != nil {
  216. return err
  217. }
  218. printObject(srv)
  219. return nil
  220. })
  221. type ServerChangeIPAddressOptions struct {
  222. SERVER string `help:"ID or Name of server"`
  223. MACORIP string `help:"Mac Or IP of NIC"`
  224. NETDESC string `help:"Network description"`
  225. Reserve bool `help:"Put the release IP address into reserved address pool"`
  226. }
  227. R(&ServerChangeIPAddressOptions{}, "server-change-ipaddr", "Change ipaddr of a virtual server", func(s *mcclient.ClientSession, args *ServerChangeIPAddressOptions) error {
  228. params := jsonutils.NewDict()
  229. if args.Reserve {
  230. params.Add(jsonutils.JSONTrue, "reserve")
  231. }
  232. if regutils.MatchMacAddr(args.MACORIP) {
  233. params.Add(jsonutils.NewString(args.MACORIP), "mac")
  234. } else if regutils.MatchIP4Addr(args.MACORIP) {
  235. params.Add(jsonutils.NewString(args.MACORIP), "ip_addr")
  236. } else if regutils.MatchInteger(args.MACORIP) {
  237. index, err := strconv.ParseInt(args.MACORIP, 10, 64)
  238. if err != nil {
  239. return err
  240. }
  241. params.Add(jsonutils.NewInt(index), "index")
  242. } else {
  243. return fmt.Errorf("Please specify Ip or Mac")
  244. }
  245. conf, err := cmdline.ParseNetworkConfig(args.NETDESC, 0)
  246. if err != nil {
  247. return err
  248. }
  249. params.Add(conf.JSON(conf), "net_desc")
  250. srv, err := modules.Servers.PerformAction(s, args.SERVER, "change-ipaddr", params)
  251. if err != nil {
  252. return err
  253. }
  254. printObject(srv)
  255. return nil
  256. })
  257. }