reservedips.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/onecloud/pkg/mcclient"
  18. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. func init() {
  22. type NetworkReserveIPOptions struct {
  23. NETWORK string `help:"IP or name of network"`
  24. NOTES string `help:"Why reserve this IP"`
  25. IPS []string `help:"IPs to reserve"`
  26. Duration string `help:"reservation duration, e.g. 1I, 1H, 2M"`
  27. Status string `help:"ip status"`
  28. }
  29. R(&NetworkReserveIPOptions{}, "network-reserve-ip", "Reserve an IP address from pool", func(s *mcclient.ClientSession, args *NetworkReserveIPOptions) error {
  30. params := jsonutils.NewDict()
  31. params.Add(jsonutils.NewStringArray(args.IPS), "ips")
  32. params.Add(jsonutils.NewString(args.NOTES), "notes")
  33. if len(args.Duration) > 0 {
  34. params.Add(jsonutils.NewString(args.Duration), "duration")
  35. }
  36. if len(args.Status) > 0 {
  37. params.Add(jsonutils.NewString(args.Status), "status")
  38. }
  39. net, err := modules.Networks.PerformAction(s, args.NETWORK, "reserve-ip", params)
  40. if err != nil {
  41. return err
  42. }
  43. printObject(net)
  44. return nil
  45. })
  46. type NetworkReleaseReservedIPOptions struct {
  47. NETWORK string `help:"IP or name of network"`
  48. IP string `help:"IP to release"`
  49. }
  50. R(&NetworkReleaseReservedIPOptions{}, "network-release-reserved-ip", "Release a reserved IP into pool", func(s *mcclient.ClientSession, args *NetworkReleaseReservedIPOptions) error {
  51. params := jsonutils.NewDict()
  52. params.Add(jsonutils.NewString(args.IP), "ip")
  53. net, err := modules.Networks.PerformAction(s, args.NETWORK, "release-reserved-ip", params)
  54. if err != nil {
  55. return err
  56. }
  57. printObject(net)
  58. return nil
  59. })
  60. type ReservedIPListOptions struct {
  61. options.BaseListOptions
  62. Network string `help:"Network filter"`
  63. All bool `help:"show expired reserved ips"`
  64. }
  65. R(&ReservedIPListOptions{}, "reserved-ip-list", "Show all reserved IPs for any network", func(s *mcclient.ClientSession, args *ReservedIPListOptions) error {
  66. var params *jsonutils.JSONDict
  67. {
  68. var err error
  69. params, err = args.BaseListOptions.Params()
  70. if err != nil {
  71. return err
  72. }
  73. }
  74. if len(args.Network) > 0 {
  75. params.Add(jsonutils.NewString(args.Network), "network")
  76. }
  77. if args.All {
  78. params.Add(jsonutils.JSONTrue, "all")
  79. }
  80. result, err := modules.ReservedIPs.List(s, params)
  81. if err != nil {
  82. return err
  83. }
  84. printList(result, modules.ReservedIPs.GetColumns(s))
  85. return nil
  86. })
  87. type ReservedIpUpdateOptions struct {
  88. ID string `help:"ID of reserved ip" json:"-"`
  89. Notes string `help:"notes"`
  90. }
  91. R(&ReservedIpUpdateOptions{}, "reserved-ip-update", "update reserved ip notes", func(s *mcclient.ClientSession, args *ReservedIpUpdateOptions) error {
  92. params := jsonutils.Marshal(args)
  93. result, err := modules.ReservedIPs.Update(s, args.ID, params)
  94. if err != nil {
  95. return err
  96. }
  97. printObject(result)
  98. return nil
  99. })
  100. }