mod_reservedips.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "strings"
  17. "sync"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  21. "yunion.io/x/onecloud/pkg/mcclient/modules"
  22. )
  23. type ReservedIPManager struct {
  24. modulebase.ResourceManager
  25. }
  26. var (
  27. ReservedIPs ReservedIPManager
  28. )
  29. func (this *ReservedIPManager) DoBatchReleaseReservedIps(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  30. // params format:
  31. // {
  32. // "ips": ["1.2.3.4", "1.1.1.1"],
  33. // }
  34. ret := jsonutils.NewDict()
  35. _ips, e := params.GetArray("ips")
  36. // get ips
  37. if e != nil {
  38. return ret, e
  39. }
  40. ips := make([]string, 0)
  41. for _, u := range _ips {
  42. ip, _ := u.GetString()
  43. ips = append(ips, ip)
  44. }
  45. // filter ip and network pairs.
  46. originFilter, _ := params.Get("query")
  47. if originFilter == nil {
  48. originFilter = jsonutils.NewDict()
  49. }
  50. ipFilterOps := originFilter.(*jsonutils.JSONDict)
  51. arr := jsonutils.NewArray()
  52. filterCondition := "ip_addr.in(" + strings.Join(ips, ",") + ")"
  53. arr.Add(jsonutils.NewString(filterCondition))
  54. ipFilterOps.Add(arr, "filter")
  55. ipFilterOps.Add(jsonutils.NewInt(int64(1024)), "limit")
  56. result, err := ReservedIPs.List(s, ipFilterOps)
  57. if err != nil {
  58. return ret, err
  59. }
  60. if len(result.Data) < 1 {
  61. return ret, nil
  62. }
  63. // release ips within Goroutines
  64. var wg sync.WaitGroup
  65. wg.Add(len(result.Data))
  66. releaseIPStatus := jsonutils.NewArray()
  67. for _, res := range result.Data {
  68. networkId, _ := res.GetString("network_id")
  69. ip_addr, _ := res.GetString("ip_addr")
  70. go func(networkId, ip_addr string) {
  71. defer wg.Done()
  72. releaseOptionParams := jsonutils.NewDict()
  73. releaseOptionParams.Add(jsonutils.NewString(ip_addr), "ip")
  74. Networks.PerformAction(s, networkId, "release-reserved-ip", releaseOptionParams)
  75. if err != nil {
  76. releaseIPStatus.Add(jsonutils.NewString("[Error]" + networkId + " " + ip_addr))
  77. }
  78. }(networkId, ip_addr)
  79. }
  80. wg.Wait()
  81. ret.Add(releaseIPStatus, "ret")
  82. return ret, nil
  83. }
  84. func init() {
  85. ReservedIPs = ReservedIPManager{modules.NewComputeManager("reservedip", "reservedips",
  86. []string{},
  87. []string{"Id", "Network_ID", "Network", "IP_addr", "Notes", "Expired_At", "Expired", "Status"})}
  88. modules.RegisterCompute(&ReservedIPs)
  89. }