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