yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/loadbalancer_listenerrule.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 qcloud
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"time"
    21  
    22  	"yunion.io/x/jsonutils"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/onecloud/pkg/cloudcommon/db/lockman"
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  	"yunion.io/x/cloudmux/pkg/multicloud"
    28  )
    29  
    30  type SLBListenerRule struct {
    31  	multicloud.SResourceBase
    32  	multicloud.SLoadbalancerRedirectBase
    33  	QcloudTags
    34  	listener *SLBListener
    35  
    36  	Domain            string      `json:"Domain"`
    37  	Certificate       certificate `json:"Certificate"`
    38  	URL               string      `json:"Url"`
    39  	HealthCheck       healthCheck `json:"HealthCheck"`
    40  	LocationID        string      `json:"LocationId"`
    41  	Scheduler         string      `json:"Scheduler"`
    42  	SessionExpireTime int64       `json:"SessionExpireTime"`
    43  }
    44  
    45  // https://cloud.tencent.com/document/api/214/30688
    46  func (self *SLBListenerRule) Delete(ctx context.Context) error {
    47  	lockman.LockRawObject(ctx, "qcloud.SLBListenerRule.Delete", self.listener.lb.region.client.ownerId)
    48  	defer lockman.ReleaseRawObject(ctx, "qcloud.SLBListenerRule.Delete", self.listener.lb.region.client.ownerId)
    49  
    50  	_, err := self.listener.lb.region.DeleteLBListenerRule(self.listener.lb.GetId(), self.listener.GetId(), self.GetId())
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	return cloudprovider.WaitDeleted(self, 5*time.Second, 60*time.Second)
    56  }
    57  
    58  func (self *SLBListenerRule) GetId() string {
    59  	return self.LocationID
    60  }
    61  
    62  func (self *SLBListenerRule) GetName() string {
    63  	return self.LocationID
    64  }
    65  
    66  func (self *SLBListenerRule) GetGlobalId() string {
    67  	return self.LocationID
    68  }
    69  
    70  func (self *SLBListenerRule) GetStatus() string {
    71  	return api.LB_STATUS_ENABLED
    72  }
    73  
    74  func (self *SLBListenerRule) Refresh() error {
    75  	err := self.listener.Refresh()
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	for _, rule := range self.listener.Rules {
    81  		if rule.GetId() == self.GetId() {
    82  			rule.listener = self.listener
    83  			return jsonutils.Update(self, rule)
    84  		}
    85  	}
    86  
    87  	return cloudprovider.ErrNotFound
    88  }
    89  
    90  func (self *SLBListenerRule) IsDefault() bool {
    91  	return false
    92  }
    93  
    94  func (self *SLBListenerRule) IsEmulated() bool {
    95  	return false
    96  }
    97  
    98  func (self *SLBListenerRule) GetDomain() string {
    99  	return self.Domain
   100  }
   101  
   102  func (self *SLBListenerRule) GetCondition() string {
   103  	return ""
   104  }
   105  
   106  func (self *SLBListenerRule) GetPath() string {
   107  	return self.URL
   108  }
   109  
   110  func (self *SLBListenerRule) GetProjectId() string {
   111  	return self.listener.GetProjectId()
   112  }
   113  
   114  func (self *SLBListenerRule) GetBackendGroup() *SLBBackendGroup {
   115  	t := self.listener.GetListenerType()
   116  	if t == api.LB_LISTENER_TYPE_HTTP || t == api.LB_LISTENER_TYPE_HTTPS {
   117  		return &SLBBackendGroup{
   118  			lb:       self.listener.lb,
   119  			listener: self.listener,
   120  			rule:     self,
   121  		}
   122  	}
   123  
   124  	return nil
   125  }
   126  
   127  // 只有http、https协议监听规则有backendgroupid
   128  func (self *SLBListenerRule) GetBackendGroupId() string {
   129  	bg := self.GetBackendGroup()
   130  	if bg == nil {
   131  		return ""
   132  	}
   133  
   134  	return bg.GetId()
   135  }
   136  
   137  // https://cloud.tencent.com/document/api/214/30688
   138  // 返回requestId及error
   139  func (self *SRegion) DeleteLBListenerRule(lbid, listenerId, ruleId string) (string, error) {
   140  	if len(ruleId) == 0 {
   141  		return "", fmt.Errorf("DeleteLBListenerRule rule id should not be empty")
   142  	}
   143  	return self.DeleteLBListenerRules(lbid, listenerId, []string{ruleId})
   144  }
   145  
   146  func (self *SRegion) DeleteLBListenerRules(lbid, listenerId string, ruleIds []string) (string, error) {
   147  	if len(lbid) == 0 {
   148  		return "", fmt.Errorf("DeleteLBListenerRules loadbalancer id should not be empty")
   149  	}
   150  
   151  	if len(listenerId) == 0 {
   152  		return "", fmt.Errorf("DeleteLBListenerRules listener id should not be empty")
   153  	}
   154  
   155  	if len(ruleIds) == 0 {
   156  		return "", fmt.Errorf("DeleteLBListenerRules rule id should not be empty")
   157  	}
   158  
   159  	params := map[string]string{"LoadBalancerId": lbid, "ListenerId": listenerId}
   160  	for i, ruleId := range ruleIds {
   161  		params[fmt.Sprintf("LocationIds.%d", i)] = ruleId
   162  	}
   163  
   164  	resp, err := self.clbRequest("DeleteRule", params)
   165  	if err != nil {
   166  		return "", err
   167  	}
   168  
   169  	return resp.GetString("RequestId")
   170  }