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