yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/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 google
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"strings"
    21  	"time"
    22  
    23  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  )
    26  
    27  type SLoadbalancerListenerRule struct {
    28  	lbl            *SLoadbalancerListener
    29  	pathMatcher    PathMatcher
    30  	pathRule       PathRule
    31  	backendService SBackendServices
    32  
    33  	ListenerName       string `json:"listener_name"`
    34  	BackendServiceName string `json:"backend_service_name"`
    35  	Domain             string `json:"domain"`
    36  	Path               string `json:"path"`
    37  	Port               string `json:"Port"`
    38  }
    39  
    40  func (self *SLoadbalancerListenerRule) GetId() string {
    41  	return fmt.Sprintf("%s::%s::%s", self.lbl.GetGlobalId(), self.Domain, strings.Join(self.pathRule.Paths, ","))
    42  }
    43  
    44  func (self *SLoadbalancerListenerRule) GetName() string {
    45  	return fmt.Sprintf("%s::%s::%s", self.lbl.GetName(), self.Domain, strings.Join(self.pathRule.Paths, ","))
    46  }
    47  
    48  func (self *SLoadbalancerListenerRule) GetGlobalId() string {
    49  	return self.GetId()
    50  }
    51  
    52  func (self *SLoadbalancerListenerRule) GetStatus() string {
    53  	return api.LB_STATUS_ENABLED
    54  }
    55  
    56  func (self *SLoadbalancerListenerRule) Refresh() error {
    57  	return nil
    58  }
    59  
    60  func (self *SLoadbalancerListenerRule) IsEmulated() bool {
    61  	return true
    62  }
    63  
    64  func (self *SLoadbalancerListenerRule) GetCreatedAt() time.Time {
    65  	return time.Time{}
    66  }
    67  
    68  func (self *SLoadbalancerListenerRule) GetSysTags() map[string]string {
    69  	return nil
    70  }
    71  
    72  func (self *SLoadbalancerListenerRule) GetTags() (map[string]string, error) {
    73  	return nil, nil
    74  }
    75  
    76  func (self *SLoadbalancerListenerRule) SetTags(tags map[string]string, replace bool) error {
    77  	return cloudprovider.ErrNotSupported
    78  }
    79  
    80  func (self *SLoadbalancerListenerRule) GetProjectId() string {
    81  	return self.lbl.GetProjectId()
    82  }
    83  
    84  func (self *SLoadbalancerListenerRule) GetRedirect() string {
    85  	return ""
    86  }
    87  
    88  func (self *SLoadbalancerListenerRule) GetRedirectCode() int64 {
    89  	return 0
    90  }
    91  
    92  func (self *SLoadbalancerListenerRule) GetRedirectScheme() string {
    93  	return ""
    94  }
    95  
    96  func (self *SLoadbalancerListenerRule) GetRedirectHost() string {
    97  	return ""
    98  }
    99  
   100  func (self *SLoadbalancerListenerRule) GetRedirectPath() string {
   101  	return ""
   102  }
   103  
   104  func (self *SLoadbalancerListenerRule) IsDefault() bool {
   105  	return false
   106  }
   107  
   108  func (self *SLoadbalancerListenerRule) GetDomain() string {
   109  	return self.Domain
   110  }
   111  
   112  func (self *SLoadbalancerListenerRule) GetPath() string {
   113  	return self.Path
   114  }
   115  
   116  func (self *SLoadbalancerListenerRule) GetCondition() string {
   117  	return ""
   118  }
   119  
   120  func (self *SLoadbalancerListenerRule) GetBackendGroupId() string {
   121  	return self.backendService.GetGlobalId()
   122  }
   123  
   124  func (self *SLoadbalancerListenerRule) Delete(ctx context.Context) error {
   125  	return cloudprovider.ErrNotSupported
   126  }
   127  
   128  func (self *SLoadbalancerListener) GetLoadbalancerListenerRules() ([]SLoadbalancerListenerRule, error) {
   129  	if !self.lb.isHttpLb {
   130  		return nil, nil
   131  	}
   132  
   133  	if self.rules != nil {
   134  		return self.rules, nil
   135  	}
   136  
   137  	hostRules := self.lb.urlMap.HostRules
   138  	pathMatchers := self.lb.urlMap.PathMatchers
   139  
   140  	pmm := make(map[string]PathMatcher, 0)
   141  	for i := range pathMatchers {
   142  		name := pathMatchers[i].Name
   143  		pmm[name] = pathMatchers[i]
   144  	}
   145  
   146  	ret := make([]SLoadbalancerListenerRule, 0)
   147  	for _, rule := range hostRules {
   148  		pm, ok := pmm[rule.PathMatcher]
   149  		if !ok {
   150  			continue
   151  		}
   152  
   153  		for i := range rule.Hosts {
   154  			host := rule.Hosts[i]
   155  			for j := range pm.PathRules {
   156  				pr := pm.PathRules[j]
   157  
   158  				if pr.Service != self.backendService.GetId() {
   159  					continue
   160  				}
   161  
   162  				r := SLoadbalancerListenerRule{
   163  					lbl:                self,
   164  					backendService:     self.backendService,
   165  					BackendServiceName: self.backendService.GetName(),
   166  					pathMatcher:        pm,
   167  					pathRule:           pr,
   168  
   169  					ListenerName: self.GetName(),
   170  					Domain:       host,
   171  					Path:         strings.Join(pr.Paths, ","),
   172  					Port:         self.Port,
   173  				}
   174  
   175  				ret = append(ret, r)
   176  			}
   177  		}
   178  	}
   179  
   180  	self.rules = ret
   181  	return ret, nil
   182  }