yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/loadbalancer_listener_rule.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 hcs
    16  
    17  import (
    18  	"context"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/log"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/multicloud"
    25  	"yunion.io/x/cloudmux/pkg/multicloud/huawei"
    26  )
    27  
    28  type SElbListenerPolicy struct {
    29  	multicloud.SResourceBase
    30  	multicloud.SLoadbalancerRedirectBase
    31  	huawei.HuaweiTags
    32  	region   *SRegion
    33  	lb       *SLoadbalancer
    34  	listener *SElbListener
    35  
    36  	RedirectPoolId     string  `json:"redirect_pool_id"`
    37  	RedirectListenerId *string `json:"redirect_listener_id"`
    38  	Description        string  `json:"description"`
    39  	AdminStateUp       bool    `json:"admin_state_up"`
    40  	Rules              []Rule  `json:"rules"`
    41  	TenantId           string  `json:"tenant_id"`
    42  	ProjectId          string  `json:"project_id"`
    43  	ListenerId         string  `json:"listener_id"`
    44  	RedirectURL        *string `json:"redirect_url"`
    45  	ProvisioningStatus string  `json:"provisioning_status"`
    46  	Action             string  `json:"action"`
    47  	Position           int64   `json:"position"`
    48  	Id                 string  `json:"id"`
    49  	Name               string  `json:"name"`
    50  }
    51  
    52  type Rule struct {
    53  	Id string `json:"id"`
    54  }
    55  
    56  type SElbListenerPolicyRule struct {
    57  	region *SRegion
    58  	policy *SElbListenerPolicy
    59  
    60  	CompareType        string      `json:"compare_type"`
    61  	ProvisioningStatus string      `json:"provisioning_status"`
    62  	AdminStateUp       bool        `json:"admin_state_up"`
    63  	TenantId           string      `json:"tenant_id"`
    64  	ProjectId          string      `json:"project_id"`
    65  	Invert             bool        `json:"invert"`
    66  	Value              string      `json:"value"`
    67  	Key                interface{} `json:"key"`
    68  	Type               string      `json:"type"`
    69  	Id                 string      `json:"id"`
    70  }
    71  
    72  func (self *SElbListenerPolicy) GetId() string {
    73  	return self.Id
    74  }
    75  
    76  func (self *SElbListenerPolicy) GetName() string {
    77  	return self.Name
    78  }
    79  
    80  func (self *SElbListenerPolicy) GetGlobalId() string {
    81  	return self.GetId()
    82  }
    83  
    84  // 负载均衡没有启用禁用操作
    85  func (self *SElbListenerPolicy) GetStatus() string {
    86  	return api.LB_STATUS_ENABLED
    87  }
    88  
    89  func (self *SElbListenerPolicy) Refresh() error {
    90  	res := "lbaas/l7policies/" + self.Id
    91  	ret := &SElbListenerPolicy{}
    92  	err := self.lb.region.lbGet(res, ret)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	return jsonutils.Update(self, ret)
    97  }
    98  
    99  func (self *SElbListenerPolicy) IsDefault() bool {
   100  	return false
   101  }
   102  
   103  func (self *SElbListenerPolicy) IsEmulated() bool {
   104  	return false
   105  }
   106  
   107  func (self *SElbListenerPolicy) GetProjectId() string {
   108  	return ""
   109  }
   110  
   111  func (self *SElbListenerPolicy) GetRules() ([]SElbListenerPolicyRule, error) {
   112  	ret, err := self.region.GetLoadBalancerPolicyRules(self.GetId())
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	for i := range ret {
   118  		ret[i].region = self.lb.region
   119  		ret[i].policy = self
   120  	}
   121  
   122  	return ret, nil
   123  }
   124  
   125  func (self *SElbListenerPolicy) GetDomain() string {
   126  	rules, err := self.GetRules()
   127  	if err != nil {
   128  		log.Errorf("loadbalancer rule GetDomain %s", err)
   129  	}
   130  
   131  	for i := range rules {
   132  		if rules[i].Type == "HOST_NAME" {
   133  			return rules[i].Value
   134  		}
   135  	}
   136  
   137  	return ""
   138  }
   139  
   140  func (self *SElbListenerPolicy) GetCondition() string {
   141  	return ""
   142  }
   143  
   144  func (self *SElbListenerPolicy) GetPath() string {
   145  	rules, err := self.GetRules()
   146  	if err != nil {
   147  		log.Errorf("loadbalancer rule GetPath %s", err)
   148  	}
   149  
   150  	for i := range rules {
   151  		if rules[i].Type == "PATH" {
   152  			return rules[i].Value
   153  		}
   154  	}
   155  
   156  	return ""
   157  }
   158  
   159  func (self *SElbListenerPolicy) GetBackendGroupId() string {
   160  	return self.RedirectPoolId
   161  }
   162  
   163  func (self *SElbListenerPolicy) Delete(ctx context.Context) error {
   164  	return self.region.DeleteLoadBalancerPolicy(self.GetId())
   165  }
   166  
   167  func (self *SRegion) DeleteLoadBalancerPolicy(policyId string) error {
   168  	return self.lbDelete("lbaas/l7policies/" + policyId)
   169  }