network.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/pkg/errors"
  18. "yunion.io/x/onecloud/cmd/climc/shell"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/mcclient/options"
  21. )
  22. type NetworkListOptions struct {
  23. options.BaseListOptions
  24. Ip string `help:"search networks that contain this IP"`
  25. ZoneIds []string `help:"search networks in zones"`
  26. Wire string `help:"search networks belongs to a wire" json:"-"`
  27. Host string `help:"search networks attached to a host"`
  28. Vpc string `help:"search networks belongs to a VPC"`
  29. Region string `help:"search networks belongs to a CloudRegion" json:"cloudregion"`
  30. City string `help:"search networks belongs to a city"`
  31. Usable *bool `help:"search usable networks"`
  32. ServerType string `help:"search networks belongs to a ServerType" choices:"baremetal|container|eip|guest|ipmi|pxe|hostlocal"`
  33. Schedtag string `help:"filter networks by schedtag"`
  34. HostSchedtagId string `help:"filter by host schedtag"`
  35. IsAutoAlloc *bool `help:"search network with is_auto_alloc"`
  36. IsClassic *bool `help:"search classic on-premise network"`
  37. // Status string `help:"filter by network status"`
  38. GuestIpStart []string `help:"search by guest_ip_start"`
  39. GuestIpEnd []string `help:"search by guest_ip_end"`
  40. IpMatch []string `help:"search by network ips"`
  41. BgpType []string `help:"filter by bgp_type"`
  42. HostType string `help:"filter by host_type"`
  43. RouteTableId string `help:"Filter by RouteTable"`
  44. OrderByIpStart string
  45. OrderByIpEnd string
  46. }
  47. func (opts *NetworkListOptions) GetContextId() string {
  48. return opts.Wire
  49. }
  50. func (opts *NetworkListOptions) Params() (jsonutils.JSONObject, error) {
  51. return options.ListStructToParams(opts)
  52. }
  53. type NetworkCreateOptions struct {
  54. WIRE string `help:"ID or Name of wire in which the network is created"`
  55. NETWORK string `help:"Name of new network"`
  56. StartIp string `help:"Start of IPv4 address range" positional:"true" json:"start_ip"`
  57. EndIp string `help:"End of IPv4 address rnage" positional:"true" json:"end_ip"`
  58. NetMask int64 `help:"Length of network mask" positional:"true" json:"net_mask"`
  59. Gateway string `help:"Default gateway"`
  60. StartIp6 string `help:"IPv6 start ip"`
  61. EndIp6 string `help:"IPv6 end ip"`
  62. NetMask6 int64 `help:"IPv6 netmask"`
  63. Gateway6 string `help:"IPv6 gateway"`
  64. VlanId int64 `help:"Vlan ID" default:"1"`
  65. IfnameHint string `help:"Hint for ifname generation"`
  66. AllocPolicy string `help:"Address allocation policy" choices:"none|stepdown|stepup|random"`
  67. ServerType string `help:"Server type" choices:"baremetal|container|eip|guest|ipmi|pxe|hostlocal"`
  68. IsAutoAlloc *bool `help:"Auto allocation IP pool"`
  69. BgpType string `help:"Internet service provider name" positional:"false"`
  70. Desc string `help:"Description" metavar:"DESCRIPTION"`
  71. }
  72. func (opts *NetworkCreateOptions) Params() (jsonutils.JSONObject, error) {
  73. params := jsonutils.NewDict()
  74. params.Add(jsonutils.NewString(opts.WIRE), "wire")
  75. params.Add(jsonutils.NewString(opts.NETWORK), "name")
  76. if len(opts.StartIp) > 0 {
  77. params.Add(jsonutils.NewString(opts.StartIp), "guest_ip_start")
  78. }
  79. if len(opts.EndIp) > 0 {
  80. params.Add(jsonutils.NewString(opts.EndIp), "guest_ip_end")
  81. }
  82. if opts.NetMask > 0 {
  83. params.Add(jsonutils.NewInt(opts.NetMask), "guest_ip_mask")
  84. }
  85. if len(opts.Gateway) > 0 {
  86. params.Add(jsonutils.NewString(opts.Gateway), "guest_gateway")
  87. }
  88. if len(opts.StartIp6) > 0 {
  89. params.Add(jsonutils.NewString(opts.StartIp6), "guest_ip6_start")
  90. }
  91. if len(opts.EndIp6) > 0 {
  92. params.Add(jsonutils.NewString(opts.EndIp6), "guest_ip6_end")
  93. }
  94. if opts.NetMask6 > 0 {
  95. params.Add(jsonutils.NewInt(opts.NetMask6), "guest_ip6_mask")
  96. }
  97. if len(opts.Gateway6) > 0 {
  98. params.Add(jsonutils.NewString(opts.Gateway6), "guest_gateway6")
  99. }
  100. if opts.VlanId > 0 {
  101. params.Add(jsonutils.NewInt(opts.VlanId), "vlan_id")
  102. }
  103. if len(opts.ServerType) > 0 {
  104. params.Add(jsonutils.NewString(opts.ServerType), "server_type")
  105. }
  106. if len(opts.IfnameHint) > 0 {
  107. params.Add(jsonutils.NewString(opts.IfnameHint), "ifname_hint")
  108. }
  109. if len(opts.AllocPolicy) > 0 {
  110. params.Add(jsonutils.NewString(opts.AllocPolicy), "alloc_policy")
  111. }
  112. if len(opts.Desc) > 0 {
  113. params.Add(jsonutils.NewString(opts.Desc), "description")
  114. }
  115. if len(opts.BgpType) > 0 {
  116. params.Add(jsonutils.NewString(opts.BgpType), "bgp_type")
  117. }
  118. if opts.IsAutoAlloc != nil {
  119. params.Add(jsonutils.NewBool(*opts.IsAutoAlloc), "is_auto_alloc")
  120. }
  121. return params, nil
  122. }
  123. type NetworkUpdateOptions struct {
  124. options.BaseUpdateOptions
  125. StartIp string `help:"Start ip"`
  126. EndIp string `help:"end ip"`
  127. NetMask int64 `help:"Netmask"`
  128. Gateway string `help:"IP of gateway"`
  129. StartIp6 string `help:"IPv6 start ip"`
  130. EndIp6 string `help:"IPv6 end ip"`
  131. NetMask6 int64 `help:"IPv6 netmask"`
  132. Gateway6 string `help:"IPv6 gateway"`
  133. Dns string `help:"IP of DNS server"`
  134. Domain string `help:"Domain"`
  135. Dhcp string `help:"DHCP server IP"`
  136. Ntp string `help:"Ntp server domain names"`
  137. VlanId int64 `help:"Vlan ID"`
  138. ExternalId string `help:"External ID"`
  139. AllocPolicy string `help:"Address allocation policy" choices:"none|stepdown|stepup|random"`
  140. IsAutoAlloc *bool `help:"Add network into auto-allocation pool" negative:"no_auto_alloc"`
  141. ServerType string `help:"specify network server_type" choices:"baremetal|container|guest|pxe|ipmi|eip|hostlocal"`
  142. }
  143. func (opts *NetworkUpdateOptions) Params() (jsonutils.JSONObject, error) {
  144. params := jsonutils.NewDict()
  145. if len(opts.Name) > 0 {
  146. params.Add(jsonutils.NewString(opts.Name), "name")
  147. }
  148. if len(opts.Desc) > 0 {
  149. params.Add(jsonutils.NewString(opts.Desc), "description")
  150. }
  151. if len(opts.StartIp) > 0 {
  152. params.Add(jsonutils.NewString(opts.StartIp), "guest_ip_start")
  153. }
  154. if len(opts.EndIp) > 0 {
  155. params.Add(jsonutils.NewString(opts.EndIp), "guest_ip_end")
  156. }
  157. if opts.NetMask > 0 {
  158. params.Add(jsonutils.NewInt(opts.NetMask), "guest_ip_mask")
  159. }
  160. if len(opts.Gateway) > 0 {
  161. params.Add(jsonutils.NewString(opts.Gateway), "guest_gateway")
  162. }
  163. if len(opts.StartIp6) > 0 {
  164. params.Add(jsonutils.NewString(opts.StartIp6), "guest_ip6_start")
  165. }
  166. if len(opts.EndIp6) > 0 {
  167. params.Add(jsonutils.NewString(opts.EndIp6), "guest_ip6_end")
  168. }
  169. if opts.NetMask6 > 0 {
  170. params.Add(jsonutils.NewInt(opts.NetMask6), "guest_ip6_mask")
  171. }
  172. if len(opts.Gateway6) > 0 {
  173. params.Add(jsonutils.NewString(opts.Gateway6), "guest_gateway6")
  174. }
  175. if len(opts.Dns) > 0 {
  176. if opts.Dns == "none" {
  177. params.Add(jsonutils.NewString(""), "guest_dns")
  178. } else {
  179. params.Add(jsonutils.NewString(opts.Dns), "guest_dns")
  180. }
  181. }
  182. if len(opts.Domain) > 0 {
  183. if opts.Domain == "none" {
  184. params.Add(jsonutils.NewString(""), "guest_domain")
  185. } else {
  186. params.Add(jsonutils.NewString(opts.Domain), "guest_domain")
  187. }
  188. }
  189. if len(opts.Dhcp) > 0 {
  190. if opts.Dhcp == "none" {
  191. params.Add(jsonutils.NewString(""), "guest_dhcp")
  192. } else {
  193. params.Add(jsonutils.NewString(opts.Dhcp), "guest_dhcp")
  194. }
  195. }
  196. if len(opts.Ntp) > 0 {
  197. if opts.Ntp == "none" {
  198. params.Add(jsonutils.NewString(""), "guest_ntp")
  199. } else {
  200. params.Add(jsonutils.NewString(opts.Ntp), "guest_ntp")
  201. }
  202. }
  203. if opts.VlanId > 0 {
  204. params.Add(jsonutils.NewInt(opts.VlanId), "vlan_id")
  205. }
  206. if len(opts.ExternalId) > 0 {
  207. params.Add(jsonutils.NewString(opts.ExternalId), "external_id")
  208. }
  209. if len(opts.AllocPolicy) > 0 {
  210. params.Add(jsonutils.NewString(opts.AllocPolicy), "alloc_policy")
  211. }
  212. if opts.IsAutoAlloc != nil {
  213. params.Add(jsonutils.NewBool(*opts.IsAutoAlloc), "is_auto_alloc")
  214. }
  215. if len(opts.ServerType) > 0 {
  216. params.Add(jsonutils.NewString(opts.ServerType), "server_type")
  217. }
  218. if params.Size() == 0 {
  219. return nil, shell.InvalidUpdateError()
  220. }
  221. return params, nil
  222. }
  223. type NetworkIdOptions struct {
  224. ID string `help:"ID or Name of the network to show"`
  225. }
  226. func (opts *NetworkIdOptions) GetId() string {
  227. return opts.ID
  228. }
  229. func (opts *NetworkIdOptions) Params() (jsonutils.JSONObject, error) {
  230. return nil, nil
  231. }
  232. type NetworkIpMacIdOptions struct {
  233. ID string `help:"ID or Name of the network_ip_mac to show"`
  234. }
  235. func (opts *NetworkIpMacIdOptions) GetId() string {
  236. return opts.ID
  237. }
  238. func (opts *NetworkIpMacIdOptions) Params() (jsonutils.JSONObject, error) {
  239. return nil, nil
  240. }
  241. type NetworkIpMacListOptions struct {
  242. options.BaseListOptions
  243. Network string `help:"search networks" json:"network_id"`
  244. MacAddr []string `help:"search by mac addr"`
  245. IpAddr []string `help:"search by ip addr"`
  246. }
  247. func (opts *NetworkIpMacListOptions) Params() (jsonutils.JSONObject, error) {
  248. return options.ListStructToParams(opts)
  249. }
  250. type NetworkIpMacUpdateOptions struct {
  251. ID string `help:"ID or Name of resource to update"`
  252. MacAddr string `help:"update mac addr"`
  253. IpAddr string `help:"update ip addr"`
  254. }
  255. func (opts *NetworkIpMacUpdateOptions) GetId() string {
  256. return opts.ID
  257. }
  258. func (opts *NetworkIpMacUpdateOptions) Params() (jsonutils.JSONObject, error) {
  259. return options.ListStructToParams(opts)
  260. }
  261. type NetworkIpMacCreateOptions struct {
  262. NETWORK string `help:"network id" json:"network_id"`
  263. MACADDR string `help:"mac address" json:"mac_addr"`
  264. IPADDR string `help:"ip address" json:"ip_addr"`
  265. }
  266. func (opts *NetworkIpMacCreateOptions) Params() (jsonutils.JSONObject, error) {
  267. if opts.NETWORK == "" {
  268. return nil, errors.Errorf("missing network params")
  269. }
  270. if opts.MACADDR == "" {
  271. return nil, errors.Errorf("missing mac_addr params")
  272. }
  273. if opts.IPADDR == "" {
  274. return nil, errors.Errorf("missing ip_addr params")
  275. }
  276. return options.ListStructToParams(opts)
  277. }
  278. type NetworkSwitchWireOptions struct {
  279. ID string `help:"ID or Name of resource to update"`
  280. api.NetworkSwitchWireInput
  281. }
  282. func (opts *NetworkSwitchWireOptions) GetId() string {
  283. return opts.ID
  284. }
  285. func (opts *NetworkSwitchWireOptions) Params() (jsonutils.JSONObject, error) {
  286. return jsonutils.Marshal(opts), nil
  287. }
  288. type NetworkSyncAdditionalWiresOptions struct {
  289. ID string `help:"ID or Name of resource to update"`
  290. api.NetworSyncAdditionalWiresInput
  291. }
  292. func (opts *NetworkSyncAdditionalWiresOptions) GetId() string {
  293. return opts.ID
  294. }
  295. func (opts *NetworkSyncAdditionalWiresOptions) Params() (jsonutils.JSONObject, error) {
  296. return jsonutils.Marshal(opts), nil
  297. }