yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/loadbalancer_acl.go (about) 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 15 package huawei 16 17 import ( 18 "net/url" 19 "strings" 20 21 "yunion.io/x/jsonutils" 22 23 api "yunion.io/x/cloudmux/pkg/apis/compute" 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 "yunion.io/x/cloudmux/pkg/multicloud" 26 ) 27 28 type SElbACL struct { 29 multicloud.SResourceBase 30 HuaweiTags 31 region *SRegion 32 33 ID string `json:"id"` 34 ListenerID string `json:"listener_id"` 35 TenantID string `json:"tenant_id"` 36 EnableWhitelist bool `json:"enable_whitelist"` 37 Whitelist string `json:"whitelist"` 38 } 39 40 func (self *SElbACL) GetAclListenerID() string { 41 return self.ListenerID 42 } 43 44 func (self *SElbACL) GetId() string { 45 return self.ID 46 } 47 48 func (self *SElbACL) GetName() string { 49 return self.ID 50 } 51 52 func (self *SElbACL) GetGlobalId() string { 53 return self.GetId() 54 } 55 56 func (self *SElbACL) GetStatus() string { 57 if self.EnableWhitelist { 58 return api.LB_BOOL_ON 59 } 60 61 return api.LB_BOOL_OFF 62 } 63 64 func (self *SElbACL) Refresh() error { 65 acl, err := self.region.GetLoadBalancerAcl(self.GetId()) 66 if err != nil { 67 return err 68 } 69 return jsonutils.Update(self, acl) 70 } 71 72 func (self *SElbACL) IsEmulated() bool { 73 return false 74 } 75 76 func (self *SElbACL) GetProjectId() string { 77 return "" 78 } 79 80 func (self *SElbACL) GetAclEntries() []cloudprovider.SLoadbalancerAccessControlListEntry { 81 ret := []cloudprovider.SLoadbalancerAccessControlListEntry{} 82 for _, cidr := range strings.Split(self.Whitelist, ",") { 83 ret = append(ret, cloudprovider.SLoadbalancerAccessControlListEntry{CIDR: cidr}) 84 } 85 86 return ret 87 } 88 89 func (self *SElbACL) Sync(acl *cloudprovider.SLoadbalancerAccessControlList) error { 90 whiteList := "" 91 cidrs := []string{} 92 for _, entry := range acl.Entrys { 93 cidrs = append(cidrs, entry.CIDR) 94 } 95 whiteList = strings.Join(cidrs, ",") 96 params := map[string]interface{}{ 97 "whitelist": whiteList, 98 "enable_whitelist": acl.AccessControlEnable, 99 } 100 _, err := self.region.lbUpdate("elb/whitelists/"+self.GetId(), map[string]interface{}{"whitelist": params}) 101 return err 102 } 103 104 func (self *SElbACL) Delete() error { 105 _, err := self.region.lbDelete("elb/whitelists/" + self.GetId()) 106 return err 107 } 108 109 func (self *SRegion) GetLoadBalancerAcl(aclId string) (*SElbACL, error) { 110 resp, err := self.lbGet("elb/whitelists/" + aclId) 111 if err != nil { 112 return nil, err 113 } 114 ret := &SElbACL{region: self} 115 return ret, resp.Unmarshal(ret, "whitelist") 116 } 117 118 // https://support.huaweicloud.com/api-elb/zh-cn_topic_0096561582.html 119 func (self *SRegion) GetLoadBalancerAcls(listenerId string) ([]SElbACL, error) { 120 query := url.Values{} 121 if len(listenerId) > 0 { 122 query.Set("listener_id", listenerId) 123 } 124 resp, err := self.lbList("elb/whitelists", query) 125 if err != nil { 126 return nil, err 127 } 128 ret := []SElbACL{} 129 return ret, resp.Unmarshal(&ret, "whitelists") 130 } 131 132 func (self *SRegion) CreateLoadBalancerAcl(acl *cloudprovider.SLoadbalancerAccessControlList) (*SElbACL, error) { 133 params := map[string]interface{}{ 134 "listener_id": acl.ListenerId, 135 } 136 if len(acl.Entrys) > 0 { 137 whitelist := []string{} 138 for i := range acl.Entrys { 139 whitelist = append(whitelist, acl.Entrys[i].CIDR) 140 } 141 params["enable_whitelist"] = acl.AccessControlEnable 142 params["whitelist"] = strings.Join(whitelist, ",") 143 } else { 144 params["enable_whitelist"] = false 145 } 146 resp, err := self.lbCreate("elb/whitelists", map[string]interface{}{"whitelist": params}) 147 if err != nil { 148 return nil, err 149 } 150 ret := &SElbACL{region: self} 151 return ret, resp.Unmarshal(ret, "whitelist") 152 }