loadbalanceracl.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 openstack
  15. import (
  16. "fmt"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/cloudmux/pkg/apis"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type AclEntrys struct {
  25. AclEntry []AclEntry
  26. }
  27. type AclEntry struct {
  28. AclEntryComment string
  29. AclEntryIP string
  30. }
  31. type SLoadbalancerAcl struct {
  32. multicloud.SResourceBase
  33. OpenStackTags
  34. listener *SLoadbalancerListener
  35. }
  36. func (acl *SLoadbalancerAcl) GetName() string {
  37. return acl.listener.Name + "AllowedCidrs"
  38. }
  39. func (acl *SLoadbalancerAcl) GetId() string {
  40. return acl.listener.ID
  41. }
  42. func (acl *SLoadbalancerAcl) GetGlobalId() string {
  43. return acl.listener.ID
  44. }
  45. func (acl *SLoadbalancerAcl) GetStatus() string {
  46. return apis.STATUS_AVAILABLE
  47. }
  48. func (acl *SLoadbalancerAcl) Refresh() error {
  49. return acl.listener.Refresh()
  50. }
  51. func (acl *SLoadbalancerAcl) GetAclEntries() []cloudprovider.SLoadbalancerAccessControlListEntry {
  52. aclEntrys := []cloudprovider.SLoadbalancerAccessControlListEntry{}
  53. for i := 0; i < len(acl.listener.AllowedCidrs); i++ {
  54. aclEntry := cloudprovider.SLoadbalancerAccessControlListEntry{}
  55. aclEntry.CIDR = acl.listener.AllowedCidrs[i]
  56. aclEntry.Comment = "AllowedCidr"
  57. aclEntrys = append(aclEntrys, aclEntry)
  58. }
  59. return aclEntrys
  60. }
  61. func (region *SRegion) UpdateLoadbalancerListenerAllowedCidrs(listenerId string, cidrs []string) error {
  62. params := jsonutils.NewDict()
  63. listenerParam := jsonutils.NewDict()
  64. listenerParam.Add(jsonutils.NewStringArray(cidrs), "allowed_cidrs")
  65. params.Add(listenerParam, "listener")
  66. _, err := region.lbUpdate(fmt.Sprintf("/v2/lbaas/listeners/%s", listenerId), params)
  67. if err != nil {
  68. return errors.Wrapf(err, `region.lbUpdate(/v2/lbaas/listeners/%s, params)`, listenerId)
  69. }
  70. return nil
  71. }
  72. func (acl *SLoadbalancerAcl) Delete() error {
  73. // ensure listener status
  74. err := waitLbResStatus(acl.listener, 10*time.Second, 8*time.Minute)
  75. if err != nil {
  76. return errors.Wrap(err, `waitLbResStatus(acl.listener, 10*time.Second, 8*time.Minute)`)
  77. }
  78. err = acl.listener.region.UpdateLoadbalancerListenerAllowedCidrs(acl.listener.ID, []string{})
  79. if err != nil {
  80. return errors.Wrap(err, `acl.listener.region.UpdateLoadbalancerListenerAllowedCidrs(acl.listener.ID, []string{})`)
  81. }
  82. err = waitLbResStatus(acl.listener, 10*time.Second, 8*time.Minute)
  83. if err != nil {
  84. return errors.Wrap(err, `waitLbResStatus(acl.listener, 10*time.Second, 8*time.Minute)`)
  85. }
  86. return nil
  87. }
  88. func (region *SRegion) GetLoadbalancerAclDetail(aclId string) (*SLoadbalancerAcl, error) {
  89. listener, err := region.GetLoadbalancerListenerbyId(aclId)
  90. if err != nil {
  91. return nil, errors.Wrapf(err, "region.GetLoadbalancerListenerbyId(%s)", aclId)
  92. }
  93. acl := SLoadbalancerAcl{}
  94. acl.listener = listener
  95. return &acl, nil
  96. }
  97. func (region *SRegion) GetLoadBalancerAcls() ([]SLoadbalancerAcl, error) {
  98. listeners, err := region.GetLoadbalancerListeners()
  99. if err != nil {
  100. return nil, errors.Wrap(err, "region.GetLoadbalancerListeners()")
  101. }
  102. acls := []SLoadbalancerAcl{}
  103. for i := 0; i < len(listeners); i++ {
  104. if len(listeners[i].AllowedCidrs) < 1 {
  105. continue
  106. }
  107. acl := new(SLoadbalancerAcl)
  108. acl.listener = &listeners[i]
  109. acls = append(acls, *acl)
  110. }
  111. return acls, nil
  112. }
  113. func (region *SRegion) CreateLoadBalancerAcl(acl *cloudprovider.SLoadbalancerAccessControlList) (*SLoadbalancerAcl, error) {
  114. return nil, cloudprovider.ErrNotSupported
  115. }
  116. func (acl *SLoadbalancerAcl) Sync(_acl *cloudprovider.SLoadbalancerAccessControlList) error {
  117. // ensure listener status
  118. err := waitLbResStatus(acl.listener, 10*time.Second, 8*time.Minute)
  119. if err != nil {
  120. return errors.Wrap(err, "waitLbResStatus(acl.listener, 10*time.Second, 8*time.Minute)")
  121. }
  122. cidrs := []string{}
  123. for i := 0; i < len(_acl.Entrys); i++ {
  124. cidrs = append(cidrs, _acl.Entrys[i].CIDR)
  125. }
  126. err = acl.listener.region.UpdateLoadbalancerListenerAllowedCidrs(acl.listener.ID, cidrs)
  127. if err != nil {
  128. return errors.Wrapf(err, "UpdateLoadbalancerListenerAllowedCidrs(%s, cidrs)", acl.listener.ID)
  129. }
  130. err = waitLbResStatus(acl.listener, 10*time.Second, 8*time.Minute)
  131. if err != nil {
  132. return errors.Wrap(err, "waitLbResStatus(acl.listener, 10*time.Second, 8*time.Minute)")
  133. }
  134. return nil
  135. }
  136. func (acl *SLoadbalancerAcl) GetProjectId() string {
  137. return acl.listener.ProjectID
  138. }