yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/elasticcache_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 apsara
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  )
    26  
    27  type SElasticcacheAcl struct {
    28  	multicloud.SElasticcacheAclBase
    29  	ApsaraTags
    30  
    31  	cacheDB *SElasticcache
    32  
    33  	SecurityIPList           string `json:"SecurityIpList"`
    34  	SecurityIPGroupAttribute string `json:"SecurityIpGroupAttribute"`
    35  	SecurityIPGroupName      string `json:"SecurityIpGroupName"`
    36  }
    37  
    38  func (self *SElasticcacheAcl) GetId() string {
    39  	return fmt.Sprintf("%s/%s", self.cacheDB.GetId(), self.SecurityIPGroupName)
    40  }
    41  
    42  func (self *SElasticcacheAcl) GetName() string {
    43  	return self.SecurityIPGroupName
    44  }
    45  
    46  func (self *SElasticcacheAcl) GetGlobalId() string {
    47  	return self.GetId()
    48  }
    49  
    50  func (self *SElasticcacheAcl) GetStatus() string {
    51  	return api.ELASTIC_CACHE_ACL_STATUS_AVAILABLE
    52  }
    53  
    54  func (self *SElasticcacheAcl) Refresh() error {
    55  	iacl, err := self.cacheDB.GetICloudElasticcacheAcl(self.GetId())
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	err = jsonutils.Update(self, iacl.(*SElasticcacheAcl))
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  func (self *SElasticcacheAcl) GetIpList() string {
    69  	return self.SecurityIPList
    70  }
    71  
    72  // https://help.apsara.com/document_detail/61002.html?spm=a2c4g.11186623.6.764.3752782fJpbjxH
    73  func (self *SElasticcacheAcl) Delete() error {
    74  	params := make(map[string]string)
    75  	params["InstanceId"] = self.cacheDB.GetId()
    76  	params["ModifyMode"] = "Delete"
    77  	params["SecurityIpGroupName"] = self.GetName()
    78  	params["SecurityIps"] = self.SecurityIPList
    79  
    80  	err := DoAction(self.cacheDB.region.kvsRequest, "ModifySecurityIps", params, nil, nil)
    81  	if err != nil {
    82  		return errors.Wrap(err, "elasticcacheAcl.Delete")
    83  	}
    84  
    85  	return nil
    86  }
    87  
    88  // https://help.apsara.com/document_detail/61002.html?spm=a2c4g.11186623.6.764.3752782fJpbjxH
    89  func (self *SElasticcacheAcl) UpdateAcl(securityIps string) error {
    90  	return self.cacheDB.region.createAcl(self.cacheDB.GetId(), self.GetName(), securityIps)
    91  }
    92  
    93  func (self *SRegion) createAcl(instanceId, aclName, securityIps string) error {
    94  	params := make(map[string]string)
    95  	params["InstanceId"] = instanceId
    96  	params["SecurityIpGroupName"] = aclName
    97  	params["ModifyMode"] = "Cover"
    98  	params["SecurityIps"] = securityIps
    99  
   100  	err := DoAction(self.kvsRequest, "ModifySecurityIps", params, nil, nil)
   101  	if err != nil {
   102  		return errors.Wrap(err, "region.UpdateAcl")
   103  	}
   104  
   105  	return nil
   106  }