networks.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/cmd/climc/shell"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  23. "yunion.io/x/onecloud/pkg/mcclient/options"
  24. compute_options "yunion.io/x/onecloud/pkg/mcclient/options/compute"
  25. "yunion.io/x/onecloud/pkg/util/netutils2"
  26. )
  27. func init() {
  28. cmd := shell.NewResourceCmd(&modules.Networks).WithContextManager(&modules.Wires)
  29. cmd.List(&compute_options.NetworkListOptions{})
  30. cmd.Create(&compute_options.NetworkCreateOptions{})
  31. cmd.Update(&compute_options.NetworkUpdateOptions{})
  32. cmd.Show(&compute_options.NetworkIdOptions{})
  33. cmd.Delete(&compute_options.NetworkIdOptions{})
  34. cmd.GetMetadata(&compute_options.NetworkIdOptions{})
  35. cmd.Perform("private", &compute_options.NetworkIdOptions{})
  36. cmd.Perform("public", &options.SharableResourcePublicOptions{})
  37. cmd.Perform("syncstatus", &compute_options.NetworkIdOptions{})
  38. cmd.Perform("sync", &compute_options.NetworkIdOptions{})
  39. cmd.Perform("purge", &compute_options.NetworkIdOptions{})
  40. cmd.Get("change-owner-candidate-domains", &compute_options.NetworkIdOptions{})
  41. cmd.Perform("set-class-metadata", &options.ResourceMetadataOptions{})
  42. cmd.Perform("switch-wire", &compute_options.NetworkSwitchWireOptions{})
  43. cmd.Perform("sync-additional-wires", &compute_options.NetworkSyncAdditionalWiresOptions{})
  44. cmd.Get("available-addresses", &compute_options.NetworkIdOptions{})
  45. type NetworkCreateOptions2 struct {
  46. Wire string `help:"ID or Name of wire in which the network is created"`
  47. Vpc string `help:"ID or Name of vpc in which the network is created"`
  48. Zone string `help:"ID or Name of zone in which the network is created"`
  49. NAME string `help:"Name of new network"`
  50. PREFIX string `help:"IPv4 prefix"`
  51. Prefix6 string `help:"IPv6 prefix"`
  52. BgpType string `help:"Internet service provider name" positional:"false"`
  53. AssignPublicIp bool
  54. Desc string `help:"Description" metavar:"DESCRIPTION"`
  55. }
  56. R(&NetworkCreateOptions2{}, "network-create2", "Create a virtual network", func(s *mcclient.ClientSession, args *NetworkCreateOptions2) error {
  57. params := jsonutils.NewDict()
  58. params.Add(jsonutils.NewString(args.NAME), "name")
  59. params.Add(jsonutils.NewString(args.PREFIX), "guest_ip_prefix")
  60. if len(args.Prefix6) > 0 {
  61. params.Add(jsonutils.NewString(args.Prefix6), "guest_ip6_prefix")
  62. }
  63. params.Set("assign_public_ip", jsonutils.NewBool(args.AssignPublicIp))
  64. if len(args.BgpType) > 0 {
  65. params.Add(jsonutils.NewString(args.BgpType), "bgp_type")
  66. }
  67. if len(args.Desc) > 0 {
  68. params.Add(jsonutils.NewString(args.Desc), "description")
  69. }
  70. if len(args.Wire) > 0 {
  71. params.Add(jsonutils.NewString(args.Wire), "wire")
  72. } else if len(args.Vpc) > 0 {
  73. if len(args.Zone) > 0 {
  74. params.Add(jsonutils.NewString(args.Zone), "zone")
  75. params.Add(jsonutils.NewString(args.Vpc), "vpc")
  76. } else {
  77. return fmt.Errorf("Either wire or VPC/Zone must be provided")
  78. }
  79. } else {
  80. return fmt.Errorf("Either wire or VPC/Zone must be provided")
  81. }
  82. net, e := modules.Networks.Create(s, params)
  83. if e != nil {
  84. return e
  85. }
  86. printObject(net)
  87. return nil
  88. })
  89. type NetworkCreate3Options struct {
  90. compute_options.NetworkCreateOptions `start_ip->positional:"false" end_ip->positional:"false" net_mask->positional:"false"`
  91. }
  92. R(&NetworkCreate3Options{}, "network-create3", "Create a dual-stack virtual network", func(s *mcclient.ClientSession, args *NetworkCreate3Options) error {
  93. params, err := args.Params()
  94. if err != nil {
  95. return err
  96. }
  97. net, err := modules.Networks.Create(s, params)
  98. if err != nil {
  99. return err
  100. }
  101. printObject(net)
  102. return nil
  103. })
  104. type NetworkSplitOptions struct {
  105. NETWORK string `help:"ID or name of network to split"`
  106. IP string `help:"Start ip of the split network"`
  107. Name string `help:"Name of the new network"`
  108. }
  109. R(&NetworkSplitOptions{}, "network-split", "Split a network at the specified IP address", func(s *mcclient.ClientSession, args *NetworkSplitOptions) error {
  110. params := jsonutils.NewDict()
  111. params.Add(jsonutils.NewString(args.IP), "split_ip")
  112. if len(args.Name) > 0 {
  113. params.Add(jsonutils.NewString(args.Name), "name")
  114. }
  115. net, err := modules.Networks.PerformAction(s, args.NETWORK, "split", params)
  116. if err != nil {
  117. return err
  118. }
  119. printObject(net)
  120. return nil
  121. })
  122. type NetworkMergeOptions struct {
  123. FROM string `help:"ID or name of merge network from"`
  124. TARGET string `help:"ID or name of merge network target"`
  125. }
  126. R(&NetworkMergeOptions{}, "network-merge", "Merge two network to be one", func(s *mcclient.ClientSession, args *NetworkMergeOptions) error {
  127. params := jsonutils.NewDict()
  128. params.Add(jsonutils.NewString(args.TARGET), "target")
  129. net, err := modules.Networks.PerformAction(s, args.FROM, "merge", params)
  130. if err != nil {
  131. return err
  132. }
  133. printObject(net)
  134. return nil
  135. })
  136. type NetworkStaticRoutesOptions struct {
  137. NETWORK string `help:"ID or name of the network"`
  138. Net []string `help:"destination network of static route"`
  139. Gw []string `help:"gateway address for the static route"`
  140. }
  141. R(&NetworkStaticRoutesOptions{}, "network-set-static-routes", "Set static routes for a network", func(s *mcclient.ClientSession, args *NetworkStaticRoutesOptions) error {
  142. params := jsonutils.NewDict()
  143. if len(args.Net) > 0 && len(args.Gw) > 0 {
  144. if len(args.Net) != len(args.Gw) {
  145. return fmt.Errorf("Inconsistent network and gateway pairs")
  146. }
  147. routes := jsonutils.NewDict()
  148. for i := 0; i < len(args.Net); i += 1 {
  149. routes.Add(jsonutils.NewString(args.Gw[i]), args.Net[i])
  150. }
  151. params.Add(routes, "static_routes")
  152. } else {
  153. params.Add(jsonutils.JSONNull, "static_routes")
  154. }
  155. result, err := modules.Networks.PerformAction(s, args.NETWORK, "metadata", params)
  156. if err != nil {
  157. return err
  158. }
  159. printObject(result)
  160. return nil
  161. })
  162. type NetworkAddressOptions struct {
  163. NETWORK string `help:"id or name of network to query"`
  164. api.GetNetworkAddressesInput
  165. }
  166. R(&NetworkAddressOptions{}, "network-addresses", "Query used addresses of network", func(s *mcclient.ClientSession, args *NetworkAddressOptions) error {
  167. result, err := modules.Networks.GetSpecific(s, args.NETWORK, "addresses", jsonutils.Marshal(args.GetNetworkAddressesInput))
  168. if err != nil {
  169. return err
  170. }
  171. addrList, _ := result.GetArray("addresses")
  172. if addrList == nil {
  173. fmt.Println("no result")
  174. return nil
  175. }
  176. listResult := printutils.ListResult{Data: addrList}
  177. printList(&listResult, nil)
  178. return nil
  179. })
  180. type NetworkStatusOptions struct {
  181. NETWORK string `help:"id or name of network to sync" json:"-"`
  182. STATUS string `help:"status of network" choices:"available|unavailable" json:"status"`
  183. Reason string `help:"reason to change status" json:"reason"`
  184. }
  185. R(&NetworkStatusOptions{}, "network-status", "Set on-premise network status", func(s *mcclient.ClientSession, args *NetworkStatusOptions) error {
  186. params := jsonutils.Marshal(args)
  187. result, err := modules.Networks.PerformAction(s, args.NETWORK, "status", params)
  188. if err != nil {
  189. return err
  190. }
  191. printObject(result)
  192. return nil
  193. })
  194. type NetworkChangeOwnerOptions struct {
  195. ID string `help:"Network to change owner" json:"-"`
  196. PROJECT string `help:"Project ID or change" json:"tenant"`
  197. }
  198. R(&NetworkChangeOwnerOptions{}, "network-change-owner", "Change owner project of a network", func(s *mcclient.ClientSession, args *NetworkChangeOwnerOptions) error {
  199. params, err := options.StructToParams(args)
  200. if err != nil {
  201. return err
  202. }
  203. net, err := modules.Networks.PerformAction(s, args.ID, "change-owner", params)
  204. if err != nil {
  205. return err
  206. }
  207. printObject(net)
  208. return nil
  209. })
  210. type NetworkSetBgpTypeOptions struct {
  211. ID string `help:"Network to set BgpType" json:"-"`
  212. BgpType *string `help:"new BgpType name"`
  213. }
  214. R(&NetworkSetBgpTypeOptions{}, "network-set-bgp-type", "Set BgpType of a network", func(s *mcclient.ClientSession, args *NetworkSetBgpTypeOptions) error {
  215. params, err := options.StructToParams(args)
  216. if err != nil {
  217. return err
  218. }
  219. net, err := modules.Networks.PerformAction(s, args.ID, "set-bgp-type", params)
  220. if err != nil {
  221. return err
  222. }
  223. printObject(net)
  224. return nil
  225. })
  226. type NetworkInterfaceInfoOptions struct {
  227. DEV string `help:"name of device, e.g. eth0"`
  228. }
  229. R(&NetworkInterfaceInfoOptions{}, "network-interface-info", "Show addr and routes of interface", func(s *mcclient.ClientSession, args *NetworkInterfaceInfoOptions) error {
  230. netIf := netutils2.NewNetInterface(args.DEV)
  231. fmt.Println("[Slave Addresses]")
  232. for _, addr := range netIf.GetSlaveAddresses() {
  233. fmt.Printf("%s/%d\n", addr.Addr, addr.MaskLen)
  234. }
  235. fmt.Println("[Routes]")
  236. for _, r := range netIf.GetRouteSpecs() {
  237. fmt.Printf("%s via %s dev %d\n", r.Dst.String(), r.Gw.String(), r.LinkIndex)
  238. }
  239. return nil
  240. })
  241. }