loadbalanceracl.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 aliyun
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/log"
  18. "yunion.io/x/cloudmux/pkg/apis"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. )
  22. type AclEntrys struct {
  23. AclEntry []AclEntry
  24. }
  25. type AclEntry struct {
  26. AclEntryComment string
  27. AclEntryIP string
  28. }
  29. type SLoadbalancerAcl struct {
  30. multicloud.SResourceBase
  31. AliyunTags
  32. region *SRegion
  33. AclId string
  34. AclName string
  35. AclEntrys AclEntrys
  36. }
  37. func (acl *SLoadbalancerAcl) GetName() string {
  38. return acl.AclName
  39. }
  40. func (acl *SLoadbalancerAcl) GetId() string {
  41. return acl.AclId
  42. }
  43. func (acl *SLoadbalancerAcl) GetGlobalId() string {
  44. return acl.AclId
  45. }
  46. func (acl *SLoadbalancerAcl) GetStatus() string {
  47. return apis.STATUS_AVAILABLE
  48. }
  49. func (acl *SLoadbalancerAcl) Refresh() error {
  50. loadbalancerAcl, err := acl.region.GetLoadbalancerAclDetail(acl.AclId)
  51. if err != nil {
  52. return err
  53. }
  54. return jsonutils.Update(acl, loadbalancerAcl)
  55. }
  56. func (acl *SLoadbalancerAcl) GetAclEntries() []cloudprovider.SLoadbalancerAccessControlListEntry {
  57. detail, err := acl.region.GetLoadbalancerAclDetail(acl.AclId)
  58. if err != nil {
  59. log.Errorf("GetLoadbalancerAclDetail %s failed: %v", acl.AclId, err)
  60. return nil
  61. }
  62. entrys := []cloudprovider.SLoadbalancerAccessControlListEntry{}
  63. for _, entry := range detail.AclEntrys.AclEntry {
  64. entrys = append(entrys, cloudprovider.SLoadbalancerAccessControlListEntry{CIDR: entry.AclEntryIP, Comment: entry.AclEntryComment})
  65. }
  66. return entrys
  67. }
  68. func (region *SRegion) UpdateAclName(aclId, name string) error {
  69. params := map[string]string{}
  70. params["RegionId"] = region.RegionId
  71. params["AclId"] = aclId
  72. params["AclName"] = name
  73. _, err := region.lbRequest("SetAccessControlListAttribute", params)
  74. return err
  75. }
  76. func (region *SRegion) RemoveAccessControlListEntry(aclId string, data jsonutils.JSONObject) error {
  77. params := map[string]string{}
  78. params["RegionId"] = region.RegionId
  79. params["AclId"] = aclId
  80. params["AclEntrys"] = data.String()
  81. _, err := region.lbRequest("RemoveAccessControlListEntry", params)
  82. return err
  83. }
  84. func (acl *SLoadbalancerAcl) Delete() error {
  85. params := map[string]string{}
  86. params["RegionId"] = acl.region.RegionId
  87. params["AclId"] = acl.AclId
  88. _, err := acl.region.lbRequest("DeleteAccessControlList", params)
  89. return err
  90. }
  91. func (region *SRegion) GetLoadbalancerAclDetail(aclId string) (*SLoadbalancerAcl, error) {
  92. params := map[string]string{}
  93. params["RegionId"] = region.RegionId
  94. params["AclId"] = aclId
  95. body, err := region.lbRequest("DescribeAccessControlListAttribute", params)
  96. if err != nil {
  97. return nil, err
  98. }
  99. detail := SLoadbalancerAcl{region: region}
  100. return &detail, body.Unmarshal(&detail)
  101. }
  102. func (region *SRegion) GetLoadBalancerAcls() ([]SLoadbalancerAcl, error) {
  103. params := map[string]string{}
  104. params["RegionId"] = region.RegionId
  105. body, err := region.lbRequest("DescribeAccessControlLists", params)
  106. if err != nil {
  107. return nil, err
  108. }
  109. acls := []SLoadbalancerAcl{}
  110. return acls, body.Unmarshal(&acls, "Acls", "Acl")
  111. }
  112. func (acl *SLoadbalancerAcl) Sync(_acl *cloudprovider.SLoadbalancerAccessControlList) error {
  113. if acl.AclName != _acl.Name {
  114. if err := acl.region.UpdateAclName(acl.AclId, _acl.Name); err != nil {
  115. return err
  116. }
  117. }
  118. entrys := jsonutils.NewArray()
  119. for _, entry := range acl.AclEntrys.AclEntry {
  120. entrys.Add(jsonutils.Marshal(map[string]string{"entry": entry.AclEntryIP, "comment": entry.AclEntryComment}))
  121. }
  122. if entrys.Length() > 0 {
  123. if err := acl.region.RemoveAccessControlListEntry(acl.AclId, entrys); err != nil && !isError(err, "Acl does not have any entry") {
  124. return err
  125. }
  126. }
  127. if len(_acl.Entrys) > 0 {
  128. return acl.region.AddAccessControlListEntry(acl.AclId, _acl.Entrys)
  129. }
  130. return nil
  131. }
  132. func (acl *SLoadbalancerAcl) GetProjectId() string {
  133. return ""
  134. }