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