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