yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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 hcso
    16  
    17  import (
    18  	"strings"
    19  
    20  	"yunion.io/x/jsonutils"
    21  
    22  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  	"yunion.io/x/cloudmux/pkg/multicloud/huawei"
    26  )
    27  
    28  type SElbACL struct {
    29  	multicloud.SResourceBase
    30  	huawei.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.GetLoadBalancerAclById(self.GetId())
    66  	if err != nil {
    67  		return err
    68  	}
    69  
    70  	err = jsonutils.Update(self, acl)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  func (self *SElbACL) IsEmulated() bool {
    79  	return false
    80  }
    81  
    82  func (self *SElbACL) GetProjectId() string {
    83  	return ""
    84  }
    85  
    86  func (self *SElbACL) GetAclEntries() []cloudprovider.SLoadbalancerAccessControlListEntry {
    87  	ret := []cloudprovider.SLoadbalancerAccessControlListEntry{}
    88  	for _, cidr := range strings.Split(self.Whitelist, ",") {
    89  		ret = append(ret, cloudprovider.SLoadbalancerAccessControlListEntry{CIDR: cidr})
    90  	}
    91  
    92  	return ret
    93  }
    94  
    95  func (self *SElbACL) Sync(acl *cloudprovider.SLoadbalancerAccessControlList) error {
    96  	whiteList := ""
    97  	cidrs := []string{}
    98  	for _, entry := range acl.Entrys {
    99  		cidrs = append(cidrs, entry.CIDR)
   100  	}
   101  
   102  	whiteList = strings.Join(cidrs, ",")
   103  
   104  	params := jsonutils.NewDict()
   105  	whiteListObj := jsonutils.NewDict()
   106  	whiteListObj.Set("whitelist", jsonutils.NewString(whiteList))
   107  	whiteListObj.Set("enable_whitelist", jsonutils.NewBool(acl.AccessControlEnable))
   108  	params.Set("whitelist", whiteListObj)
   109  	return DoUpdate(self.region.ecsClient.ElbWhitelist.Update, self.GetId(), params, nil)
   110  }
   111  
   112  func (self *SElbACL) Delete() error {
   113  	return DoDelete(self.region.ecsClient.ElbWhitelist.Delete, self.GetId(), nil, nil)
   114  }